How can I get multichannel PCM input (may be 2 or more channels input from audio interface), and split the bytes into individual channels. I'm new with Java. I've done this with python but it was slow. I'm working with 16 bit signed PCM at 48000 Hz sample rate. I'm confused about Java Sound API terms, I don't know if I have to use TargetDataLine or AudioInputStream for reading audio input. And how can I split the channels to process each one individually.
1 Answers
2
votes
If you are combining wav
file resources, AudioInputStream
is the usual way to read files.
The data is arranged by frame and then by channel and then by byte. For example, if stereo 16-bit, it's two bytes for frame 1, left, two bytes for frame 1, right, then frame 2, two bytes left, two byte right, frame 3, etc.
I assume that you know to combine the two bytes to form PCM values before processing, and then converting the results back to bytes before playback or saving as wav
.
A code snippet example of accessing the data at the byte level is in the Oracle tutorial Using Files and Format Converters, in the section on "Reading Sound Files".
Here is a code example translating LittleEndian bytes to PCM.
pcmVal = ( audioBytes[n] & 0xff ) | ( audioBytes[n + 1] << 8 ) ;