我试图在Windows上以wav格式录制音频。但是,记录的字节总是等于零。我是否误解了Windows API中的某些内容?
void RecordSound(void)
{
static int ChannelOn = 0;
int err;
int fp;
static WAVEFORMATEX fmt;
static WAVEHDR hdr;
HWAVEIN hwi = 1;
fmt.nSamplesPerSec = 44100;
fmt.wBitsPerSample = 16;
fmt.wFormatTag= WAVE_FORMAT_PCM;
fmt.nChannels= 2;
fmt.nBlockAlign = fmt.wBitsPerSample*fmt.nChannels/8;
fmt.nAvgBytesPerSec= fmt.nSamplesPerSec*fmt.nBlockAlign;
hdr.lpData = (LPSTR)buf;
hdr.dwBufferLength = BUF_SIZE;
if(!ChannelOn) {
err = waveInOpen(&hwi, 0, &fmt, 0, 0, 0);
printf("\nerr = %x\n", err);
if(!err) {
waveInAddBuffer(hwi, &hdr, 0);
waveInStart(hwi);
puts("Record channel on\n");
ChannelOn = 1;
}
} else {
waveInClose(hwi);
puts("Record channel off\n");
ChannelOn = 0;
puts("Writing wav to test.wav");
fp = open("test.wav" , "w");
write(fp, &fmt, sizeof(WAVEFORMATEX));
write(fp, &buf, hdr.dwBytesRecorded);
memset(&hdr, 0 , sizeof(WAVEHDR));
memset(&fmt, 0 , sizeof(WAVEFORMATEX));
}
}