diff --git a/core/src/main/java/org/gagravarr/opus/OpusAudioData.java b/core/src/main/java/org/gagravarr/opus/OpusAudioData.java index c27e76a..5c2639a 100644 --- a/core/src/main/java/org/gagravarr/opus/OpusAudioData.java +++ b/core/src/main/java/org/gagravarr/opus/OpusAudioData.java @@ -20,6 +20,9 @@ * Raw, compressed audio data */ public class OpusAudioData extends OggStreamAudioData implements OpusPacket { + private int numFrames = -1; + private int numSamples = -1; + public OpusAudioData(OggPacket pkt) { super(pkt); } @@ -30,4 +33,56 @@ public OpusAudioData(byte[] data) { protected boolean isEndOfStream() { return getOggPacket().isEndOfStream(); } + + public int getNumberOfFrames() { + if (numFrames == -1) { + calculateStructure(); + } + return numFrames; + } + public int getNumberOfSamples() { + if (numSamples == -1) { + calculateStructure(); + } + return numSamples; + } + + private void calculateStructure() { + byte[] d = getData(); + numFrames = packet_get_nb_frames(d); + numSamples = numFrames * packet_get_samples_per_frame(d, 48000); + } + + private static int packet_get_samples_per_frame(byte[] data, int fs) { + int audiosize; + if ((data[0]&0x80) != 0) { + audiosize = ((data[0]>>3)&0x3); + audiosize = (fs<>3)&0x3); + if (audiosize == 3) + audiosize = fs*60/1000; + else + audiosize = (fs<5760 || (spp%120)!=0) { + int samples = audioData.getNumberOfSamples(); + if (samples<120 || samples>5760 || (samples%120) != 0) { System.err.println("WARNING: Invalid packet TOC in stream with sid "+sid); return; } - total_samples += spp; - page_samples += spp; + total_samples += samples; + page_samples += samples; total_packets++; //last_packet_duration = spp; - if (max_packet_durationspp) min_packet_duration = spp; + if (max_packet_durationsamples) min_packet_duration = samples; if (max_packet_bytesd.length) min_packet_bytes = d.length; } - private static int packet_get_samples_per_frame(byte[] data, int Fs) { - int audiosize; - if ((data[0]&0x80) != 0) - { - audiosize = ((data[0]>>3)&0x3); - audiosize = (Fs<>3)&0x3); - if (audiosize == 3) - audiosize = Fs*60/1000; - else - audiosize = (Fs<