// derived from org.gagravarr.opus.OpusFile /* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ lib 1400511 // VorbisJava (to unpack ogg file) lib 1400502 // Concentus 1.0 (to decode OPUS audio in ogg file) import org.gagravarr.ogg.*; import org.gagravarr.ogg.audio.*; import org.gagravarr.opus.*; import static org.gagravarr.ogg.OggStreamIdentifier.OggStreamType; import org.concentus.*; sclass OpusFromStream_v2 implements OggAudioStream, OggAudioHeaders, Closeable { InputStream in; OggStreamPacketReader_v2 r; int sid = -1; int channels; OpusDecoder decoder; OpusInfo info; OpusTags tags; // channels = 1 or 2 (mono/stereo) *(InputStream *in, int *channels) throws IOException, OpusException { decoder = new OpusDecoder(48000, channels); r = new OggStreamPacketReader_v2(in); } bool initialized() { ret tags != null; } bool initStep() throws IOException { OggStreamPacket p = null; // find first packet (info) if (sid < 0) { while ((p = getNextPacket()) != null) { if (p.isBeginningOfStream() && p.getData().length > 10) { if (OpusPacketFactory.isOpusStream(p)) { sid = p.getSid(); info = (OpusInfo) OpusPacketFactory.create(p); ifdef OpusFromStream_debug printVars OpusFromStream(+sid, +info); endifdef break; } } } } // find second packet (tags) if (tags == null) { if ((p = getNextPacketWithSid(sid)) != null) { tags = (OpusTags) OpusPacketFactory.create(p); ifdef OpusFromStream_debug printVars OpusFromStream(+tags); endifdef } } ret initialized(); } public OpusAudioData getNextAudioPacket() throws IOException { if (!initStep()) null; OggStreamPacket p = null; OpusPacket op = null; while ((p = getNextPacketWithSid(sid)) != null) { op = OpusPacketFactory.create(p); if (op cast OpusAudioData) ret op; else warn("Skipping non audio packet " + op + " mid audio stream"); } null; } /** * Returns the Ogg Stream ID */ public int getSid() { ret sid; } /** * This is an Opus file */ public OggStreamType getType() { ret OggStreamIdentifier.OPUS_AUDIO; } public OpusInfo getInfo() { return info; } public OpusTags getTags() { return tags; } public void skipToGranule(long granulePosition) throws IOException { unimplemented(); } /** * Opus doesn't have setup headers, so this is always null */ public OggAudioSetupHeader getSetup() { null; } close { dispose r; dispose in; } // return some audio samples. null if none available short[] nextSamples() throws OpusException, IOException { OpusAudioData data; while ((data = getNextAudioPacket()) != null) { byte[] bytes = data.getData(); if (empty(bytes)) continue; // decode packet short[] pcm = new[5760*channels]; // max opus frame size int nSamples = decoder.decode(bytes, 0, l(bytes), pcm, 0, l(pcm), false); ret subShortArray(pcm, 0, nSamples); } null; } public OggStreamPacket getNextPacket() throws IOException { in.mark(65536); OggStreamPacket p; try { p = r.getNextPacket(); } catch (EOFException e) { ifdef OpusFromStream_debug print("OpusFromStream: EOFException, resetting"); endifdef in.reset(); null; } if (p == null) { ifdef OpusFromStream_debug print("OpusFromStream: packet null, resetting"); endifdef in.reset(); } ifdef OpusFromStream_debug printVars OpusFromStream(packet := p, +in); endifdef ret p; } /** * Returns the next packet with the given SID (Stream ID), or * null if no more packets remain. * Any packets from other streams will be silently discarded. */ public OggStreamPacket getNextPacketWithSid(int sid) throws IOException { OggStreamPacket p = null; while ((p = getNextPacket()) != null) { if (p.getSid() == sid) ret p; } null; } // e.g. for mark/reset static int bufferSizeNeeded() { ret 65536; } }