PIANO - SOLAS

By Jamie Duffy (adaptation Denis Burel). Coded by Amaia Apiñaniz. (Not finished)

Log in to post a comment.

// PIANO - SOLAS by Jamie Duffy (adaptation Denis Burel). Coded by Amaia Apiñaniz.

ditty.bpm = 80;
const piano = synth.def(class {
   constructor(options) {
        this.phase = 0;
        this.freq = midi_to_hz(options.note);
    }
    process(note, env, tick, options) {
        this.phase += this.freq * ditty.dt;
        return Math.sin(this.phase * Math.PI * 2) * env.value;
    }
}, {    
    levels: [0, 0.2, 0.2, 0], 
    times:  [0.1, 0.1, 0.25], 
    curves: [0, -2, -2],
    env:    segenv,
    duration: 0.5
});

const rightChorusNotes = [
    {number: 'e', octave: 4}, 
    {number: 'gb', octave: 4}, 
    {number: 'g', octave: 4}, 
    {number: 'b', octave: 4}, 
    {number: 'g', octave: 4}, 
    {number: 'gb', octave: 4}
];

const leftDoNotes = [
    {number: 'c', octave: 3}, 
    {number: 'e', octave: 3}, 
    {number: 'g', octave: 3}, 
    {number: 'c', octave: 4}, 
    {number: 'g', octave: 3}, 
    {number: 'e', octave: 3}
];

const leftReNotes = [
    {number: 'd', octave: 3}, 
    {number: 'gb', octave: 3}, 
    {number: 'a', octave: 3}, 
    {number: 'd', octave: 4}, 
    {number: 'a', octave: 3}, 
    {number: 'gb', octave: 3}
];

const leftMiNotes = [
    {number: 'e', octave: 2}, 
    {number: 'g', octave: 2}, 
    {number: 'b', octave: 2}, 
    {number: 'e', octave: 3}, 
    {number: 'b', octave: 2}, 
    {number: 'g', octave: 2}
];

const leftSiNotes = [
    {number: 'b', octave: 2}, 
    {number: 'd', octave: 3}, 
    {number: 'gb', octave: 3}, 
    {number: 'b', octave: 3}, 
    {number: 'gb', octave: 3},
    {number: 'd', octave: 3}
];
    
// 1 tresillos
function playOneHandTripletWabe(notes) {
    for (let i = 0; i < notes.length ; i++) {
        piano.play(notes[i].number + notes[i].octave);
        sleep(1/3); 
    }
}

// 2 tresillos a la vez
function playTwoHandsTripletWabe(leftNotes, rightNotes) {
    for (let i = 0; i < leftNotes.length ; i++) {
        piano.play(leftNotes[i].number + leftNotes[i].octave);
        piano.play(rightNotes[i].number + rightNotes[i].octave);
        sleep(1/3); 
    }
}

// 2 tresillos a la vez con negra con puntillo al final
function playTwoHandsTripletWabeConclusion(leftNotes, rightNotes) {
    for (let i = 0; i < leftNotes.length ; i++) {
        piano.play(leftNotes[i].number + leftNotes[i].octave);
        piano.play(rightNotes[i].number + rightNotes[i].octave);
        if (i + 1 === leftNotes.length){
            sleep(1.3); 
        } else {
            sleep(1/3); 
        }
    }
}

function playChorus() {
    for (let i = 0 ; i < 2 ; i++) {
        playOneHandTripletWabe(rightChorusNotes);
    }
}

function playIntroSecondPart(notes) {
    for (var i = 0; i < notes.length ; i++) {
        piano.play(notes[i].number + notes[i].octave, {duration: 3});
        piano.play(notes[i].number + (notes[i].octave - 1), {duration: 3});
        playChorus();
    }
}

function playIntro() {
    piano.play(e5);
    sleep( 1 ); 
    piano.play(b5);
    sleep( 1 ); 
    piano.play(gb5);
    sleep( 1.1 ); 
    piano.play(eb5);
    sleep( 1.5 ); 
    const introNotes = [
        {number: 'c', octave: 2}, 
        {number: 'e', octave: 2}, 
        {number: 'b', octave: 2}, 
        {number: 'c', octave: 3}, 
        {number: 'c', octave: 3}, 
        {number: 'e', octave: 3}, 
        {number: 'b', octave: 2}, 
        {number: 'c', octave: 2} 
    ];
    playIntroSecondPart(introNotes);
}

