在 Delphi 下使用 DirectSound (15): 测试混响效果器 IDirectSoundFXWavesReverb8


{相关结构:}
TDSFXWavesReverb = packed record
  fInGain: Single;          //  -96 .. 0     : 0.0    (dB)
  fReverbMix: Single;       //  -96 .. 0     : 0.0    (db)
  fReverbTime: Single;      //0.001 .. 3000  : 1000.0 (ms)
  fHighFreqRTRatio: Single; //0.001 .. 0.999 : 0.001
end;

//该结构相关常量:
DSFX_WAVESREVERB_INGAIN_MIN              = -96.0;
DSFX_WAVESREVERB_INGAIN_MAX              = 0.0;
DSFX_WAVESREVERB_INGAIN_DEFAULT          = 0.0;

DSFX_WAVESREVERB_REVERBMIX_MIN           = -96.0;
DSFX_WAVESREVERB_REVERBMIX_MAX           = 0.0;
DSFX_WAVESREVERB_REVERBMIX_DEFAULT       = 0.0;

DSFX_WAVESREVERB_REVERBTIME_MIN          = 0.001;
DSFX_WAVESREVERB_REVERBTIME_MAX          = 3000.0;
DSFX_WAVESREVERB_REVERBTIME_DEFAULT      = 1000.0;

DSFX_WAVESREVERB_HIGHFREQRTRATIO_MIN     = 0.001;
DSFX_WAVESREVERB_HIGHFREQRTRATIO_MAX     = 0.999;
DSFX_WAVESREVERB_HIGHFREQRTRATIO_DEFAULT = 0.001;

