Windows - 录音程序发出噪音

时间:2015-06-07 18:05:13

标签: windows audio wav

我编写了以下程序,通过声卡在Windows中录制声音,并从waveheader的缓冲区打印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(&microHandle, 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;
}

如果有人能帮助我,我将非常感激......

1 个答案:

答案 0 :(得分: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;
}