androidaudioandroid-mediacodectempopitch-shifting

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?


Solution

  • 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