#06 Advanced Synths

You may want to define a more complicated synthesizer that has its own state. This can be done by passing not a lambda function but a class to synth.def().

The process method will be called at a ditty.dt time interval and should return a value -1 to 1 (mono) or an array [l, r] (both -1 to 1) for stereo.

dittytoy.net/syntax#synthesizers

#dittytoy #tutorial

Log in to post a comment.

// #06 Advanced Synths. DittyToy 2022.
// The MIT License.
//
// https://dittytoy.net/ditty/82860b6d28
//
// You may want to define a more complicated synthesizer that has its own state. 
// This can be done by passing not a lambda function but a class to synth.def().
//
// The process method will be called at a ditty.dt time interval and should return a value -1 to 1 (mono),
// or an array [l, r] (both -1 to 1) for stereo.
//
// https://dittytoy.net/syntax#synthesizers
//

const osc = synth.def(
    class {
        constructor(options) {
            // The value of the note argument of the play call is retrievable via options.note.
            this.phase = 0;
        }
        process(note, env, tick, options) {
            this.phase += midi_to_hz(note) * ditty.dt * (env.value ** options.xenv);
            return Math.sin(this.phase * Math.PI * 2) * env.value;
        }
    }, { xenv: 0.5, amp: 1, attack: 0.01, release: 0.3, env: adsr2 }
);

loop( (loopCount) => {
    scale(c3, scales.major, 2).forEach( note => {
        osc.play(note, { pan: Math.random() * 2 - 1 });
        sleep(0.25);
    });
});