Windows - sound recording program giving noise

154 views Asked by At

I wrote the following program to record the sound through soundcard in windows and print the PCM data from buffer of waveheader. But it gives only the data 32600-32700. Is the problem with my soundcard? I have used WAVE_MAPPER which automatically selects the source...Please help me

#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;
}

I would be very grateful if someone could help me...

1

There are 1 answers

2
jaket On BEST ANSWER

One obvious problem is that you're mixing the usage of 16 and 8 bits. You're buffer is defined as a 16-bit short. Notice your own comment:

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

Yet, when defining the audio format you are specifying 8 bits:

format.wBitsPerSample = 8; //  16 for high quality, 8 for telephone-grade

Either change format.wBitsPerSample = 16 or if you want 8 bit audio then do something like this:

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;
}