ofxPDSP: triggering samples based on midi note

I am working on a little drum machine similar to ableton’s impulse and I am wondering how to archive this best with ofxPDSP. How can I trigger samples based on incomming midi notes?
Any pointers? @npisanti maybe you have some code online or can give some advices.

ah yes there is a class to use instead of pdsp::midi::Keys, you should use pdsp::midi::Pads, i still have to write an example for it, you should add the layers with the range of notes for each output trigger, and you got the out_trig( int layerindex ) for each added layer



Thanks a lot, i will give it a try.
Can i somehow filter by midi channel? And can i patch the out_trig directly into a sampler?

    _midiPads.addTriggerLayer(0, 127); // maybe it could even return the index
    _midiPads.out_trig(0) >> _impulse._samplers[0];

Currently i am testing with the following incomplete patch.

#pragma once

#include "ofMain.h"
#include "ofxPDSP.h"

class impulse : public pdsp::Patchable {
       // ...

        for(auto i = 0; i < 8; i++){
            _samplers[i].setSample(&_samples[i], 0);
            _sampleTriggers[i] >> _samplers[i] >> _amps[i] >> _amp;
            // _sampleTriggers[i] >> _envelopes[i] >> _amps[i].in_mod();
        addModuleOutput("out", _amp);

    void trigger(int i){
        // _envelopeGates[i].trigger(1.0f);
    ofParameterGroup _parameters;

    pdsp::Sampler _samplers[8];
    pdsp::SampleBuffer _samples[8];
    pdsp::TriggerControl _sampleTriggers[8];
    pdsp::TriggerControl _envelopeGates[8];
    pdsp::Amp _amps[8];
    pdsp::AHR _envelopes[8];
    pdsp::Amp _amp;

there was no way to filtering by midi channel, so i just made a change to add a third argument to addTriggerLayer for the midi channel, git pull ofxPDSP again.

Mind that the low and high value for the layer are not for the velocity but for the midi note, there is no filtering by velocity at the moment.

you can patch the trigger directly to the sampler, but instead of using arrays of all the units it would be better to make yourself a little class with a pdsp::Sampler, a pdsp::Amp and an pdsp::AHR, it would be a lot cleaner.

i was also thinking, as this class probably has never been used, if you need low and high velocity thresholds maybe it’s better for me to just refactor it and made it with a layers vector, each layer being an object with its low and high values for notes and velocity and a value for the midi channel.

thanks for your help!
i will definitely tidy it up and create a class for the slots.
in the example above i was listening to all midi notes for testing. for now i do not need velocities, but later it could be handy to have. Could the trigger layer also stream the velocity, so i could change the volume?

let me know if i can support somehow. i should have some time this weekend and could have a look into refactoring the pads class.

the out_trig() also send the value for the dynamics, that is used to control the envelope amount, so to control some values with the midi note velocity you just have to patch an envelope to something and the envelope will be scaled according to the velocity. If you want to use it to control amp volume, the envelopes have a method called enableDBTriggering

that scale the 0.0f - 1.0f linear range to dB values with a minumum and maximum, it will make it more responsive for volume but then you cannot use the same envelope for modulation as the value won’t be linear from 0 to 1 anymore.

PS: the velocity dynamics amount is controlled by in_velocity()


Thanks a lot. I owe you several beers. Great work.

thank you for using pdsp! =D