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

50
LINES

< > BotCompany Repo | #1027052 // AudioInputDispatcher

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

Libraryless. Click here for Pure Java version (4524L/29K).

// TODO: copy buffers less (have multiple listeners keep buffers without copying)

sclass AudioInputDispatcher {
  new Flag stopFlag;
  short[] convertedBuf;
  L<VF1<short[]>> audioListeners;
  SimpleLiveValue<Float> lvVolume;
  bool cloneDataOnce;
  bool verbose;
  O currentListener; // who is holding up audio processing

  *(int bufSize) {
    convertedBuf = new short[bufSize/2];
  }
  
  void handleData(byte[] data) {
    handleData(data, l(data));
  }
  
  void handleData(byte[] data, int n) {
    if (verbose) printWithMilliseconds("Handling audio data (" + nBytes(n) + ", first: " + first(data) + ")");
    pcall {
      bytesToShorts_littleEndian(data, convertedBuf, n);
      short[] convertedData = subShortArray(convertedBuf, 0, n/2);
      if (lvVolume != null) {
        float vol = shortSamplesToPercentVolume(convertedData);
        if (verbose) {
          printStruct(subShortArray(convertedData, 0, 5));
          //print("Volume: " + vol);
        }
        lvSet(lvVolume, vol);
      }
  
      if (cloneDataOnce)
        convertedData = cloneShortArray(convertedData);
        
      for i over audioListeners: { // synchro-safe, garbage-free iteration
        VF1<short[]> l = syncGet(audioListeners, i);
        continue if l == null;
        try {
          currentListener = l;
          pcallF(l, convertedData);
        } finally {
          currentListener = null;
        }
      }
    }
    if (verbose) printWithMilliseconds("Done handling audio data");
  }
}

Author comment

Began life as a copy of #1018247

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027052
Snippet name: AudioInputDispatcher
Eternal ID of this version: #1027052/9
Text MD5: 8068a1ea870f0122843c88c8d4f441fa
Transpilation MD5: e6deaaeae69d7da2903f16e00df48d46
Author: stefan
Category: javax / audio
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-15 14:03:46
Source code size: 1544 bytes / 50 lines
Pitched / IR pitched: No / No
Views / Downloads: 223 / 546
Version history: 8 change(s)
Referenced in: [show references]