Perraco
Perraco

Reputation: 17380

Modify audio pitch / tempo while encoding with android MediaCodec

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?

Upvotes: 0

Views: 887

Answers (2)

Léon Pelletier
Léon Pelletier

Reputation: 2741

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

Upvotes: 1

Wolfram Hofmeister
Wolfram Hofmeister

Reputation: 349

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.

Upvotes: 0

Related Questions