Click here to Skip to main content
15,886,963 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: one doubt Pin
Nuri Ismail23-Sep-09 3:02
Nuri Ismail23-Sep-09 3:02 
AnswerRe: one doubt Pin
Richard MacCutchan23-Sep-09 3:02
mveRichard MacCutchan23-Sep-09 3:02 
QuestionRotate Metafile displayed in a Static Control Pin
Erik22-Sep-09 23:47
Erik22-Sep-09 23:47 
QuestionWhy Messagebox display twice It's Urgent Pin
jadhavjitendrar22-Sep-09 22:59
jadhavjitendrar22-Sep-09 22:59 
AnswerRe: Why Messagebox display twice It's Urgent [modified] PinPopular
CPallini22-Sep-09 23:39
mveCPallini22-Sep-09 23:39 
AnswerRe: Why Messagebox display twice It's Urgent Pin
theCPkid23-Sep-09 1:25
theCPkid23-Sep-09 1:25 
AnswerRe: Why Messagebox display twice It's Urgent Pin
David Crow23-Sep-09 4:41
David Crow23-Sep-09 4:41 
QuestionHow to write mic data to .wav file ? Pin
Souldrift22-Sep-09 22:18
Souldrift22-Sep-09 22:18 
Good morning,

I have trouble creating a wave file from the data stream I got from the microphone input.
The recording seems to go quite well but know I´m stuck at the task of converting that data into a wave file.

I recorded the mic data into a WAVEHDR structure like this ...

const int NUMPTS = 44100 * 10;		// 10 seconds
int sampleRate = 44100;
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

HWAVEIN     hWaveIn;
WAVEHDR     WaveInHdr;
MMRESULT    result;

int main( int argc, char *argv[] )
{
	// Specify recording parameters
	WAVEFORMATEX pFormat;
	pFormat.wFormatTag=WAVE_FORMAT_PCM;     // simple, uncompressed format
	pFormat.nChannels=1;                    //  1=mono, 2=stereo
	pFormat.nSamplesPerSec=sampleRate;      // 44100
	pFormat.nAvgBytesPerSec=sampleRate*2;   // = nSamplesPerSec * n.Channels * wBitsPerSample/8
	pFormat.nBlockAlign=2;                  // = n.Channels * wBitsPerSample/8
	pFormat.wBitsPerSample=16;              //  16 for high quality, 8 for telephone-grade
	pFormat.cbSize=0;

	result = waveInOpen(&hWaveIn, WAVE_MAPPER,&pFormat, 0L, 0L, WAVE_FORMAT_DIRECT);
	if (result)
	{
		char fault[256];
		waveInGetErrorText(result, fault, 256);

		return 1;
	}

	// Set up and prepare header for input
	WaveInHdr.lpData = (LPSTR)waveIn;
	WaveInHdr.dwBufferLength = NUMPTS*2;
	WaveInHdr.dwBytesRecorded=0;
	WaveInHdr.dwUser = 0L;
	WaveInHdr.dwFlags = 0L;
	WaveInHdr.dwLoops = 0L;
	waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

	// Insert a wave input buffer
	result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
	if (result)
	{
		return 1;
	}


	// Commence sampling input
	result = waveInStart(hWaveIn);
	if (result)
	{
		return 1;
	}


	// Wait until finished recording
	do
	{
		// record for 10 seconds
	} 
	while (waveInUnprepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR))==WAVERR_STILLPLAYING);

	waveInClose(hWaveIn);

	save();

	std::cin.get();
	return 0;
}


... and now I am trying to store the data to a wave file like this:
bool save()
{
	int subchunk2size = WaveInHdr.dwBufferLength*1*2;

	fstream myFile ("test.wav", ios::out | ios::binary);

	// write the wav file per the wav file format
	myFile.seekp (0, ios::beg); 
	myFile.write ("RIFF", 4);					// chunk id
	myFile.write ((char*) 36+subchunk2size, 4);			// chunk size (36 + SubChunk2Size))
	myFile.write ("WAVE", 4);					// format
	myFile.write ("fmt ", 4);					// subchunk1ID
	myFile.write ((char*) 16, 4);					// subchunk1size (16 for PCM)
	myFile.write ((char*) 1, 2);					// AudioFormat (1 for PCM)
	myFile.write ((char*) 1, 2);					// NumChannels
	myFile.write ((char*) 44100, 4);				// sample rate
	myFile.write ((char*) 88200, 4);				// byte rate (SampleRate * NumChannels * BitsPerSample/8)
	myFile.write ((char*) 2, 2);					// block align (NumChannels * BitsPerSample/8)
	myFile.write ((char*) 16, 2);					// bits per sample
	myFile.write ("data", 4);					// subchunk2ID
	myFile.write ((char*) subchunk2size, 4);         		// subchunk2size (NumSamples * NumChannels * BitsPerSample/8)
		
	myFile.write (WaveInHdr.lpData, WaveInHdr.dwBufferLength);	// data

	return true;
}


But somewhere I got a bad memory access or such something since I get a read error (in memcpy.asm).
Can anybody help me here?

Souldrift
AnswerRe: How to write mic data to .wav file ? Pin
Souldrift22-Sep-09 22:39
Souldrift22-Sep-09 22:39 
QuestionString split functionality Pin
NarVish22-Sep-09 21:07
NarVish22-Sep-09 21:07 
AnswerRe: String split functionality Pin
chandu00422-Sep-09 21:09
chandu00422-Sep-09 21:09 
AnswerRe: String split functionality Pin
Hamid_RT22-Sep-09 21:30
Hamid_RT22-Sep-09 21:30 
GeneralRe: String split functionality Pin
NarVish22-Sep-09 21:37
NarVish22-Sep-09 21:37 
GeneralRe: String split functionality Pin
CPallini22-Sep-09 22:09
mveCPallini22-Sep-09 22:09 
GeneralRe: String split functionality Pin
NarVish22-Sep-09 23:59
NarVish22-Sep-09 23:59 
GeneralRe: String split functionality Pin
CPallini23-Sep-09 0:25
mveCPallini23-Sep-09 0:25 
GeneralRe: String split functionality Pin
Richard MacCutchan23-Sep-09 0:30
mveRichard MacCutchan23-Sep-09 0:30 
AnswerRe: String split functionality Pin
CPallini22-Sep-09 21:37
mveCPallini22-Sep-09 21:37 
QuestionMessage box display twice Pin
jadhavjitendrar22-Sep-09 20:51
jadhavjitendrar22-Sep-09 20:51 
AnswerRe: Message box display twice Pin
theCPkid22-Sep-09 21:40
theCPkid22-Sep-09 21:40 
Questionautomation files and excel Pin
prithaa22-Sep-09 20:40
prithaa22-Sep-09 20:40 
QuestionHelp with regex in VS2008 Pin
theCPkid22-Sep-09 20:22
theCPkid22-Sep-09 20:22 
AnswerRe: Help with regex in VS2008 Pin
theCPkid22-Sep-09 21:37
theCPkid22-Sep-09 21:37 
QuestionSave as Excel file Pin
Davitor22-Sep-09 19:58
Davitor22-Sep-09 19:58 
AnswerRe: Save as Excel file Pin
Davitor22-Sep-09 21:29
Davitor22-Sep-09 21:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.