How would I implement fast, sample-resolution random seeking with MAD? In
other words, I need to be able to reposition the stream to a specific
sample and get resynced in constant time and without too much overhead.
The MP3 also needs to be streamed from disk rather than loaded entirely in
RAM. I imagine the main problem would be reconstructing the bit pool at
the seek point.
From my understanding of the MP3 format, the furthest back bit pool data
can be stored is 9 frames. So here is my idea:
1. On initial load, create an index of all the frame offsets in the mp3
and cache it in RAM
2. When seeking to sample N, calculate the containing frame number F as
F=N/1152
3. Use the cached frame index to find the file offset of frame F-9
4. Seek to that offset in the file and begin decoding
5. Decode 9 frames silently, ignoring any MAD_ERROR_BADDATAPTR
6. Begin playing at sample N which is within the current frame
Will this work? Is there a faster way that avoids decoding the audio data
for the extra 9 frames?