[Speex-dev] Calculate the audio duration of a speex file

Conrad Parker conrad at metadecks.org
Thu Sep 4 16:29:41 PDT 2008

2008/9/5 Nathan Thatcher <n8thatcher at gmail.com>:
> I am having some trouble calculating the duration of my speex files. I am
> writing a speex player in Java and I am using the JSpeex port of the codec
> which works but doesn't give me the audio duration which I need to show
> progress or implement a scrubber. JSpeex appears to be all but dead but I
> don't see any other option for Java playback.
> I have tried the method posted here:
> http://lists.xiph.org/pipermail/speex-dev/2006-May/004509.html but it looks
> like I don't have direct access to the number of packets in the file. I am
> currently reading through the entire speex file manually, finding the 'OggS'
> tags and then applying an offset to get the page segments value for each
> page. From here I am unsure how to get the actual number of packets in the
> page which according to the link above I need.

sounds like you have started writing a parser for Ogg framing. You can probably
use the parser from JOrbis or Cortado.

> I think I might be going about this the wrong way. How is the best way to
> get the duration of the speex audio?

The granulepos of the last page should tell you the total number of
samples in the file. That should be good enough for most files.

If the beginning of the file was chopped off (ie. it doesn't begin at
time 0), then you'll need to subtract the timestamp of the first
sample to get the total duration. To get that initial timestamp, you
take the granulepos of the first page, which represents its end time,
and subtract the number of samples encoded in that page.

To find the number of samples encoded in a page, you find the number
of completed packets and multiply that by the number of speex frames
per packet, and by the number of samples per speex frame.

To find the number of completed packets, you take the number of
segments, and subtract one if the last lacing value in the page is 255
(which incidentally answers the question you actually asked ;-)



More information about the Speex-dev mailing list