Trying to create a peak file from an audio file


I’m trying to build a “peak array” to display an audio file waveform.

I wrote that piece of code :

        peaksArraySize = (int)(audioFormatReader->lengthInSamples/blockSize);
	peaksArray = BlockMinMax[peaksArraySize];

	int64 posInAudioFile = 0;
	int posInArray = 0;
	float minLeft = 1;
	float maxLeft = -1;
	float minRight = 1;
	float maxRight = -1;
	int buildingWaveformPercentCompleted;

	while (posInAudioFile < audioFormatReader->lengthInSamples) {
		buildingWaveformPercentCompleted = posInAudioFile*100/audioFormatReader->lengthInSamples;

		peaksArray[posInArray].min = std::min(minLeft,minRight);
		peaksArray[posInArray].max = std::max(maxLeft,maxRight);




but it’s very very slow, it seems to take exponentially more time as the file is read.

Should I use something else than the AudioFormatReader ?

Many thanks for helping a newbie!


You should make sure you use a BufferedInputStream to buffer your FileInputStream - that’ll make a big difference.


I used the following code to create my reader :

		//load file 
		File audioFile(fileName);

		AudioFormatManager audioFormatManager;

		audioFormatReader = audioFormatManager.createReaderFor(audioFile);

		peaksArraySize = (int)(audioFormatReader->lengthInSamples/blockSize)+1;
		peaksArray = new BlockMinMax[peaksArraySize];


I tried to replace the createReaderFor call with a BufferedInputStream

		BufferedInputStream * bufferedInputStream = new BufferedInputStream(new FileInputStream(audioFile),blockSize*10,true);
		audioFormatReader = audioFormatManager.createReaderFor(bufferedInputStream);

(blockSize is 512 here)

but it’s even worse.

Check your parameters to readMaxLevel - looks to me like you’re reading the whole file instead of just the bit you want.

Thanks Jules!

You were right, the 2nd argument must be blockSize, not posInAudioFile+blockSize.

I will try not to ask for help when not needed anymore!