sclass StreamSoundToAPlay implements AutoCloseable { Process process; InputStream in; OutputStream out; int sampleRate = 44100, channels = 2, bits = 16; int bufSizeMS = 0; // 0 = default double duration = yearsToSeconds(1); // max seconds GSWavFile wavStream; long frameCounter = 0; void start ctex { assertOnPATH("aplay"); Process process = Runtime.getRuntime().exec("aplay" + (bufSizeMS == 0 ? "" : " -B " + bufSizeMS)); // should probably print those instead drainStreamInNewThread(process.getInputStream()); drainStreamInNewThread(process.getErrorStream()); out = process.getOutputStream(); long numFrames = (long)(duration * sampleRate); wavStream = GSWavFile.newWavFile(out, null, channels, numFrames, bits, sampleRate); } void writeSamples(short[] samples) ctex { if (samples != null) for (short x : samples) { wavStream.writeSample(x); ++frameCounter; } } public void close { dispose wavStream; // also closes the stream & thereby ends the process } }