4 Replies - 7189 Views - Last Post: 17 October 2010 - 10:04 AM Rate Topic: -----

#1 MathiasVP  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 154
  • Joined: 08-August 10

Play sound recorded with waveInStart()?

Posted 15 October 2010 - 06:25 PM

Hey people!

I just followed a quick tutorial on how to communicate with a microphone, and it compiles and reports no erros through GetLastError(), so that's good...

Now I just have 1 question. How do I play the sound I just recorded into memory with waveInStart?

Here is my code:

#include <iostream>
#include <Windows.h>
#include <MMSystem.h>
using namespace std;

int main()
{
	const int NUMPTS = 44100 * 10;
	int sampleRate = 44100;
	short int waveIn[NUMPTS];

	HWAVEIN hWaveIn;
	WAVEHDR WaveInHdr;
	MMRESULT result;

	WAVEFORMATEX pFormat;
	pFormat.wFormatTag = WAVE_FORMAT_PCM;
	pFormat.nChannels = 1;
	pFormat.nSamplesPerSec = sampleRate;
	pFormat.nAvgBytesPerSec = 2 * sampleRate;
	pFormat.nBlockAlign = 2;
	pFormat.wBitsPerSample = 16;
	pFormat.cbSize = 0;

	result = waveInOpen(&hWaveIn, WAVE_MAPPER, &pFormat, 0, 0, WAVE_FORMAT_DIRECT);

	if(result)
	{
		char fault[256];
		waveInGetErrorTextA(result, fault, 256);
		MessageBoxA(NULL, fault, "Failed to open waveform input device.", MB_OK | MB_ICONEXCLAMATION);
		return 1;
	}

	WaveInHdr.lpData = (LPSTR)waveIn;
	WaveInHdr.dwBufferLength = 2 * NUMPTS;
	WaveInHdr.dwBytesRecorded = 0;
	WaveInHdr.dwUser = 0;
	WaveInHdr.dwFlags = 0;
	WaveInHdr.dwLoops = 0;
	waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

	result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
	if(result)
	{
		MessageBoxA(NULL, "Failed to read block from device", NULL, MB_OK | MB_ICONEXCLAMATION);
		return 1;
	}

	result = waveInStart(hWaveIn);
	if(result)
	{
		MessageBoxA(NULL, "Failed to start recording", NULL, MB_OK | MB_ICONEXCLAMATION);
		return 1;
	}

	cout << "Recording..." << endl;

	do {
	} while(waveInUnprepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR)) == WAVERR_STILLPLAYING);
	waveInClose(hWaveIn);

	return 0;
}


I have a feeling that I need to use http://msdn.microsof...676(VS.85).aspx, but I dont quite understand the first parameter when used with SND_MEMORY.

Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Play sound recorded with waveInStart()?

#2 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 774
  • View blog
  • Posts: 2,293
  • Joined: 20-March 10

Re: Play sound recorded with waveInStart()?

Posted 15 October 2010 - 07:27 PM

You need to use waveoutopen

HWAVEOUT hWaveOut; 
......
if (waveOutOpen (&hWaveOut, WAVE_MAPPER, &pFormat, 
                                0, 0, WAVE_FORMAT_DIRECT))
               {
                   
                    MessageBox (NULL, "Falied ro Play", NULL,
                         MB_ICONEXCLAMATION | MB_OK) ;
               }


Was This Post Helpful? 0
  • +
  • -

#3 MathiasVP  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 154
  • Joined: 08-August 10

Re: Play sound recorded with waveInStart()?

Posted 16 October 2010 - 04:21 AM

Thanks for your reply snoopy11, I still can't make it work though :(
It's just returning immediately after recording, without playing anything

Here's my code now:

#include <iostream>
#include <Windows.h>
#include <MMSystem.h>
using namespace std;

