Electric Piano 1

A simple Electric Piano synth using frequency modulation, playing some jazz

Log in to post a comment.

ditty.bpm = 150;
const TAU = 2*Math.PI;
const msin = (x,m) => Math.sin(TAU*x + m);
// Choose random element from array
const choose = (array) => array[Math.floor(Math.random() * array.length)];


// Electric piano using phase modulation
const ep1 = synth.def( (phase, env, tick, options) => { 
    let sig = msin(phase, msin(11*phase,0) * clamp01(1 - 10*tick)*options.amp);
    sig = sig + msin(phase, msin(phase,0) * 2 * options.amp);
    return sig * env.value;
}, {attack:0.01, release: 0.3, decay: 4, env: adsr2}
);


const mychords = [[c4,'major7'], [a3,'m7'], [d4,'m7'], [g3,'7'],
                 [c4,'major7'], [a3, '7'], [ab3,'7'], [g3,'7'],
                 [e4,'m7'], [e4,'m7-5'], [a3,'7sus4'], [a3,'7'],
                 [d4,'major7'], [d4,'6'], [d4,'m7'], [g3,'7']];

const rhythms = [[0.6,3.4], [1.6,2.4], [2.6,1.4], [3.6,0.4], [0.6, 2, 1.4], [2,1,0.6,0.4]];


loop( () => {
    for(let i=0; i<16; i++)
    {
        const mynote = mychords[i][0];
        const mychordname = mychords[i][1];
        const mychordnotes = chord(mynote, chords[mychordname]);
        
        const rhythm = choose(rhythms);
        for(let ri=0; ri < rhythm.length; ri++)
        {
            let dur = (Math.random() > 0.5) ? rhythm[ri] : 0;
            dur = (dur > 0.8) ? 1 : dur + 0.2;
            for(let j=0; j<mychordnotes.length; j++)
            {
                ep1.play(mychordnotes[j], {amp: Math.random() + 0.3, decay:dur});
            }
            sleep(rhythm[ri]);
        }
    }
    
}, { name: 'EP right hand', amp: 0.1});


loop ( () => {
    for(let i=0; i<16; i++)
    {
        const mynote = mychords[i][0];
        const mychordname = mychords[i][1];
        const mychordnotes = chord(mynote, chords[mychordname]);
        
        const bassnotes = [mynote-24, mychordnotes[2]-24, mynote-12, mynote-24];
        for(let j = 0; j<4; j++)
        {
            ep1.play(bassnotes[j], {amp:1.7-0.2*j + 0.6*Math.random(), decay: 1});
            sleep(1);
        }
   }
}, {name:"EP left hand", amp:0.2});