Win32 Audio Data Acquisition --- Wavein-Out Realization

xiaoxiao2021-03-06  20

Basic steps and ideas: Set the audio collection parameters (sampling rate, channel, etc.), open the audio device and open the cache, and operate the data. 1. Define audio acquisition parameters

Waveformatex Waveform; // Wave Mode Collection Parameters

Typedef struct {

Word wformattag;

Word nchaneless;

DWORD NSAMPLESPERSEC;

DWORD NAVGBYTESPERSEC;

Word nblockalign;

Word wbitsPersample;

Word cbsize;

Waveformatex; WAVEFORMATEX;

Waveform.wformattag = Wave_FORMAT_PCM;

Waveform.nchannels = 1; // The channel is set to 1

Waveform.nsamplespersec = 11025; // Sample rate 11025

Waveform.navgbytespersec = 11025;

Waveform.nblockAlign = 1;

Waveform.wbitsPersample = 8; // The data bit is set to 8 digits

Waveform.cbsize = 0;

PWAVEHDR PWAVEHDR1, PWAVEHDR2; / / WAVE waveform header information 2. Open the audio acquisition device and open a cache

IF (Waveinin (& Hwavein, Wave_Mapper, & Waveform, (DWORD) THIS-> M_HWND, NULL, CALLBACK_WINDOW) {// Open Device

Free (PBuffer1);

Free (PBuffer2);

MessageBeep (MB_ICONEXCLAMATION);

AfxMessageBox ("Audio Can not be open!");

}

PWAVEHDR1-> LPDATA = (LPTSTSTS) PBUFFER1;

PWAVEHDR1-> dwbufferLength = INP_BUFFER_SIZE;

PWAVEHDR1-> DWBYTESRECORDED = 0;

PWAVEHDR1-> dwuser = 0;

PWAVEHDR1-> dwflags = 0;

PWAVEHDR1-> dwloops = 1;

PWAVEHDR1-> lpnext = NULL;

PWAVEHDR1-> reserved = 0;

WaveInpreparehead (Hwavein, PWaveHDR1, SIZEOF (WaveHDR));

PWAVEHDR2-> lpdata = (lptstr) PBuffer2;

PWAVEHDR2-> dwbufferLength = INP_BUFFER_SIZE;

PWAVEHDR2-> dwbytessrecorded = 0;

PWAVEHDR2-> dwuser = 0;

PWAVEHDR2-> dwflags = 0;

PWAVEHDR2-> dwloops = 1;

PWAVEHDR2-> lpnext = null;

PWAVEHDR2-> reserved = 0;

WaveinPrepareheader (Hwavein, PWaveHDR2, SIZEOF (WaveHDR));

//

PsaveBuffer = (PBYTE) Realloc (psavebuffer, 1);

// add the buffers

Waveinaddbuffer (Hwavein, PWaveHDR1, SIZEOF (WaveHDR));

WaveinaddBuffer (Hwavein, PWaveHDR2, SIZEOF (WaveHDR)); 3. Audio data acquired

There are several messages in Wavein

MM_WOM_OPEN / / Open

MM_WIM_DATA // Acquisition and operation of device data

MM_WIM_CLOSE // device close

Void CRecordhWnddlg :: OnMM_WIM_DATA (Uint WPARAM, Long LPARAM)

{

// Todo: add your message handler code here and / or call default

// Reallocate Save Buffer Memory

//

PnewBuffer = (Pbyte) Realloc (Psavebuffer, DwdatAngth

(PWaveHDR) LPARAM -> DWBYTESRecorded;

IF (pnewbuffer == null)

{

Waveinclose (hwavein);

MessageBeep (MB_ICONEXCLAMATION);

AfxMessageBox ("Erro Memory");

Return;

}

Psavebuffer = pnewbuffer;

//

CopyMemory (psavebuffer dwdatalength, ((pWaveHDR) LPARAM -> LPDATA,

(PWaveHDR) LPARAM -> DWBYTESRecorded;

DWDATALENGTH = (PWAVEHDR) LPARAM -> DWBYTESRECORDED;

IF (bending)

{

Waveinclose (hwavein);

Return;

}

CFILE FILE;

File.open ("DATA.TXT", CFile :: ModeWrite | CFILE :: MODECREATE);

File.write (PsaveBuffer, Dwdatarativeth);

File.Close ();

// send Out a new buffer

Waveinaddbuffer (Hwavein, (PWaveHDR) LParam, Sizeof (WaveHDR));

Trace ("DONE INPUT DATA / N");

Return;

}

转载请注明原文地址:https://www.9cbs.com/read-71212.html

New Post(0)