The example of this article introduces the method of using winmm.h to obtain the audio stream, the following steps are as follows:
First, you need to include the following reference objects
#include <Windows.h>
#include "mmsystem.h"
#pragma comment (lib, "Winmm.lib")
Second, the acquisition of audio needs to call 7 functions
1. Waveingetnumdevs: Returns the number of waveform sound input devices that are ready in the system
UINT Waveingetnumdevs (VOID);
2. Waveingetdevcaps: Check the characteristics of the specified waveform input device
Mmresult waveingetdevcaps (
uint_ptr Udeviceid,
lpwaveincaps Pwic, UINT cbwic
);
Udeviceid audio input device identification, or a handle to an open audio input device. personally think that if you get multiple devices in the previous step, you can identify each device with an index.
//pwic A pointer to a WAVEINCAPS structure that contains the audio characteristics of the device.
//cbwic waveincaps The size of the structure, the use of sizeof can be.
The result of the//mmresult function execution// mmsyserr_noerror indicates that the execution succeeded
//Mmsyserr_baddeviceid index is out of bounds
// Mmsyserr_nodriver devices
//Mmsyserr_nomem not ready to allocate or lock memory
Introduce the meaning of Waveincaps structure:
typedef struct {
WORD wmid; Audio device manufacturer-defined driver identification
WORD wpid; Audio input Equipment Product identification
mmversion vdriverversion; Driver version number
TCHAR szpname[maxpnamelen];//manufacturer name
DWORD dwformats; Supported formats, see MSDN
WORD wchannels; Number of channels supported
WORD wReserved1; Retention parameters
} waveincaps;
3. Waveinopen: Open the specified audio input device for recording
Mmresult Waveinopen (
lphwavein phwi, ///Receive a pointer to the Hwavein structure identified by an open audio input device uint_ptr Udeviceid, //Specify a device ID that needs to be opened. You can use Wave_mapper to select a device
to record in the specified recording format Lpwaveformatex pwfx,/ /A required format for recording the WaveFormatEx pointer
dword_ptr dwcallback,//pointing to a callback function, event handle, A window handle, thread identification, and processing of a recording event.
Dword_ptr dwcallbackinstance,//pass to the callback mechanism parameter
DWORD fdwopen //Open device's method ID, specifying the type of callback. See also CSDN
);
Introduce the meaning of WAVEFORMATEX structure:
typedef struct {
WORD wformattag; Waveform sound format, mono dual channel using WAVE_FORMAT_PCM. When included in the waveformatextensible structure, use wave_format_extensible.
WORD nchannels; Channel quantity
DWORD nsamplespersec; Sampling rate. When wFormatTag is WAVE_FORMAT_PCM, there are 8.0khz,11.025khz,22.05khz, and 44.1kHz.
DWORD navgbytespersec; Number of bytes sampled per second. WORD nblockalign is computed by nsamplespersec * nchannels * WBITSPERSAMPLE/8
; The number of bytes per sample. Calculate WORD wbitspersample by nchannels * WBITSPERSAMPLE/8
; Number of sampling bits. When wFormatTag is WAVE_FORMAT_PCM, it is 8 or
WORD cbsize; When wFormatTag is WAVE_FORMAT_PCM, this parameter is ignored
} WaveFormatEx;
Describes the Dwcallback callback function format:
void CALLBACK Waveinproc (
hwavein hwi, //callback device handle for this function
UINT umsg, //waveform sound input information, identity off (wim_close), buffer full (Wim_data), open (Wim_open).
Dword_ptr dwinstance,//user specified in Waveinopen data
dword_ptr dwParam1, //(LPWAVEHDR) dwParam1, user specified buffer
Dword_ptr dwParam2
);
4. Waveinprepareheader: Prepare a buffer for the audio input device
Mmresult Waveinprepareheader (
hwavein hwi, //audio input device handle
LPWAVEHDR pwh,//pointer to the WAVEHDR structure, identifying the prepared buffer
UINT CBWH //wavehdr the size of the structure, the use of sizeof can be
);
Introduction to WAVEHDR Structure:
typedef struct WAVEHDR_TAG {
LPSTR lpdata; Buffer DWORD in waveform format dwbufferlength;//Buffer size
DWORD dwbytesrecorded;//How much data is currently stored
Dword_ PTR Dwuser; User Data
DWORD dwflags; The information provided for the buffer, using the whdr_prepared DWORD dwloops in the Waveinprepareheader function ; The output is used, identifies the number of playback
struct Wavehdr_tag * lpnext;//reserved
dword_ptr reserved; Reserved
} wavehdr, *LPWAVEHDR;
5. Waveinaddbuffer: The buffer is sent to the device, and if the buffer fills up, it does not work. (Parameter ditto)
Mmresult Waveinaddbuffer (
hwavein hwi,
lpwavehdr pwh, UINT CBWH
);
6. Waveinstart: Start recording
Mmresult Waveinstart (
hwavein hwi//device handle
);
7. Waveinclose: Turn off the equipment
Mresult waveinclose (
hwavein hwi//device handle
);
Three, the complete example code is as follows:
RUN.C file #include <Windows.h> #include <stdio.h> #include "mmsystem.h" #pragma comment (lib, "Winmm.lib") vo
ID Playmusi ();
void Waveinitformat (Lpwaveformatex m_waveformat, WORD nch,dword nsamplerate,word bitspersample);
DWORD CALLBACK Miccallback (Hwavein Hwavein, UINT umsg, DWORD dwinstance, DWORD dwParam1, DWORD dwParam2);
void Recordwave ();
void Main () {//playmusi ();
Recordwave ();
while (1);
void Recordwave () {int count = Waveingetnumdevs ();//1 printf ("\ n audio Input quantity:%d\n", count);
Waveincaps Waveincaps; Mmresult Mmresult = Waveingetdevcaps (0,&waveincaps,sizeof (waveincaps))//2 printf ("\ n audio input device:%s\n",
Waveincaps.szpname);
if (mmsyserr_noerror==mmresult) {Hwavein phwi;
WaveFormatEx pwfx;
Waveinitformat (&pwfx,1,8000,8);
printf ("\ n request to open audio input device");
printf ("\ n sampling parameters: Mono 8kHz 8bit\n"); Mmresult=waveinopen (&PHWI,WAVE_MAPPER,&PWFX, (DWORD) (miccallback), null,callback_function);//3 if (
Mmsyserr_noerror==mmresult) {WAVEHDR pwh1;
Char buffer1[10240]; Pwh1.lpdata=buffer1;
pwh1.dwbufferlength=10240;
Pwh1.dwuser=1;
pwh1.dwflags=0;
Mmresult=waveinprepareheader (phwi,&pwh1,sizeof (WAVEHDR));//4 printf ("Prepare buffer 1");
WAVEHDR Pwh2;
Char buffer2[10240];
Pwh2.lpdata=buffer2;
pwh2.dwbufferlength=10240;
pwh2.dwuser=2;
pwh2.dwflags=0;
Mmresult=waveinprepareheader (phwi,&pwh2,sizeof (WAVEHDR));//4 printf ("Prepare buffer 2\n"); if (Mmsyserr_noerror==mmresult) {Mmresult=waveinaddbuffer (phwi,&pwh1,sizeof (WAVEHDR));//5 printf ("\
n Add buffer 1 to the audio input device ");
Mmresult=waveinaddbuffer (phwi,&pwh2,sizeof (WAVEHDR));//5 printf ("\ n Add buffer 2 to the audio input device \ n");
if (Mmsyserr_noerror==mmresult) {Mmresult=waveinstart (PHWI);//6 printf ("\ n request to start recording \ n");
"}}}} DWORD CALLBACK Miccallback (Hwavein Hwavein, UINT umsg, DWORD dwinstance, DWORD dwParam1, DWORD dwParam2) {
Switch (umsg) {case wim_open:printf ("\ n device already open ... \ n");
Break
Case wim_data:printf ("\ n buffer%d full ... \ n", ((LPWAVEHDR) dwParam1)->dwuser); WaveinaddbuFfer (Hwavein, (LPWAVEHDR) dwParam1, sizeof (WAVEHDR));
Break
Case wim_close:printf ("\ n device is closed ... \ n");
Break
Default:break;
return 0; } void Waveinitformat (Lpwaveformatex m_waveformat, WORD nch,dword nsamplerate,word bitspersample) {m_WaveFormat->
wFormatTag = WAVE_FORMAT_PCM;
M_waveformat->nchannels = nch;
M_waveformat->nsamplespersec = nsamplerate;
m_waveformat->navgbytespersec = nsamplerate * nch * BITSPERSAMPLE/8;
M_waveformat->nblockalign = M_waveformat->nchannels * BITSPERSAMPLE/8;
M_waveformat->wbitspersample = BitsPerSample;
m_waveformat->cbsize = 0; void Playmusi () {int error = mcisendstring ("Open c:\\users\\angel\\desktop\\ how much love can be redone. mp3 Alias mydivece", NULL, 0, NUL
L);
if (Error = = 0) {mcisendstring ("play mydivece", NULL, 0, NULL);/playing}}