Piano

Not really a piano, just the cello trick but used with a pulse instead of continuous

Log in to post a comment.

// inspired by https://www.youtube.com/watch?v=Aktb_dmY4vk
// essentially, pass a bandpassed saw wave with vibrato and a bit of noise into a short reverb
// 
// It is possible to not do the reverb, as the diffusion it does that creates the sound
// is essentially a number of parallel delays. A chorus of saw waves with random phases
// gives the same effect.

// bit slower, original is 210
ditty.bpm = 140;

input.tuning = 0.5; //min=0.125, max=2, step=0.125

input.soft = 30; //min=0, max=50, step=1

function softclip(x) {
    return x < -1 ? -1 : x > 1 ? 1 : 1.5*(1-x*x/3)*x;
}

function varsaw(p, formant) {
    let x = p-~~p;
    return (x - 0.5) * softclip(formant*x*(1-x)) * 2;
}

const osc = synth.def(
    class {
        constructor(options) {
            
            // The value of the note argument of the play call is retrievable via options.note.
            this.phases = new Float32Array(16).map(_ => Math.random());
            this.damping = new Float32Array(16).map(_ => Math.random());
            
            // frequencies
            this.freq = midi_to_hz(options.note) * ditty.dt;
            
            // phasor
            this.time = 0;
        }
        process(note, env, tick, options) {
            // phasor times
            this.time += ditty.dt;
            
            // total
            let l = 0.0;
            let r = 0.0;
            
            // all saw waves
            for (let i = 0; i < this.phases.length; i++) {
                // phasor offset
                const offset = Math.sin(i + this.time * 2 * Math.PI * 4);
                
                // sample wave
                const v = varsaw(
                    this.phases[i],
                    this.damping[i] * input.soft * env.value
                ) * env.value;
                
                // update phase
                this.phases[i] += this.freq * input.tuning + offset * ditty.dt * 0. + (i & 8) * ditty.dt * 0.25;
                this.phases[i] -= this.phases[i]|0;
                
                // stereo
                if (i < this.phases.length / 2) l += v;
                else r += v;
            }
            
            return [l * 0.707, r * 0.707];
        }
    }, {
        // attack parameters
        levels: [0, 1, 0, 0],
        times: [0.005, 4, 1],
        env: segenv,
    }
);

// === original ===
// Forked from "Wizards & Warriors (main menu)" by romaindurand
// https://dittytoy.net/ditty/3066954356

function melodyPattern(notes, baseNote) {
    return () => {
        for(i = 0; i < notes.length; i++) {
            osc.play(notes[i], { duration: 0.5, pan: 0.2 - Math.random() * 0.1 });
            sleep(0.5);
            osc.play(baseNote, { duration: 0.5, pan: 0.2 - Math.random() * 0.1 });
            sleep(0.5);
        }
    };
}

function simpleMelodyPattern(notes) {
    // calculate length for it to sound nice
    let lens = new Array(notes.length).fill(0.45);
    let last = 0;
    
    // every off note adds to the on length of the last on note
    for (let i = 0; i < lens.length; i++) {
        if (notes[i] === 0) lens[last] += 0.5;
        else last = i;
    }
    
    return () => {
        for(let i = 0; i < notes.length; i++) {
            osc.play(notes[i], { duration: lens[i], pan: -0.2 - Math.random() * 0.1 });
            sleep(0.5);
        }
    };
}

const melodySeq0 = () => {
    melodyPattern([d5, e5, f5, g5], a4)();
    melodyPattern([bb4, d5, g5, f5], g4)();
    melodyPattern([e5, f5], c5)();
    melodyPattern([g5, c5], g4)();
    melodyPattern([bb4, a4, f5, e5], f4)();
    
    const notes0 = [d5, e5, f5, d5];
    melodyPattern(notes0, bb4)();
    //same as previous pattern with another base note
    melodyPattern(notes0, g4)();
    
    melodyPattern([e5, cs5, e5, cs5], a4)();
    melodyPattern([a5, cs5, a5, a5], a4)();
};

const melodySeq1 = () => {
    melodyPattern([f5, d5], a4)();
    simpleMelodyPattern([f5, g5, a5, a4])();
    simpleMelodyPattern([
        a5, bb4, d5, bb5, a5, bb4, d5, bb5,
        g5, g4, c5, g4, e5, f5, g5, c5,
        g5, a4, c5, a5, f5, a4, e5, c5,
        f5, bb4, d5, bb4, d5, e5, f5, bb4,
        f5, g4, bb4, g4, d5, e5, f5, g4
    ])();
    melodyPattern([f5, cs5, e5, cs5, a5, cs5], a4)();
    melodyPattern([a5, a5], cs5)();
};

const melodySeqEnd = simpleMelodyPattern([d5, a4, e5, f5]);

const bassPattern = simpleMelodyPattern([
        d3, 0, 0, 0, 0, d3, e3, f3,
        g3, 0, 0, 0, 0, g3, a3, bb3,
        c4, 0, 0, 0, 0, bb3, a3, g3,
        f3, 0, g3, 0, a3, 0, f3, 0,
        bb3, 0, 0, 0, 0, c4, bb3, a3,
        g3, 0, 0, 0, 0, a3, bb3, g3,
        a3, 0, 0, 0, a3, 0, 0, 0,
        a3, 0, g3, 0, f3, 0, e3, 0
    ]);

loop(() => {
    melodySeq0();
    melodySeq0();
    melodySeq1();
    melodySeq1();
    melodySeqEnd();
    melodySeqEnd();
    melodySeqEnd();
    simpleMelodyPattern([d5, 0, 0, 0])();
}, { name: 'melody', amp: 0.3 });

loop(() => {
    sleep(32);
    bassPattern();
    bassPattern();
    bassPattern();
    simpleMelodyPattern([d3, 0, 0, 0, 0, 0, 0, 0])();
    sleep(4);
}, { name: 'bass', amp: 0.5 });