function playFirstPart() {
    const notes = [leftDoNotes, leftMiNotes, leftSiNotes, leftDoNotes, leftDoNotes, leftMiNotes, leftSiNotes];
    for (let i = 0; i < notes.length ; i++) {
        for (let j = 0; j < 2 ; j++) {
            playTwoHandsTripletWabe(notes[i], rightChorusNotes);
        }
    }
    const leftDoNotesConclusion = leftDoNotes.slice(0, 4);
    const rightChorusNotesConclusion = rightChorusNotes.slice(0, 4);
    playTwoHandsTripletWabe(leftDoNotes, rightChorusNotes);
    playTwoHandsTripletWabeConclusion(leftDoNotesConclusion, rightChorusNotesConclusion);
}

// INTRO
playIntro();

// FIRST PART
playFirstPart();

// FIRST MELODY
const noteNumbers = ['c','d','e','f','g','a','b'];
function getNextNote(note) {
    let noteNumber = note.number;
    let octave = note.octave;
    let index = noteNumbers.indexOf(noteNumber);
    if (index === -1) {
        return {number: noteNumber.substring(0, 1), octave: octave};
    }
    let nextNoteNumber = index + 1 === noteNumbers.length ? noteNumbers[0] : noteNumbers[index + 1];
    let newOctave = index + 1 === noteNumbers.length ? octave + 1 : octave;
    return {number: nextNoteNumber, octave: newOctave};
}

function playMordent(note) {
    const mordentNote = getNextNote(note);
    piano.play(note.number + note.octave);
    piano.play(mordentNote.number + mordentNote.octave);
}

function playMelodyFirstPart(leftNotes, rightNotes) {
    for (let i = 0 ; i < rightNotes.length ; i++) {
        piano.play(leftNotes[i].number + leftNotes[i].octave, {duration: 1/3});
        
        if (rightNotes[i]){
            let duration = rightNotes[i].duration ? (rightNotes[i].duration) : (1/3);
            if (rightNotes[i].mordent) {
                playMordent(rightNotes[i]);
            }
            piano.play(rightNotes[i].number + rightNotes[i].octave, {duration: duration});
        }
        sleep(1/3); 
    }
}

const melodyFirstRightNotes = [ 
    {number: 'b', octave: 4}, 
    {number: 'd', octave: 5}, 
    {number: 'e', octave: 5}, 
    {number: 'gb', octave: 5, duration: 2/3}, 
    null,
    {number: 'g', octave: 5}, 
    {number: 'gb', octave: 5}, 
    {number: 'e', octave: 5}, 
    {number: 'd', octave: 5}, 
    {number: 'b', octave: 4}, 
    {number: 'a', octave: 4}, 
    {number: 'g', octave: 4} 
];
const duplicateLeftDoNotes = leftDoNotes.concat(leftDoNotes);
playMelodyFirstPart(duplicateLeftDoNotes, melodyFirstRightNotes);

const melodySecondRightNotes = [ 
    {number: 'a', octave: 4}, 
    {number: 'b', octave: 4}, 
    {number: 'd', octave: 5}, 
    {number: 'a', octave: 4, duration: 2/3, mordent: true}, 
    null,
    {number: 'g', octave: 4},
];
const duplicateLeftMiNotes = leftMiNotes.concat(leftMiNotes);
const duplicateRightMelodyNotes = melodySecondRightNotes.concat(melodySecondRightNotes);
playMelodyFirstPart(duplicateLeftMiNotes, duplicateRightMelodyNotes);


const melodyThirdRightNotes = [ 
    {number: 'a', octave: 4}, 
    {number: 'b', octave: 4}, 
    {number: 'd', octave: 5}, 
    {number: 'a', octave: 4, duration: 2/3, mordent: true}, 
    null,
    {number: 'g', octave: 4},
    {number: 'gb', octave: 4}, 
    {number: 'g', octave: 4}, 
    {number: 'b', octave: 4}, 
    {number: 'gb', octave: 4, duration: 2/3, mordent: true}, 
    null,
    {number: 'e', octave: 4},
];
const duplicateLeftReNotes = leftReNotes.concat(leftReNotes);
playMelodyFirstPart(duplicateLeftReNotes, melodyThirdRightNotes);