!7 cmodule ConcurrentMP3 > DynPrintLogAndEnabled { transient StreamingMP3Encoder encoder; switchable int bitRate = 64; start { dm_vmBus_onMessage_q startedRecording((IVF1) lambda1 started); dm_vmBus_onMessage_q recordedChunk((IVF1) lambda1 gotChunk); dm_vmBus_onMessage_q doneRecording(r done); } void cleanMeUp { dispose encoder; } void started(File wavFileBeingMade) { if (!enabled) ret; File mp3 = changeExtension(wavFileBeingMade, ".live.mp3"); float sampleRate = dm_audioInputSampleRate(); int channels = dm_audioInputChannels(); print("started MP3 encoding: " + mp3 + " at " + sampleRate + " Hz" + " (" + nChannels(channels) + ")"); encoder = new StreamingMP3Encoder; encoder.mp3 = mp3; encoder.format = javaSound_pcmSignedLittleEndian(sampleRate, channels); encoder.bitRate = bitRate; encoder.init(); vmBus_send startedRecordingMP3(mp3); encoder.onBytesWritten = (array, i, j) -> vmBus_send wroteMP3Chunk(mp3, array, i, j); } void gotChunk(short[] samples) { //print("got " + nSamples(samples)); if (enabled && encoder != null) encoder.addData(samples); } void done { if (encoder == null) ret; File mp3 = encoder.mp3; dispose encoder; vmBus_send newMP3Recording(mp3); print("Wrote " + fileInfo(mp3)); } }