#03 Define Synths

The first building block for creating generative music on Dittytoy is the synthesizer. The synthesizer is a function (or class) that generates a sound.
Note how the return value of the lambda function is multiplied by the env(elope) value. You don't have to do this, but if you do, your synth will work seamlessly with the built-in support for envelopes.

dittytoy.net/syntax#synthesizers

#dittytoy #tutorial

Log in to post a comment.

// #03 Define Synths. DittyToy 2022.
// The MIT License.
//
// https://dittytoy.net/ditty/e23c3c9952
//
// The first building block for creating generative music on Dittytoy is the synthesizer. The synthesizer is a 
// function (or class) that generates a sound. 
//
// Simple synth definition: use synth.def to create a synth based on a lambda function.
// 
// arguments: phase, env(elope) object (with a duration and a value property), tick and options
// return:    you can return a mono value (-1 to 1), or a stereo signal [l, r] - array
//
// second argument of synth.def are the default options of the synth (you can override these options when you call synth.play)
//
// https://dittytoy.net/syntax#synthesizers
//

const noise  = synth.def( (phase, env, tick, options) => (Math.random() * 2 - 1) * env.value );
const kick   = synth.def( (phase, env, tick, options) => Math.sin(phase * 2 * Math.PI * (1.5 - tick * 4)) * env.value, { attack: 0.0125, release: 0.075 });

loop( (loopCount) => {
    
    kick.play(c3);
    sleep(1);
    
}, { name: 'kick' });

loop( (loopCount) => {
    
     noise.play(c3, { attack: 0.0125, release: 0.075, amp: .05 + Math.random() * 0.05 });
     sleep(0.25);
     
}, { name: 'hi-hat' });

loop( (loopCount) => {

    for (let i=0; i<4; i++) {
        sine.play(c4, { attack: 0.01, release: 0.25,  duration: 0.125, pan: Math.random() * 2 - 1, amp: 1.0 });
        sleep( 0.25 );
    }

    sine.play(d4, { attack: 0.01, release: 0.25,  duration: 0.25 }); // attack and release in seconds, duration in ticks
    sleep(0.5); // sleep in ticks

    sine.play(f4, { attack: 0.01, release: 0.75,  duration: 0.25 });
    sleep(0.5);

}, { name: 'melody' });