ofxMaxim - adjust pitch

Hi I was wondering if anyone could give an example of how to change the pitch of a sound file using the ofxMaxim addon for open frameworks.

So far I have this as a base for the output of the sound

void InteSiVis::audioOut(float *output, int bufferSize, int nChannnels)
{
    for (auto it = keys.begin(); it!=keys.end(); it++) {
        for (int i = 0; i < bufferSize; i++) {
            float value = it->env.ar(it->sample->playOnce(),attack,release,holdTime,it->noteOn);

            output[2*i]+=value ;
            output[2*i+1]+=value;
        }
    }
}

what is it I add to i change the pitch of the sound? thanks in advance.

Hi @Malaa you can use the other playOnce() method that takes a floating point value that dictates speed.

playOnce(double speed) 

If you want to make the pitch higher give it a value greater than 1. This works just the same way as a tape player or turntable. As speed increases the sample gets shorter and the pitch rises and as speed decreases the sample gets longer and pitch falls.

If this isn’t the behaviour that you are looking for (i.e you want to be able to change the pitch of the sample without altering the samples playback duration then you will be to use one of the maxi granular pitch shifting methods

so in setup()

samp.load("mySample.wav"));
ps = new maxiPitchStretch<grainPlayerWin>(&samp);
ofxMaxiSettings::setup(sampleRate, 2, bufferSize);

and in the audioRequested()

    for (int i = 0; i < bufferSize; i++)
{
    //Play the Granular Synth play method
    wave = ps->play(pitch, speed, grainSize, (int)overlaps);
    
    mymix.stereo(wave, outputs, 0.5);
    output[i*nChannels    ] = outputs[0] * volume;
    output[i*nChannels + 1] = outputs[1] * volume;
}

You’ll need to declare these in your .h file

    //////////// ofxMaxim ////////////
maxiPitchStretch<grainPlayerWin> *ps;
maxiMix mymix;
maxiSample samp;
double wave;
double outputs[2];

Hey Joshua,

I tried that but I wasn’t able to get it working because I’m using this method so the keyboard keys match up to the sound and so it the sound fades out gradually after it’s pressed. (As well as so multiple sounds can be played at once)

struct Key {
bool noteOn;
shared_ptr sample;
char triggerLetter;
maxiEnv env;
Key(shared_ptr _sample,char _triggerLetter){ // Keyboard
// Key(shared_ptr _samp le, int _triggerLetter){ // Piano
noteOn = false;
sample = _sample;
triggerLetter = _triggerLetter;
}
};

How do I go about using both the pitch adjusted and this vector of key’s as they seem to clash with each other or is there some other way to go about it using the ofxmaxim library.

Thank you,

Hi Malaa, could you confirm which mode of pitch shifting you need. Can you get away with pitch and sample duration being interlinked or do you need to alter the pitch without altering the duration for your project?

I need to alter the pitch without altering the duration of the project I can try the other method but I do 't know show it would work with multiple key presses and I want to play multiple keys at once.

The maxiPitchStretch method allows you to set position of the file. What I would do i on a keyPressed trigger a counter that scrubs from 0.0 to 1.0 and then terminates. This will emulate the same functionality as the playOnce() method. If you make a vector of maxiPitchShifts then you can put all of your samples in a unique one and then trigger off the correct sample according to which key you press… Does this make sense?

I kind of understand but do you think you could give some example code? I just don’t understand how to do this with this function.

struct Key {
bool noteOn;
shared_ptr sample;
char triggerLetter;
maxiEnv env;
Key(shared_ptr _sample,char _triggerLetter){ // Keyboard
// Key(shared_ptr _samp le, int _triggerLetter){ // Piano
noteOn = false;
sample = _sample;
triggerLetter = _triggerLetter;
}
};

void InteSiVis::audioOut(float *output, int bufferSize, int nChannels)
{
for (auto it = keys.begin(); it!=keys.end(); it++)
{
for (int i = 0; i < bufferSize; i++) {

        float value = it->env.ar(it->sample->playOnce(),attack,release,holdTime,it->noteOn);

        output[2*i]+=value ;
        output[2*i+1]+=value;
    }
}

}

or edit the above?

Sure i’ll give it a go. Can you post your ofApp.cpp and ofApp.h as an attachment and ill edit it from there. Just so I can more easily see exactly what you are trying to achieve. thanks.

@Malaa, this may be off topic but I recently added a pitch shift example to ofxStk, Its pretty bad, compared to ableton live (i have not heard Maximilian’s pitch shift). but at least it might give you some ideas.

Here’s the link to the whole project file just encase you need it all.

I’ll give ofxStk ago! What do you mean it’s bad, as in the quality? I’m looking to get quite a nice quality sound, I think ofxMaxim works but it’s just cause I have the sound fading out so i’m not sure how to use both features.

Thank you!

I’ve attached the whole project file! I’ve used Xcode, it should be fairly easy to read.

Thanks again, i really appreciate the help :smiley: