在delphi中从数组数据中播放声音的最简单方法是啥

Posted

技术标签:

【中文标题】在delphi中从数组数据中播放声音的最简单方法是啥【英文标题】:what is the simpliest way to play sound from array data in delphi在delphi中从数组数据中播放声音的最简单方法是什么 【发布时间】:2011-03-28 06:30:23 【问题描述】:

有什么简单的功能吗?我正在寻找类似的东西

播放(@data, 44000, 100 time);

【问题讨论】:

【参考方案1】:

我在 PCM 音频处理方面做了很多工作。在播放自定义波形音频数据的短序列时,我总是使用此功能:

var
  PlaySoundStopper: PBoolean;
  SoundPlayerActive: boolean = false;

procedure PlaySound(const Sound: TASSound);
var
  hWave: HWAVEOUT;
  hdr: TWaveHdr;
  buf: PAnsiChar;
  fmt: TWaveFormatEx;
  i: Integer;
  n: Integer;
begin

  try

    with fmt do
    begin
      wFormatTag := WAVE_FORMAT_PCM;
      nChannels := length(Sound.Channels);
      nSamplesPerSec := Sound.SampleRate;
      wBitsPerSample := 32;
      nAvgBytesPerSec := nChannels * nSamplesPerSec * wBitsPerSample div 8;
      nBlockAlign := nChannels * wBitsPerSample div 8;
      cbSize := 0;
    end;

    GetMem(buf, fmt.nChannels * length(Sound.Channels[0]) * sizeof(TASWaveformSample));
    if length(Sound.Channels) = 1 then
      CopyMemory(buf, @(Sound.Channels[0, 0]), length(Sound.Channels[0]) * sizeof(TASWaveformSample))
    else
      for i := 0 to high(Sound.Channels[0]) do
        for n := 0 to high(Sound.Channels) do
          CopyMemory(buf + sizeof(TASWaveformSample) * (i * fmt.nChannels + n), @(Sound.Channels[n, i]), sizeof(TASWaveformSample));

    if waveOutOpen(@hWave, WAVE_MAPPER, @fmt, 0, 0, CALLBACK_NULL) <> MMSYSERR_NOERROR then
      raise Exception.Create('SoundPlayerThread.Execute: waveOutOpen failed: ' + SysErrorMessage(GetLastError));

    ZeroMemory(@hdr, sizeof(hdr));
    with hdr do
    begin
      lpData := buf;
      dwBufferLength := fmt.nChannels * length(Sound.Channels[0]) * sizeof(TASWaveformSample);
      dwFlags := 0;
    end;

    try

      SoundPlayerActive := true;

      waveOutPrepareHeader(hWave, @hdr, sizeof(hdr));
      waveOutWrite(hWave, @hdr, sizeof(hdr));
      sleep(500);

      while waveOutUnprepareHeader(hWave, @hdr, sizeof(hdr)) = WAVERR_STILLPLAYING do
        if PlaySoundStopper^ then
        begin
          waveOutPause(hWave);
          waveOutUnprepareHeader(hWave, @hdr, sizeof(hdr));
          break;
        end
        else
          sleep(100);

    finally
      SoundPlayerActive := false;
      waveOutClose(hWave);
      FreeMem(buf);
    end;

  except
    on E: Exception do MessageBox(0, PChar(E.ClassName + ': ' + E.Message), 'Sound Playback Error', MB_ICONERROR);
  end;
end;

在哪里

type
  TASWaveformSample = integer; // signed 32-bit; -2147483648..2147483647
  TASWaveformSamples = packed array of TASWaveformSample; // one channel
  PASSound = ^TASSound;
  TASSound = record
    Channels: packed array of TASWaveformSamples;
    SampleRate: cardinal;
  end;

也许更好的方法是使用线程进行播放。然后我做

var
  OwnerForm: HWND; // = 0;
  SndSource: PASSound; // = nil;
  ThreadPlaying: boolean; // = false;

type
  TSoundPlayerThread = class(TThread)
  private
     Private declarations 
  protected
    procedure Execute; override;
  end;

实现为

procedure TSoundPlayerThread.Execute;
var
  hWave: HWAVEOUT;
  hdr: TWaveHdr;
  buf: PAnsiChar;
  fmt: TWaveFormatEx;
  i: Integer;
  n: Integer;
