程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 在 Delphi 下使用 DirectSound (12): 測試失真效果器 IDirectSoundFXDistortion8

在 Delphi 下使用 DirectSound (12): 測試失真效果器 IDirectSoundFXDistortion8

編輯:Delphi

{相關結構:}  
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 

  運行效果圖:

在 Delphi 下使用 DirectSound (12): 測試失真效果器 IDirectSoundFXDistortion8



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved