Problem writing audio files

I am trying to write audio files in different formats using Juce and am facing a few problems

I am facing a problem while trying to write FLAC files at a sample rate of 96000 hz.
The file gets written but produces distorted sound.
I have no problems when I write flac files at 44100 or 48000 hz at 16 bits per sample.
But other formats namely wav,aiff play fine at all sample rates

Also I understand that I cannot write 8 bits per sample using FLAC.
But I get distorted sound when I write FLAC files at 24 bits per sample.
16 bits per sample plays perfectly.

I am using AudioFormatReader for reading from the inputstream and AudioFormatWriter to write into the OutputStream and
using Resamplingaudiosource to do all my resampling.

Could u give me an insight into this


Don’t know… I guess you need to find out in what way it’s distorted, and whether it’s the encoding or decoding that’s getting it wrong.

Are you saying that 24-bit flacs go wrong at every sample rate? And are the 96k ones going wrong at 16 bit, or only at 24 bit?

1)Writing flac at 24 bits/sample for any sample rate produces distorted sound
2)Also Writing Flac at 96khz samplerate produces distored sound regardless of any other parameter


So is it the encoder or decoder? Try loading the files into a non-juce app and see if they work ok.

I am reading a wav file,transcoding it to flac format,resampling it and writing it into a file.All this I do using juce.
I am using quick time to play back the written file.All formats play fine in QuickTime other than (flac/24 bit and flac/96khz)
So I dont think its the decoder.The problem could be with the encoder.


Sorry, that doesn’t make much sense - you say you’re transcoding it to flac, then resampling it? Are you playing the actual flac files in Quicktime? Could be that quicktime isn’t using the latest flac decoder that can handle 24-bit files…

And what’s the distortion? Are the numbers out of range, or is the timing messed up?

Sorry for that bit of confusion.

I read the wav file using AudioformatReader,resample it to the required frequency using ResamplingAudioSource,do the writing using AudioFormatWriter into flac format and play the file in QuickTime
The distortion at 24 bits/sample is noise with actual audio.So the numbers are out of range.The timing is perfect.

Heres the code of what Im doing

double OutputsampleRate = 48000;//Does not Work for 96000

juce::File SrcFile = juce::File(/Input wav file path/);

juce::FileInputStream* InputStream = SrcFile.createInputStream();

if(InputStream == NULL)
return false;

juce::AudioFormat *WavFormat = new juce::WavAudioFormat();

juce::AudioFormatReader *WavFormatReader = WavFormat->createReaderFor (InputStream, true);

if(WavFormatReader == NULL)
return false;

juce::AudioFormatReaderSource *WavReaderSource = new juce::AudioFormatReaderSource(WavFormatReader,false);

juce::ResamplingAudioSource *Resample = new juce::ResamplingAudioSource(WavReaderSource,false);
Resample->setResamplingRatio(WavFormatReader->sampleRate/(double)OutputsampleRate); Resample->prepareToPlay(128,(double)OutputsampleRate);

double InputLength = (double) WavFormatReader->lengthInSamples;

double TotalSamplestoWrite = (int) (InputLength/(_wavFormatReader->sampleRate/OutputsampleRate));

juce::File FlacFile = juce::File(/Output Flac file path/);

juce::FileOutputStream* OutputStream = NULL;
juce::AudioFormatWriter* AudioFormatWriter = NULL;
juce::AudioFormat *AudioRenderer = NULL;

AudioRenderer = new juce::FlacAudioFormat();
OutputStream = FlacFile.createOutputStream();

if(OutputStream == NULL)
return false;

AudioFormatWriter = AudioRenderer->createWriterFor (OutputStream,OutputsampleRate,2,24,NULL,0);

//Works if bitspersample is 16

bool Success = AudioFormatWriter->writeFromAudioSource(Resample,TotalSamplestoWrite);


The output file doesnt play with Winamp as well.


Ok, well I just spent a long time replicating this but the 24 bit flacs play fine in tracktion. My quicktime and realplayer won’t attempt to play the file, but I suspect that you’ve got players using old flac codecs that can’t cope with 24 bit, rather than there being anything wrong with the file itself. You can see from a quick look at the juce flac code that there’s nothing special about the number of bits it encodes, there’s really nothing to go wrong there.

Did you actually try playing your files in something like the juce demo audio page?

Well,I couldnt play it in Juce Demo but I could in a player called VU player.I looked at the juce code and found that the FlacAudioWriter and Reader are for Native Flac format.Just out of curiosity,I would like to know if juce supports(Ogg-FLAC).


Well yes, it’s native flac. I’ve not looked into doing an Ogg-flac version.

It played fine for me in the juce demo - are you saying it’s playing incorrectly, or not at all?

I am using the AudioDemo to read from a microphone and then trying to use an AudioFormatWriter to write it out in wav. The writer’s write takes const int** while the audioCallBack gets float**. Can I just cast them over or do I need to convert them? Is this a Resample thing?