Rainy Night

Rain sounds and piano melody.

Log in to post a comment.

const piano = filter.def(class { //transofms warp sound into piano sound with rainy background
    constructor(options) {
        this.p = 0.0;
        this.v = 0.0;
    }
    process(input, options) {
        this.v -= (this.v - input[0])*0.1 + 0.25*Math.random()*(Math.sin(Date.now()*0.000325*Math.PI*2)*0.5+0.5 + Math.sin(Date.now()*0.0001*Math.PI*2)*0.5+0.5)*0.5;
        this.p -= (this.p - this.v)*0.1;
        return [this.p, -this.p];
    }
});
const ambiance = filter.def(class {  //makes a rainy sound
    // somehow started to sound good after few minits of messing around
    constructor(options) {
        this.t = 0;
        this.k = 0.0;
        this.l = 0.0;
    }
    process(input, options) {
        this.t += this.t*0.0001 + Math.random()*0.01;
        if (this.t > 2000.) this.t = 0.0;
        let v = (this.t)%1;
        this.k -= (this.k-v)*0.1 + Math.random()*2.0;
        this.l = this.k*0.01+this.l*0.9;
        v = Math.sin(this.l*10.0) * (Math.sin(Date.now()*0.000723523)*0.5+0.5);
        return [-v*0.4, v*0.4];
    }
});
const smoothen = filter.def(class {
    constructor(options) {
        this.p = 0.0;
        this.v = 0.0;
    }
    process(input, options) {
        this.v -= (this.v - input[0])*0.1;
        this.p -= (this.p - this.v)*0.1;
        var atten = Math.sin(Date.now()*0.000025*Math.PI*2)*0.5+0.5;
        return [this.p*atten, -this.p*atten];
    }
});
const warp = 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;
            return ((this.phase)%2 - 1) * env.value;
        }
    }
);

function playSequance(type, notes) { //play list of notes
//notes are defined by [frequancy, durration, release]
//if release is set, durration will be zero, but will be delayed by durration
    for (let note of notes) {
        let duration = note[1];
        let release = note[2];
        if (!release) {
            release = Math.random()*duration*0.1+0.1;
            duration -= release;
        }
        type.play(note[0], {duration: note[2]?0:duration, release: release, attack: 0.005});
        sleep(duration);
    }
}
function setEnding() {
    playSequance(warp, [
        [a3, 1.0], 
        [b3, 1.0], 
        [c4, 1.0], 
        [e4, 1.0]
    ]);
    
    playSequance(warp, [
        [a3, 1.1], 
        [b3, 1.0], 
        [c4, 1.0], 
        [e4, 0.9]
    ]);
}
loop( () => {
    setEnding(); // preper to end sequance
    playSequance(warp, [
        [ds4, 0.4],
        [e4, 0.5], 
        [ds4, 0.6], 
        [c4, 1], 
    ]);
}, { name: 'background' }) 
.connect(smoothen.create()) //smoothns the background to make low frequancys more common
function alertComing() {
    playSequance(warp, [
        [c6, 0.45, 2.0],
        [b5, 0.65, 2.0],
        [g5, 2.1, 2.0],
    ])
}
loop(() => {
    alertComing(); //sepporates parts
    playSequance(warp, [
        [d5, 2, 2],
        [e5, 8, 8],
    ]);
    
    alertComing();
    playSequance(warp, [
        [d5, 2, 2],
        [c5, 8, 8],
    ]);
}, {name: 'main'})
.connect(piano.create()); //connect main to piano filter
loop(()=>sleep(10000000),{name:"ambiance"}).connect(ambiance.create()); //ambiant rain sounds