在 Delphi 下使用 DirectSound (10): 测试合唱效果器 IDirectSoundFXChorus8



{相关结构}

TDSFXChorus = packed record //最大值 .. 最大值 : 默认值

  fWetDryMix: Single; //  0 .. 100 : 50 (%)

  fDepth: Single;     //  0 .. 100 : 10 (%)

  fFeedback: Single;  //-99 .. 99  : 25 (%)

  fFrequency: Single; //  0 .. 10  : 1.1

  lWaveform: Longint; //  0 .. 1   : 1

  fDelay: Single;     //  0 .. 20  : 16 (ms)

  lPhase: Longint;    //  0 .. 4   : 3

end;



//该结构相关常量:

DSFXCHORUS_WETDRYMIX_MIN  = 0.0;

DSFXCHORUS_WETDRYMIX_MAX  = 100.0;



DSFXCHORUS_DEPTH_MIN      = 0.0;

DSFXCHORUS_DEPTH_MAX      = 100.0;



DSFXCHORUS_FEEDBACK_MIN   = -99.0;

DSFXCHORUS_FEEDBACK_MAX   = 99.0;



DSFXCHORUS_FREQUENCY_MIN  = 0.0;

DSFXCHORUS_FREQUENCY_MAX  = 10.0;



DSFXCHORUS_WAVE_TRIANGLE  = 0;

DSFXCHORUS_WAVE_SIN       = 1;



DSFXCHORUS_DELAY_MIN      = 0.0;

DSFXCHORUS_DELAY_MAX      = 20.0;



DSFXCHORUS_PHASE_MIN      = 0;

DSFXCHORUS_PHASE_MAX      = 4;

DSFXCHORUS_PHASE_NEG_180  = 0;

DSFXCHORUS_PHASE_NEG_90   = 1;

DSFXCHORUS_PHASE_ZERO     = 2;

DSFXCHORUS_PHASE_90       = 3;

DSFXCHORUS_PHASE_180      = 4;



测试代码:

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;

    LabeledEdit5: TLabeledEdit;

    RadioGroup1: TRadioGroup;

    RadioGroup2: TRadioGroup;

    procedure FormCreate(Sender: TObject);

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

    procedure RadioGroup1Click(Sender: TObject);

    procedure LabeledEdit1Change(Sender: TObject);

    procedure FormDestroy(Sender: TObject);

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



uses DirectSound, ReadWave2;



var

  myDSound: IDirectSound8;

  buf8: IDirectSoundBuffer8;

  fxChorus: IDirectSoundFXChorus8; //IDirectSoundFXChorus8 效果器



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 := 'fWetDryMix: 0.0 - 100.0';

  LabeledEdit1.Text := '50.0';

  LabeledEdit1.Tag := 1;



  LabeledEdit2.EditLabel.Caption := 'fDepth: 0.0 - 100.0';

  LabeledEdit2.Text := '10.0';

  LabeledEdit2.Tag := 2;



  LabeledEdit3.EditLabel.Caption := 'fFeedback: -99.0 - 99.0';

  LabeledEdit3.Text := '25.0';

  LabeledEdit3.Tag := 3;



  LabeledEdit4.EditLabel.Caption := 'fFrequency: 0.0 - 10.0';

  LabeledEdit4.Text := '1.1';

  LabeledEdit4.Tag := 4;



  LabeledEdit5.EditLabel.Caption := 'ffDelay: 0.0 - 20.0';

  LabeledEdit5.Text := '16.0';

  LabeledEdit5.Tag := 5;



  RadioGroup1.Caption := 'lWaveform:';

  RadioGroup1.Items.CommaText := 'WAVE_TRIANGLE, WAVE_SIN';

  RadioGroup1.ItemIndex := 1;



  RadioGroup2.Caption := 'lPhase:';

  RadioGroup2.Items.CommaText := '-180,-90,0,90,180';

  RadioGroup2.ItemIndex := 3;



  LabeledEdit2.OnChange := LabeledEdit1.OnChange;

  LabeledEdit3.OnChange := LabeledEdit1.OnChange;

  LabeledEdit4.OnChange := LabeledEdit1.OnChange;

  LabeledEdit5.OnChange := LabeledEdit1.OnChange;

  RadioGroup2.OnClick := RadioGroup1.OnClick;

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_STANDARD_CHORUS;

  buf8.SetFX(1, @rEffect, nil);

  buf8.GetObjectInPath(GUID_DSFX_STANDARD_CHORUS, 0, IID_IDirectSoundFXChorus8, fxChorus);

  buf8.Play(0, 0, DSBPLAY_LOOPING);

