// 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 implements OggAudioStream, OggAudioHeaders, Closeable { OggPacketReader 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 OggPacketReader(in); } bool initialized() { ret tags != null; } bool initStep() throws IOException { OggPacket p = null; // find first packet (info) if (sid < 0) { while ((p = r.getNextPacket()) != null) { if (p.isBeginningOfStream() && p.getData().length > 10) { if (OpusPacketFactory.isOpusStream(p)) { sid = p.getSid(); info = (OpusInfo) OpusPacketFactory.create(p); break; } } } } // find second packet (tags) if (tags == null) { if ((p = r.getNextPacketWithSid(sid)) != null) tags = (OpusTags) OpusPacketFactory.create(r.getNextPacketWithSid(sid)); } ret initialized(); } public OpusAudioData getNextAudioPacket() throws IOException { if (!initStep()) null; OggPacket p = null; OpusPacket op = null; while ((p = r.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; } // 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; } }