Windows - 录音程序发出噪音
Windows - sound recording program giving noise
我写了下面的程序来通过windows中的声卡录制声音,并从波头的缓冲区打印PCM数据。但它只给出数据 32600-32700。是我声卡的问题吗?我使用了 WAVE_MAPPER 自动选择来源...请帮助我
#include <Windows.h>
#include <MMSystem.h>
#include <iostream>
using namespace std;
int main(){
HWAVEIN microHandle;
WAVEHDR waveHeader;
MIXERCAPS mixerCaps;
WAVEFORMATEX format;
//HWAVEOUT hwo; // play
while (1){
const int NUMPTS = 44100 * 0.01; // 10 seconds
int sampleRate = 44100; //can get frequency from here
short int waveIn[NUMPTS]; // 'short int' is a 16-bit type; I request 16-bit samples below
// for 8-bit capture, you'd use 'unsigned char' or 'BYTE' 8-bit types
MMRESULT result = 0;
format.wFormatTag = WAVE_FORMAT_PCM; // simple, uncompressed format
format.wBitsPerSample = 8; // 16 for high quality, 8 for telephone-grade
format.nChannels = 1; // 1=mono, 2=stereo
format.nSamplesPerSec = sampleRate; // 22050
format.nAvgBytesPerSec = format.nSamplesPerSec*format.nChannels*format.wBitsPerSample / 8;
// = nSamplesPerSec * n.Channels * wBitsPerSample/8
format.nBlockAlign = format.nChannels*format.wBitsPerSample / 8;
// = n.Channels * wBitsPerSample/8
format.cbSize = 0;
result = waveInOpen(µHandle, WAVE_MAPPER, &format, 0L, 0L, WAVE_FORMAT_DIRECT);
cout << "checking step 1" << endl;
if (result)
{
cout << "Fail step 1" << endl;
cout << result << endl;
Sleep(10000);
return 0;
}
// Set up and prepare header for input
waveHeader.lpData = (LPSTR)waveIn;
waveHeader.dwBufferLength = NUMPTS;// *2;//why *2
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));
int NumOfMixers = mixerGetNumDevs();
cout << NumOfMixers << endl;
//cout<<(char*)mixerCaps.szPname<<endl;
//system("pause");
cout << "checking step 2" << endl;
if (result)
{
cout << "Fail step 2" << endl;
cout << result << endl;
Sleep(10000);
return 0;
}
//system("pause");
result = waveInStart(microHandle);
cout << "checking step 3......started recording..." << endl;
if (result)
{
cout << "Fail step 3" << endl;
cout << result << endl;
Sleep(10000);
return 0;
}
// Wait until finished recording
do { cout << "still"; } while (waveInUnprepareHeader(microHandle, &waveHeader, sizeof(WAVEHDR)) == WAVERR_STILLPLAYING);
waveInStop(microHandle);
waveInReset(microHandle);
//waveInUnprepareHeader(hwi, lpWaveHdr, sizeof(WAVEHDR));
waveInClose(microHandle);
//printing the buffer
for (int i = 0; i < waveHeader.dwBufferLength; i++)
{
if (waveIn[i] > 0)
cout << i << "\t" << waveIn[i] << endl;
}
}
system("pause");
//waveInClose(microHandle);
return 0;
}
如果有人能帮助我,我将不胜感激...
一个明显的问题是您混合使用了 16 位和 8 位。你的缓冲区被定义为 16 位短。注意你自己的评论:
short int waveIn[NUMPTS]; // 'short int' is a 16-bit type; I request 16-bit samples below
// for 8-bit capture, you'd use 'unsigned char' or 'BYTE' 8-bit types
然而,在定义音频格式时您指定了 8 位:
format.wBitsPerSample = 8; // 16 for high quality, 8 for telephone-grade
要么更改 format.wBitsPerSample = 16
,要么如果您想要 8 位音频,则执行如下操作:
unsigned char waveIn[NUMPTS];
...
format.wBitsPerSample = 8; // 16 for high quality, 8 for telephone-grade
//printing the buffer
for (int i = 0; i < waveHeader.dwBufferLength; i++)
{
cout << i << "\t" << (int)waveIn[i] << endl;
}
我写了下面的程序来通过windows中的声卡录制声音,并从波头的缓冲区打印PCM数据。但它只给出数据 32600-32700。是我声卡的问题吗?我使用了 WAVE_MAPPER 自动选择来源...请帮助我
#include <Windows.h>
#include <MMSystem.h>
#include <iostream>
using namespace std;
int main(){
HWAVEIN microHandle;
WAVEHDR waveHeader;
MIXERCAPS mixerCaps;
WAVEFORMATEX format;
//HWAVEOUT hwo; // play
while (1){
const int NUMPTS = 44100 * 0.01; // 10 seconds
int sampleRate = 44100; //can get frequency from here
short int waveIn[NUMPTS]; // 'short int' is a 16-bit type; I request 16-bit samples below
// for 8-bit capture, you'd use 'unsigned char' or 'BYTE' 8-bit types
MMRESULT result = 0;
format.wFormatTag = WAVE_FORMAT_PCM; // simple, uncompressed format
format.wBitsPerSample = 8; // 16 for high quality, 8 for telephone-grade
format.nChannels = 1; // 1=mono, 2=stereo
format.nSamplesPerSec = sampleRate; // 22050
format.nAvgBytesPerSec = format.nSamplesPerSec*format.nChannels*format.wBitsPerSample / 8;
// = nSamplesPerSec * n.Channels * wBitsPerSample/8
format.nBlockAlign = format.nChannels*format.wBitsPerSample / 8;
// = n.Channels * wBitsPerSample/8
format.cbSize = 0;
result = waveInOpen(µHandle, WAVE_MAPPER, &format, 0L, 0L, WAVE_FORMAT_DIRECT);
cout << "checking step 1" << endl;
if (result)
{
cout << "Fail step 1" << endl;
cout << result << endl;
Sleep(10000);
return 0;
}
// Set up and prepare header for input
waveHeader.lpData = (LPSTR)waveIn;
waveHeader.dwBufferLength = NUMPTS;// *2;//why *2
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));
int NumOfMixers = mixerGetNumDevs();
cout << NumOfMixers << endl;
//cout<<(char*)mixerCaps.szPname<<endl;
//system("pause");
cout << "checking step 2" << endl;
if (result)
{
cout << "Fail step 2" << endl;
cout << result << endl;
Sleep(10000);
return 0;
}
//system("pause");
result = waveInStart(microHandle);
cout << "checking step 3......started recording..." << endl;
if (result)
{
cout << "Fail step 3" << endl;
cout << result << endl;
Sleep(10000);
return 0;
}
// Wait until finished recording
do { cout << "still"; } while (waveInUnprepareHeader(microHandle, &waveHeader, sizeof(WAVEHDR)) == WAVERR_STILLPLAYING);
waveInStop(microHandle);
waveInReset(microHandle);
//waveInUnprepareHeader(hwi, lpWaveHdr, sizeof(WAVEHDR));
waveInClose(microHandle);
//printing the buffer
for (int i = 0; i < waveHeader.dwBufferLength; i++)
{
if (waveIn[i] > 0)
cout << i << "\t" << waveIn[i] << endl;
}
}
system("pause");
//waveInClose(microHandle);
return 0;
}
如果有人能帮助我,我将不胜感激...
一个明显的问题是您混合使用了 16 位和 8 位。你的缓冲区被定义为 16 位短。注意你自己的评论:
short int waveIn[NUMPTS]; // 'short int' is a 16-bit type; I request 16-bit samples below
// for 8-bit capture, you'd use 'unsigned char' or 'BYTE' 8-bit types
然而,在定义音频格式时您指定了 8 位:
format.wBitsPerSample = 8; // 16 for high quality, 8 for telephone-grade
要么更改 format.wBitsPerSample = 16
,要么如果您想要 8 位音频,则执行如下操作:
unsigned char waveIn[NUMPTS];
...
format.wBitsPerSample = 8; // 16 for high quality, 8 for telephone-grade
//printing the buffer
for (int i = 0; i < waveHeader.dwBufferLength; i++)
{
cout << i << "\t" << (int)waveIn[i] << endl;
}