Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

184
LINES

< > BotCompany Repo | #1032917 // OpusFromStream_v2

JavaX fragment (include) [tags: use-pretranspiled]

Uses 521K of libraries. Compilation Failed (6322L/37K).

// 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; }
}

Author comment

Began life as a copy of #1032905

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1032917
Snippet name: OpusFromStream_v2
Eternal ID of this version: #1032917/2
Text MD5: ed1e341c49cc56de18588e4faad0ec56
Transpilation MD5: bcbc6e20ecb2406fd873b9029aef59f9
Author: stefan
Category: javax / audio
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-10 21:55:52
Source code size: 4951 bytes / 184 lines
Pitched / IR pitched: No / No
Views / Downloads: 84 / 174
Version history: 1 change(s)
Referenced in: [show references]