1
$\begingroup$

I want to write an algorithm to add pitch-shifted version of audio signal to itself (like Electro-Harmonix POG does). And I need to do it in realtime. How to implement it?

My guess is: break signal into pieces, get FFT of each piece, copy each of the frequencies in the original signal (for example, for 1 octave up shift copy 20hz to 40hz, 40hz to 80hz and so on), get the iFFT, play it back. But I don't know if I got the right idea. Will it work?

$\endgroup$
3
  • 1
    $\begingroup$ if you do any pitch shifting using the FFT, you're in for a helluva delay. could be "real-time", but not so good for live real-time (like a stompbox). you need to figure out how to do pitch shifting in real time without going into the frequency domain. think: "splicing". $\endgroup$ Mar 20, 2015 at 21:32
  • $\begingroup$ @robertbristow-johnson 2 questions. 1) will it work if I'll use high sampling rate and relatively low pieces? and 2) could you provide some additional info about pitch shifting without FFT? because all tutorials I've found on the internet about pitch shifting use FFT. $\endgroup$ Mar 20, 2015 at 22:22
  • $\begingroup$ upsampling helps with quality and makes linear interpolation work better, but it's costly and pitch shifting is also costly. might not be a good idea. as for question (2), see answer below. $\endgroup$ Mar 20, 2015 at 22:52

3 Answers 3

2
$\begingroup$

okay, try googling "pitch shifting AMDF" or "pitch shifting TDHS" and see what you hit there. also check out DSP Dimension and maybe music-dsp and the mailing list.

you need three concurrent processes:

  1. a good pitch detector that gives you a good estimate of the period (to a precision of a fraction of a sample). this period estimate should be updated as least as often as every 15 or 20 ms.

  2. a real-time resampling and interpolation alg with a pointer that has precision at the fractional sample. this pointer will have both integer and fractional parts, the integer part tells you where in your delay buffer you are interpolating, the fractional part tells you how you're interpolating (like the interpolation coefficients). try linear interpolation at first, it's easier and you can improve it later. now this pointer will advance faster than the write pointer for upshifting and slower for downshifting. either way, that pointer will run into the edge of the delay buffer (and glitch or click or pop when it wraps around) unless you do something about it.

  3. a splicing and crossfading alg that allows you to splice or seamlessly "jump" from one instantaneous pointer position in the delay buffer to another pointer position. the difference between those two pointer positions should be one period as determined by your pitch detector.

if i hint more than that, i might be spilling beans (internal trade secrets) from one manufacturer or another. but this is the main idea. think of splicing as necessary to time-scale the input (say time-expand if you're upshifting), and then step 2 is playing that time-expanded audio back faster which both returns it to the original tempo, but bumps it up in pitch.

$\endgroup$
1
$\begingroup$

@robertbristow-johnson give you nice tips, i can suggest you, PSOLA algorithm (aka Keith Lent1) to pitch shift in time domain.

PSOLA assumes that, if you know the pitch of a given segment, then you'll be able to remove or add periods moving synchronously and preserving envelope information.

The main step of PSOLA algorithm are:

  • Extract Pitch/Periodos and find pitch marks (this can be hard), this marks are tipically positioned in the Glottal Closure Instants (GCI), and this positions are explicitly linked to quality of your Pitch Shifted signal, this step give you analysis marks.
  • split your signal using a hann window, every window need be centred in the analyis mark position!
  • Now from Analysis marks do you need rearrange this marks based in your pitch shift factor, it is called syntesysed marks.
  • Overlap and add every windowed splited piece

The representation of these steps:

enter image description here

Mybe can be ease to see how some steps are build using a sinusoid example, so, here an real example, the picture bellow show an 150hz sampled at 22050hz (blue signal), on top of that sign you can see the pitch mark positions marked directly on the sign, the green dots on top are the pitch marks again and the green marks below are the synthesised marks using a pitch factor of 1.5, the red line between up/down dots tell you what centred hann whindow get to overlap and add.

enter image description here

So this steps pitch shift 150Hz signal to 1.5*150 = 225Hz.

1 - An efficient Method for Pitch Shifting Digitally Sampled Sounds", published in the Computer Music Journal, Vol 13, No. 4, Winter 1989

$\endgroup$
1
  • 1
    $\begingroup$ eder, this is a pretty good description of the Lent (or Hamon) PSOLA technique, which was used in the Digiteck Vocalist. the EHX POG is a different animal. one thing to point out is that this PSOLA technique has the ability to shift the pitch without shifting the spectral envelope (a.k.a. "formants") along with it. also, unlike the older (and simpler) TDHS splicing method (that does shift the spectral envelope), this PSOLA is critically dependent on the pitch detection and octave errors will cause a nasty glitch that does not happen with TDHS. $\endgroup$ Apr 17, 2015 at 15:27
-1
$\begingroup$

There are many ways to do pitch shift. here is sample way. You can do it by re sampling (up and down). As you know down sampling and up sampling , they make your data frequency change too.

You can use lib Sox.

$\endgroup$
1
  • 1
    $\begingroup$ It's right that resampling changes the pitch (if played back at the original sampling rate), but this is not what is usually referred to as "pitch shifting". The playback speed is supposed to stay the same, with the timing of all audible events unchanged. $\endgroup$
    – Jazzmaniac
    Mar 21, 2015 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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