在 Delphi 下使用 DirectSound (12): 测试失真效果器 IDirectSoundFXDistortion8
在 Delphi 下使用 DirectSound (12): 测试失真效果器 IDirectSoundFXDistortion8
涂孟超 发表于3年前
在 Delphi 下使用 DirectSound (12): 测试失真效果器 IDirectSoundFXDistortion8
  • 发表于 3年前
  • 阅读 1
  • 收藏 0
  • 点赞 0
  • 评论 0

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


{相关结构:}
TDSFXDistortion = packed record
  fGain: Sinle;                  //-60 .. 0    : -18  (dB) //好像默认值不是 -18
  fEdge: Single;                  //  0 .. 100  : 15   (%)
  fPostEQCenterFrequency: Single; //100 .. 8000 : 2400 (Hz)
  fPostEQBandwidth: Single;       //100 .. 8000 : 2400 (Hz)
  fPreLowpassCutoff: Single;      //100 .. 8000 : 800  (Hz)
end;

//该结构相关常量:
DSFXDISTORTION_GAIN_MIN                  = -60.0;
DSFXDISTORTION_GAIN_MAX                  = 0.0;

DSFXDISTORTION_EDGE_MIN                  = 0.0;
DSFXDISTORTION_EDGE_MAX                  = 100.0;

DSFXDISTORTION_POSTEQCENTERFREQUENCY_MIN = 100.0;
DSFXDISTORTION_POSTEQCENTERFREQUENCY_MAX = 8000.0;

DSFXDISTORTION_POSTEQBANDWIDTH_MIN       = 100.0;
DSFXDISTORTION_POSTEQBANDWIDTH_MAX       = 8000.0;

DSFXDISTORTION_PRELOWPASSCUTOFF_MIN      = 100.0;
DSFXDISTORTION_PRELOWPASSCUTOFF_MAX      = 8000.0;


测试代码:
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;
    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;
  fxDistortion: IDirectSoundFXDistortion8; //IDirectSoundFXDistortion8 效果器

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 := 'fGain: -60 - 0';
  LabeledEdit1.Text := '-18.0';
  LabeledEdit1.Tag := 1;

  LabeledEdit2.EditLabel.Caption := 'fEdge: 0 - 100';
  LabeledEdit2.Text := '15.0';
  LabeledEdit2.Tag := 2;

  LabeledEdit3.EditLabel.Caption := 'fPostEQCenterFrequency: 100 - 8000';
  LabeledEdit3.Text := '2400.0';
  LabeledEdit3.Tag := 3;

  LabeledEdit4.EditLabel.Caption := 'fPostEQBandwidth: 100 - 8000';
  LabeledEdit4.Text := '2400.0';
  LabeledEdit4.Tag := 4;

  LabeledEdit5.EditLabel.Caption := 'fPreLowpassCutoff: 100 - 8000';
  LabeledEdit5.Text := '800.0';
  LabeledEdit5.Tag := 5;

  LabeledEdit2.OnChange := LabeledEdit1.OnChange;
  LabeledEdit3.OnChange := LabeledEdit1.OnChange;
  LabeledEdit4.OnChange := LabeledEdit1.OnChange;
  LabeledEdit5.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_STANDARD_DISTORTION;
  buf8.SetFX(1, @rEffect, nil);
  buf8.GetObjectInPath(GUID_DSFX_STANDARD_DISTORTION, 0, IID_IDirectSoundFXDistortion8, fxDistortion);
  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
  rDistortion: TDSFXDistortion;
  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 < -60)  or (f > 0)   then obj.Text := '-18.0';
    2: if (f < 0) or (f > 100)  then obj.Text := '15.0';
    3: if (f < 100) then Exit else if (f > 8000) then obj.Text := '2400.0';
    4: if (f < 100) then Exit else if (f > 8000) then obj.Text := '2400.0';
    5: if (f < 100) then Exit else if (f > 8000) then obj.Text := '800.0';
  end;

  if buf8 = nil then Exit;
  rDistortion.fGain                  := StrToFloat(LabeledEdit1.Text);
  rDistortion.fEdge                  := StrToFloat(LabeledEdit2.Text);
  rDistortion.fPostEQCenterFrequency := StrToFloat(LabeledEdit3.Text);
  rDistortion.fPostEQBandwidth       := StrToFloat(LabeledEdit4.Text);
  rDistortion.fPreLowpassCutoff      := StrToFloat(LabeledEdit5.Text);
  fxDistortion.SetAllParameters(rDistortion);
end;

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

end.


窗体设计:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 253
  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
  object LabeledEdit5: TLabeledEdit
    Left = 144
    Top = 221
    Width = 193
    Height = 21
    EditLabel.Width = 61
    EditLabel.Height = 13
    EditLabel.Caption = 'LabeledEdit5'
    TabOrder = 6
  end
end


运行效果图:


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