end;



procedure TForm1.Button2Click(Sender: TObject);

begin

  if Assigned(buf8) then buf8.Stop;

end;



procedure TForm1.RadioGroup1Click(Sender: TObject);

var

  rChorus: TDSFXChorus;

begin

  if buf8 = nil then Exit;

  rChorus.fWetDryMix := StrToFloat(LabeledEdit1.Text);

  rChorus.fDepth     := StrToFloat(LabeledEdit2.Text);

  rChorus.fFeedback  := StrToFloat(LabeledEdit3.Text);

  rChorus.fFrequency := StrToFloat(LabeledEdit4.Text);

  rChorus.lWaveform  := RadioGroup1.ItemIndex;

  rChorus.fDelay     := StrToFloat(LabeledEdit5.Text);

  rChorus.lPhase     := RadioGroup2.ItemIndex;

  fxChorus.SetAllParameters(rChorus);

end;



procedure TForm1.LabeledEdit1Change(Sender: TObject);

var

  f: Single;

  obj: TLabeledEdit;

begin

  obj := Sender as TLabeledEdit;

  if obj.Text = '-' then Exit;

  f := StrToFloatDef(obj.Text, MaxInt);

  case obj.Tag of

    1: if (f < 0)   or (f > 100) then obj.Text := '50.0';

    2: if (f < 0)   or (f > 100) then obj.Text := '10.0';

    3: if (f < -99) or (f > 99)  then obj.Text := '25.0';

    4: if (f < 0)   or (f > 10)  then obj.Text := '1.1';

    5: if (f < 0)   or (f > 20)  then obj.Text := '16.0';

  end;

  RadioGroup1.OnClick(nil);

end;



procedure TForm1.FormDestroy(Sender: TObject);

begin

  buf8 := nil;

  myDSound := nil;

end;



end.



窗体设计:

object Form1: TForm1

  Left = 0

  Top = 0

  Caption = 'Form1'

  ClientHeight = 287

  ClientWidth = 314

  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 = 24

    Top = 18

    Width = 75

    Height = 25

    Caption = 'Button1'

    TabOrder = 4

    OnClick = Button1Click

  end

  object Button2: TButton

    Left = 120

    Top = 18

    Width = 75

    Height = 25

    Caption = 'Button2'

    TabOrder = 5

    OnClick = Button2Click

  end

  object LabeledEdit1: TLabeledEdit

    Left = 24

    Top = 72

    Width = 138

    Height = 21

    EditLabel.Width = 61

    EditLabel.Height = 13

    EditLabel.Caption = 'LabeledEdit1'

    TabOrder = 0

    OnChange = LabeledEdit1Change

  end

  object LabeledEdit2: TLabeledEdit

    Left = 24

    Top = 115

    Width = 138

    Height = 21

    EditLabel.Width = 61

    EditLabel.Height = 13

    EditLabel.Caption = 'LabeledEdit2'

    TabOrder = 1

  end

  object LabeledEdit3: TLabeledEdit

    Left = 24

    Top = 160

    Width = 138

    Height = 21

    EditLabel.Width = 61

    EditLabel.Height = 13

    EditLabel.Caption = 'LabeledEdit3'

    TabOrder = 2

  end

  object LabeledEdit4: TLabeledEdit

    Left = 24

    Top = 208

    Width = 138

    Height = 21

    EditLabel.Width = 61

    EditLabel.Height = 13

    EditLabel.Caption = 'LabeledEdit4'

    TabOrder = 3

  end

  object LabeledEdit5: TLabeledEdit

    Left = 24

    Top = 253

    Width = 138

    Height = 21

    EditLabel.Width = 61

    EditLabel.Height = 13

    EditLabel.Caption = 'LabeledEdit5'

    TabOrder = 8

  end

  object RadioGroup1: TRadioGroup

    Left = 177

    Top = 56

    Width = 129

    Height = 64

    Caption = 'RadioGroup1'

    TabOrder = 6

    OnClick = RadioGroup1Click

  end

  object RadioGroup2: TRadioGroup

    Left = 176

    Top = 136

    Width = 129

    Height = 138

    Caption = 'RadioGroup2'

    TabOrder = 7

  end

end



运行效果图:

在 Delphi 下使用 DirectSound (10): 测试合唱效果器 IDirectSoundFXChorus8

你可能感兴趣的:(Delphi)