WavAudioFormat duration of the generated file is 0 seconds


#1

Hello,

I'm making my first steps with the JUCE Framework (and with C++, too). I want to write a simple Application to convert a Aif-File to a Wav-File.
Here is my Code:

File aifFile = File("/Users/tedferry/Desktop/SoundFile.aif");
FileInputStream *soundFileInputStream = aifFile.createInputStream();

File wavFile = File("/Users/tedferry/Desktop/SoundFile.wav");
FileOutputStream *soundFileOutputStream = wavFile.createOutputStream();

AiffAudioFormat aif;
AudioFormatReader *aifReader = aif.createReaderFor(soundFileInputStream, 0);

WavAudioFormat wav;
AudioFormatWriter *wavWriter = wav.createWriterFor(soundFileOutputStream, 44100, 2, 16, nullptr, 0);

wavWriter->writeFromAudioReader(*aifReader, 0, -1);

The aif-data is written to the wav-file. The only problem is if I'm trying to open the wav-File with the finder on my mac the duration shown is 0 seconds. The filesize on the other hand is nearly as big as the aif-file. 

What I'm missing? I've read the framework documentation with the related classes but I can't find my mistake.

Please let me know, if anyone has any advice for me and sorry for my bad english.

Cheers
Ted

 


#2

Must have seen hundreds of posts on this forum that are almost word-for-word the same as this one..

Like all those other beginners, you're creating some objects and just letting them leak, so their destructors never run, and they never get the chance to finish writing to the file.

I've added leak-detector code that will have thrown an assertion when your program exits to tell you exactly what you're doing wrong. Every C++ tutorial in the world talks endlessly about object lifetime management and that you should never just create bare objects like you're doing. But still every beginner seems to just fail to get it - I give up!


#3

I can understand your answer and hopefully my next questions are not as stupid as this one!

Thanks
Ted


#4

Sorry, I didn't mean your question was stupid - it's a perfectly reasonable and very very common beginner mistake!

With a lot of errors that come up frequently, I manage to add an assertion or something that helps people understand what's wrong, but for this one I don't know what else I could add! Didn't you see the leaked object assertions and think that maybe you should sort that out?