-1

I recently finished a simple socket-based messaging program that uses 256-byte block encryption and wanted to upgrade it to handle sound. After a bit of research, I found that javax.sound.* doesn't seem to be able to play or record a continuous stream of sound. I thought I could use AudioSystem.getAudioInputStream(InputStream), but its Javadoc says:

The stream must point to valid audio file data.

So I can't use it for a continuous stream.

How would I go about making a client capable of sending and receiving continuous streams of audio in Java? (Better yet, how would I make a server capable of taking multiple streams and combining them, for a group chat feature?)

  • To output sound to speakers you can just write bytes to a `SourceDataLine`, where the bytes come from is up to you. – greg-449 Jul 18 '21 at 09:10

1 Answers1

1

The class you are looking for, for streaming incoming audio data is TargetDataLine.

If you want to mix audio, you will first need to derive the PCM from the bytes. PCM values can simply be added together (though often a max and min are applied to prevent overflow values which can sound really horrible), and the results directed to a SourceDataLine.

I guess the trickiest part is reading from the various lines in an orderly fashion. My first guess is to create multiple FIFO buffers that read from your various incoming lines (one FIFO per incoming line). Yet another thread can then poll the data from those buffers on an orderly, on-needed-basis. The main reason for the inclusion of these buffers is to allow the mixing to occur without being affected by any vagaries in the input rate of the various contributing lines. This way, if a line is lagging, there is no risk it will block the whole works, the buffer for that line can simply present 0 values, and the mixing thread can send data continuously to the speakers uninterupted.

Something like a ConcurrentLinkedQueue might be best, as you will need to have both the server inputs and the mixer thread accessing the buffers.

For some reason, though, I've never seen chat done with multiple speakers being mixed live. Usually only one line is allowed to be active at a time. If you try to get it to work with live mixing, I'd be interested in hearing how it goes. I suspect there are solid practical reasons why this isn't done.

I've used this buffering scheme with a single microphone input line. The buffer allowed me to successfully mix that data with live playback from .wav files and synth lines on my PC. I haven't tried inputting audio from a socket yet.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41