Use directsound (11) in Delphi: test the compression efficiency idirectsoundfxcompressor8

Source: Internet
Author: User
{Related structure:} tdsfxcompressor = packed record fgain: single; //-60 .. 60: 0 (db) fattack: single; // 0.01 .. 500: 10 (MS) frelease: single; // 50 .. 3000: 200 (MS) fthreshold: single; //-60 .. 0:-20 (db) fratio: single; // 1 .. 100: 3 (%) fpredelay: single; // 0 .. 4: 4 (MS) end; // The structure related constant: cost =-60.0; cost = 60.0; cost = 0.01; dsfxcompressor_attack_max = 500.0; dsfxcompressor_release_min = 50.0; dsfxcompressor_release_max = 3000.0; response =-60.0; response = 0.0; dsfxcompressor_ratio_min = 1.0; dsfxcompressor_ratio_max = 100.0; dsfxcompressor_predelay_min = 0.0; dsfxcompressor_predelay_max = 4.0;
Test code:
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls, extctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; labelededit1: tlabelededit; tags: tags; tags: tlabelededit; tags: tags; Procedure formcreate (Sender: tobject); Procedure formdestroy (Sender: tobject); Procedure button1click (Sender: tobject); Procedure button2click (Sender: tobject); Procedure destroy (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} uses directsound, readwave2; // readwave2 is the preceding (9) custom unit var mydsound: idirectsound8; buf8: idirectsoundbuffer8; fxcompressor: idirectsoundfxcompressor8; // idirectsoundfxcompressor8 Effect Generator procedure tform1.formcreate (Sender: tobject); begin system. reportmemoryleaksonshutdown: = true; directsoundcreate8 (nil, mydsound, nil); mydsound. setcooperativelevel (handle, dsscl_normal); button1.caption: = 'open and play'; button2.caption: = 'Stop play'; labelededit1.editlabel. caption: = 'fgain:-60-60'; labelededit1.text: = '0. 0'; labelededit1.tag: = 1; labelededit2.editlabel. caption: = 'fattack: 0.01-500 '; labelededit2.text: = '10. 0'; labelededit2.tag: = 2; labelededit3.editlabel. caption: = 'frelease: 50-3000 '; labelededit3.text: = '2017. 0'; labelededit3.tag: = 3; labelededit4.editlabel. caption: = 'fthreshold:-60-0'; labelededit4.text: = '-20.0'; labelededit4.tag: = 4; labelededit5.editlabel. caption: = 'fratio: 1-100 '; labelededit5.text: = '3. 0'; labelededit5.tag: = 5; labelededit6.editlabel. caption: = 'fpredelay: 0-4'; labelededit6.text: = '4. 0'; labelededit6.tag: = 6; tags: = tags; tags: = labelededit1.onchange; labelededit5.onchange: = tags; tags: = labelededit1.onchange; end; Procedure handle (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: = require; buf8.setfx (1, @ reffect, nil); buf8.getobjectinpath (latency, 0, latency, fxcompressor); buf8.play (0, 0, dsbplay_looping); end; procedure merge (Sender: tobject); begin if assigned (buf8) Then buf8.stop; end; procedure merge (Sender: tobject); var rcompressor: tdsfxcompressor; 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) Then obj. text: = '0. 0'; 2: If (F 500) Then obj. text: = '10. 0 '; 3: If (F 3000) Then obj. text: = '100. 0'; 4: If (F 0) Then obj. text: = '-20.0'; 5: If (F 100) Then obj. text: = '3. 0 '; 6: If (F 4) Then obj. text: = '4. 0'; end; If buf8 = nil then exit; rcompressor. fgain: = strtofloat (labelededit1.text); rcompressor. fattack: = strtofloat (labelededit2.text); rcompressor. frelease: = strtofloat (labelededit3.text); rcompressor. fthreshold: = strtofloat (labelededit4.text); rcompressor. fratio: = strtofloat (labelededit5.text); rcompressor. fpredelay: = strtofloat (labelededit6.text); fxcompressor. setallparameters (rcompressor); end; Procedure tform1.formdestroy (Sender: tobject); begin buf8: = nil; mydsound: = nil; end.
Form Design:
object Form1: TForm1  Left = 0  Top = 0  Caption = 'Form1'  ClientHeight = 307  ClientWidth = 306  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 = 121    Height = 21    EditLabel.Width = 61    EditLabel.Height = 13    EditLabel.Caption = 'LabeledEdit1'    TabOrder = 2    OnChange = LabeledEdit1Change  end  object LabeledEdit2: TLabeledEdit    Left = 144    Top = 77    Width = 121    Height = 21    EditLabel.Width = 61    EditLabel.Height = 13    EditLabel.Caption = 'LabeledEdit2'    TabOrder = 3  end  object LabeledEdit3: TLabeledEdit    Left = 144    Top = 125    Width = 121    Height = 21    EditLabel.Width = 61    EditLabel.Height = 13    EditLabel.Caption = 'LabeledEdit3'    TabOrder = 4  end  object LabeledEdit4: TLabeledEdit    Left = 144    Top = 173    Width = 121    Height = 21    EditLabel.Width = 61    EditLabel.Height = 13    EditLabel.Caption = 'LabeledEdit4'    TabOrder = 5  end  object LabeledEdit5: TLabeledEdit    Left = 144    Top = 221    Width = 121    Height = 21    EditLabel.Width = 61    EditLabel.Height = 13    EditLabel.Caption = 'LabeledEdit5'    TabOrder = 6  end  object LabeledEdit6: TLabeledEdit    Left = 144    Top = 269    Width = 121    Height = 21    EditLabel.Width = 61    EditLabel.Height = 13    EditLabel.Caption = 'LabeledEdit6'    TabOrder = 7  endend
Run:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.