Skip to content

Commit

Permalink
Merge pull request #89 from ThebestkillerTBK/master
Browse files Browse the repository at this point in the history
Dynamic tempo support
  • Loading branch information
grimmdude authored Feb 8, 2022
2 parents bc8751e + 9a69d5c commit 0a06954
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/meta-events/tempo-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class TempoEvent {

this.type = 'tempo';

this.tick = fields.tick;

const tempo = Math.round(60000000 / fields.bpm);

// Start with zero time delta
Expand Down
8 changes: 6 additions & 2 deletions src/track.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ class Track {
precisionLoss = Utils.getPrecisionLoss(event.deltaWithPrecisionCorrection || 0);
this.data = this.data.concat(built.data);
this.tickPointer = Utils.getRoundedIfClose(event.tick);
} else if (event instanceof TempoEvent) {
this.tickPointer = Utils.getRoundedIfClose(event.tick);
this.data = this.data.concat(event.data);
} else {
this.data = this.data.concat(event.data);
}
Expand Down Expand Up @@ -206,10 +209,11 @@ class Track {
/**
* Sets tempo of the MIDI file.
* @param {number} bpm - Tempo in beats per minute.
* @param {number} tick - Start tick.
* @return {Track}
*/
setTempo(bpm) {
return this.addEvent(new TempoEvent({bpm}));
setTempo(bpm, tick = 0) {
return this.addEvent(new TempoEvent({bpm, tick}));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ declare module "midi-writer-js" {
event: EventType | EventType[],
mapFunction?: (index: number, event: EventType) => Partial<EventType>
): void;
setTempo(tempo: number): void;
setTempo(tempo: number, tick: number): void;
addText(text: string): void;
addCopyright(text: string): void;
addTrackName(text: string): void;
Expand Down

0 comments on commit 0a06954

Please sign in to comment.