begin

  ThreadPlaying := true;
  try

   try

      if not Assigned(SndSource) then
        Exit;

      with fmt do
      begin
        wFormatTag := WAVE_FORMAT_PCM;
        nChannels := length(SndSource^.Channels);
        nSamplesPerSec := SndSource^.SampleRate;
        wBitsPerSample := 32;
        nAvgBytesPerSec := nChannels * nSamplesPerSec * wBitsPerSample div 8;
        nBlockAlign := nChannels * wBitsPerSample div 8;
        cbSize := 0;
      end;

      GetMem(buf, fmt.nChannels * length(SndSource^.Channels[0]) * sizeof(TASWaveformSample));
      if length(SndSource^.Channels) = 1 then
        CopyMemory(buf, @(SndSource^.Channels[0, 0]), length(SndSource^.Channels[0]) * sizeof(TASWaveformSample))
      else
        for i := 0 to high(SndSource^.Channels[0]) do
          for n := 0 to high(SndSource^.Channels) do
            CopyMemory(buf + sizeof(TASWaveformSample) * (i * fmt.nChannels + n), @(SndSource^.Channels[n, i]), sizeof(TASWaveformSample));

      if waveOutOpen(@hWave, WAVE_MAPPER, @fmt, 0, 0, CALLBACK_NULL) <> MMSYSERR_NOERROR then
        raise Exception.Create('SoundPlayerThread.Execute: waveOutOpen failed: ' + SysErrorMessage(GetLastError));

      ZeroMemory(@hdr, sizeof(hdr));
      with hdr do
      begin
        lpData := buf;
        dwBufferLength := fmt.nChannels * length(SndSource^.Channels[0]) * sizeof(TASWaveformSample);
        dwFlags := 0;
      end;

      waveOutPrepareHeader(hWave, @hdr, sizeof(hdr));
      waveOutWrite(hWave, @hdr, sizeof(hdr));
      sleep(500);

      while waveOutUnprepareHeader(hWave, @hdr, sizeof(hdr)) = WAVERR_STILLPLAYING do
      begin
        sleep(100);
        if Terminated then
          waveOutReset(hWave);
      end;

      waveOutClose(hWave);
      FreeMem(buf);

    except
      on E: Exception do MessageBox(0, PChar(E.ClassName + ': ' + E.Message), 'TSoundPlayerThread', MB_ICONERROR);
    end;

  finally
    ThreadPlaying := false;
  end;
end;

【讨论】:

我想了解更多关于 PCM 的信息。这是我的 E-Add xbasic300@yahoo.com 我需要你的帮助。【参考方案2】:

Wave Audio Package 有 TLiveAudioPlayer 组件。它从缓冲区播放音频。

【讨论】:

是的,它也对我有用。而且它的好处是,没有像 bass.dll lame.exe 等外部组件。 我认为这个答案应该被标记为最佳答案。【参考方案3】:

Win32 API PlaySound 函数可以使用其SND_MEMORY 标志从内存块播放标准 RIFF 编码音频(例如 WAV 音频)。或者,如果音频在应用的资源中,您可以改用 SND_RESOURCE 标志。

【讨论】:

【参考方案4】:

Microsoft 有一个Knowledge Base article 告诉您如何使用 MCI 从内存中播放声音。您可能需要在数组中包含波形文件头,或者在第一次读取时复制正确的数据,但除此之外它应该很容易移植。

【讨论】:

【参考方案5】:

我找不到不基于过时的 sndPlaySound 的完整解决方案,所以这里有两个函数可以从 TMemoryStream 和文件中播放“.wav”文件:

uses mmsystem;

procedure PlaySoundFromFile(FileName : String);
var
  mStream : TMemoryStream;
begin
  mStream := TMemoryStream.Create;
  Try mStream.LoadFromFile(FileName); Except End;
  If mStream.Size > 0 then PlaySoundFromStream(mStream);
  mStream.Free;
end;

procedure PlaySoundFromStream(mStream : TMemoryStream);
begin
  PlaySound(mStream.Memory,0,SND_MEMORY or SND_SYNC);
end;

声音是从内存中同步播放的,您可以在 Remy 答案的链接中找到其他 PlaySound 标志。如果切换到异步播放,请确保在播放结束前不要清除声音内存。

【讨论】:

以上是关于在delphi中从数组数据中播放声音的最简单方法是啥的主要内容,如果未能解决你的问题,请参考以下文章

在 Swift 3 中从数组中播放随机声音

在纯 WinAPI 中从内存缓冲区播放声音

在 Java 应用程序中加载和播放声音的最简单方法是啥?

从 C/C++ 播放声音的最简单方法

同时播放两种声音的最简单方法(c++ winapi)

在 JavaScript 中从 1..20 创建整数数组的最简单方法