测试代码:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    LabeledEdit1: TLabeledEdit;
    LabeledEdit2: TLabeledEdit;
    LabeledEdit3: TLabeledEdit;
    LabeledEdit4: TLabeledEdit;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure LabeledEdit1Change(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses DirectSound, ReadWave2; //ReadWave2 是前面(9)自定义的单元

var
  myDSound: IDirectSound8;
  buf8: IDirectSoundBuffer8;
  fxWavesReverb: IDirectSoundFXWavesReverb8; //IDirectSoundFXWavesReverb8 效果器

procedure TForm1.FormCreate(Sender: TObject);
begin
  System.ReportMemoryLeaksOnShutdown := True;
  DirectSoundCreate8(nil, myDSound, nil);
  myDSound.SetCooperativeLevel(Handle, DSSCL_NORMAL);

  Button1.Caption := '打开并播放';
  Button2.Caption := '停止播放';

  LabeledEdit1.EditLabel.Caption := 'fInGain: -96.0 - 0.0';
  LabeledEdit1.Text := '0.0';
  LabeledEdit1.Tag := 1;

  LabeledEdit2.EditLabel.Caption := 'fReverbMix: -96.0 - 0.0';
  LabeledEdit2.Text := '0.0';
  LabeledEdit2.Tag := 2;

  LabeledEdit3.EditLabel.Caption := 'fReverbTime: 0.001 - 3000';
  LabeledEdit3.Text := '1000.0';
  LabeledEdit3.Tag := 3;

  LabeledEdit4.EditLabel.Caption := 'fHighFreqRTRatio: 0.001 - 0.999';
  LabeledEdit4.Text := '0.001';
  LabeledEdit4.Tag := 4;

  LabeledEdit2.OnChange := LabeledEdit1.OnChange;
  LabeledEdit3.OnChange := LabeledEdit1.OnChange;
  LabeledEdit4.OnChange := LabeledEdit1.OnChange;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  buf: IDirectSoundBuffer;
  bufDesc: TDSBufferDesc;
  rEffect: TDSEffectDesc;
  wav: TReadWave;
  p1: Pointer;
  n1: DWORD;
begin
  wav := TReadWave.Create;
  if not wav.OpenDialog then begin wav.Free; Exit; end;

  ZeroMemory(@bufDesc, SizeOf(TDSBufferDesc));
  bufDesc.dwSize := SizeOf(TDSBufferDesc);
  bufDesc.dwFlags := DSBCAPS_CTRLFX;
  bufDesc.dwBufferBytes := wav.Size;
  bufDesc.lpwfxFormat := @wav.Format;
  myDSound.CreateSoundBuffer(bufDesc, buf, nil);
  buf.QueryInterface(IID_IDirectSoundBuffer8, buf8);

  buf8.Lock(0, 0, @p1, @n1, nil, nil, DSBLOCK_ENTIREBUFFER);
  wav.Read(p1, n1);
  wav.Free;
  buf8.Unlock(p1, n1, nil, 0);

  ZeroMemory(@rEffect, SizeOf(TDSEffectDesc));
  rEffect.dwSize := SizeOf(TDSEffectDesc);
  rEffect.dwFlags := 0;
  rEffect.guidDSFXClass := GUID_DSFX_WAVES_REVERB;
  buf8.SetFX(1, @rEffect, nil);
  buf8.GetObjectInPath(GUID_DSFX_WAVES_REVERB, 0, IID_IDirectSoundFXWavesReverb8, fxWavesReverb);
  buf8.Play(0, 0, DSBPLAY_LOOPING);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if Assigned(buf8) then buf8.Stop;
end;

procedure TForm1.LabeledEdit1Change(Sender: TObject);
var
  rWavesReverb: TDSFXWavesReverb;
  f: Single;
  obj: TLabeledEdit;
begin
  obj := Sender as TLabeledEdit;
  if obj.Text = '-' then Exit;

  f := StrToFloatDef(obj.Text, MaxInt);
  case obj.Tag of
    1,2: if (f < -96) or (f > 0)     then obj.Text := '0.0';
    3: if (f < 0.001) or (f > 3000)  then obj.Text := '1000.0';
    4: if (f < 0.001) or (f > 0.999) then obj.Text := '0.001';
  end;

  if buf8 = nil then Exit;
  rWavesReverb.fInGain          := StrToFloat(LabeledEdit1.Text);
  rWavesReverb.fReverbMix       := StrToFloat(LabeledEdit2.Text);
  rWavesReverb.fReverbTime      := StrToFloat(LabeledEdit3.Text);
  rWavesReverb.fHighFreqRTRatio := StrToFloat(LabeledEdit4.Text);
  fxWavesReverb.SetAllParameters(rWavesReverb);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  buf8 := nil;
  myDSound := nil;
end;

end.

窗体设计:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 205
  ClientWidth = 363
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 32
    Top = 28
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 32
    Top = 75
    Width = 75
    Height = 25
    Caption = 'Button2'
    TabOrder = 1
    OnClick = Button2Click
  end
  object LabeledEdit1: TLabeledEdit
    Left = 144
    Top = 30
    Width = 193
    Height = 21
    EditLabel.Width = 61
    EditLabel.Height = 13
    EditLabel.Caption = 'LabeledEdit1'
    TabOrder = 2
    OnChange = LabeledEdit1Change
  end
  object LabeledEdit2: TLabeledEdit
    Left = 144
    Top = 77
    Width = 193
    Height = 21
    EditLabel.Width = 61
    EditLabel.Height = 13
    EditLabel.Caption = 'LabeledEdit2'
    TabOrder = 3
  end
  object LabeledEdit3: TLabeledEdit
    Left = 144
    Top = 125
    Width = 193
    Height = 21
    EditLabel.Width = 61
    EditLabel.Height = 13
    EditLabel.Caption = 'LabeledEdit3'
    TabOrder = 4
  end
  object LabeledEdit4: TLabeledEdit
    Left = 144
    Top = 173
    Width = 193
    Height = 21
    EditLabel.Width = 61
    EditLabel.Height = 13
    EditLabel.Caption = 'LabeledEdit4'
    TabOrder = 5
  end
end

运行效果图:

在 Delphi 下使用 DirectSound (15): 测试混响效果器 IDirectSoundFXWavesReverb8

你可能感兴趣的:(在 Delphi 下使用 DirectSound (15): 测试混响效果器 IDirectSoundFXWavesReverb8)