gpt4 book ai didi

windows - 为什么录音机代码可以工作8位,但不能工作16位?

转载 作者:行者123 更新时间:2023-12-03 02:21:34 25 4
gpt4 key购买 nike

我正在尝试在Windows上录制音频,这是我的代码。它适用于8位,但不能适用于16位。谁能帮我?
#包括
#包括
#包括
#pragma comment(lib,“winmm.lib”)
使用命名空间std;

int test(){

HWAVEIN microHandle;
WAVEHDR waveHeader;

MMRESULT result = 0;

WAVEFORMATEX waveformat;
waveformat.wFormatTag = WAVE_FORMAT_PCM;

waveformat.wBitsPerSample=8;
waveformat.nSamplesPerSec=16000;//8000;
waveformat.nAvgBytesPerSec=waveformat.nSamplesPerSec*waveformat.nSamplesPerSec/8;

waveformat.nChannels=1;
waveformat.nBlockAlign=waveformat.nChannels*waveformat.wBitsPerSample/8;

waveformat.cbSize=0;

result = waveInOpen(&microHandle, WAVE_MAPPER, &waveformat, 0L, 0L, CALLBACK_EVENT);

if (result)
{
cout << "Fail step 1" << endl;
cout << result << endl;
Sleep(10000);
return 0;
}

const int BUFSIZE = 16000*4;
char * buf = (char *)malloc(BUFSIZE);
// Set up and prepare header for input
waveHeader.lpData = (LPSTR)buf;
waveHeader.dwBufferLength = BUFSIZE;
waveHeader.dwBytesRecorded=0;
waveHeader.dwUser = 0L;
waveHeader.dwFlags = 0L;
waveHeader.dwLoops = 0L;
waveInPrepareHeader(microHandle, &waveHeader, sizeof(WAVEHDR));

// Insert a wave input buffer
result = waveInAddBuffer(microHandle, &waveHeader, sizeof(WAVEHDR));

if (result)
{
cout << "Fail step 2" << endl;
cout << result << endl;
Sleep(10000);
return 0;
}

result = waveInStart(microHandle);

if (result)
{
cout << "Fail step 3" << endl;
cout << result << endl;
Sleep(10000);
return 0;
}


// Wait until finished recording
do {} while (waveInUnprepareHeader(microHandle, &waveHeader, sizeof(WAVEHDR))==WAVERR_STILLPLAYING);

FILE *fp = fopen("output.pcm","w");
fwrite(buf,1,BUFSIZE,fp);
fclose(fp);

waveInClose(microHandle);

return 0;
}
void main()
{
test();
}

如果我将参数waveformat.wBitsPerSample = 8设置为可以正确记录音频,
但是如果我将其设置为waveformat.wBitsPerSample = 16,它将记录噪声!!!
谁能帮我?

谢谢。

最佳答案

它应该BU FILE * fp = fopen(“output.pcm”,“wb”);非文件* fp = fopen(“output.pcm”,“w”);

关于windows - 为什么录音机代码可以工作8位,但不能工作16位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11859318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com