#11 Debug your Ditty

Dittytoy allows you to log values to the console at the left of your screen. You can also log values to probes.

dittytoy.net/syntax#built-in

#dittytoy #tutorial

Log in to post a comment.

// #11 Debug your Ditty. DittyToy 2022.
// The MIT License.
//
// https://dittytoy.net/ditty/6bd9bbe6d8
//
// Debugging your ditty while it runs 44100 times per second can be pretty hard. Unfortunately, you can't set breakpoints,
// and writing values to the default console.log at this rate will most likely crash your browser.
//
// However, Dittytoy allows you to log values to the console at the left of your screen. You can also log values to probes.
//
// https://dittytoy.net/syntax#built-in
// 

input.pan = 0; //min=-1, max=1, step=0.001
input.note = 60; // min=0, max=120, step=0.001

input.envAttack   = 0.1; // min=0,  max=0.2, step=0.001
input.envDecay    = 0.2; // min=0,  max=2,   step=0.001
input.envDuration = 1.0; // min=0,  max=2,   step=0.001
input.envSustain  = 0.5; // min=0,  max=1,   step=0.001
input.envRelease  = 0.5; // min=0,  max=2,   step=0.001
input.envCurve    =  -2; // min=-9, max=9,   step=0.1

const osc = synth.def(
    class {
        constructor(options) {
            this.phase = 0;
        }
        process(note, env, tick, options) {
            // debug log functionality. The log appears in the console at the left of your ditty. You can use:
            //
            // debug.log( label, value)
            // debug.warn( label, value)
            // debug.error( label, value)
            
            debug.log('note', note);
            
            this.phase += midi_to_hz(note) * ditty.dt;
            const waveform = Math.sin(this.phase * Math.PI * 2);
            
            // debug probes (Safari not supported!). For each label an oscilloscope will appear left of your ditty.
            //
            // debug.probe( label, value, amp (optional), duration (horizontal resolution in seconds, optional) );
            //
            // a probe expects one value per sample (more will be ignored, less will pause the probe)
            //
            
            debug.probe('waveform', waveform, 1, 5 / midi_to_hz(note));
            debug.probe('envelope', env.value, 1, tick_to_second(env.duration) );
            
            return waveform * env.value;
        }
    }, {}
);

// signalProbe - by athibaul
const signalProbe = filter.def(  
    class {
        process(input) {
            debug.probe('signal', input[0]+input[1], 2, 1);
            return input;
        }   
    });
    
loop( (i) => {
    debug.warn('loop', i);
    
    osc.play( () => input.note, 
    { 
        attack:         input.envAttack,
        decay:          input.envDecay,
        sustain:        input.envSustain,
        duration:       input.envDuration,
        release:        input.envRelease,
        curve:          input.envCurve,
        env:            adsr,
        pan: () => input.pan
    });
    sleep( Math.max(second_to_tick(input.envAttack + input.envDecay), input.envDuration) + second_to_tick(input.envRelease));
}, { name: 'ADSR envelope' }).connect( signalProbe.create() );