如何将 PCM 流拆分为每个通道的 AudioInputStreams?

How to split PCM stream to AudioInputStreams for each channel?

我有一个立体声 AudioInputStream,我想将它分成两个单声道 AudioInputStream,一个用于立体声流的左声道,一个用于右声道。我该怎么做?

注意:我已经测试了以下问题的答案,但对我没有任何帮助(这就是我再次提问的原因)。

How to split a Wav file into channels in java?

这是我要开始工作的内容(主要来自上面的 link)。在下面的代码中,while 循环无限运行。 readsize 总是 4(或者 sampleInBytes * 2 是什么),它永远不会变成 -1(这意味着它是流的结尾)所以它永远不会中断。

   AudioInputStream originalAudioInputStream = null;
    try {
        originalAudioInputStream = AudioSystem.getAudioInputStream(audiofile);
    } catch (UnsupportedAudioFileException e) {
        //file not supported
    } catch (IOException e) {
        //error
    }
    //read file and convert it to PCM SIGNED big endian
    AudioFormat destaf = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
            originalAudioInputStream.getFormat().getSampleRate(),
            originalAudioInputStream.getFormat().getSampleSizeInBits(),
            originalAudioInputStream.getFormat().getChannels(),
            originalAudioInputStream.getFormat().getSampleSizeInBits() / 8 * originalAudioInputStream.getFormat().getChannels(),
            originalAudioInputStream.getFormat().getSampleRate(), true);
    AudioInputStream signedBigEndianInputStream = AudioSystem.getAudioInputStream(destaf, originalAudioInputStream);

    //split stereo AudioInputStream
    ByteArrayOutputStream leftbaos = new ByteArrayOutputStream();
    ByteArrayOutputStream rightbaos = new ByteArrayOutputStream();

    byte[] bytes = new byte[(signedBigEndianInputStream.getFormat().getSampleSizeInBits()/8)*2];

    while (true) {

        int readsize = 0;
        try {
            readsize = signedBigEndianInputStream.read(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }  

        if(readsize==-1){
            break;
        }

        rightbaos.write(bytes,0,bytes.length/2);
        leftbaos.write(bytes,bytes.length/2,bytes.length/2);
    }

    byte[] leftData = leftbaos.toByteArray();
    byte[] rightData = rightbaos.toByteArray();

    AudioFormat outFormat = new AudioFormat(signedBigEndianInputStream.getFormat().getEncoding(),signedBigEndianInputStream.getFormat().getSampleRate(),signedBigEndianInputStream.getFormat().getSampleSizeInBits(),1,signedBigEndianInputStream.getFormat().getFrameSize()/2, signedBigEndianInputStream.getFormat().getFrameRate(),signedBigEndianInputStream.getFormat().isBigEndian());

    ByteArrayInputStream leftbais = new ByteArrayInputStream(leftData);
    AudioInputStream leftoutputAIS = new AudioInputStream(leftbais, outFormat, leftData.length / outFormat.getFrameSize());

    ByteArrayInputStream rightbais = new ByteArrayInputStream(rightData);
    AudioInputStream rightoutputAIS = new AudioInputStream(rightbais, outFormat, rightData.length / outFormat.getFrameSize());

    //close inputstreams
    try {
        rightoutputAIS.close();
        leftoutputAIS.close();
        rightbais.close();
        leftbais.close();
        rightbaos.close();
        leftbaos.close();
        signedBigEndianInputStream.close();
        originalAudioInputStream.close();

    } catch (IOException e) {
        //error
    }

谢谢!

您假设音频存储在两个块中:

  1. 所有剩余样本
  2. 所有正确的样本

相反,音频通常存储在 interleaved fashion

即:

  1. 还剩一个样本
  2. 一个正确的样本
  3. 还剩一个样本
  4. 一个正确的样本
  5. ...(你懂的)

为了分离这两个流,您必须执行如下操作:

// just to show which types are used
// i.e., initialization is not shown
AudioFormat destaf;
ByteArrayOutputStream leftbaos;
ByteArrayOutputStream rightbaos;
AudioInputStream signedBigEndianInputStream;
byte[] bytes;

[...]

final int bytesPerSample = destaf.getSampleSizeInBits() / 8;
final int channels = 2;

while (true) {
    int readsize = 0;
    try {
        readsize = signedBigEndianInputStream.read(bytes);
    } catch (IOException e) {
        e.printStackTrace();
    }  

    if (readsize==-1){
        break;
    }
    for (int sample=0; sample<readsize/channels/bytesPerSample;sample++) {
        final int offset = sample * bytesPerSample * channels;
        leftbaos.write(bytes, offset, bytesPerSample);
        rightbaos.write(bytes, offset + bytesPerSample, bytesPerSample);
    }    
}
[...]