Modify audio pitch / tempo while encoding with android MediaCodec

872 Views Asked by At

I'm using 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 classes.

Is there any way to change the Pitch and (or) Tempo of the audio stream before it is saved to file?

2

There are 2 best solutions below

0
On BEST ANSWER

By pitch/tempo, do you mean the frequency itself, or really the speed of the samples? If so, then each sample should be projected in a shorter or longer period of time:

Example:

    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;
    }

If you just want to change the pitch without affecting the speed, then you need to read about phase vocoder. This is sound science, and there are a lot of projects to achieve this. https://en.wikipedia.org/wiki/Phase_vocoder

0
On

To modify the pitch/tempo of the audio stream you'll have to resample it yourself before you encode it using the codec. Keep in mind that you also need to modify the timestamps if you change the tempo of the stream.