混合两个 16 位编码立体声 PCM 样本,导致生成的音频出现噪音和失真

问题描述

我从两个来源获得两个不同的音频样本。

  1. 对于麦克风声音:

    audioRecord =
             new AudioRecord(MediaRecorder.AudioSource.DEFAULT,44100,AudioFormat.CHANNEL_IN_STEREO,AudioFormat.ENCODING_PCM_16BIT,(AudioRecord.getMinBufferSize(44100,AudioFormat.ENCODING_PCM_16BIT)*5));
    
  2. 对于内部声音:

    audioRecord = new AudioRecord.Builder()
                     .setAudioPlaybackCaptureConfig(config)
                     .setAudioFormat(new AudioFormat.Builder()
                             .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
                             .setSampleRate(44100)
                             .setChannelMask(AudioFormat.CHANNEL_IN_STEREO)
                             .build())
                     .setBufferSizeInBytes((AudioRecord.getMinBufferSize(44100,AudioFormat.ENCODING_PCM_16BIT)*5))
                     .build();
    

为了从 audioRecord 对象中读取,我们创建了单独的框架对象(称为框架的自定义对象)-

private ByteBuffer pcmBuffer = ByteBuffer.allocateDirect(4096);
private Frame read() {
  pcmBuffer.rewind();
  int size = audioRecord.read(pcmBuffer,pcmBuffer.remaining());
  if (size <= 0) {
   return null;
  }
    return new Frame(pcmBuffer.array(),pcmBuffer.arrayOffset(),size);
}

我们创建了两个单独的 LL(Linked List)来添加我们从 read 函数中获得的这些帧。

private LinkedList internalAudioQueue = new LinkedList(); private LinkedListmicAudioQueue = new LinkedList();

public void onFrameReceived(Frame frame,boolean isInternalAudio) {
    if (isInternalAudio) {
        internalAudioQueue.add(frame);
    } else {
        microphoneAudioQueue.add(frame);
    }
    checkAndPoll();
}

每次我们在相应的 LL 中添加一个帧时,我们都会调用以下 checkAndPoll() 函数,并根据情况将帧传递给 audioEncoder。

public void checkAndPoll() {
    Frame frame1 = internalAudioQueue.poll();
    Frame frame2 = microphoneAudioQueue.poll();
    if (frame1 == null && frame2 != null) {
        audioEncoder.inputPCMData(frame2);
    } else if (frame1 != null && frame2 == null) {
        audioEncoder.inputPCMData(frame1);
    } else if (frame1 != null && frame2 != null) {
        Frame frame = new Frame(PCMUtil.mix(frame1.getBuffer(),frame2.getBuffer(),frame1.getSize(),frame2.getSize(),false),frame1.getorientation(),frame1.getSize());
        audioEncoder.inputPCMData(frame);
    }
}

现在我们在 Hendrik 的帮助下以这种方式混合来自两个源的 ByteBuffer 形式的音频样本。

public static byte[] mix(final byte[] a,final byte[] b,final boolean bigEndian) {
    final byte[] aa;
    final byte[] bb;

    final int length = Math.max(a.length,b.length);
    // ensure same lengths
    if (a.length != b.length) {
        aa = new byte[length];
        bb = new byte[length];
        System.arraycopy(a,aa,a.length);
        System.arraycopy(b,bb,b.length);
    } else {
        aa = a;
        bb = b;
    }

    // convert to samples
    final int[] aSamples = toSamples(aa,bigEndian);
    final int[] bSamples = toSamples(bb,bigEndian);

    // mix by adding
    final int[] mix = new int[aSamples.length];
    for (int i=0; i<mix.length; i++) {
        mix[i] = aSamples[i] + bSamples[i];
        // enforce min and max (may introduce clipping)
        mix[i] = Math.min(Short.MAX_VALUE,mix[i]);
        mix[i] = Math.max(Short.MIN_VALUE,mix[i]);
    }

    // convert back to bytes
    return toBytes(mix,bigEndian);
}

