// 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. */ import org.gagravarr.ogg.*; import org.gagravarr.ogg.audio.*; sclass OpusFromStream implements OggAudioStream, OggAudioHeaders, Closeable { private OggFile ogg; private OggPacketReader r; private int sid = -1; private OpusInfo info; private OpusTags tags; public OpusFile(File f) throws IOException, FileNotFoundException { this(new OggFile(new FileInputStream(f))); } /** * Opens the given file for reading */ public OpusFile(OggFile ogg) throws IOException { this(ogg.getPacketReader()); this.ogg = ogg; } /** * Loads a Opus File from the given packet reader. */ public OpusFile(OggPacketReader r) throws IOException { this.r = r; OggPacket p = null; while( (p = r.getNextPacket()) != null ) { if (p.isBeginningOfStream() && p.getData().length > 10) { if (OpusPacketFactory.isOpusStream(p)) { sid = p.getSid(); break; } } } if (sid == -1) { throw new IllegalArgumentException("Supplied File is not Opus"); } // First two packets are required to be info then tags info = (OpusInfo)OpusPacketFactory.create( p ); tags = (OpusTags)OpusPacketFactory.create( r.getNextPacketWithSid(sid) ); // Everything else should be audio data } public OpusAudioData getNextAudioPacket() throws IOException { OggPacket p = null; OpusPacket op = null; while( (p = r.getNextPacketWithSid(sid)) != null ) { op = OpusPacketFactory.create(p); if(op instanceof OpusAudioData) { return (OpusAudioData)op; } else { System.err.println("Skipping non audio packet " + op + " mid audio stream"); } } return null; } /** * Skips the audio data to the next packet with a granule * of at least the given granule position. * Note that skipping backwards is not currently supported! */ public void skipToGranule(long granulePosition) throws IOException { r.skipToGranulePosition(sid, granulePosition); } /** * Returns the Ogg Stream ID */ public int getSid() { return sid; } /** * This is an Opus file */ public OggStreamType getType() { return OggStreamIdentifier.OPUS_AUDIO; } public OpusInfo getInfo() { return info; } public OpusTags getTags() { return tags; } /** * Opus doesn't have setup headers, so this is always null */ public OggAudioSetupHeader getSetup() { return null; } public void close() throws IOException { dispose r; dispose ogg; } }