Best approach for a simple pitch adjustment plugin

Hi, I’ve searched the forums and web looking for approaches to making a simple pitch changing plugin and haven’t found what I’ve been looking for. I’m hoping to make a super simple pitch plugin that can be inserted on a track, allowing basic pitch / sample rate to be varied with a slider in the same manner as a fine tune control. Not looking to seperate musical key from speed or anything fancy.

Any ideas on how to approach this would be greatly appreciated.

1 Like

well… even a simple pitchshifter is somewhat complex
the “simple” one would be a PSOLA (time-domain) approach
the “fancy” one would be a phase vocoder (frequency-domain)
both are not that trivial to implement

iirc @adamski had one

EDIT: sorry Adam has a pitch detector hehehehe

1 Like

Unfortunately that is not really even feasible to do with the standard plugin formats. The plugins need to take in the same number of samples as they are going to produce out, which makes implementing a pure resampling based pitch shifter very tricky.

You might be able to hack together something by using internal buffering in the plugin, but it’s not going to be straightforward and probably won’t work exactly like you’d hope it to work.

Wouldn’t it only be possible to pitch down audio like this? If you’re playing back the current buffer faster, then you’ll run out of input audio to fill the output and there’s no way to get the next buffer of input. Plus you’d have to decide on some arbitrary limit of how much input you can stuff into your buffer to play slower, or face allocating on the audio thread (= nope).
I suppose if you wanted to pitch up using buffers you could specify in the plugin how long your input is, and then not output anything until you’ve buffered enough that you won’t run out of input signal whilst playing back faster, but this doesn’t sound like what OP wants to achieve at all.

I’d go as far as saying literally impossible without using ARA (assuming it would solve the problem).

Thanks for this info and it sounds more complicated that I was first assuming. If I was to adjust the scope to making a basic sample player for playing a 2 second long sound and attempt to implement a fine tune pitch control, would the same issues exist?

I had originally thought that adjusting the samplerate of an audioclip would achieve this, but have only just started learning about buffers and not sure if this is possible.

Thanks again!

If you have the whole sound already in a buffer or file, you can play it in any speed or order. There’s just no standard API to get that audio directly into the plugin from the host’s timeline. (There is the ARA extension for doing that but that is not supported in all hosts nor officially by Juce.)

Thanks Xenakios, for my purpose I can use a 2 second audio file that is added in JUCE. I’m not sure the best approach for changing speed of the playback, would this just be done through changing the sample rate of the audio file?

BTW, this has all been really helpful so far so thanks.

the easiest thing you can do to get a sense of detuning is if you create a class that has quite a huge buffer (maybe 1 sec or more) and let it capture the current sample constantly with a circular index while at the same time reading from the buffer with this index multiplicated by some value. index * 2 would be an octave up and index * .5f would be an octave down. also you need to add a function that always lets the index jump back into bounds before being used on the buffer ofc. this implementation has some problems. one of them is that you’d need at least linear interpolation to get a somewhat clean sound on all pitches lower than 1. also this doesn’t sound very natural. more like a granular effect.

edit: if you use plug’n script as prototyping environment i can show you code examples

Just because many answers are based on assumptions, what you want to achieve:
To change the pitch a simple resampling will do the job. But this will change the duration of the audio. If that is not a problem (for monotonous audio like a sine wave or for playing samples, where you crossfade through different phases of the sample anyway).
The challenge starts, if you want the duration to be constant while resampling.
There were already algorithms mentioned above, so I don’t repeat them here.

For the plugin case (streaming through), you need to keep the duration constant though, like Xenakios explained above…

Thanks Daniel, yes resampling is the style that I’m most interested in!

I’ll focus on building a sample player with set sounds that can be resampled to alter the pitch.

Thanks for the comments everyone, they’ve all been really helpful.

Hi hdlAudio, I’m going down the simple resampling route so thanks for your offer of code examples but I don’t want to waste your time being it’s a different approach. But thankyou for the offer!!