Use WindowsAPI to get audio recordings

Source: Internet
Author: User

Non-reproduced

Introduction to the acquisition of audio streams using winmm.h

You first need to include the following reference objects

#include <Windows.h>
#include "mmsystem.h"
#pragma comment (lib, "Winmm.lib")

Audio acquisition requires 7 functions to be called

1. Waveingetnumdevs: Returns the number of waveform sound input devices 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 the audio input device identity, or a handle to an open audio input device.//personally, if you have more than one device in the previous step, you can identify each device with an index.//    //Pwic A pointer to the WAVEINCAPS structure that contains the audio characteristics of the device.////Cbwic the size of the WAVEINCAPS structure, use sizeof.////results of mmresult function execution//Mmsyserr_noerror indicates successful execution//Mmsyserr_baddeviceid index out of bounds//Mmsyserr_nodriver not ready for the device//Mmsyserr_nomem cannot allocate or lock memory

Introduction to the meaning of Waveincaps structural body

 typedef struct   {WORD wmid;                 //  audio device manufacturer-defined driver identification  WORD wpid; //            Mmversion vdriverversion; //     driver version number  TCHAR Szpname[maxpnamelen]; //     manufacturer name             DWORD Dwformats; //     supported formats, see MSDN             WORD Wchannels; //     number of channels supported             WORD wReserved1; //  reserved parameter } Waveincaps; 

3. Waveinopen: Open the specified audio input device for recording

mmresult waveinopen (  lphwavein       phwi,                // pointer  to Hwavein structure that receives an open audio input device identity Uint_ptr       Udeviceid,            // Specify a device ID that needs to be opened. You can use Wave_mapper to select a device  to record in a specified recording format Lpwaveformatex pwfx,                //  dword_ptr      dwcallback,/        /  Points to a callback function, an event handle, a window handle, a thread identity, and processes the recording event.  Dword_ptr      // pass to callback mechanism parameter  DWORD          fdwopen            // Open device method ID, Specifies the type of callback. See Csdn);

Introduction to the meaning of WaveFormatEx structural body

typedefstruct{WORD wFormatTag; //waveform sound format, mono dual channel using WAVE_FORMAT_PCM. When included in the waveformatextensible structure, use the wave_format_extensible.WORD nchannels;//Number of channelsDWORD nsamplespersec;//sample rate. When wFormatTag is WAVE_FORMAT_PCM, there are 8.0khz,11.025khz,22.05khz, and 44.1kHz.DWORD navgbytespersec;//the number of sampled bytes per second. Calculated by Nsamplespersec * Nchannels * WBITSPERSAMPLE/8WORD nblockalign;//the number of bytes per sample. Calculated by Nchannels * WBITSPERSAMPLE/8WORD wBitsPerSample;//The number of sample bits. When wFormatTag is WAVE_FORMAT_PCM, it is 8 orWORD cbsize;//when wFormatTag is WAVE_FORMAT_PCM, this parameter is ignored} WaveFormatEx;

Introducing the Dwcallback callback function format

void CALLBACK Waveinproc (  hwavein hwi,          / / callback device handle for this function  UINT umsg,            // waveform Sound input information, identification off (wim_close), buffer Full (wim_data), open (Wim_open).  // data specified by the user in Waveinopen   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,//  A pointer to the WAVEHDR structure that identifies the size of the prepared buffer  UINT CBWH        //WAVEHDR structure, using sizeof );


Introducing the WAVEHDR structure

typedefstructWavehdr_tag {LPSTR lpdata; //a buffer that points to the waveform formatDWORD dwbufferlength;//size of the bufferDWORD dwbytesrecorded;//How much data is currently storedDword_ptr Dwuser;//User DataDWORD DwFlags;//The information provided for the buffer, used in the Waveinprepareheader function whdr_preparedDWORD Dwloops;//use when output, identify the number of plays    structWavehdr_tag * LPNEXT;//reservedDWORD_PTR reserved;//reserved} WAVEHDR, *lpwavehdr;

5. Waveinaddbuffer: Sends the buffer to the device and does not work if the buffer fills up. (parameter same as above)

Mmresult Waveinaddbuffer (  hwavein hwi,   lpwavehdr PWH,   

6. Waveinstart: Start recording

mmresult Waveinstart (  hwavein hwi  //device handle);

7. Waveinclose: Turn off the device

mresult waveinclose (  hwavein hwi  //device handle);

The following example:

http://download.csdn.net/detail/long7782/7771019

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.