I have an MP3 decoder running on a ESP32, and I'd like to change the pitch of the streaming audio in real time.

I plan to build a little toy that streams Amazon Alexa and changes her voice so that it is higher pitched. So no pro audio requirements at all, it is just supposed to be a funny effect.

I was thinking maybe I can operate on the raw DCT data? Or is there another way that is fast enough for that chip? I cannot use time compression/dilation because the audio is streamed.

I'm using the MAD fixed point decoder.

A description of (or a link to) an algorithm working on DCT or PCM data would be fine, but code or pseudo-code in whatever language would be very welcome.


migrated from electronics.stackexchange.com Jan 19 '17 at 18:06

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • $\begingroup$ Are you wishing to preserve harmonic relationships between points in the spectrum or just shift up or down the whole lot by x? $\endgroup$ – Andy aka Jan 19 '17 at 9:14
  • 1
    $\begingroup$ explaining why Andy aka is asking: shifting in the frequency domain (i.e. adding an offset) can be done very easily by mixing with a sinusoidal (but that doesn't preserve the harmonic relationships). Compressing/streching the frequency domain would preserve harmonic relationships but takes much more effort (FFT and IFFT) . $\endgroup$ – Curd Jan 19 '17 at 9:39
  • $\begingroup$ I have a hard time answering that question because I don't really understand the differences or drawbacks. I suspect doing an FFT/IFFT would be too expensive, and basically I just want it to sound funny - if theres some distortion, thats ok. $\endgroup$ – Michael Böckling Jan 19 '17 at 9:49
  • $\begingroup$ @MichaelBöckling to help you answer that question: Could you simply edit your original question to explain to what end / for what purpose you want to do this? $\endgroup$ – Marcus Müller Jan 19 '17 at 10:00
  • $\begingroup$ You're right, just did that. $\endgroup$ – Michael Böckling Jan 19 '17 at 10:15

Use a "phase vocoder" which will use short term FFT properties to change the pitch without changing the duration.

There is plenty of information and links to "phase vocoder" on Wikipedia and Google in general.

It is a great educational exercise to go through its operation as you learn from it the critical importance of the phase information in the FFT for proper reconstruction.


The easiest way, I guess, to change your MP3's pitch is to actually play them at a different sampling rate that the header (bits 21 & 22) specifies – that will of course change the overall duration of playback.

  • $\begingroup$ This might work if I just wanted to slow down, but I wanted to pitch it up and since its streaming audio (though with limited length) and I can't buffer everything I'm not sure that'll work out. But I'll try that first, just in case my assumptions are wrong. $\endgroup$ – Michael Böckling Jan 19 '17 at 20:55
  • $\begingroup$ why only to slow down? If you fake a higher sampling rate than what was used, tones should be higher! $\endgroup$ – Marcus Müller Jan 19 '17 at 20:57
  • 1
    $\begingroup$ I tried halving the sample rate and started to stream web radio, then the server drops the connection because I read data too slow. If I double the sample rate instead, will I not get buffer underruns because samples will be consumed faster than they come in? $\endgroup$ – Michael Böckling Jan 19 '17 at 23:11
  • $\begingroup$ @MichaelBöckling aah I forgot about the streaming aspect; well, then things get more haiy :/ $\endgroup$ – Marcus Müller Jan 20 '17 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.