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

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

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

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

[code] //save the filtered output
ScopedPointer 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 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;[/code]

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.

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

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?

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…

That was precisely the problem! Thanks a bunch.

I’m trying to get this to work. What is the datatype of filtered_output?

FYI be careful copying the code above - there’s some really bad C++ style in there. Search the juce repo for writeFromAudioSampleBuffer and I’m sure you’ll find some better example code to copy.

1 Like