Harpsichord

A try to synth a harpsichord

Log in to post a comment.

//add a honky tonky piano effect. Updates only after a complete loop
input.honkyTonky = .05 //min=0, max=.3 step=.05


//make it electric!
input.overdriveLevel = 1 //min=1, max=30 step=.1


//
// RTFM You can find the DittyToy API Reference here: https://dittytoy.net/syntax
//
const harpsichord = synth.def(class {
    constructor(options) {
        this.phases = Array.from({length: 24}, () => 0);
    }
    process(note, env, tick, options) {
        let freq = midi_to_hz(note);

        this.phases = this.phases.map((v, i) => v + freq * (i+1) * ditty.dt);

        return this.phases.map(p => Math.sin(p * Math.PI * 2) * env.value).reduce((p, c) => p+c, 0) / this.phases.length;
    }
}, { name: 'harpsichord', release: .4, env: adsr2 });

const overdrive = filter.def( (input, options) => [...input.map(i => clamp(i * options.level, -1, 1))], { level: 1 });

ditty.bpm = 80;
// Adapted from Bach's Cello Suite No. 1 in G Major BWV 1007, copied from https://dittytoy.net/ditty/0c920dd635
let melody = [d3,a3,fs4,e4,fs4,a3,fs4,a3,
             d3,a3,fs4,e4,fs4,a3,fs4,a3,
             d3,b3,g4,fs4,g4,b3,g4,b3,
             d3,b3,g4,fs4,g4,b3,g4,b3,
             d3,cs4,g4,fs4,g4,cs4,g4,cs4,
             d3,cs4,g4,fs4,g4,cs4,g4,cs4,
             d3,b3,fs4,e4,fs4,d4,cs4,d4,
             b3,d4,cs4,d4,fs3,a3,gs3,fs3,
             gs3,d4,e4,d4,e4,d4,e4,d4,
             gs3,d4,e4,d4,e4,d4,e4,d4,
             cs4,e4,a4,gs4,a4,e4,d4,e4,
             cs4,e4,d4,e4,a3,cs4,b3,a3,
             b2,fs3,d4,cs4,d4,fs3,d4,fs3,
             b2,fs3,d4,cs4,d4,fs3,d4,fs3,
             b2,gs3,a3,b3,a3,gs3,fs3,e3,
             d4,cs4,b3,a4,gs4,fs4,e4,d4,
             a2,b3,cs4,a4,e4,a4,cs4,e4,
             g4,b3,cs4,e4,g4,e4,cs4,a3,
             ];

loop( (count, options) => {
    melody.forEach(note => {
        [note].forEach((n, i) => {
            harpsichord.play(n + 6 + (Math.random() * options.honkyTonky), {amp:(Math.random() + 0.5 + (i%8==0?0.8:0)) * .5, pan:0.5 * (2*((n*0.618)%1)-1)});
            sleep(0.25);
        });
    })
}, { name: 'right_hand', honkyTonky: () => input.honkyTonky } ).connect(overdrive.create({level: () => input.overdriveLevel}));

loop( (count, options) => {
    melody.reduce((p, c, ci, a) => { if(ci % 4 == 0) { p.push([c]); } else if(ci % 4 == 1 && (ci < 116 || 128 < ci) ) { p[p.length - 1].push(c); } return p; }, []).forEach(chord => {
        chord.forEach(n => {
            harpsichord.play(n + (Math.random() * options.honkyTonky) - 6, { release: 1.5, amp: .5 })
        });
        sleep(1);
    });
}, { name: 'left_hand', honkyTonky: () => input.honkyTonky } ).connect(overdrive.create({level: () => input.overdriveLevel}));