window如何采集回放设备声音 并重采样
在windows平台下采集输入设备的音频数据资料已经很多了,但是采集声卡回放设备的方法却比较少,在此写下本人开发的一个用于采集声卡回放输出设备(桌面声音)的音频数据,并做重采样处理的功能模块;当然同时也支持从输入设备中采集音频数据。 在实现过程中使用了MMDevice API等较新的接口,该接口在windows vista之后的版本才出现,所以在此提供的代码只支持windows vista以后的版本,包括vista。由于在windows下不同的声卡可以输出不同的音频格式,比如采样率、位深、声道数,所以从声卡设备中获取到的PCM数据格式与每台PC的声卡设置保持一致,因此必须在采集到PCM数据后统一做一次重采样处理,后面提供的代码中将最终输出双声道,s16、44100格式的PCM数据,以便后续处理。其中数据的重采样借助的是libsamplerate开源代码库,其可以使用VS工具直接编译。大家如果需要直接使用一下提供的代码的话需要自己去下载并静态编译libsamplerate库。
下面只贴出头文件,其它代码请到本人的已上传资料中下载;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#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 C64
typedef long long PARAM;
typedef unsigned long longUPARAM;
#else
typedef long PARAM;
typedef unsigned long UPARAM;
#endif
typedef 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 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);
bool Init(bool isPlayBack, const string devGUID = "Default");
void StartCapture();
void StopCapture();
int QueryAudioBuffer(string &outData);
int GetAudioDevices(list<AudioDeviceInfo *> &deviceList, AudioDeviceType deviceType, bool bConnectedOnly);
bool GetDefaultDevice(string &strVal, AudioDeviceType deviceType);
bool GetDefaultMicID(string &strVal);
bool GetDefaultSpeakerID(string &strVal);
protected:
wchar_t* MByteToWChar(uint32_t CodePage,LPCSTR lpcszSrcStr);
char* WCharToMByte(uint32_t CodePage,LPCWSTR lpcwszSrcStr);
bool GetNextBuffer(string &buffer, uint32_t &numFrames);
void FreeData();
protected:
list<AudioDeviceInfo *> m_DeviceList;
string m_CurDeviceID;
string m_CurDeviceName;
IMMDeviceEnumerator *mmEnumerator_;
IMMDevice *mmDevice_;
IAudioClient *mmClient_;
IAudioCaptureClient *mmCapture_;
uint32_t m_SampleWindowSize;
DWORD m_InputChannelMask;
WORD m_InputChannels;
uint32_t m_InputSamplesPerSec;
uint32_t m_InputBufferSize;
uint32_t m_InputBitsPerSample;
bool m_bFloat;
NotAResampler *m_pResampler;
double m_ResampleRatio;
uint8_t *m_pBlankBuffer, *m_pTempResampleBuffer;
uint8_t *m_pDataOutputBuffer;
uint8_t *m_pTmpBuffer;
};
页:
[1]