Recording Formats (waveInOpen())

[b][red]This message was edited by dnewmon at 2005-4-13 23:23:16[/red][/b][hr]
I can record in any of the standard 8-bit formats and they come
out just fine, but when I try recording a 16-bit format it comes
out full of static.

I'm using Audacity to import the "Recording.raw" file and play it.
Using every combination of importing the program gets audio full of
static when its a 16-bit sample.

Sorry for the lack of comments.
Currently it records 10 seconds. (See BUFFER_SIZE)

All I change is the SAMPLE_SEC #define to 1 byte and below I change
the WAVEFORMATEX wBitsPerSample member to 8 and it records a perfect
8-bit sample.

[code]
using namespace std;

// Hz * Bytes * Channels
#define SAMPLE_SEC (44100 * 2 * 2)
#define BUFFER_SIZE (SAMPLE_SEC * 10)

bool select_device(UINT * devId);
bool open_device(UINT devId, LPHWAVEIN device);

int _tmain(int argc, _TCHAR* argv[])
{
UINT devId;
HWAVEIN hDevice;

SetConsoleTitle("Select Device");
select_device(&devId);
if (open_device(devId, &hDevice))
{
cout << "Device Opened for Recording!" << endl;

ofstream output;
output.open("C:\Recording.raw");

cout << "Initializing Buffer: " << BUFFER_SIZE << endl;
WAVEHDR buffer;
ZeroMemory(&buffer, sizeof(buffer));
buffer.lpData = (LPSTR)new BYTE[BUFFER_SIZE];
buffer.dwBufferLength = BUFFER_SIZE;

ZeroMemory(buffer.lpData, BUFFER_SIZE);

if (waveInPrepareHeader(hDevice, &buffer, sizeof(buffer)) == MMSYSERR_NOERROR)
{
waveInAddBuffer(hDevice, &buffer, sizeof(buffer));

cout << "Recording Data..." << endl;
SetConsoleTitle("Recording...");
waveInStart(hDevice);
while ((buffer.dwFlags & WHDR_DONE) != WHDR_DONE)
Sleep(100);
waveInStop(hDevice);

cout << "Recorded " << buffer.dwBytesRecorded << " bytes" << endl;

cout << "Saving Recording..." << endl;
output.write(buffer.lpData, buffer.dwBytesRecorded);

waveInUnprepareHeader(hDevice, &buffer, sizeof(buffer));
}
delete [] buffer.lpData;

SetConsoleTitle("Done!");
cout << "Done!" << endl;
output.close();
waveInClose(hDevice);
}

return 0;
}

bool select_device(UINT * devId)
{
UINT count = waveInGetNumDevs();
UINT index = 1;

WAVEINCAPS caps;
for (UINT i = 0; i < count; i++)
{
if (waveInGetDevCaps(i, &caps, sizeof(caps)) == MMSYSERR_NOERROR)
{
cout << " " << index << ") " << caps.szPname << endl;
index++;
}
}

cout << "Select Wave Device: ";
cin >> i;

while (i < 0 || i >= index)
{
cout << "Invalid Device Selection" << endl;
cout << "Select Wave Device: ";
cin >> i;
}

*devId = i - 1;
return true;
}

bool open_device(UINT devId, LPHWAVEIN device)
{
WAVEFORMATEX format;
format.cbSize = 0;
format.wFormatTag = WAVE_FORMAT_PCM;
format.nChannels = 2;
format.wBitsPerSample = 16;
format.nSamplesPerSec = 44100;
format.nBlockAlign = format.nChannels * (format.wBitsPerSample / 8);
format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
cout << "Bytes/Sec: " << format.nAvgBytesPerSec << endl;

MMRESULT err = waveInOpen(device, WAVE_MAPPER, &format, 0, 0, CALLBACK_NULL|WAVE_FORMAT_DIRECT);
if (err == MMSYSERR_NOERROR)
return true;
else
{
cout << "waveInOpen() : " << err << endl;
return false;
}
}
[/code]


