JavaSound: Extract one channel of stereo audio

2019-08-05 18:03发布

问题:

I have a stereo wave file that I need to read and play back only the selected channel. What is the best way to accomplish this?

回答1:

When you bring the wav file in via an AudioInputStream, use the AudioFileFormat info to convert the bytes to PCM. The data for the right and left alternates. So, if the line is 16-bit, you will have 4 bytes per frame. The first two will be assembled into the left channel and the second two will be assembled into the right channel. (Or vice versa--I have trouble keeping straight in my mind which channel is left or right.)

Here's a good tutorial with example on how to read a line: http://docs.oracle.com/javase/tutorial/sound/converters.html

Some of the earlier tutorials in the trail might be needed to help clarify. Also, if you have questions about converting bytes to PCM and back, there are several explanations already on StackOverflow to reference. Should not be too hard to find them.