Last active
February 24, 2018 06:47
-
-
Save timsgardner/3d730e417f8fdc5fffc15254aacd2df7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Note { | |
constructor(noteValue, pitch){ | |
this.pitch = pitch; | |
this.noteValue = noteValue; | |
} | |
} | |
class Bar { | |
constructor(...notes) { | |
this.notes = notes; | |
} | |
} | |
const theScore = [ | |
new Bar(new Note(4, 3), new Note(4, 3), new Note(4, 1), new Note(4, 3)), | |
new Bar(new Note(1, 2)), | |
new Bar(new Note(4, 5)) | |
]; | |
class TimeSignature { | |
constructor(a, b) { | |
this.a = a; // "beats per bar" | |
this.b = b; // "note value per beat" | |
} | |
} | |
// noteValue * (beat / noteValue) * (minute / beat) = duration of noteValue | |
function noteDuration(note, timeSignature, bpm){ | |
const realNoteValue = 1 / note.noteValue; | |
const beatPerRealNoteValue = timeSignature.b; // flips twice | |
return realNoteValue * beatPerRealNoteValue * (1 / bpm) * 60; | |
} | |
function barNoteDurations(bar, timeSignature, bpm){ | |
return bar.notes.map(note => noteDuration(note, timeSignature, bpm)); | |
} | |
function barDuration(bar, timeSignature, bpm) { | |
return barNoteDurations(bar, timeSignature, bpm).reduce((a, b) => a + b, 0); | |
} | |
function scoreBarDurations(score, timeSignature, bpm){ | |
return score.map(bar => barDuration(bar, timeSignature, bpm), timeSignature); | |
} | |
// ====================================================== | |
console.log(theScore); | |
// compute duration in seconds of each bar in the score, given a time signature and BPM | |
console.log(scoreBarDurations(theScore, new TimeSignature(4,4), 120)); | |
// gives [ 2, 2, 0.5 ], which looks right |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what am i doing wrong? if nothing, what's the point of
a
inTimeSignature
?