Use the window to receive messages from the audio device:
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; button3: tbutton; Procedure upload (Sender: tobject); Procedure button1click (Sender: tobject); Procedure button2click (Sender: tobject); Procedure button3click (Sender: tobject ); protected procedure wndproc (var m: tmessage); override; end; var form1: tform1; implementation {$ R *. DFM} uses mmsystem; var whin1, whin2, whout: twavehdr; hwavein, hwaveout: hwave; FMT: average; buf1, buf2, Bytes: tbytes; Procedure threshold (Sender: tobject ); begin button1.caption: = 'start recording '; button2.caption: = 'Stop recording'; button3.caption: = 'play recording '; end; // start recording procedure tform1.button1click (Sender: tobject ); begin {specify the format to be recorded} FMT. wformattag: = wave_format_pcm; FMT. nchannels: = 2; FMT. nsamplespersec: = 22050; FMT. navgbytespersec: = 88200; FMT. nblockalign: = 4; FMT. wbitspersample: = 16; FMT. cbsize: = 0; savebuf: = nil; {clear recorded content} If waveinopen (@ hwavein, wave_mapper, @ FMT, handle, 0, callback_window) = 0 then begin setlength (buf1, 1024*8); setlength (buf2, 1024*8); whin1.lpdata: = pansichar (buf1); whin1.dwbufferlength: = length (buf1); whin1.dwbytesrecorded: = 0; whin1.dwuser: = 0; whin1.dwflags: = 0; whin1.dwloops: = 0; whin1.lpnext: = nil; whin1.reserved: = 0; whin2.lpdata: = pansichar (buf2); Region: = length (buf2); bandwidth: = 0; whin2.dwuser: = 0; whin2.dwflags: = 0; bandwidth: = 0; whin2.lpnext: = nil; whin2.reserved: = 0; waveinprepareheader (hwavein, @ whin1, sizeof (twavehdr); contains (hwavein, @ whin2, sizeof (twavehdr); contains (hwavein, @ whin1, sizeof (twavehdr); contains (hwavein, @ whin2, sizeof (twavehdr); waveinstart (hwavein); end; // stop recording procedure success (Sender: tobject); begin waveinstop (hwavein); quit (hwavein, @ whin1, sizeof (twavehdr); waveinunprepareheader (hwavein, @ whin2, sizeof (twavehdr); waveinclose (hwavein); end; // play the recording procedure alert (Sender: tobject); begin whout. lpdata: = pansichar (savebuf); whout. dwbufferlength: = length (savebuf); whout. dwbytesrecorded: = 0; whout. dwuser: = 0; whout. dwflags: = 0; whout. dwloops: = 1; whout. lpnext: = nil; whout. reserved: = 0; outputs (@ hwaveout, wave_mapper, @ FMT, handle, 0, callback_window); outputs (hwaveout, @ whout, sizeof (twavehdr); waveoutwrite (hwaveout, @ whout, sizeof (twavehdr); end; Procedure tform1.wndproc (var m: tmessage); var ordlen: integer; begin inherited; Case M. MSG of {process recording message} mm_wim_open :;{ this message only carries the device handle} mm_wim_close :;{ this message only carries the device handle} mm_wim_data: begin {This message carries the device handle and wavehdr pointer (lparam)} {Save the recorded data} ordlen: = length (savebuf); setlength (savebuf, ordlen + pwavehdr (M. lparam ). dwbytesrecorded); copymemory (PTR (DWORD (savebuf) + ordlen), pwavehdr (M. lparam ). lpdata, pwavehdr (M. lparam ). dwbytesrecorded); {continue recording} waveinaddbuffer (hwavein, pwavehdr (M. lparam), sizeof (twavehdr); end; {process the playback message} mm_wom_open :;{ this message only carries the device handle} mm_wom_close :; {This message only carries the device handle} mm_wom_done: begin {This message carries the device handle and wavehdr pointer (lparam)} waveoutunprepareheader (hwaveout, pwavehdr (M. lparam), sizeof (twavehdr); waveoutclose (hwaveout); end.
Use the callback function to process the messages sent by the audio device:
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; button3: tbutton; Procedure upload (Sender: tobject); Procedure button1click (Sender: tobject); Procedure upload (Sender: tobject); Procedure button3click (Sender: tobject); end; vaR form1: tform1; implementation {$ R *. DFM} uses mmsystem; var whin1, whin2, whout: twavehdr; hwavein, hwaveout: hwave; FMT: twaveformatex; buf1, buf2, savebuf: tbytes; // callback function; error-prone: The local variables procedure waveproc (hwave: hwave; umsg, dwinstance, dwparam1, dwparam2: DWORD), stdcall, VAR ordlen: integer cannot be used in the system callback function; begin case umsg of {process recording message} mm_wim_open :;{ this message only carries the device handle} mm_wim_close :;{ this message only carries the device handle} mm_wim_data: begin {This message carries the device handle and wavehdr pointer (dwparam1)} {Save the recorded data} ordlen: = length (savebuf); setlength (savebuf, ordlen + pwavehdr (dwparam1 ). dwbytesrecorded); copymemory (PTR (DWORD (savebuf) + ordlen), pwavehdr (dwparam1 ). lpdata, pwavehdr (dwparam1 ). dwbytesrecorded); {continue recording} waveinaddbuffer (hwave, pwavehdr (dwparam1), sizeof (twavehdr); end; {process playback message} mm_wom_open :; {This message only carries the device handle} mm_wom_close :;{ this message only carries the device handle} mm_wom_done: begin {This message carries the device handle and wavehdr pointer (dwparam1 )} combine (hwave, pwavehdr (dwparam1), sizeof (twavehdr); waveoutclose (hwave); end; Procedure tform1.formcreate (Sender: tobject); begin button1.caption: = 'start recording '; button2.caption: = 'Stop recording'; button3.caption: = 'play recording '; end; // start recording procedure tform1.button1click (Sender: tobject ); begin {specify the format to be recorded} FMT. wformattag: = wave_format_pcm; FMT. nchannels: = 2; FMT. nsamplespersec: = 22050; FMT. navgbytespersec: = 88200; FMT. nblockalign: = 4; FMT. wbitspersample: = 16; FMT. cbsize: = 0; savebuf: = nil; {clear recorded content} If waveinopen (@ hwavein, wave_mapper, @ FMT, DWORD (@ waveproc), 0, callback_function) = 0 then begin setlength (buf1, 1024*8); setlength (buf2, 1024*8); whin1.lpdata: = pansichar (buf1); whin1.dwbufferlength: = length (buf1); whin1.dwbytesrecorded: = 0; whin1.dwuser: = 0; whin1.dwflags: = 0; whin1.dwloops: = 0; whin1.lpnext: = nil; whin1.reserved: = 0; whin2.lpdata: = pansichar (buf2); Region: = length (buf2); bandwidth: = 0; whin2.dwuser: = 0; whin2.dwflags: = 0; bandwidth: = 0; whin2.lpnext: = nil; whin2.reserved: = 0; waveinprepareheader (hwavein, @ whin1, sizeof (twavehdr); contains (hwavein, @ whin2, sizeof (twavehdr); contains (hwavein, @ whin1, sizeof (twavehdr); contains (hwavein, @ whin2, sizeof (twavehdr); waveinstart (hwavein); end; // stop recording procedure success (Sender: tobject); begin waveinstop (hwavein); quit (hwavein, @ whin1, sizeof (twavehdr); waveinunprepareheader (hwavein, @ whin2, sizeof (twavehdr); waveinclose (hwavein); end; // play the recording procedure alert (Sender: tobject); begin whout. lpdata: = pansichar (savebuf); whout. dwbufferlength: = length (savebuf); whout. dwbytesrecorded: = 0; whout. dwuser: = 0; whout. dwflags: = 0; whout. dwloops: = 1; whout. lpnext: = nil; whout. reserved: = 0; waveoutopen (@ hwaveout, wave_mapper, @ FMT, DWORD (@ waveproc), 0, callback_function); Digest (hwaveout, @ whout, sizeof (twavehdr )); waveoutwrite (hwaveout, @ whout, sizeof (twavehdr); end.