如果自己完成,不使用其他库的话,就是 录音,然后分析录音数据,录音的话我有代码
/* 录音wave部分
*
* 曙光 2014年8月2日
*/
#include
#include
#pragma comment(lib, "winmm.lib")
static WAVEFORMATEX WaveFormat;
static HWAVEIN WaveIn;
static WAVEHDR WaveHdr[2];
static short Data1[8820], Data2[8820];
static BOOL WaveOver = FALSE;
static volatile int HdrIndex;
// extern void WaveRecordStart(void);
// extern void WaveRecordStop(void);
extern void OutputWaveData(short *data, DWORD dataLen); // 导出数据,此函数需要注意线程安全
// 回调函数
static void CALLBACK WaveProc(HWAVEOUT, UINT uMsg, DWORD, DWORD, DWORD) {
if ((uMsg != WIM_DATA) || WaveOver) {
return;
}
OutputWaveData((short *) WaveHdr[HdrIndex].lpData, WaveHdr[HdrIndex].dwBytesRecorded);
waveInAddBuffer(WaveIn, WaveHdr + HdrIndex, sizeof(WAVEHDR)); // 有人说这里不能调用,可能会线程死锁,但是测试没发现问题
HdrIndex = ! HdrIndex; // 0 or 1
}
// 初始化设备并且开始录音
void WaveRecordStart(void) {
WaveFormat.cbSize = 0;
WaveFormat.nChannels = 2;
WaveFormat.nSamplesPerSec = 44100;
WaveFormat.nBlockAlign = 4;
WaveFormat.nAvgBytesPerSec = 88200;
WaveFormat.wBitsPerSample = 16;
WaveFormat.wFormatTag = 1;
waveInOpen(&WaveIn, WAVE_MAPPER, &WaveFormat, (DWORD_PTR) &WaveProc, 0, CALLBACK_FUNCTION);
WaveOver = FALSE;
HdrIndex = 0;
WaveHdr[0].lpData = (LPSTR) Data1;
WaveHdr[0].dwBufferLength = sizeof(Data1);
WaveHdr[1].lpData = (LPSTR) Data2;
WaveHdr[1].dwBufferLength = sizeof(Data2);
WaveHdr[0].dwBytesRecorded = WaveHdr[1].dwBytesRecorded = 0;
WaveHdr[0].dwUser = WaveHdr[1].dwUser = 0;
WaveHdr[0].dwFlags = WaveHdr[1].dwFlags = 1;
WaveHdr[0].dwLoops = WaveHdr[1].dwLoops = 1;
WaveHdr[0].lpNext = WaveHdr[1].lpNext = 0;
WaveHdr[0].reserved = WaveHdr[1].reserved = 0;
waveInPrepareHeader(WaveIn, WaveHdr, sizeof(WAVEHDR));
waveInAddBuffer(WaveIn, WaveHdr, sizeof(WAVEHDR));
waveInPrepareHeader(WaveIn, WaveHdr + 1, sizeof(WAVEHDR));
waveInAddBuffer(WaveIn, WaveHdr + 1, sizeof(WAVEHDR));
waveInStart(WaveIn);
}
// 结束录音
void WaveRecordStop(void) {
WaveOver = TRUE;
waveInStop(WaveIn);
waveInReset(WaveIn);
waveInUnprepareHeader(WaveIn, WaveHdr, sizeof(WAVEHDR));
waveInUnprepareHeader(WaveIn, WaveHdr + 1, sizeof(WAVEHDR));
waveInClose(WaveIn);
}