JavaSound:提取一个立体声音频通道

时间:2014-09-10 23:39:47

标签: java audio javasound

我有一个立体声波文件,我需要阅读和播放所选的频道。完成此任务的最佳方法是什么?

2 个答案:

答案 0 :(得分:2)

当您通过AudioInputStream引入wav文件时,请使用AudioFileFormat信息将字节转换为PCM。右侧和左侧的数据交替显示。因此,如果该行是16位,则每帧将有4个字节。前两个将组装到左通道中,后两个将组装到右通道中。 (反之亦然 - 我无法直接考虑哪个频道是左或右。)

这是一个很好的教程,有关如何阅读一行的示例: http://docs.oracle.com/javase/tutorial/sound/converters.html

可能需要一些早期的教程来帮助澄清。此外,如果您有关于将字节转换为PCM并返回的问题,则StackOverflow上已有多种解释方法可供参考。不应该太难找到它们。

答案 1 :(得分:0)

这是从多通道直接音频线路(JavaSound)中提取单个通道的简单方法。在我的Line6(r)Helix(r)吉他音效板(8通道)中进行了尝试,效果很好。我猜它可以与任何类型的DataTargetLine一起使用。在这种情况下,我们基于一个AudioFormat承载16位样本来处理数据。希望对您有所帮助。

    public ArrayList<byte[]> extract16BitsSingleChannels(byte[] audioBuffer, int channels) {

    /* Parameters :
     * 
     * audioBuffer : the buffer that has just been produced by
     * your targetDataLine.read();
     * channels : the number of channels defined in the AudioFormat you 
     * use with the line
     * 
     *  the AudioFormat which I tested :
     *       float sampleRate = 44100; 
     *       int sampleSizeInBits = 16;
     *       int channels = 8;
     *       boolean signed = true;
     *       boolean bigEndian = true;
     */


    /* let's create a container which will receive our "per channel" buffers */

    ArrayList<byte[]> channelsData = new ArrayList<byte[]>();

    /* take care of adjusting the size of the audioBuffer so that 
     * audioBuffer % channels == 0 is true ... because :        
     */

    final int channelLength=audioBuffer.length/channels;

    /* let's create one buffer per channel and place them in the
     * container 
     */

    for (int c=0 ; c < channels ; c++) 

    {
        byte[] channel=new byte[channelLength];
        channelsData.add(channel);
    }

    /* then process bytes from audioBuffer and copy each channels byte 
     * in its dedicated buffer 
     */

    int byteIndex=0; 

    for(int i = 0; i < channelLength; i+=2) //i+=2 for 16 bits=2 Bytes samples
    {
        for (int c=0 ; c < channels ; c++) {
            channelsData.get(c)[i]=audioBuffer[byteIndex];   // 1st Byte
            byteIndex++;
            channelsData.get(c)[i+1]=audioBuffer[byteIndex]; // 2nd Byte
            byteIndex++;
        }

    }

    /* Returns each set of bytes from each channel in its buffer you can use to
       write on whatever Byte streamer you like. */

    return channelsData;   

}