Skip to content

Commit baf6521

Browse files
committed
fix soft loop tempo mismatch
1 parent a54474e commit baf6521

1 file changed

Lines changed: 14 additions & 13 deletions

File tree

src/sequencer/sequencer.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
import { processEventInternal } from "./process_event";
22
import { processTick } from "./process_tick";
3-
import {
4-
assignMIDIPortInternal,
5-
loadNewSequenceInternal
6-
} from "./song_control";
3+
import { assignMIDIPortInternal, loadNewSequenceInternal } from "./song_control";
74
import { setTimeToInternal } from "./play";
85

96
import { MIDI_CHANNEL_COUNT } from "../synthesizer/audio_engine/engine_components/synth_constants";
107
import { BasicMIDI } from "../midi/basic_midi";
118
import type { SpessaSynthProcessor } from "../synthesizer/processor";
12-
import {
13-
type MIDIController,
14-
midiControllers,
15-
midiMessageTypes
16-
} from "../midi/enums";
9+
import { type MIDIController, midiControllers, midiMessageTypes } from "../midi/enums";
1710
import type { SequencerEvent, SequencerEventData } from "./types";
1811
import { SpessaSynthWarn } from "../utils/loggin";
1912
import { arrayToHexString } from "../utils/other";
@@ -501,26 +494,34 @@ export class SpessaSynthSequencer {
501494

502495
/**
503496
* Jumps to a MIDI tick without any further processing.
504-
* @param tick The MIDI tick to jump to.
497+
* @param targetTicks The MIDI tick to jump to.
505498
* @protected
506499
*/
507-
protected jumpToTick(tick: number) {
500+
protected jumpToTick(targetTicks: number) {
508501
if (!this._midiData) {
509502
return;
510503
}
511504
this.sendMIDIAllOff();
512-
const seconds = this._midiData.midiTicksToSeconds(tick);
505+
const seconds = this._midiData.midiTicksToSeconds(targetTicks);
513506
this.callEvent("timeChange", { newTime: seconds });
514507

515508
// Recalculate time and reset indexes
516509
this.recalculateStartTime(seconds);
517510
this.playedTime = seconds;
518511
this.eventIndexes.length = 0;
519512
for (const track of this._midiData.tracks) {
520-
const idx = track.events.findIndex((e) => e.ticks >= tick);
513+
const idx = track.events.findIndex((e) => e.ticks >= targetTicks);
521514
// Not length - 1 since we want to mark the track as finished
522515
this.eventIndexes.push(idx < 0 ? track.events.length : idx);
523516
}
517+
518+
// Correct tempo
519+
// Some softy-looped files (example: th06_06.mid) have slightly mismatched tempos
520+
const targetTempo = this._midiData.tempoChanges.find(
521+
(t) => t.ticks <= targetTicks
522+
)!;
523+
this.oneTickToSeconds =
524+
60 / (targetTempo.tempo * this._midiData.timeDivision);
524525
}
525526

526527
/*

0 commit comments

Comments
 (0)