在 Delphi 下使用 DirectSound (17): 频率均衡效果器 IDirectSoundFXParamEq8
在 Delphi 下使用 DirectSound (17): 频率均衡效果器 IDirectSoundFXParamEq8
涂孟超 发表于3年前
在 Delphi 下使用 DirectSound (17): 频率均衡效果器 IDirectSoundFXParamEq8
  • 发表于 3年前
  • 阅读 5
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   


{相关结构:}
TDSFXParamEq = packed record
  fCenter: Single;    // 80 .. 16000 : 8000.0 (Hz)
  fBandwidth: Single; //  1 .. 36    : 12.0
  fGain: Single;      //-15 .. 15    : 0.0
end;

//该结构相关常量:
DSFXPARAMEQ_CENTER_MIN    = 80.0;
DSFXPARAMEQ_CENTER_MAX    = 16000.0;

DSFXPARAMEQ_BANDWIDTH_MIN = 1.0;
DSFXPARAMEQ_BANDWIDTH_MAX = 36.0;

DSFXPARAMEQ_GAIN_MIN      = -15.0;
DSFXPARAMEQ_GAIN_MAX      = 15.0;


测试代码:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    LabeledEdit1: 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;
  fxParamEqs: array[0..5] of IDirectSoundFXParamEq8; //IDirectSoundFXParamEq8 效果器数组
  arrTrack: array[0..5] of TTrackBar;

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

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

  for i := 5  downto 0 do
  begin
    arrTrack[i] := TTrackBar.Create(Self);
    arrTrack[i].Parent := Self;
    arrTrack[i].Orientation := trVertical;
    arrTrack[i].ShowSelRange := False;
    arrTrack[i].TickStyle := tsNone;
    arrTrack[i].Width := 32;
    arrTrack[i].Align := alRight;
    arrTrack[i].Tag := i;
    arrTrack[i].Min := -15;
    arrTrack[i].Max := 15;
    arrTrack[i].Position := 0;
    arrTrack[i].OnChange := LabeledEdit1.OnChange;
  end;
  LabeledEdit1.EditLabel.Caption := 'fBandwidth:';
  LabeledEdit1.Text := '12.0';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  buf: IDirectSoundBuffer;
  bufDesc: TDSBufferDesc;
  rEffect: TDSEffectDesc;
  wav: TReadWave;
  p1: Pointer;
  n1: DWORD;
  i: Integer;
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_PARAMEQ;

  for i := 0 to 5 do
  begin
    buf8.SetFX(1, @rEffect, nil);
    buf8.GetObjectInPath(GUID_DSFX_STANDARD_PARAMEQ, 0, IID_IDirectSoundFXParamEq8, fxParamEqs[i]);
  end;

  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
  rParamEqs: TDSFXParamEq;
  i: Integer;
  f: Single;
begin
  if buf8 = nil then Exit;
  if Sender.ClassNameIs('TLabeledEdit') then
  begin
    f := StrToFloatDef(LabeledEdit1.Text, MaxInt);
    if (f < 1) or (f > 36) then LabeledEdit1.Text := '12.0';
    for i := 0 to 5 do arrTrack[i].OnChange(arrTrack[i]);
  end;
  rParamEqs.fBandwidth := StrToFloat(LabeledEdit1.Text);

  i := TTrackBar(Sender).Tag;
  case i of
    0: rParamEqs.fCenter := 100;
    1: rParamEqs.fCenter := 2000;
    2: rParamEqs.fCenter := 4000;
    3: rParamEqs.fCenter := 8000;
    4: rParamEqs.fCenter := 10000;
    5: rParamEqs.fCenter := 12000;
  end;
  rParamEqs.fGain := - TTrackBar(Sender).Position;
  fxParamEqs[i].SetAllParameters(rParamEqs);
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 = 343
  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 = 67
    Width = 75
    Height = 25
    Caption = 'Button2'
    TabOrder = 1
    OnClick = Button2Click
  end
  object LabeledEdit1: TLabeledEdit
    Left = 32
    Top = 144
    Width = 89
    Height = 21
    EditLabel.Width = 61
    EditLabel.Height = 13
    EditLabel.Caption = 'LabeledEdit1'
    TabOrder = 2
    OnChange = LabeledEdit1Change
  end
end


运行效果图:


共有 人打赏支持
粉丝 13
博文 2004
码字总数 14107
×
涂孟超
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: