In the Windows platform to capture the input device audio data is a lot, but the method of collecting sound card playback device is relatively small, in this writing I developed a sound card playback output device (desktop sound) audio data, and do the resampling processing function module And, of course, it also supports capturing audio data from input devices.
Newer interfaces such as the Mmdevice API are used in the implementation process, and the interface appears only after the Windows Vista version, so the code provided here supports only the later versions of Windows Vista, including Vista.
Because different sound cards in Windows can output different audio formats, such as sample rate, bit depth, number of channels, the PCM data format obtained from the sound card device is consistent with the sound card settings of each PC, so it is necessary to do a resampling process once the PCM data has been collected. The code provided later will eventually output the PCM data in the two-channel, S16, 44100 format for subsequent processing. The resampling of the data is based on the Libsamplerate open source codebase, which can be compiled directly using the VS tool. If you need to use the provided code directly, you need to download and statically compile the Libsamplerate library.
The following only stick to the paper, other code please to my uploaded information to download;
#pragma once#include <list> #include <string> #include <stdint.h> #include <mmdeviceapi.h> #include <Audioclient.h> #include <propsys.h> #include <Functiondiscoverykeys_devpkey.h> #include ". /libsamplerate/samplerate.h "#pragma comment (lib, " Libsamplerate.lib ") using namespace std;# ifdef c64typedef long long param;typedef unsigned long long uparam; #elsetypedef long param;typedef unsigned long UPARAM; #endiftypedef char *lpstr;typedef const char *lpcstr;typedef wchar_t *wstr;typedef const wchar_t *cwstr;typedef tchar *TSTR;typedef const TCHAR *CTSTR; #define default_sample_rate 44100#define ksaudio_ speaker_4point1 (ksaudio_speaker_quad| speaker_low_frequency) #define KSAUDIO_SPEAKER_3POINT1 (Ksaudio_speaker_ stereo| speaker_front_center| speaker_low_frequency) #define KSAUDIO_SPEAKER_2POINT1 (Ksaudio_speaker_ stereo| speaker_low_frequency) #define saferelease (Var) if (Var) {var->release (); var = null;} Enum edges {edgeleft = 0x01,edgeright = 0x02,edgetop = 0x04,edgebottom = 0x08,};struct&nBsp Audiodeviceinfo{string strid;string strname;~audiodeviceinfo () {strid.empty (); Strname.empty ();}}; union tripletolong{long val;struct {word wval; byte tripleval; byte lastbyte;};}; struct notaresampler{src_state *resampler;uint64_t jumprange;}; Enum audiodevicetype {adt_playback,adt_recording};class cdesktopaudiodevice{public: Cdesktopaudiodevice (void); ~cdesktopaudiodevice (void); Boolinit (bool isplayback, const string devGUID = "Default"); Voidstartcapture (); Voidstopcapture (); Intqueryaudiobuffer (string &outdata); Intgetaudiodevices (List<audiodeviceinfo *> &devicelist, audiodevicetype devicetype, bool bconnectedonly); Boolgetdefaultdevice (string &strval, Audiodevicetype devicetype); Boolgetdefaultmicid (String &strval); Boolgetdefaultspeakerid (string &strval);p ROTECTED:WCHAR_T*MBYtetowchar (UINT32_T&NBSP;CODEPAGE,LPCSTR&NBSP;LPCSZSRCSTR); Char*wchartombyte (uint32_t codepage,lpcwstr &NBSP;LPCWSZSRCSTR); Boolgetnextbuffer (string &buffer, uint32_t &numframes); voidFreeData ();p Rotected:list<audiodeviceinfo *> m_devicelist;stringm_curdeviceid;stringm_curdevicename;i Mmdeviceenumerator*mmenumerator_;immdevice*mmdevice_;iaudioclient*mmclient_;iaudiocaptureclient*mmcapture_; Uint32_tm_samplewindowsize;dwordm_inputchannelmask; Wordm_inputchannels;uint32_tm_inputsamplespersec;uint32_tm_inputbuffersize;uint32_tm_inputbitspersample;boolm_ Bfloat; Notaresampler*m_presampler;doublem_resampleratio;uint8_t*m_pblankbuffer, *m_ptempresamplebuffer;uint8_t*m _pdataoutputbuffer;uint8_t*m_ptmpbuffer;};
This article is from the "Big bro" blog, make sure to keep this source http://9662841.blog.51cto.com/9652841/1585759
Window how to collect playback device sound and resample