请用c++代码实现一个录音程序,可通过麦克风录制用户的语音,并保存为音频文件
时间: 2024-02-13 12:06:45 浏览: 827
好的,以下是一个简单的C++录音程序的代码示例,基于Windows平台下的WinAPI实现:
```c++
#include <Windows.h>
#include <mmsystem.h>
#include <iostream>
#include <fstream>
#pragma comment(lib, "winmm.lib")
int main()
{
// 打开麦克风设备
WAVEFORMATEX wfx = {};
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = 1;
wfx.nSamplesPerSec = 16000;
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nChannels * 2;
wfx.nBlockAlign = wfx.nChannels * 2;
wfx.wBitsPerSample = 16;
HWAVEIN hWaveIn;
MMRESULT result = waveInOpen(&hWaveIn, WAVE_MAPPER, &wfx, 0, 0, WAVE_FORMAT_DIRECT);
if (result != MMSYSERR_NOERROR)
{
std::cerr << "Failed to open microphone device.\n";
return 1;
}
// 设置录音缓冲区
const int kBufferSize = 16000;
char buffer[kBufferSize] = {};
WAVEHDR waveHdr = {};
waveHdr.lpData = buffer;
waveHdr.dwBufferLength = kBufferSize;
result = waveInPrepareHeader(hWaveIn, &waveHdr, sizeof(WAVEHDR));
if (result != MMSYSERR_NOERROR)
{
std::cerr << "Failed to prepare header for recording.\n";
waveInClose(hWaveIn);
return 1;
}
// 开始录音
result = waveInAddBuffer(hWaveIn, &waveHdr, sizeof(WAVEHDR));
if (result != MMSYSERR_NOERROR)
{
std::cerr << "Failed to add buffer for recording.\n";
waveInUnprepareHeader(hWaveIn, &waveHdr, sizeof(WAVEHDR));
waveInClose(hWaveIn);
return 1;
}
result = waveInStart(hWaveIn);
if (result != MMSYSERR_NOERROR)
{
std::cerr << "Failed to start recording.\n";
waveInUnprepareHeader(hWaveIn, &waveHdr, sizeof(WAVEHDR));
waveInClose(hWaveIn);
return 1;
}
// 等待录音结束
std::cout << "Recording...\n";
while (waveHdr.dwFlags != WHDR_DONE)
{
Sleep(10);
}
// 停止录音
result = waveInStop(hWaveIn);
if (result != MMSYSERR_NOERROR)
{
std::cerr << "Failed to stop recording.\n";
waveInUnprepareHeader(hWaveIn, &waveHdr, sizeof(WAVEHDR));
waveInClose(hWaveIn);
return 1;
}
result = waveInUnprepareHeader(hWaveIn, &waveHdr, sizeof(WAVEHDR));
if (result != MMSYSERR_NOERROR)
{
std::cerr << "Failed to unprepare header for recording.\n";
waveInClose(hWaveIn);
return 1;
}
waveInClose(hWaveIn);
// 保存录音为WAV文件
std::ofstream ofs("record.wav", std::ios::binary);
if (!ofs)
{
std::cerr << "Failed to create WAV file.\n";
return 1;
}
const int kDataSize = waveHdr.dwBytesRecorded;
const int kChunkSize = kDataSize + 36;
char header[44] = {};
header[0] = 'R';
header[1] = 'I';
header[2] = 'F';
header[3] = 'F';
*(int*)(header + 4) = kChunkSize - 8;
header[8] = 'W';
header[9] = 'A';
header[10] = 'V';
header[11] = 'E';
header[12] = 'f';
header[13] = 'm';
header[14] = 't';
header[15] = ' ';
*(int*)(header + 16) = 16;
*(short*)(header + 20) = 1;
*(short*)(header + 22) = 1;
*(int*)(header + 24) = wfx.nSamplesPerSec;
*(int*)(header + 28) = wfx.nAvgBytesPerSec;
*(short*)(header + 32) = wfx.nBlockAlign;
*(short*)(header + 34) = wfx.wBitsPerSample;
header[36] = 'd';
header[37] = 'a';
header[38] = 't';
header[39] = 'a';
*(int*)(header + 40) = kDataSize;
ofs.write(header, sizeof(header));
ofs.write(buffer, kDataSize);
ofs.close();
std::cout << "Record saved as record.wav.\n";
return 0;
}
```
该代码通过`waveInOpen`函数打开麦克风设备,设置音频格式后,使用`waveInPrepareHeader`函数为录音缓冲区做好准备。录音开始后,通过`waveInAddBuffer`将录音缓冲区加入到录音设备中,`waveInStart`启动录音,等待`waveHdr.dwFlags`标志位变为`WHDR_DONE`,即录音结束后,关闭录音设备并保存录音为WAV格式文件。
阅读全文