Time Machine

A melody played by a simple piano synthesizer with a reverb and some other stuff.

Log in to post a comment.

ditty.bpm = 120;
const quality = 12;
let bandPass = (f0, f1) => {
    //something like a band pass filter. i doubt that its actualy a band pass filter
    let a = {
        f0: f0,
        f1: f1,
        z0: 0,
        z1: 0,
        p: 0,
        v: 0,
        sample(sample) {
           a.z0 = lerp(a.z0, sample, a.f0);
           a.z1 = lerp(a.z1, sample, a.f1);
           
           let k = a.z0 + (sample-a.z1);
           return k;
        }
    }
    return a;
}
let resonancePass = (k, damp=0.99) => {
    // passes spacific freqancys more than others based on k
    let a = {
        k: k,
        p: 0,
        v: 0,
        damp: damp,
        sample(sample) {
           let k = sample;
           let acc = k-a.p;
           a.v += acc*a.k*(Math.cos(a.p*3)*0.5+0.5);
           a.p += a.v;
           a.p *= a.damp;
           return a.p;
        }
    }
    return a;
}
let saw = (x) => {
    return 
}
let ding = synth.def(class {
    constructor(options) {
        if (options.resonance === undefined) options.resonance = [0.01, 0.1]
        this.z = 0;
        this.filters = [];
        for (let i = 0; i < quality; i++) {
            this.filters.push(resonancePass(lerp(options.resonance[0], options.resonance[1], Math.pow(Math.random(), 4))));
        }
        this.band = bandPass(0.05, 0.1);
        this.time = 0;
    }
    process(note, env) {
        let wave = (x) => {
            return ((x + x%2)%1)*2-1;
        }
        this.time += ditty.dt * (midi_to_hz(note));
        let signal = wave(this.time)*env.value;
        let mean = 0;
        for (let i of this.filters) mean += i.sample(signal);
        mean /= this.filters.length;
        return this.band.sample(mean);
    }
}, "ding");
let loop1 = 0;
loop(() => {
    // ding dong ding ding dong dong dingdong !! 
    let amp = 0;
    if (loop1 > 0) amp = 0.125;
    if (loop1 > 4) amp = 0;
    ding.play(c3, { attack: 0, release: 3,  duration: 0, amp: amp});
    ding.play(e4, { attack: 0, release: 2,  duration: 0, amp: amp});
    sleep(2);
    ding.play(c4, { attack: 0, release: 1.5,  duration: 0, amp: amp});
    sleep(1);
    ding.play(d4, { attack: 0, release: 1.5,  duration: 0, amp: amp});
    if (loop1%2 == 0) {
        sleep(1);
        ding.play(e4, { attack: 0, release: 1.5,  duration: 0, amp: amp});
        
        sleep(1);
    } else sleep(2);
    ding.play(c4, { attack: 0, release: 1.5,  duration: 0, amp: amp});
    sleep(1);
    ding.play(d3, { attack: 0, release: 3,  duration: 0, amp: amp});
    ding.play(a3, { attack: 0, release: 2,  duration: 0, amp: amp});
    sleep(2);
    ding.play(g3, { attack: 0, release: 1.5,  duration: 0, amp: amp});
    sleep(1);
    ding.play(e3, { attack: 0, release: 2,  duration: 0, amp: 0.8*amp});
    sleep(2);
    ding.play(g3, { attack: 0, release: 2,  duration: 0, amp: 1*amp});
    sleep(2);
    
    let a = () => {
        ding.play(c3, { attack: 0, release: 3,  duration: 0, amp: amp});
        ding.play(e4, { attack: 0, release: 2,  duration: 0, amp: amp});
        sleep(1.5);
        ding.play(g4, { attack: 0, release: 2,  duration: 0, amp: 2*amp});
        sleep(1.5);
        ding.play(d4, { attack: 0, release: 4,  duration: 0, amp: amp});
        ding.play(d3, { attack: 0, release: loop1==4?8:4,  duration: 0, amp: amp});
        sleep(4);
    }
    let b = () => {
        ding.play(c3, { attack: 0, release: 3,  duration: 0, amp: 0.5*amp});
        ding.play(e4, { attack: 0, release: 2,  duration: 0, amp: 0.5*amp});
        sleep(1.5);
        ding.play(g4, { attack: 0, release: 2,  duration: 0, amp: 1*amp});
        sleep(1.5);
        ding.play(c4, { attack: 0, release: 4,  duration: 0, amp: 1*amp});
        ding.play(c3, { attack: 0, release: 4,  duration: 0, amp: 0.5*amp});
        sleep(2);
    };
    loop1%2==1 ? (a(),b()) : (b(),a());
    sleep(10);
    
    loop1++;
}, {name: "1"});
let loop2 = 0;
loop(() => {
    let amp = 0.0;
    if (loop2 > 3) {
        amp = 0.15;
    }
    if (loop2 > 35) {
        amp /= Math.exp(loop2-35);
    }
    let k = lerp(0.5, 1, Math.random())*amp;
    ding.play(c5, { attack: 0, release: 1.5,  duration: 0, amp: 1*k});
    sleep(1);
    ding.play(b4, { attack: 0, release: 1.5,  duration: 0, amp: 0.8*k});
    sleep(1);
    ding.play(g4, { attack: 0, release: 1.5,  duration: 0, amp: 0.7*k});
    sleep(1);
    ding.play(e4, { attack: 0, release: 1.5,  duration: 0, amp: 0.8*k});
    sleep(1);
    loop2++;
}, {name: "2"});
let loop3 = 0;
loop(() => {
    let amp = 0.09*(1-Math.exp(-loop3));
    if (loop3 > 138) {
        amp /= Math.exp(loop3-138);
    }
    ding.play(d4, { attack: 0, release: 1+Math.random()*0.1,  duration: 0, amp: amp, resonance: [0.02, 0.1]});
    sleep(0.5);
    ding.play(a3, { attack: 0, release: 1.5+Math.random()*0.1,  duration: 0, amp: amp, resonance: [0.02, 0.1]});
    sleep(0.5);
    loop3 ++;
}, {name: "3"});
let loop4 = 0;
loop(() => {
    let amp = 0;
    if (loop4 > 4) amp = 0.09;
    if (loop4 > 22) {
        amp /= Math.exp(loop4-22);
    }
    ding.play(c3, { attack: 0, release: 1+Math.random()*0.1,  duration: 0, amp: amp, resonance: [0.02, 0.1]});
    sleep(1.5);
    ding.play(b2, { attack: 0, release: 1.5+Math.random()*0.1,  duration: 0, amp: amp, resonance: [0.02, 0.1]});
    sleep(1.5);
    ding.play(a2, { attack: 0, release: 1.5+Math.random()*0.1,  duration: 0, amp: amp, resonance: [0.02, 0.1]});
    sleep(1.5);
    ding.play(a2, { attack: 0, release: 1.5+Math.random()*0.1,  duration: 0, amp: amp, resonance: [0.02, 0.1]});
    sleep(1.5);
    loop4++;
}, {name: "4"});