Comments

  • : [b][red]This message was edited by dnewmon at 2005-4-13 23:23:16[/red][/b][hr]
    : I can record in any of the standard 8-bit formats and they come
    : out just fine, but when I try recording a 16-bit format it comes
    : out full of static.
    :
    : I'm using Audacity to import the "Recording.raw" file and play it.
    : Using every combination of importing the program gets audio full of
    : static when its a 16-bit sample.
    :
    : Sorry for the lack of comments.
    : Currently it records 10 seconds. (See BUFFER_SIZE)
    :
    : All I change is the SAMPLE_SEC #define to 1 byte and below I change
    : the WAVEFORMATEX wBitsPerSample member to 8 and it records a perfect
    : 8-bit sample.
    :
    : [code]
    : using namespace std;
    :
    : // Hz * Bytes * Channels
    : #define SAMPLE_SEC (44100 * 2 * 2)
    : #define BUFFER_SIZE (SAMPLE_SEC * 10)
    :
    : bool select_device(UINT * devId);
    : bool open_device(UINT devId, LPHWAVEIN device);
    :
    : int _tmain(int argc, _TCHAR* argv[])
    : {
    : UINT devId;
    : HWAVEIN hDevice;
    :
    : SetConsoleTitle("Select Device");
    : select_device(&devId);
    : if (open_device(devId, &hDevice))
    : {
    : cout << "Device Opened for Recording!" << endl;
    :
    : ofstream output;
    : output.open("C:\Recording.raw");
    :
    : cout << "Initializing Buffer: " << BUFFER_SIZE << endl;
    : WAVEHDR buffer;
    : ZeroMemory(&buffer, sizeof(buffer));
    : buffer.lpData = (LPSTR)new BYTE[BUFFER_SIZE];
    : buffer.dwBufferLength = BUFFER_SIZE;
    :
    : ZeroMemory(buffer.lpData, BUFFER_SIZE);
    :
    : if (waveInPrepareHeader(hDevice, &buffer, sizeof(buffer)) == MMSYSERR_NOERROR)
    : {
    : waveInAddBuffer(hDevice, &buffer, sizeof(buffer));
    :
    : cout << "Recording Data..." << endl;
    : SetConsoleTitle("Recording...");
    : waveInStart(hDevice);
    : while ((buffer.dwFlags & WHDR_DONE) != WHDR_DONE)
    : Sleep(100);
    : waveInStop(hDevice);
    :
    : cout << "Recorded " << buffer.dwBytesRecorded << " bytes" << endl;
    :
    : cout << "Saving Recording..." << endl;
    : output.write(buffer.lpData, buffer.dwBytesRecorded);
    :
    : waveInUnprepareHeader(hDevice, &buffer, sizeof(buffer));
    : }
    : delete [] buffer.lpData;
    :
    : SetConsoleTitle("Done!");
    : cout << "Done!" << endl;
    : output.close();
    : waveInClose(hDevice);
    : }
    :
    : return 0;
    : }
    :
    : bool select_device(UINT * devId)
    : {
    : UINT count = waveInGetNumDevs();
    : UINT index = 1;
    :
    : WAVEINCAPS caps;
    : for (UINT i = 0; i < count; i++)
    : {
    : if (waveInGetDevCaps(i, &caps, sizeof(caps)) == MMSYSERR_NOERROR)
    : {
    : cout << " " << index << ") " << caps.szPname << endl;
    : index++;
    : }
    : }
    :
    : cout << "Select Wave Device: ";
    : cin >> i;
    :
    : while (i < 0 || i >= index)
    : {
    : cout << "Invalid Device Selection" << endl;
    : cout << "Select Wave Device: ";
    : cin >> i;
    : }
    :
    : *devId = i - 1;
    : return true;
    : }
    :
    : bool open_device(UINT devId, LPHWAVEIN device)
    : {
    : WAVEFORMATEX format;
    : format.cbSize = 0;
    : format.wFormatTag = WAVE_FORMAT_PCM;
    : format.nChannels = 2;
    : format.wBitsPerSample = 16;
    : format.nSamplesPerSec = 44100;
    : format.nBlockAlign = format.nChannels * (format.wBitsPerSample / 8);
    : format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
    : cout << "Bytes/Sec: " << format.nAvgBytesPerSec << endl;
    :
    : MMRESULT err = waveInOpen(device, WAVE_MAPPER, &format, 0, 0, CALLBACK_NULL|WAVE_FORMAT_DIRECT);
    : if (err == MMSYSERR_NOERROR)
    : return true;
    : else
    : {
    : cout << "waveInOpen() : " << err << endl;
    : return false;
    : }
    : }
    : [/code]
    :
    :
    :


    i think u should use waveInGetDevCaps() to check the format u want to record if it is supported by your device or not.
    ak

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion