#05 Advanced Options

A powerful feature of the options object is that you can not only assign a constant value to a property, but a lambda function in the form (tick, options) => ... as well.
This makes it possible to create dynamic properties that change over time.

dittytoy.net/syntax#options
dittytoy.net/syntax#portamento

#dittytoy #tutorial

Log in to post a comment.

// #05 Advanced Options. DittyToy 2022.
// The MIT License.
//
// https://dittytoy.net/ditty/4ce1fc3837
//
// A powerful feature of the options object is that you can not only assign a constant value to a property, but a 
// lambda function in the form (tick, options) => ... as well.
// This makes it possible to create dynamic properties that change over time, which makes it very easy to create a 
// portamento or a Shepard tone.
//
// The note argument of the play call on a synthesizer definition is, behind the scenes, an option property as well 
// (it is an alias for options.note). This also makes it possible to use a lambda function instead of a fixed midi number 
// when triggering the play call.
//
// https://dittytoy.net/syntax#options
// https://dittytoy.net/syntax#portamento
//

const tri = synth.def( (phase, env) => { const v = (phase % 1) * 4; return (v < 2 ? v - 1 : 3 - v) * env.value }, { attack: 0.0125, release: 0.25, amp: 0.8 });

loop( (loopCount) => {
    sine.play(d3, { attack: 0.025, release: 24, amp: (tick, options) => 0.6 + 0.5 * Math.sin(tick * Math.PI * 4), env: adsr2 });

    for (let i=0; i<6; i++) {
        [d, 0, 0, a, f5, 0, a, 0].forEach( note => {
            if(note) tri.play(note, { pan: (tick, options) => Math.sin(ditty.time)});
            sleep(0.25);
        });
    }

    sine.play(g3, { attack: 0.025, release: 8, amp: (tick, options) => 0.6 + 0.5 * Math.sin(tick * Math.PI * 4), env: adsr2 });

    for (let i=0; i<2; i++) {
        [d, 0, 0, Bb, g5, 0, Bb, 0].forEach( note => {
            // And, the .play and loop funtions return an object with options,
            // that can be used to change specific values (at a later time)
            if(note) {
                const foo = tri.play(note);
                foo.options.pan = Math.sin(ditty.time);
            }
            sleep(0.25);
        });
    }
}, { name: 'melody' });