Libraryless. Click here for Pure Java version (4524L/29K).
1 | // TODO: copy buffers less (have multiple listeners keep buffers without copying) |
2 | |
3 | sclass AudioInputDispatcher { |
4 | new Flag stopFlag; |
5 | short[] convertedBuf; |
6 | L<VF1<short[]>> audioListeners; |
7 | SimpleLiveValue<Float> lvVolume; |
8 | bool cloneDataOnce; |
9 | bool verbose; |
10 | O currentListener; // who is holding up audio processing |
11 | |
12 | *(int bufSize) { |
13 | convertedBuf = new short[bufSize/2]; |
14 | } |
15 | |
16 | void handleData(byte[] data) { |
17 | handleData(data, l(data)); |
18 | } |
19 | |
20 | void handleData(byte[] data, int n) { |
21 | if (verbose) printWithMilliseconds("Handling audio data (" + nBytes(n) + ", first: " + first(data) + ")"); |
22 | pcall { |
23 | bytesToShorts_littleEndian(data, convertedBuf, n); |
24 | short[] convertedData = subShortArray(convertedBuf, 0, n/2); |
25 | if (lvVolume != null) { |
26 | float vol = shortSamplesToPercentVolume(convertedData); |
27 | if (verbose) { |
28 | printStruct(subShortArray(convertedData, 0, 5)); |
29 | //print("Volume: " + vol); |
30 | } |
31 | lvSet(lvVolume, vol); |
32 | } |
33 | |
34 | if (cloneDataOnce) |
35 | convertedData = cloneShortArray(convertedData); |
36 | |
37 | for i over audioListeners: { // synchro-safe, garbage-free iteration |
38 | VF1<short[]> l = syncGet(audioListeners, i); |
39 | continue if l == null; |
40 | try { |
41 | currentListener = l; |
42 | pcallF(l, convertedData); |
43 | } finally { |
44 | currentListener = null; |
45 | } |
46 | } |
47 | } |
48 | if (verbose) printWithMilliseconds("Done handling audio data"); |
49 | } |
50 | } |
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: | 342 / 674 |
Version history: | 8 change(s) |
Referenced in: | [show references] |