Skip to content

Commit

Permalink
Add a way to prevent auto playback
Browse files Browse the repository at this point in the history
resolves #62
  • Loading branch information
spessasus committed Oct 13, 2024
1 parent c788bea commit 4d16ba4
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 31 deletions.
37 changes: 28 additions & 9 deletions src/spessasynth_lib/sequencer/sequencer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ import { DUMMY_MIDI_DATA, MidiData } from "../midi_parser/midi_data.js";

/**
* @typedef {Object} SequencerOptions
* @property {boolean} skipToFirstNoteOn - if true, the sequencer will skip to the first note
* @property {boolean|undefined} skipToFirstNoteOn - if true, the sequencer will skip to the first note
* @property {boolean|undefined} autoPlay - if true, the sequencer will automatically start playing the MIDI
*/

/**
* @type {SequencerOptions}
*/
const DEFAULT_OPTIONS = {
skipToFirstNoteOn: true
skipToFirstNoteOn: true,
autoPlay: true
};

export class Sequencer
Expand Down Expand Up @@ -117,6 +119,13 @@ export class Sequencer
*/
this.isFinished = false;

/**
* Indicates if the sequencer is paused.
* Paused if a number, undefined if playing
* @type {undefined|number}
*/
this.pausedTime = undefined;

/**
* The current sequence's length, in seconds
* @type {number}
Expand All @@ -137,7 +146,7 @@ export class Sequencer
this._sendMessage(WorkletSequencerMessageType.setSkipToFirstNote, false);
}

this.loadNewSongList(midiBinaries);
this.loadNewSongList(midiBinaries, options?.autoPlay ?? false);

window.addEventListener("beforeunload", this.resetMIDIOut.bind(this));
}
Expand Down Expand Up @@ -167,7 +176,7 @@ export class Sequencer
get currentTime()
{
// return the paused time if it's set to something other than undefined
if (this.pausedTime)
if (this.pausedTime !== undefined)
{
return this.pausedTime;
}
Expand Down Expand Up @@ -198,7 +207,7 @@ export class Sequencer
*/
get currentHighResolutionTime()
{
if (this.pausedTime)
if (this.pausedTime !== undefined)
{
return this.pausedTime;
}
Expand Down Expand Up @@ -364,7 +373,11 @@ export class Sequencer
this.absoluteStartTime = 0;
this.duration = this.midiData.duration;
Object.entries(this.onSongChange).forEach((callback) => callback[1](songChangeData));
this.unpause();
// if is auto played, unpause
if (messageData[2] === true)
{
this.unpause();
}
break;

case WorkletSequencerReturnMessageType.textEvent:
Expand Down Expand Up @@ -437,22 +450,28 @@ export class Sequencer
}

/**
* @param midiBuffers {MIDIFile[]}
* Loads a new song list
* @param midiBuffers {MIDIFile[]} - the MIDI files to play
* @param autoPlay {boolean} - if true, the first sequence will automatically start playing
*/
loadNewSongList(midiBuffers)
loadNewSongList(midiBuffers, autoPlay = true)
{
this.pause();
// add some dummy data
this.midiData = DUMMY_MIDI_DATA;
this.hasDummyData = true;
this.duration = 99999;
this._sendMessage(WorkletSequencerMessageType.loadNewSongList, midiBuffers);
this._sendMessage(WorkletSequencerMessageType.loadNewSongList, [midiBuffers, autoPlay]);
this.songIndex = 0;
this.songsAmount = midiBuffers.length;
if (this.songsAmount > 1)
{
this.loop = false;
}
if (autoPlay === false)
{
this.pausedTime = this.currentTime;
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/spessasynth_lib/sequencer/worklet_sequencer/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function processMessage(messageType, messageData)
break;

case WorkletSequencerMessageType.loadNewSongList:
this.loadNewSongList(messageData);
this.loadNewSongList(messageData[0], messageData[1]);
break;

case WorkletSequencerMessageType.pause:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ export const WorkletSequencerMessageType = {
*/
export const WorkletSequencerReturnMessageType = {
midiEvent: 0, // [...midiEventBytes<number>]
songChange: 1, // [midiData<MidiData>, songIndex<number>]
songChange: 1, // [midiData<MidiData>, songIndex<number>, isAutoPlayed<boolean>]
textEvent: 2, // [messageData<number[]>, statusByte<number]
timeChange: 3, // newAbsoluteTime<number>
pause: 4, // no data
getMIDI: 5, // midiData<MIDI>
midiError: 6 // errorMSG<string>
midiError: 6 // errorMSG<string>
};
23 changes: 17 additions & 6 deletions src/spessasynth_lib/sequencer/worklet_sequencer/song_control.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ export function assignMIDIPort(trackNum, port)
/**
* Loads a new sequence
* @param parsedMidi {BasicMIDI}
* @param autoPlay {boolean}
* @this {WorkletSequencer}
*/
export function loadNewSequence(parsedMidi)
export function loadNewSequence(parsedMidi, autoPlay = true)
{
this.stop();
if (!parsedMidi.tracks)
Expand Down Expand Up @@ -122,9 +123,8 @@ export function loadNewSequence(parsedMidi)
this.firstNoteTime = MIDIticksToSeconds(this.midiData.firstNoteOn, this.midiData);
SpessaSynthInfo(`%cTotal song time: ${formatTime(Math.ceil(this.duration)).time}`, consoleColors.recognized);

this.post(WorkletSequencerReturnMessageType.songChange, [new MidiData(this.midiData), this.songIndex]);
this.post(WorkletSequencerReturnMessageType.songChange, [new MidiData(this.midiData), this.songIndex, autoPlay]);

this.synth.resetAllControllers();
if (this.duration <= 1)
{
SpessaSynthWarn(
Expand All @@ -133,14 +133,25 @@ export function loadNewSequence(parsedMidi)
);
this.loop = false;
}
this.play(true);
if (autoPlay)
{
this.play(true);
}
else
{
// this shall not play: play to the first note and then wait
const targetTime = this._skipToFirstNoteOn ? this.midiData.firstNoteOn - 1 : 0;
this.setTimeTicks(targetTime);
this.pause();
}
}

/**
* @param midiBuffers {MIDIFile[]}
* @param autoPlay {boolean}
* @this {WorkletSequencer}
*/
export function loadNewSongList(midiBuffers)
export function loadNewSongList(midiBuffers, autoPlay = true)
{
/**
* parse the MIDIs (only the array buffers, MIDI is unchanged)
Expand Down Expand Up @@ -173,7 +184,7 @@ export function loadNewSongList(midiBuffers)
{
this.loop = false;
}
this.loadNewSequence(this.songs[this.songIndex]);
this.loadNewSequence(this.songs[this.songIndex], autoPlay);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class WorkletSequencer
get currentTime()
{
// return the paused time if it's set to something other than undefined
if (this.pausedTime)
if (this.pausedTime !== undefined)
{
return this.pausedTime;
}
Expand Down
14 changes: 7 additions & 7 deletions src/spessasynth_lib/synthetizer/worklet_processor.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/website/js/manager/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ class Manager
if (this.seq)
{
this.seq.loadNewSongList(parsedMidi);
this.seq.play(true);
return;
}

Expand All @@ -464,7 +465,7 @@ class Manager
this.settingsUI.addSequencer(this.seq);

// play the midi
this.seq.play(true);
//this.seq.play(true);
}

downloadDesfont()
Expand Down
4 changes: 2 additions & 2 deletions src/website/minified/demo_main.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/website/minified/local_main.min.js

Large diffs are not rendered by default.

0 comments on commit 4d16ba4

Please sign in to comment.