Use directsound (4) in Delphi: Set the volume, phase, playback frequency, and playback position.

Source: Internet
Author: User
The setvolume, setpan, setfrequency, and setcurrentposition methods of idirectsoundbuffer can be used to perform these settings.
At the same time, idirectsoundbuffer also has the getvolume, getpan, getfrequency, and getcurrentposition methods.

The key point is that if the buffer zone can accept the volume, phase, and frequency settings, you must specify the corresponding flag when creating the buffer zone.

The following constants describe their value ranges:

Dsbvolume_max = 0; // The maximum volume, which is kept in the control panel. dsbvolume_min =-10000; // dsbpan_left =-10000; // left-channel dsbpan_center = 0; // balanced dsbpan_right = 10000; // right-channel dsbfrequency_original = 0; // use the default dsbfrequency_min = 100; // dsbfrequency_max = 200000; // The maximum frequency, version under directsound 9.0. The value is 100000.
In the previous example, the two functions take up the most space. To make it easier to use them, we put them in a treadwavefile class:
{Units implementing the treadwavefile class} unit readwavefile; interfaceuses windows, classes, sysutils, mmsystem; handler = effecffilehandle: hmmio; fformat: twaveformatex; fsize: DWORD; Public constructor create; destructor destroy; override; function open (filename: string): Boolean; // open the file and read the information. Function read (pdest: pointer; Size: DWORD): Boolean; // read the waveform data property format: twaveformatex read fformat; // read the format data property size: DWORD read fsize; // read the waveform data size end; implementation {treadwavefile} constructor treadwavefile. create; begin inherited; end; destructor treadwavefile. destroy; begin if ffilehandle> 0 then mmioclose (ffilehandle, 0); inherited; end; function treadwavefile. open (filename: string): Boolean; var ckiriff, ckifmt, ckidata: tmmckinfo; begin result: = false; if not fileexists (filename) Then exit; ffilehandle: = mmioopen (pchar (filename), nil, mmio_read); If ffilehandle = 0 Then exit; zeromemory (@ ckiriff, sizeof (tmmckinfo); terminate (ffilehandle, @ ckiriff, Nil, mmio_findriff); If (ckiriff. ckid fourcc_riff) or (ckiriff. fcctype mmiostringtofourcc ('wave ', 0) Then exit; zeromemory (@ fformat, sizeof (twaveformatex); zeromemory (@ ckifmt, sizeof (tmmckinfo); ckifmt. ckid: = mmiostringtofourcc ('fmt', 0); zeromemory (@ ckidata, sizeof (tmmckinfo); ckidata. ckid: = require ('data', 0); If (mmiodescend (ffilehandle, @ ckifmt, @ ckiriff, handle) = mmsyserr_noerror) Then mmioread (ffilehandle, @ fformat, sizeof (twaveformatex); then (ffilehandle, @ ckifmt, 0); If (then (ffilehandle, @ ckidata, @ ckiriff, timeout) = mmsyserr_noerror) Then fsize: = ckidata. cksize; Result: = fformat. wformattag = wave_format_pcm; end; function treadwavefile. read (pdest: pointer; Size: DWORD): Boolean; begin result: = mmioread (ffilehandle, pdest, size) = size; end.
The test program uses four buttons and three trackbars and Their default events:
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls, comctrls; Type tform1 = Class (tform) button1: tbutton; // turn on and play button2: tbutton; // play button3: tbutton repeatedly; // pause button4: tbutton; // play trackbar1: ttrackbar from scratch; // used to adjust the volume trackbar2: ttrackbar; // used for phase adjustment trackbar3: ttrackbar; // procedure formcreate (Sender: tobject); Procedure formdestroy (Sender: tobject); Procedure button1click (Sender: tobject ); procedure future (Sender: tobject); Procedure trackbar2change (Sender: tobject ); procedure trackbar3change (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} uses directsound, readwavefile; // readwavefile is the unit of the treadwavefile class var mydsound: idirectsound8; Buf: plugin; Procedure plugin (Sender: tobject); begin trackbar1.min: = response; trackbar1.max: = large; trackbar2.min: = dsbpan_left; trackbar2.max: = dsbpan_right; trackbar3.min: = 100; trackbar3.max: = 100000; button1.caption: = 'open and play'; button2.caption: = 'replay play'; button3.caption: = 'paused '; button4.caption: = 'replay play'; system. reportmemoryleaksonshutdown: = true; end; Procedure tform1.button1click (Sender: tobject); var bufdesc: tdsbufferdesc; P1: pointer; N1: DWORD; wavpath: string; WAV: treadwavefile; // begin Buf: = nil; mydsound: = nil; with topendialog. create (NiL) Do begin filter: = 'wave file (*. wav) | *. wav '; If execute then wavpath: = filename; free; end; WAV: = treadwavefile. create; if not WAV. open (wavpath) then begin showmessage ('only PCM-format wave file'); WAV. free; exit; end; directsoundcreate8 (nil, mydsound, nil); mydsound. setcooperativelevel (self. handle, dsscl_normal); zeromemory (@ bufdesc, sizeof (tdsbufferdesc); bufdesc. dwsize: = sizeof (tdsbufferdesc); {specify the buffer to allow volume, phase, and frequency adjustment} bufdesc. dwflags: = dsbcaps_static or dsbcaps_ctrlvolume or dsbcaps_ctrlpan or dsbcaps_ctrlfrequency; bufdesc. dwbufferbytes: = WAV. size; bufdesc. lpwfxformat: = @ WAV. format; mydsound. createsoundbuffer (bufdesc, Buf, nil); Buf. lock (0, 0, @ P1, @ N1, nil, nil, dsblock_entirebuffer); WAV. read (P1, N1); Buf. unlock (P1, N1, nil, 0); Buf. play (0, 0, 0); trackbar1.position: = 0; trackbar2.position: = 0; trackbar3.position: = WAV. format. nsamplespersec; WAV. free; end; Procedure tform1.button2click (Sender: tobject); begin if Buf nil then Buf. play (0, 0, dsbplay_looping); end; Procedure tform1.button3click (Sender: tobject); begin if Buf nil then Buf. stop; end; {playback from scratch} procedure tform1.button4click (Sender: tobject); begin if Buf = nil then exit; Buf. stop; Buf. setcurrentposition (0); Buf. play (0, 0, 0); end; {volume adjustment} procedure tform1.trackbar1change (Sender: tobject); begin if Buf nil then Buf. setvolume (ttrackbar (sender ). position); end; {phase adjustment} procedure tform1.trackbar2change (Sender: tobject); begin if Buf nil then Buf. setpan (ttrackbar (sender ). position); end; {frequency adjustment} procedure tform1.trackbar3change (Sender: tobject); begin if Buf nil then Buf. setfrequency (ttrackbar (sender ). position); end; Procedure tform1.formdestroy (Sender: tobject); begin Buf: = nil; mydsound: = nil; end.

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.