Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect sustain pedal for playback #2

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions js/Song.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export class Song {
}
distributeEvents(track, newTrack) {
track.forEach(event => {
if (event.type == "noteOn" || event.type == "noteOff") {
if (event.type == "noteOn" || event.type == "noteOff" || ((event.type=="controller") && (event.controllerType==64) )) {
newTrack.notes.push(event)
} else if (event.type == "setTempo") {
newTrack.tempoChanges.push(event)
Expand Down Expand Up @@ -245,24 +245,44 @@ export class Song {
}

static processNotes(notes) {
let sustainPedalOn = false
for (let i = 0; i < notes.length; i++) {
let note = notes[i]
if (note.type == "controller") {
sustainPedalOn = note.value > 64
}
if (note.type == "noteOn") {
Song.findOffNote(i, notes.slice(0))
Song.findOffNote(i, notes.slice(0), sustainPedalOn)
}
}
}

static findOffNote(index, notes) {
static findOffNote(index, notes, sustainPedalOnStart) {
let onNote = notes[index]
let sustainPedalOn = sustainPedalOnStart
let noteOffWasHit = false
let noteWasRepeated = false
for (let i = index + 1; i < notes.length; i++) {
if (notes[i].type == "controller" && notes[i].controllerType==64) {
sustainPedalOn = notes[i].value > 64
}
if (
notes[i].type == "noteOff" &&
onNote.noteNumber == notes[i].noteNumber
) {
onNote.offTime = notes[i].timestamp
onNote.duration = onNote.offTime - onNote.timestamp

noteOffWasHit = true
onNote.offTime = notes[i].timestamp
}
if (
notes[i].type == "noteOn" &&
onNote.noteNumber == notes[i].noteNumber
) {
noteWasRepeated = true
}
if ((noteOffWasHit == true && sustainPedalOn==false)
|| noteWasRepeated)
{
onNote.duration = notes[i].timestamp - onNote.timestamp
break
}
}
Expand Down