使用 android MediaCodec 编码时修改音频音调/节奏

Modify audio pitch / tempo while encoding with android MediaCodec

我正在使用 AudioRecord to get audio in real-time from the device microphone, and encoding / saving it to file in the background using the MediaCodec and MediaMuxer 类.

有什么方法可以在将音频流保存到文件之前更改音高和(或)速度?

要修改音频流的 pitch/tempo,您必须在使用编解码器对其进行编码之前自行重新采样。请记住,如果更改流的速度,您还需要修改时间戳。

pitch/tempo,你指的是频率本身,还是采样的速度?如果是这样,那么每个样本应该在更短或更长的时间内进行投影:

示例:

    private static byte[] ChangePitch(byte[] samples, float ratio) {

        byte[] result = new byte[(int)(Math.Floor (samples.Length * ratio))];

        for (int i = 0; i < result.Length; i++) {
            var pointer = (int)((float)i / ratio);
            result [i] = samples [pointer];
        }

        return result;
    }

如果你只想改变音高而不影响速度,那么你需要阅读相位声码器。这是可靠的科学,有很多项目可以实现这一目标。 https://en.wikipedia.org/wiki/Phase_vocoder