Skip to content

Sequencer Class

spessasus edited this page Jun 19, 2024 · 27 revisions

Sequencer Class

This is the module that plays the parsed MIDI files using the Synthetizer class.

Importing

import {Sequencer} from "./spessasynth_lib/sequencer/sequencer.js";

Initialization

const sequencer = new Sequencer(parsedMidis, synth);
  • parsedMidis - an array of the parsed MIDI files to play, instances of the MIDI class.
  • synth - the synthetizer to use. An instance of the Synthetizer class.

Methods

play

Starts playing the sequence. If the sequence was paused, it won't change any controllers, but if it wasn't (ex. the time was changed) then it will go through all the controller changes from the start before playing. This function does NOT modify the current playback time!

sequencer.play(resetTime);
  • resetTime - boolean, if set to true then the playback will start from 0. Defaults to false;

pause

Pauses the playback of the sequence.

sequencer.pause();

stop

Stops the playback of the sequence. Currently only used internally by the pause function.

sequencer.stop();

nextSong

Plays the next song in the list.

sequencer.nextSong();

previousSong

Plays the previous song in the list.

sequencer.previousSong();

connectMidiOutput

Connects a given MIDI output port and plays the sequence to it.

sequencer.connectMidiOutput(output);
  • output - a MIDIOutput object, the output port to play to.

Tip

Pass undefined to use SpessaSynth.

addOnSongChangeEvent

Hooks up a given callback function to the song change event.

sequencer.addOnSongChangeEvent(callback, id);
  • callback - the function that gets called back, takes a MIDI instance (the new song).
  • id - string, unique identifier for the callback. Can be anything as long as it's unique.

addOnTimeChangeEvent

Hooks up a given callback function to the time change event.

sequencer.addOnTimeChangeEvent(callback, id);
  • callback - the function that gets called back, takes a number (the new time, in seconds).
  • id - string, unique identifier for the callback. Can be anything as long as it's unique.

Properties

paused

Read-only boolean, indicating that if the sequencer's playback is paused.

if(sequencer.paused)
{
   console.log("Sequencer paused!");
}
else
{
   console.log("Sequencer playing or stopped!");
}

loop

Boolean that controls if the sequencer loops.

sequencer.loop = false; // the playback will stop after reaching the end

currentTime

Property used for changing and reading the current playback time.

get

Returns the current playback time in seconds.

console.log("The sequences is playing for"+sequencer.currentTime+" seconds.");

set

Sets the current playback time. Calls stop and then play internally.

sequencer.currentTime = 0; // go to the start

duration

Length of the track in seconds. Equivalent of Audio.duration;

console.log(`The track lasts for ${sequencer.duration} seconds!`);

onTextEvent

A callback function if defined. Will be called on a text event, like lyrics.

sequencer.onTextEvent = (messageData, messageType) => {
    const text = new TextDecoder("utf-8").decode(messageData.buffer);
    console.log("Text event:", text)
}

Parameters:

  • messageData - Uint8Array, the message's data (excluding the statusByte).
  • messageType - the Status byte of the meta message useful for derermining if the message is lyrics, or something else.