syedhali / EZAudio

An iOS and macOS audio visualization framework built upon Core Audio useful for anyone doing real-time, low-latency audio processing and visualizations.
Other
4.94k stars 821 forks source link

Error: Failed to read audio data from file waveform (-50) #295

Open norbertmocsnik opened 8 years ago

norbertmocsnik commented 8 years ago

For certain mp3 files getWaveformDataWithNumberOfPoints in EZAudioFile.m crashes because it reaches the end of file sooner than expected.

Here is an example for how it turns out: 0: 4541 1: 4541 ...: 4541 994: 4541 995: 4541 996: 492 997: 0 Error: Failed to read audio data from file waveform (-50)

By default EZAudioFileWaveformDefaultResolution is 1024 so we're expecting to read 1024 times using ExtAudioFileRead but as you can see at read #996 we can already only read 492 frames instead of 4541 as calculated based on the meta data extracted from reading from the file.

I have at least 2 audio files where this is happening, both are mp3, one of them is proper music 44,100 320kbps stereo, the other is an audio book 22,050 mono and some lower bitrate.

Seems like a check for the actual number of bytes read in getWaveformDataWithNumberOfPoints is missing such as:

   for (SInt64 i = 0; i < numberOfPoints; i++)
    {
        UInt32 bufferSize = (UInt32) framesPerBuffer;
        [EZAudioUtilities checkResult:ExtAudioFileRead(self.info->extAudioFileRef,
                                                       &bufferSize,
                                                       audioBufferList)
                            operation:"Failed to read audio data from file waveform"];

        if (bufferSize == 0) {
            // unexpected end of file -- might happen for compressed formats for some reason
            break;
        }

Although I'm not sure at this point if that's the correct solution or remaining buffers/RMS values for the rest of the file that was missing need to be zeroed out.

Not sure what's causing this, perhaps it's how ExtAudioFileRead reads compressed formats, perhaps it's some internal bug, will investigate further but wanted to file an issue in the meantime.

gevariya-ajit commented 7 years ago

Having same problem, @norbertmocsnik do you have any solution?

norbertmocsnik commented 7 years ago

@gevariya-ajit Adding the if above in EZAudioFile.m seems to solve it technically although I'm not sure about the implications. It depends on how you use the data afterwards because now there's less data than expected. It works for my use case for now.