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).

1  
sclass OpusMachine implements IF0_Int, AutoCloseable {
2  
  macro dprint { if (debug) printVars } bool debug;
3  
  
4  
  InputStream in;
5  
  new ByteArraysPartialInputStream buffer;
6  
  new ArraysShortStream sampleStream;
7  
  OpusFromStream opusDecoder;
8  
  int channels = 1;
9  
  int primaryBufSize = 65536;
10  
  int minBytesPreloaded;
11  
  
12  
  long samplesSeen() { ret sampleStream.endOfQueue(); }
13  
  
14  
  *(InputStream *in) {}
15  
  
16  
  void init() ctex {
17  
    if (opusDecoder != null) ret;
18  
    opusDecoder = new OpusFromStream(buffer, channels);
19  
    dprint("OpusMachine", +in);
20  
    preload();
21  
  }
22  
  
23  
  close { dispose in; }
24  
  
25  
  void preload {
26  
    while (in != null && buffer.available() < minBytesPreloaded)
27  
      if (!fillBuffer())
28  
        ret;
29  
  }
30  
  
31  
  bool pullFromDecoder() ctex {
32  
    dprint("pullFromDecoder");
33  
    while ping (true) {
34  
      short[] samples;
35  
      if ((samples = opusDecoder.nextSamples()) != null) {
36  
        dprint("Got " + nSamples(samples));
37  
        ret true with sampleStream.add(samples);
38  
      }
39  
40  
      if (!pullFromSource()) false; // end of stuff
41  
      // we got new material from source, decode again
42  
    }
43  
  }
44  
  
45  
  bool pullFromSource() {
46  
    dprint("pullFromSource");
47  
    ret fillBuffer();
48  
  }
49  
  
50  
  swappable bool fillBuffer() {
51  
    if (in != null) try {
52  
      byte[] buf = new[primaryBufSize];
53  
      int n = in.read(buf);
54  
      if (n > 0) {
55  
        dprint("Got " + nBytes(n));
56  
        buffer.write(takeFirst(buf, n));
57  
        true;
58  
      }
59  
    } catch print {
60  
      dispose in;
61  
    }
62  
    false;
63  
  }
64  
  
65  
  // get next sample (pull mode)
66  
  public int get aka nextSample() ctex {
67  
    init();
68  
    int sample;
69  
    while ping ((sample = sampleStream.read()) == sampleStream.noElement()) {
70  
      if (!pullFromDecoder())
71  
        ret sample;
72  
    }
73  
    ret sample;
74  
  }
75  
  
76  
  CloseableShortIterator sampleIterator() {
77  
    ret new OpusMachineIterator(this);
78  
  }
79  
}

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: 228 / 425
Version history: 26 change(s)
Referenced in: [show references]