merge in some updates to jasmid
parent
072eadb4af
commit
7b12856d9d
|
@ -12,9 +12,9 @@ function MidiFile(data) {
|
|||
'data': stream.read(length)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
var lastEventTypeByte;
|
||||
|
||||
|
||||
function readEvent(stream) {
|
||||
var event = {};
|
||||
event.deltaTime = stream.readVarInt();
|
||||
|
@ -102,7 +102,7 @@ function MidiFile(data) {
|
|||
case 0x59:
|
||||
event.subtype = 'keySignature';
|
||||
if (length != 2) throw "Expected length for keySignature event is 2, got " + length;
|
||||
event.key = stream.readInt8();
|
||||
event.key = stream.readInt8(true);
|
||||
event.scale = stream.readInt8();
|
||||
return event;
|
||||
case 0x7f:
|
||||
|
@ -194,7 +194,7 @@ function MidiFile(data) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
stream = Stream(data);
|
||||
var headerChunk = readChunk(stream);
|
||||
if (headerChunk.id != 'MThd' || headerChunk.length != 6) {
|
||||
|
@ -204,13 +204,13 @@ function MidiFile(data) {
|
|||
var formatType = headerStream.readInt16();
|
||||
var trackCount = headerStream.readInt16();
|
||||
var timeDivision = headerStream.readInt16();
|
||||
|
||||
|
||||
if (timeDivision & 0x8000) {
|
||||
throw "Expressing time division in SMTPE frames is not supported yet"
|
||||
} else {
|
||||
ticksPerBeat = timeDivision;
|
||||
}
|
||||
|
||||
|
||||
var header = {
|
||||
'formatType': formatType,
|
||||
'trackCount': trackCount,
|
||||
|
@ -230,7 +230,7 @@ function MidiFile(data) {
|
|||
//console.log(event);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
'header': header,
|
||||
'tracks': tracks
|
||||
|
|
|
@ -10,27 +10,38 @@ function Replayer(midiFile, timeWarp, eventProcessor) {
|
|||
var trackStates = [];
|
||||
var beatsPerMinute = 120;
|
||||
var ticksPerBeat = midiFile.header.ticksPerBeat;
|
||||
|
||||
for (var i = 0; i < midiFile.tracks.length; i++) {
|
||||
trackStates[i] = {
|
||||
"nextEventIndex": 0,
|
||||
"ticksToNextEvent": (
|
||||
midiFile.tracks[i].length ? midiFile.tracks[i][0].deltaTime : null
|
||||
'nextEventIndex': 0,
|
||||
'ticksToNextEvent': (
|
||||
midiFile.tracks[i].length ?
|
||||
midiFile.tracks[i][0].deltaTime :
|
||||
null
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
var nextEventInfo;
|
||||
var samplesToNextEvent = 0;
|
||||
|
||||
function getNextEvent() {
|
||||
var ticksToNextEvent = null;
|
||||
var nextEventTrack = null;
|
||||
var nextEventIndex = null;
|
||||
|
||||
for (var i = 0; i < trackStates.length; i++) {
|
||||
if (trackStates[i].ticksToNextEvent != null && (ticksToNextEvent == null || trackStates[i].ticksToNextEvent < ticksToNextEvent)) {
|
||||
if (
|
||||
trackStates[i].ticksToNextEvent != null
|
||||
&& (ticksToNextEvent == null || trackStates[i].ticksToNextEvent < ticksToNextEvent)
|
||||
) {
|
||||
ticksToNextEvent = trackStates[i].ticksToNextEvent;
|
||||
nextEventTrack = i;
|
||||
nextEventIndex = trackStates[i].nextEventIndex;
|
||||
}
|
||||
}
|
||||
if (nextEventTrack != null) {
|
||||
/// consume event from that track
|
||||
/* consume event from that track */
|
||||
var nextEvent = midiFile.tracks[nextEventTrack][nextEventIndex];
|
||||
if (midiFile.tracks[nextEventTrack][nextEventIndex + 1]) {
|
||||
trackStates[nextEventTrack].ticksToNextEvent += midiFile.tracks[nextEventTrack][nextEventIndex + 1].deltaTime;
|
||||
|
@ -38,7 +49,7 @@ function Replayer(midiFile, timeWarp, eventProcessor) {
|
|||
trackStates[nextEventTrack].ticksToNextEvent = null;
|
||||
}
|
||||
trackStates[nextEventTrack].nextEventIndex += 1;
|
||||
/// advance timings on all tracks by ticksToNextEvent
|
||||
/* advance timings on all tracks by ticksToNextEvent */
|
||||
for (var i = 0; i < trackStates.length; i++) {
|
||||
if (trackStates[i].ticksToNextEvent != null) {
|
||||
trackStates[i].ticksToNextEvent -= ticksToNextEvent
|
||||
|
@ -57,11 +68,15 @@ function Replayer(midiFile, timeWarp, eventProcessor) {
|
|||
var midiEvent;
|
||||
var temporal = [];
|
||||
//
|
||||
function processEvents() {
|
||||
function processNext() {
|
||||
if (midiEvent.ticksToEvent > 0) {
|
||||
var beatsToGenerate = midiEvent.ticksToEvent / ticksPerBeat;
|
||||
var secondsToGenerate = beatsToGenerate / (beatsPerMinute / 60);
|
||||
function processEvents() {
|
||||
function processNext() {
|
||||
// respect Tempo Event - Mani
|
||||
if (midiEvent.event.type == "meta" && midiEvent.event.subtype == "setTempo" ) {
|
||||
beatsPerMinute = 60000000 / midiEvent.event.microsecondsPerBeat;
|
||||
}
|
||||
if (midiEvent.ticksToEvent > 0) {
|
||||
var beatsToGenerate = midiEvent.ticksToEvent / ticksPerBeat;
|
||||
var secondsToGenerate = beatsToGenerate / (beatsPerMinute / 60);
|
||||
}
|
||||
var time = (secondsToGenerate * 1000 * timeWarp) || 0;
|
||||
temporal.push([ midiEvent, time]);
|
||||
|
|
|
@ -29,8 +29,9 @@ function Stream(str) {
|
|||
}
|
||||
|
||||
/* read an 8-bit integer */
|
||||
function readInt8() {
|
||||
function readInt8(signed) {
|
||||
var result = str.charCodeAt(position);
|
||||
if (signed && result > 127) result -= 256;
|
||||
position += 1;
|
||||
return result;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue