Scott Hyndman
Scott Hyndman

Reputation: 933

ExtAudioFileSeek followed by ExtAudioFileRead returning many sequential 0 samples (impossible)

I'm attempting to write a waveform visualizer for the iPhone, and I'm encountering an interesting issue with ExtAudioFileRead.

I'm reading from a VBR MP3 file. My client format is 44.1kHz LPCM, mono.

I will seek to a position I would like to visualize, and read in 1024 frames. What I'm seeing is that a 70% or 80% of the time, I'm getting zeroes back for the first 512 or so frames. The read is reporting that it has successfully read all requested frames.

Furthermore, I have verified that I am seeking to the correct position. If I place the read results into an audio queue, it is clearly the correct position in the file.

So what gives? Any thoughts on why this might be the case? I can't find anything in the docs relating to this issue.

Upvotes: 2

Views: 1182

Answers (1)

Scott Hyndman
Scott Hyndman

Reputation: 933

I managed to solve this by seeking to a position 1024 frames before the position of the requested frames, reading 1024 + requestedLength frames, then pulling the last requestedLength frames out of the buffer.

I don't know for sure why this solution works, but I have a theory (in case anyone is reading this)

In a compressed format, the audio file is broken into packets. Frames mid packet require any previous frames (from the same packet) to be decoded in order to be understood.

Speculation to follow

Maybe when you seek mid-packet with ExtAudioFileSeek this decoding does not take place, and zeroes will be returned until you hit the next packet boundary.

My solution might work because I'm backtracking far enough to read in all frames prior to the ones I want from the same packet, so decoding does take place.

Upvotes: 3

Related Questions