How do I create a mono synth?

In this ditty, I'm trying to create a monophonic synth with pitch portamento. Unfortunately it does not work as expected.

Log in to post a comment.

ditty.bpm = 60;

// I'd like to create a mono synth with portamento (notes gliding from one to the next)
// This does not work unfortunately :(


// To create a mono synth with portamento, we need to glide between notes, so I'm creating
// a "pitch smoother" to do that
class Smoother {
    // We feed values to this object, and it smoothes them out
    constructor(v, dur) {
        this.v = v;
        this.target = v;
        this.a0 = clamp01(ditty.dt / dur);
    }
    setValue(v) { // should be called when we want to start moving toward a new target
        this.target = v;
    }
    update() { // should be called once per sample
        this.v += this.a0 * (this.target - this.v);
        return this.v;
    }
}

// Simple sawtooth synth
const saw = synth.def( (x,e) => (x%1) * e.value);

// Simple melody
const nns = [c4,d4,e4,b3,e4,a3,a4,e4];

loop( () => {
    const smoo_note = new Smoother(c4, 0.1);
    // Use the smoothed values as the note number
    const s = saw.play(() => smoo_note.update(), {duration:nns.length, amp:0.1});
    for(let i=0; i<nns.length; i++) { // We now communicate notes via our smoother object
        smoo_note.setValue(nns[i]);
        //smoo_note.setValue(nns[6]); // This glides to the correct note
        
        saw.play(nns[i]+0.01, {amp:0.05}); // Play another synth for comparison
        sleep(1);
    }
    // The whole function seems to be evaluated right away, and so the "smoother" actually glides to the 
    // final note right away...
});