0

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?

PerracoLabs
  • 16,449
  • 15
  • 74
  • 127
  • were you able to change tempo of audio file. If so, please provide an answer for it. I'm changing timestamps but it is not effecting audio in any way. – Manish Kumar Jan 03 '18 at 11:57

2 Answers2

1

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

Léon Pelletier
  • 2,701
  • 2
  • 40
  • 67
0

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.