Twin delay reverb

Reverb using only two delays, inspired by youtube.com/watch?v=nat7s3okdv0

Piano instrument from Rubedo

Log in to post a comment.

// === reverb ===
// see https://www.youtube.com/watch?v=Nat7S3OKDV0 for inspiration

input.wet = 1.0; // min=0, max=1, step=0.01

const reverb = filter.def(class {
    constructor() {
        // two delays
        this.d1 = new Float32Array(ditty.sampleRate);
        this.d2 = new Float32Array(ditty.sampleRate);
        
        // index into the delays
        this.i1 = 0;
        this.i2 = 0;
    }
    process(inp) {
        const i = inp[0] + inp[1];
        
        // golden ratio
        const gr = 0.5 * (1 + Math.sqrt(5));
        
        // delay lengths
        const l1 = ditty.sampleRate * 1e-3 * 30;
        const l2 = l1 * gr;
        
        // read the delay outputs
        const o1 = this.d1[(this.i1 + l1 | 0) % this.d1.length];
        const o2 = this.d2[(this.i2 + l2 | 0) % this.d1.length];
        
        // write the delays
        // anything above 0.6 seems to explode eventually
        this.d1[this.i1] = i + (o2 - o1) * 0.5;
        this.d2[this.i2] = i + (o1 + o2) * 0.5;
        
        // wrap around the read head
        this.i1 = this.i1 <= 0 ? this.d1.length - 1 : this.i1 - 1;
        this.i2 = this.i2 <= 0 ? this.d2.length - 1 : this.i2 - 1;
        
        // output the other delay line to the other ear to make it sound stereo
        return [
            o1 * input.wet + inp[0] * (1 - input.wet),
            o2 * input.wet + inp[1] * (1 - input.wet)
        ];
    }
});

// === instrument ===
// this and the song is from my other ditty "Rubedo": https://dittytoy.net/ditty/55bd6ffb2e

// basic sine oscillator that somewhat sounds like a plucked String
// tuned vibrato to match the original song
const osc = synth.def(class {
    constructor(options) {
        this.phasel = Math.random();
        this.phaser = Math.random();
        this.delta = midi_to_hz(options.note) * ditty.dt;
        
        // vibrato
        this.vib = Math.random();
        this.ofs = ditty.dt * 4;
    }
    process(note, env, tick, options) {
        // phase
        this.phasel += this.delta + Math.sin(this.vib * 2 * Math.PI) * this.delta * 0.006;
        this.phasel -= this.phase|0;
        
        this.phaser += this.delta + Math.sin(this.vib * 2 * Math.PI) * this.delta * 0.006;
        this.phaser -= this.phase|0;
        
        // vibrato
        this.vib += this.ofs;
        this.vib -= this.phase|0;
        
        // fm modulator
        let fm_mod_l = Math.sin(this.phasel * 2 * Math.PI) * env.value
            + Math.sin(this.phasel * 4 * Math.PI) * env.value * env.value * 0.4;
        let fm_mod_r = Math.sin(this.phaser * 2 * Math.PI) * env.value
            + Math.sin(this.phaser * 4 * Math.PI) * env.value * env.value * 0.4;
        
        // fm carrier
        let fml = Math.sin(this.phasel * 2 * Math.PI + fm_mod_l) * env.value;
        let fmr = Math.sin(this.phaser * 2 * Math.PI + fm_mod_r) * env.value;
        
        return [fml, fmr];
    }
}, { attack: 0.001, decay: 2.0, sustain: 0.5, release: 0.5 });

// === melody ===

ditty.bpm = 80;

// play a few notes
function melodyPattern(notes) {
    for (let i = 0; i < notes.length; i++) {
        osc.play(notes[i], { duration: 0.5, amp: 0.4 + 0.6 * Math.random() });
        sleep(0.5);
    }
}

const pat0 = () => {
    for (let i = 0; i < 4; i++) melodyPattern([e3, b3, d4, a4]);
    for (let i = 0; i < 4; i++) melodyPattern([e3, b3, d4, gs4]);
    for (let i = 0; i < 4; i++) melodyPattern([e3, b3, d4, g4]);
    for (let i = 0; i < 4; i++) melodyPattern([e3, b3, d4, gs4]);
    // TODO other notes
};

loop( () => {
    pat0();
}, { name: 'Piano', amp: 0.3 }).connect(reverb.create());