private static int[] toSamples(final byte[] byteSamples,final boolean bigEndian) {
    final int bytesPerChannel = 2;
    final int length = byteSamples.length / bytesPerChannel;
    if ((length % 2) != 0) throw new IllegalArgumentException("For 16 bit audio,length must be even: " + length);
    final int[] samples = new int[length];
    for (int sampleNumber = 0; sampleNumber < length; sampleNumber++) {
        final int sampleOffset = sampleNumber * bytesPerChannel;
        final int sample = bigEndian
                ? bytetoIntBigEndian(byteSamples,sampleOffset,bytesPerChannel)
                : bytetoIntLittleEndian(byteSamples,bytesPerChannel);
        samples[sampleNumber] = sample;
    }
    return samples;
}

private static byte[] toBytes(final int[] intSamples,final boolean bigEndian) {
    final int bytesPerChannel = 2;
    final int length = intSamples.length * bytesPerChannel;
    final byte[] bytes = new byte[length];
    for (int sampleNumber = 0; sampleNumber < intSamples.length; sampleNumber++) {
        final byte[] b = bigEndian
                ? intToByteBigEndian(intSamples[sampleNumber],bytesPerChannel)
                : intToByteLittleEndian(intSamples[sampleNumber],bytesPerChannel);
        System.arraycopy(b,bytes,sampleNumber * bytesPerChannel,bytesPerChannel);
    }
    return bytes;
}

// from https://github.com/hendriks73/jipes/blob/master/src/main/java/com/tagtraum/jipes/audio/AudioSignalSource.java#L238
private static int bytetoIntLittleEndian(final byte[] buf,final int offset,final int bytesPerSample) {
    int sample = 0;
    for (int byteIndex = 0; byteIndex < bytesPerSample; byteIndex++) {
        final int aByte = buf[offset + byteIndex] & 0xff;
        sample += aByte << 8 * (byteIndex);
    }
    return (short)sample;
}

// from https://github.com/hendriks73/jipes/blob/master/src/main/java/com/tagtraum/jipes/audio/AudioSignalSource.java#L247
private static int bytetoIntBigEndian(final byte[] buf,final int bytesPerSample) {
    int sample = 0;
    for (int byteIndex = 0; byteIndex < bytesPerSample; byteIndex++) {
        final int aByte = buf[offset + byteIndex] & 0xff;
        sample += aByte << (8 * (bytesPerSample - byteIndex - 1));
    }
    return (short)sample;
}

private static byte[] intToByteLittleEndian(final int sample,final int bytesPerSample) {
    byte[] buf = new byte[bytesPerSample];
    for (int byteIndex = 0; byteIndex < bytesPerSample; byteIndex++) {
        buf[byteIndex] = (byte)((sample >>> (8 * byteIndex)) & 0xFF);
    }
    return buf;
}

private static byte[] intToByteBigEndian(final int sample,final int bytesPerSample) {
    byte[] buf = new byte[bytesPerSample];
    for (int byteIndex = 0; byteIndex < bytesPerSample; byteIndex++) {
        buf[byteIndex] = (byte)((sample >>> (8 * (bytesPerSample - byteIndex - 1))) & 0xFF);
    }
    return buf;
}

我得到的混合样本既有失真又有噪声。无法弄清楚需要做什么才能删除它。任何帮助在这里表示赞赏。 提前致谢!

解决方法

我认为如果你要混合,你应该取两者的(加权)平均值。

如果您有样本 128 和 128,那么结果仍然是 128,而不是可能超出范围的 256。

所以只需将您的代码更改为:

// mix by adding
final int[] mix = new int[aSamples.length];
for (int i=0; i<mix.length; i++) {
    // calculating the average
    mix[i] = (aSamples[i] + bSamples[i]) >> 1;
}

这对你有用吗?