Saving Wav to Disk

9 posts / 0 new
Last post
Crandles
Offline
Last seen: 6 months 17 hours ago
Joined: 12 Jun 2012 - 18:16
Saving Wav to Disk

Hey,

I just recently started using JUCE and have found it very useful so far but I'm having a problem saving a .wav file to disk. The code giving me trouble is is:

	//save the filtered output
	ScopedPointer<WavAudioFormat> wavFormat =  new WavAudioFormat();
	File output("C:\\DEV\\FFT2\\filtered_output.wav");
	FileOutputStream *fost = output.createOutputStream();
	AudioSampleBuffer write_to_disk(&float_output, 1, M + P - 1);
	ScopedPointer<AudioFormatWriter> writer = wavFormat->createWriterFor(fost, 44100, 1, 16, StringPairArray(), 0);
	writer->writeFromAudioSampleBuffer(write_to_disk, 0, M + P - 1);

I know the data in float_output is good because I wrote it to a .txt file and imported it in Matlab to verify. I'm not sure if I'm opening the file/creating the streams/or calling write correctly.

Matlab gives the following output.

>> output = wavread('filtered_output.wav');
??? Error using ==> wavread at 166
Incorrect chunk size information in WAV file.

Any help would be greatly appreciated.

Thanks,
Graham

jules
Online
Last seen: 13 min 10 sec ago
Joined: 29 Apr 2013 - 18:37
Re: Saving Wav to Disk

Does your writer object definitely get deleted? That's usually the cause when you see unfinished files being written.

Crandles
Offline
Last seen: 6 months 17 hours ago
Joined: 12 Jun 2012 - 18:16
Re: Saving Wav to Disk

If it's a scoped pointer shouldn't it get deleted automatically?

jules
Online
Last seen: 13 min 10 sec ago
Joined: 29 Apr 2013 - 18:37
Re: Saving Wav to Disk

Yes, but your code snippet doesn't show what happens after you created it.

Crandles
Offline
Last seen: 6 months 17 hours ago
Joined: 12 Jun 2012 - 18:16
Re: Saving Wav to Disk

	//save the filtered output
	ScopedPointer<WavAudioFormat> wavFormat =  new WavAudioFormat();
	File output("C:\\DEV\\FFT2\\filtered_output.wav");
	FileOutputStream *fost = output.createOutputStream();
	AudioSampleBuffer write_to_disk(&filtered_output, 1, M + P - 1);
	ScopedPointer<AudioFormatWriter> writer = wavFormat->createWriterFor(fost, 44100, 1, 16, StringPairArray(), 0);
	writer->writeFromAudioSampleBuffer(write_to_disk, 0, M + P - 1);

	std::cout << "The Kehoe Nation rules! (Press enter)" << std::endl;
	std::cin.ignore(1);
	return 0;

Writing the file to disk is the last thing the program does before exiting. I give a prompt telling me it's finished and then it waits for me to hit enter.

jules
Online
Last seen: 13 min 10 sec ago
Joined: 29 Apr 2013 - 18:37
Re: Saving Wav to Disk

yeah.. but the writer is still in scope when you do your std::cin stuff.

Crandles
Offline
Last seen: 6 months 17 hours ago
Joined: 12 Jun 2012 - 18:16
Re: Saving Wav to Disk

	ScopedPointer<WavAudioFormat> wavFormat =  new WavAudioFormat();
	File output("C:\\DEV\\FFT2\\filtered_output.wav");
	FileOutputStream *fost = output.createOutputStream();
	AudioSampleBuffer write_to_disk(&filtered_output, 1, M);
	AudioFormatWriter* writer = wavFormat->createWriterFor(fost, 44100, 1, 16, StringPairArray(), 0);
	if(writer->writeFromAudioSampleBuffer(write_to_disk, 0, M))
	{
		std::cout << "Saving .wav succeeded" << std::endl;
	}
	else
	{
		std::cout << "Saving .wav failed" << std::endl;
	}
	delete writer;

Even when I delete the writer Matlab gives me the same error and I can't play the file in Windows Media Player. Is there something else I'm doing wrong?

jules
Online
Last seen: 13 min 10 sec ago
Joined: 29 Apr 2013 - 18:37
Re: Saving Wav to Disk

Are you deleting the file before you write to it? Otherwise, you'll just be sticking your new data on the end of the existing file..

Crandles
Offline
Last seen: 6 months 17 hours ago
Joined: 12 Jun 2012 - 18:16
Re: Saving Wav to Disk

That was precisely the problem! Thanks a bunch.