0

How does one calculate the amount of bytes to skip with the InputStream.skip() method if you want to start the InputStream of a .mp3 file at a certain position in time?

I have access to the following data: The point that you want to start at in seconds

  • The mp3's sample rate
  • The mp3's sample size
  • The mp3's framerate
  • The mp3's framesize

I have tried searching for a formula/algorithm that describes how to calculate it but I could not find it. Do any of you know how to do this?

-Edit

I tried doing framesize * framerate * position(seconds) but that was off by a factor 10. Dividing it by 10 still had it off by 3 seconds when skipped to 50 seconds in the song, even more when skipping larger chunks.

ImJustACowLol
  • 826
  • 2
  • 9
  • 27
  • Possible duplicate of [How to get audio data from a MP3?](http://stackoverflow.com/questions/938304/how-to-get-audio-data-from-a-mp3) – teppic Nov 27 '16 at 20:57
  • @teppic no, I'm using JLayer – ImJustACowLol Nov 27 '16 at 21:07
  • The linked answer describes the use of Jlayer. – teppic Nov 27 '16 at 21:54
  • @teppic The linked answer does not provide anything regards seeking within mp3 files. Aside from that the link provided uses MP3SPI which I am not using. Without MP3SPI code is fastly different. – ImJustACowLol Nov 27 '16 at 22:16
  • [This answer might help](http://stackoverflow.com/questions/9980852/how-to-implement-seek-on-mp3), particularly the by the author of [BpmDJ](http://bpmdj.yellowcouch.org/credits.html) linking to a git repo with some examples _(The git repository for the Random Access Decoder can be obtained from git://yellowcouch.org/home/git/JLayerDemos.git/)_ – teppic Nov 27 '16 at 22:46

1 Answers1

1

I am doing the same calculation in C++. In case a googler reaches this page, here is how I do it. So far it works fine. I assume the User wants to jump to time skipToTime (in seconds):

int numberOfFrames = (skipToTime * sampleRate)/1152;
int skipPositionInBytes = (numberOfFrames * frameSize);

One point is that the frameSize is not always the same. Specially, first and second frames might have different sizes than others. So it is better to measure them separately.

I assume the first two frames had different sizes and you calculated them, then the whole calculation will look like this:

int numberOfFrames = (skipToTime * sampleRate)/1152;
numberOfFrames -= 2; // Or whatever it took to reach a stable frame size
int skipPositionInBytes = (numberOfFrames * frameSize) + OFFSET;

OFFSET is the total number of bytes it took to reach a stable frame size. If it took 2 frames then:

OFFSET = frameSize1 + frameSize2;
Ehsan Enayati
  • 299
  • 2
  • 16