From 77850e233b7710723f20afadc2fa8319b35f77b7 Mon Sep 17 00:00:00 2001 From: Darryl Yeo Date: Wed, 23 Sep 2015 21:20:15 -0700 Subject: [PATCH 1/3] Update midifile.js Adds a playTime property for each event by adding deltaTimes together, to calculate event timings relative to the start of the MIDI file. --- midifile.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/midifile.js b/midifile.js index 8062f15..1045b09 100644 --- a/midifile.js +++ b/midifile.js @@ -15,9 +15,17 @@ function MidiFile(data) { var lastEventTypeByte; + var currentPlayTime = 0; + function readEvent(stream) { var event = {}; + event.deltaTime = stream.readVarInt(); + event.playTime = currentPlayTime; + if(event.deltaTime){ + currentPlayTime += event.deltaTime; + } + var eventTypeByte = stream.readInt8(); if ((eventTypeByte & 0xf0) == 0xf0) { /* system / meta event */ From 30ff770278ef2579576b6cf40fe0a6aa785a55e1 Mon Sep 17 00:00:00 2001 From: Darryl Yeo Date: Wed, 23 Sep 2015 21:24:24 -0700 Subject: [PATCH 2/3] Update midifile.js --- midifile.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/midifile.js b/midifile.js index 1045b09..50b2fef 100644 --- a/midifile.js +++ b/midifile.js @@ -22,9 +22,7 @@ function MidiFile(data) { event.deltaTime = stream.readVarInt(); event.playTime = currentPlayTime; - if(event.deltaTime){ - currentPlayTime += event.deltaTime; - } + currentPlayTime += event.deltaTime || 0; var eventTypeByte = stream.readInt8(); if ((eventTypeByte & 0xf0) == 0xf0) { From 4c68480a7878e0dc21209a98e16a0f8f74ff9603 Mon Sep 17 00:00:00 2001 From: Darryl Yeo Date: Wed, 23 Sep 2015 21:30:57 -0700 Subject: [PATCH 3/3] Update midifile.js --- midifile.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/midifile.js b/midifile.js index 50b2fef..4edef45 100644 --- a/midifile.js +++ b/midifile.js @@ -15,14 +15,9 @@ function MidiFile(data) { var lastEventTypeByte; - var currentPlayTime = 0; - function readEvent(stream) { var event = {}; - event.deltaTime = stream.readVarInt(); - event.playTime = currentPlayTime; - currentPlayTime += event.deltaTime || 0; var eventTypeByte = stream.readInt8(); if ((eventTypeByte & 0xf0) == 0xf0) { @@ -230,8 +225,11 @@ function MidiFile(data) { throw "Unexpected chunk - expected MTrk, got "+ trackChunk.id; } var trackStream = Stream(trackChunk.data); + var currentPlayTime = 0; while (!trackStream.eof()) { var event = readEvent(trackStream); + event.playTime = currentPlayTime; + currentPlayTime += event.deltaTime || 0; tracks[i].push(event); //console.log(event); }