Resonace Reverb

Calculates reverb by simulating springs

Log in to post a comment.

//
// You can find the Dittytoy API Reference here: https://Dittytoy.net/syntax
// Example ditties can be found here: https://dittytoy.net/user/Dittytoy
//
// Most of your ditty will run 44100 times per second using javascript in the browser.
// Make sure you optimize your ditty to work well on as many devices as possible. To do that, try to limit
// the number of simultaneously active synths: make sure they don't last longer than necessary, or you can
// hear them, and spread them over different loops (each loop runs in a separate worker).
//
class Imprint {
    constructor(frequancy, absorbsion, weight=1) {
    	this.x = 0;
        this.v = 0;
        this.a = 0;
        this.weight= weight;
        this.frequancy = frequancy;
        this.absorbsion = absorbsion;
    }
    sample(s) {
        this.v *= this.absorbsion;
        this.v += s+Math.random()-0.5;//Math.max(Math.min(s*50, 1), -1);
        this.v -= this.x;
        this.x += this.v*this.frequancy;
        return this.x*this.weight;
    }
}
let reverb = filter.def(class {
	constructor(options) {
    	this.mem = [];
    	this.max =0;
        for (let i = 0; i < 512; i++) {
            let res = Math.random()*options.frequancy/44000;
            let weight = Math.random();
            this.max += weight;
        	this.mem.push(new Imprint(res,1-options.decay, weight));
        }
    }
    process(input) {
        let s = input[0] // actual sound
    	let out = 0;
    	for (let i of this.mem) {
        	out += i.sample(s);
        }
        out /= this.max;
        return [out, out];
    }
})
ditty.bpm = 120;
const wave = synth.def( //synthisizer used in all notes
    class {
        constructor(options) {
            this.phase = 0;
            this.k = 1.0;
        }
        process(note, env, tick, options) {
            this.phase += midi_to_hz(note) * ditty.dt+(Math.random()*0.000001)*this.phase;
            return ((this.phase)%2) * env.value;
        }
    }
);

let combos = [[() => {
    wave.play(c4, { attack: 0.01, release: 0.75,  duration: 0.25 });
    sleep(0.5);
    
    wave.play(ds4, { attack: 0.01, release: 0.75,  duration: 0.25 });
    sleep(0.5);
    
    wave.play(f4, { attack: 0.01, release: 0.75,  duration: 0.25 });
    sleep(0.5);
    
    wave.play(gs4, { attack: 0.01, release: 0.75,  duration: 0.25 });
    sleep(0.5)

    wave.play(c4, { attack: 0.01, release: 0.75,  duration: 0.25 });
    sleep(0.5);
    
    wave.play(ds4, { attack: 0.01, release: 0.75,  duration: 0.25 });
    sleep(0.5);
    
    wave.play(gs4, { attack: 0.01, release: 0.75,  duration: 0.25 });
    sleep(0.5);
    
    wave.play(g4, { attack: 0.01, release: 1,  duration: 0.25 });
    sleep(0.5);
}, [1]], 
[() => {
    wave.play(c4, { attack: 0.01, release: 0.75,  duration: 0.25 });
    sleep(0.5);
    
    wave.play(ds4, { attack: 0.01, release: 0.75,  duration: 0.25 });
    sleep(0.5);
}, [1, 2, 3]],
[() => {
    wave.play(gs4, { attack: 0.01, release: 0.75,  duration: 0.25 });
    sleep(0.5);
    
    wave.play(g4, { attack: 0.01, release: 0.75,  duration: 0.25 });
    sleep(0.5);
    
    wave.play(ds4, { attack: 0.01, release: 2,  duration: 0.25 });
    sleep(2);
    
    
    
    wave.play(fs4, { attack: 0.01, release: 1,  duration: 0.25 });
    sleep(0.5);
    
    wave.play(f4, { attack: 0.01, release: 1,  duration: 0.25 });
    sleep(0.5);
    
    wave.play(cs4, { attack: 0.01, release: 3,  duration: 0.25 });
    sleep(2);
    
}, [2, 0]],
[() => {
    wave.play(c3, { attack: 0.01, release: 0.5,  duration: 0.25 });
    sleep(0.5);
    wave.play(c3, { attack: 0.01, release: 1,  duration: 0.25 });
    sleep(1);
    
    
    wave.play(d3, { attack: 0.01, release: 0.5,  duration: 0.25 });
    sleep(0.5);
    wave.play(d3, { attack: 0.01, release: 1,  duration: 0.25 });
    sleep(1);
    
    wave.play(e3, { attack: 0.01, release: 0.75,  duration: 0.25 });
    sleep(0.5);
    wave.play(e3, { attack: 0.01, release: 0.75,  duration: 0.25 });
    sleep(1);
    
    wave.play(g3, { attack: 0.01, release: 0.75,  duration: 0.25 });
    sleep(0.5);
    wave.play(g3, { attack: 0.01, release: 0.75,  duration: 0.25 });
    sleep(1);
    
}, [3, 0]]
]
function pickRandom(ar) {
    return ar[parseInt(Math.floor(Math.random()*ar.length))]
}
let state = 0;
loop( () => {
    combos[state][0]();
    state = pickRandom(combos[state][1])
}, { name: 'my first loop' }).connect(reverb.create({frequancy: 1000, decay: 0.0001}));

loop( () => {
    combos[3][0]();
}, { name: 'my seccond loop' }).connect(reverb.create({frequancy: 1000, decay: 0.001}));