Play an audio buffer with a specific pitch

I want to play an audio sample (a buffer) to sound in a specific pitch/frequency. Can any one help me with this?

You need to know what the fundamental of the audio buffer is, assuming it’s consistent enough to “sound in a specific pitch” as you’ve requested. You could use a simple function like this, where the default is C4 and then set it until you have a match:

x = {|midiNum = 60| SinOsc.ar(midiNum.midicps)}.play;

Then you can set the rate argument to PlayBuf ugen like so:

b = Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav", numFrames: 10000); //this is consistent-ish enough...
//it's ~D6, so 86
~originalPitch = 86;
~playAtPitch = 
    { |midiNum = 90|
        var buf = b;
        var rate = BufRateScale.kr(buf) * ((midiNum - ~originalPitch).midiratio);
        PlayBuf.ar(1, buf, rate );
    }.play;

You can also use Pitch.kr instead to figure out what the pitch of your Buffer is, but you really have to find this first somehow, whether you write a piece of code to do this somewhere in your file or find a set value and use it as a constant, and then scale from there. You can always also just multiply by value.midiratio to transpose the playback of a Buffer by semitones, but it’s going to sound worse the further you are from 0.

1 Like