Работа с микрофоном

Советы » Аудио » Работа с микрофоном

Довольно полноценный пример использования микрофона в своих программах.

unit

Unit1;

interface



uses


..., MMSystem;

TForm1 = class(TForm)
{...}
procedure StartWaveIn; procedure OnWaveIn(var Msg : TMessage); message MM_WIM_DATA; procedure StopWaveIn; {...}
private
{ Private declarations } public
{ Public declarations } end

;

var


wave_in : HWAVEIN;
buffer_handle_0 : THANDLE; buffer_handle_1 : THANDLE; buffer_0 : pointer; // Audio data buffer buffer_1 : pointer; // Audio data buffer buffer_header_0 : pWAVEHDR; // Defines the header used to identify a waveform-audio buffer
buffer_header_1 : pWAVEHDR; buffer_length : DWORD; OnceExecuted : boolean;

implementation



{$R *.DFM}

//...


Procedure

TForm1.StartWaveIn; var


result : MMResult;
header : pWAVEFORMATEX; // Defines the format of waveform-audio data.

const


buffer_size = 512;
begin


if 0 = waveInGetNumDevs then exit; // No Audio input devices are present new(header);
with header^ do
begin
wFormatTag := WAVE_FORMAT_PCM; nChannels := 1; { Stereo input} nSamplesPerSec := 22050; { 22 kHz} wBitsPerSample := 8; { 8 bit} nBlockAlign := nChannels*wBitsPerSample shr 3; nAvgBytesPerSec := nSamplesPerSec*nBlockAlign; cbSize := 0; // No additional information end;
{ Check if the wave-in device is capable to read data in the format} specified
result := waveInOpen(@wave_in, WAVE_MAPPER, header, Form1.Handle, 0, CALLBACK_WINDOW);
if result<>0then exit; {Something is going wrong} buffer_length := header.nChannels*(header.wBitsPerSample shr 3)*buffer_size; buffer_handle_0 := GlobalAlloc(GMEM_MOVEABLE and GMEM_SHARE, buffer_length); buffer_handle_1 := GlobalAlloc(GMEM_MOVEABLE and GMEM_SHARE, buffer_length); buffer_0 := GlobalLock(buffer_handle_0); buffer_1 := GlobalLock(buffer_handle_1); new(buffer_header_0);
with buffer_header_0^ do begin
lpData := buffer_0; dwBufferLength := buffer_length; dwFlags := WHDR_BEGINLOOP; end;
result := waveInPrepareHeader(wave_in, buffer_header_0, sizeof(buffer_header_0^)); result := waveInAddBuffer(wave_in, buffer_header_0, sizeof(buffer_header_0^)); if result<>0then exit; // Something is going wrong new(buffer_header_1);
with buffer_header_1^ do begin
lpData := buffer_1; dwBufferLength := buffer_length; dwFlags := WHDR_ENDLOOP; end;
result := waveInPrepareHeader(wave_in, buffer_header_1, sizeof(buffer_header_1^)); result := waveInAddBuffer(wave_in, buffer_header_1, sizeof(buffer_header_1^)); result := waveInStart(wave_in); // Start recording dispose(header)
end

;

procedure

TForm1.OnWaveIn; var


WaveHeader : pWAVEHDR; result : MMResult;
begin


WaveHeader := pointer(Msg.lParam); { Process data}
if DataProcessed then
begin
Move(WaveHeader^.lpData^, ba, 512); DataProcessed := false; end;
result := waveInAddBuffer(wave_in, WaveHeader, SizeOf(WaveHeader^)) end

;

procedure

TForm1.StopWaveIn; var


result : MMResult;
begin


if 0 = waveInGetNumDevs then exit; { No Audio input devices are present} result := waveInReset(wave_in); result := waveInUnPrepareHeader(wave_in, buffer_header_0, sizeof(buffer_header_0^)); result := waveInUnPrepareHeader(wave_in, buffer_header_1, sizeof(buffer_header_1^)); result := waveInClose(wave_in); GlobalUnlock(buffer_handle_0); GlobalUnlock(buffer_handle_1); GlobalFree(buffer_handle_0); GlobalFree(buffer_handle_1); dispose(buffer_header_0); dispose(buffer_header_1) end

;

{...}
end

.

Другое по теме:

Категории

Статьи

Советы

Copyright © 2022 - All Rights Reserved - www.delphirus.com