!7 import static x30_pkg.x30_util.VF1; sclass AudioInput > DynPrintLog { int bufSize = 40960; transient AudioLoop loop; transient SimpleLiveValue lvVolume = floatLiveValue(); // in percent visualize { ret centerAndSouthWithMargins(super.visualize(), jCenteredLine(jLiveValueLabel(mapLiveValue(func(float volume) -> S { "Volume: " + iround(volume) + " %" }, S, lvVolume)))); } start { ownTimer(loop = new AudioLoop(bufSize)).start(); } void addAudioListener(VF1 l) { loop.audioListeners.add(l); } void removeAudioListener(VF1 l) { loop.audioListeners.remove(l); } class AudioLoop extends Thread implements AutoCloseable { TargetDataLine line; new Flag stopFlag; byte[] buf; short[] convertedBuf; L> audioListeners = synchroList(); *(int bufSize) { line = javaSound_recordLine(javaSound_cdQuality(), bufSize); buf = new byte[bufSize]; convertedBuf = new short[bufSize/2]; } public void start { line.start°; super.start°; } void stopRecording { // "stop()" was taken if (stopFlag.isUp()) ret; stopFlag.raise°; line.close°; print("Audio input loop stopped."); } public void close { stopRecording(); } public void run° { print("Audio input loop started. Buffer size=" + l(buf) + " (" + iround(l(buf)/44100.0/4*1000) + " ms)"); while (licensed() && !stopFlag.isUp()) { int n = line.read(buf, 0, l(buf)); if (n < l(buf)) print("Weird: Got less than a buffer: " + n + "/" + l(buf)); handleData(buf, n); } } void handleData(byte[] data, int n) { bytesToShorts_littleEndian(buf, convertedBuf, n); short[] convertedData = subShortArray(convertedBuf, 0, n/2); lvVolume.set(shortSamplesToPercentVolume(convertedData)); for i over audioListeners: { // synchro-safe, garbage-free iteration VF1 l = syncGet(audioListeners, i); continue if l == null; callF(l, convertedData); } } } }