int main()
{
	const int NUMPTS = 44100 * 10;
	int sampleRate = 44100;
	short int waveIn[NUMPTS];

	HWAVEIN hWaveIn;
	WAVEHDR WaveInHdr;
	MMRESULT result;
	HWAVEOUT hWaveOut;

	WAVEFORMATEX pFormat;
	pFormat.wFormatTag = WAVE_FORMAT_PCM;
	pFormat.nChannels = 1;
	pFormat.nSamplesPerSec = sampleRate;
	pFormat.nAvgBytesPerSec = 2 * sampleRate;
	pFormat.nBlockAlign = 2;
	pFormat.wBitsPerSample = 16;
	pFormat.cbSize = 0;

	result = waveInOpen(&hWaveIn, WAVE_MAPPER, &pFormat, 0, 0, WAVE_FORMAT_DIRECT);

	if(result)
	{
		char fault[256];
		waveInGetErrorTextA(result, fault, 256);
		MessageBoxA(NULL, fault, "Failed to open waveform input device.", MB_OK | MB_ICONEXCLAMATION);
		return 1;
	}

	WaveInHdr.lpData = (LPSTR)waveIn;
	WaveInHdr.dwBufferLength = 2 * NUMPTS;
	WaveInHdr.dwBytesRecorded = 0;
	WaveInHdr.dwUser = 0;
	WaveInHdr.dwFlags = 0;
	WaveInHdr.dwLoops = 0;
	waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

	result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
	if(result)
	{
		MessageBoxA(NULL, "Failed to read block from device", NULL, MB_OK | MB_ICONEXCLAMATION);
		return 1;
	}

	result = waveInStart(hWaveIn);
	if(result)
	{
		MessageBoxA(NULL, "Failed to start recording", NULL, MB_OK | MB_ICONEXCLAMATION);
		return 1;
	}

	cout << "Recording..." << endl;

	do {
	} while(waveInUnprepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR)) == WAVERR_STILLPLAYING);

	cout << "Playing" << endl;
	if(waveOutOpen(&hWaveOut, WAVE_MAPPER, &pFormat, 0, 0, WAVE_FORMAT_DIRECT))
	{
		MessageBoxA(NULL, "Failed to replay", NULL, MB_OK | MB_ICONEXCLAMATION );
	}

	do {
	} while(waveOutUnprepareHeader(hWaveOut, &WaveInHdr, sizeof(WAVEHDR)) == WAVERR_STILLPLAYING);

	waveInClose(hWaveIn);
	waveOutClose(hWaveOut);

	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#4 MathiasVP  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 154
  • Joined: 08-August 10

Re: Play sound recorded with waveInStart()?

Posted 16 October 2010 - 03:07 PM

Just wanted to say that I fixed the problem myself using the following:

int main()
{
	const int NUMPTS = 44100 * 10;
	int sampleRate = 44100;
	short int waveIn[NUMPTS];

	HWAVEIN hWaveIn;
	WAVEHDR WaveInHdr;
	MMRESULT result;
	HWAVEOUT hWaveOut;

	WAVEFORMATEX pFormat;
	pFormat.wFormatTag = WAVE_FORMAT_PCM;
	pFormat.nChannels = 1;
	pFormat.nSamplesPerSec = sampleRate;
	pFormat.nAvgBytesPerSec = 2 * sampleRate;
	pFormat.nBlockAlign = 2;
	pFormat.wBitsPerSample = 16;
	pFormat.cbSize = 0;

	result = waveInOpen(&hWaveIn, WAVE_MAPPER, &pFormat, 0, 0, WAVE_FORMAT_DIRECT);

	if(result)
	{
		char fault[256];
		waveInGetErrorTextA(result, fault, 256);
		MessageBoxA(NULL, fault, "Failed to open waveform input device.", MB_OK | MB_ICONEXCLAMATION);
		return 1;
	}

	WaveInHdr.lpData = (LPSTR)waveIn;
	WaveInHdr.dwBufferLength = 2 * NUMPTS;
	WaveInHdr.dwBytesRecorded = 0;
	WaveInHdr.dwUser = 0;
	WaveInHdr.dwFlags = 0;
	WaveInHdr.dwLoops = 0;
	waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

	result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
	if(result)
	{
		MessageBoxA(NULL, "Failed to read block from device", NULL, MB_OK | MB_ICONEXCLAMATION);
		return 1;
	}

	result = waveInStart(hWaveIn);
	if(result)
	{
		MessageBoxA(NULL, "Failed to start recording", NULL, MB_OK | MB_ICONEXCLAMATION);
		return 1;
	}

	cout << "Recording..." << endl;
	Sleep((NUMPTS/sampleRate) * 1000); //Sleep while recording

	cout << "Playing..." << endl;

	if(waveOutOpen(&hWaveOut, WAVE_MAPPER, &pFormat, 0, 0, WAVE_FORMAT_DIRECT))
	{
		MessageBoxA(NULL, "Failed to replay", NULL, MB_OK | MB_ICONEXCLAMATION );
	}

	waveOutWrite(hWaveOut, &WaveInHdr, sizeof(WaveInHdr)); //<-- The line I forgot before
	Sleep((NUMPTS/sampleRate) * 1000); //Sleep for as long as there was recorded

	waveOutUnprepareHeader(hWaveOut, &WaveInHdr, sizeof(WAVEHDR));
	waveInUnprepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
	waveInClose(hWaveIn);
	waveOutClose(hWaveOut);

	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 774
  • View blog
  • Posts: 2,293
  • Joined: 20-March 10

Re: Play sound recorded with waveInStart()?

Posted 17 October 2010 - 10:04 AM

sorry I didnt get back to you but I am glad
you found a solution.

I must admit I have never written a recording program
in console only for Win32 GUI applications.
and there you process things like MM_WOM_OPEN messages.

The only thing that worries me is you dont allocate memory
for the sound sample. Also you dont free the Wave Header.
I dont know how that would affect the program in the long run.

This post has been edited by snoopy11: 17 October 2010 - 10:12 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1