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

79
LINES

< > BotCompany Repo | #1033017 // OpusMachine

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

Uses 521K of libraries. Click here for Pure Java version (17494L/112K).

sclass OpusMachine implements IF0_Int, AutoCloseable {
  macro dprint { if (debug) printVars } bool debug;
  
  InputStream in;
  new ByteArraysPartialInputStream buffer;
  new ArraysShortStream sampleStream;
  OpusFromStream opusDecoder;
  int channels = 1;
  int primaryBufSize = 65536;
  int minBytesPreloaded;
  
  long samplesSeen() { ret sampleStream.endOfQueue(); }
  
  *(InputStream *in) {}
  
  void init() ctex {
    if (opusDecoder != null) ret;
    opusDecoder = new OpusFromStream(buffer, channels);
    dprint("OpusMachine", +in);
    preload();
  }
  
  close { dispose in; }
  
  void preload {
    while (in != null && buffer.available() < minBytesPreloaded)
      if (!fillBuffer())
        ret;
  }
  
  bool pullFromDecoder() ctex {
    dprint("pullFromDecoder");
    while ping (true) {
      short[] samples;
      if ((samples = opusDecoder.nextSamples()) != null) {
        dprint("Got " + nSamples(samples));
        ret true with sampleStream.add(samples);
      }

      if (!pullFromSource()) false; // end of stuff
      // we got new material from source, decode again
    }
  }
  
  bool pullFromSource() {
    dprint("pullFromSource");
    ret fillBuffer();
  }
  
  swappable bool fillBuffer() {
    if (in != null) try {
      byte[] buf = new[primaryBufSize];
      int n = in.read(buf);
      if (n > 0) {
        dprint("Got " + nBytes(n));
        buffer.write(takeFirst(buf, n));
        true;
      }
    } catch print {
      dispose in;
    }
    false;
  }
  
  // get next sample (pull mode)
  public int get aka nextSample() ctex {
    init();
    int sample;
    while ping ((sample = sampleStream.read()) == sampleStream.noElement()) {
      if (!pullFromDecoder())
        ret sample;
    }
    ret sample;
  }
  
  CloseableShortIterator sampleIterator() {
    ret new OpusMachineIterator(this);
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1033017
Snippet name: OpusMachine
Eternal ID of this version: #1033017/27
Text MD5: b45f310e45df68b390529aee8b27f294
Transpilation MD5: fc8f985c56fc869bb92ef347db121d36
Author: stefan
Category: javax / opus
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-11 18:24:49
Source code size: 1929 bytes / 79 lines
Pitched / IR pitched: No / No
Views / Downloads: 156 / 330
Version history: 26 change(s)
Referenced in: [show references]