MidiLib master READ ONLY 0
  • explorer
  • search
a [App]
a CG.MidiLib
a README.md
a app.ts
a [Test]
a test
  • README.md

MidiLib

A simplified library to easily play midi music.

Getting Started

Minimum codes to get a midi to play.

async function examples() {
  // load the midi resource
  let player = await loadAndCreateMidiInstrumentPlayer("MidiLib.testmidi");
  player.play();
}

Also support playSound() in Base library.

CG.Base.resourceManager.playSound("MidiLib.testmidi");

Codes to get full control.

async function examples() {
  // load the midi resource
  await resourceManager.addAppResource("MidiLib.testmidi").load();
  // create the player
  let player = createMidiInstrumentPlayer("MidiLib.testmidi");
  // preload instruments, you can specify the default instrument
  // find the full list of instrument names in `instruments.ts`
  await player.preload({ defaultInstrument: "acoustic_grand_piano" });
  // play the music
  player.play({
    loop: true,
    startTick: 0,
    loopStartTick: 0,
    gain: 2,            // volume
    tempoScale: 1.5,    // tempo
  });
  // control the music
  player.stop();
  player.pause();
  player.resume();
}

Player Events

Player emits events for advanced controlling

Track Events

This event emits when a midi note is time to be played (or other meta data is dispatched.)

function catchTrackEvents(player: InstrumentPlayer) {
  player.on(MidiTrackEvent.TYPE, (event: MidiTrackEvent) => {
    let trackEvent = event.trackEvent;
    if(trackEvent.name == NoteOnEvent.NAME) {
      // check events/TrackEvent.ts for all TrackEvent names
      ...
    }
  })
}

EndOfFileEvent

This event emits when the play head running to the end of all tracks.

function catchEndOfFileEvent(player: InstrumentPlayer) {
  player.on(EndOfFileEvent.TYPE, (event: EndOfFileEvent) => {
    ...
  })
}

ControlEvents

This event emits when the player is being controled by calling control functions

function catchControlEvents(player: InstrumentPlayer) {
  // play()
  player.on(ControlEvent.TYPES.PLAY, (event: ControlEvent) => {
    // this happens when preloading completes after play() is called
  })
  // stop()
  player.on(ControlEvent.TYPES.STOP, (event: ControlEvent) => {
    ...
  })
  // pause()
  player.on(ControlEvent.TYPES.PAUSE, (event: ControlEvent) => {
    ...
  })
  // resume()
  player.on(ControlEvent.TYPES.RESUME, (event: ControlEvent) => {
    ...
  })
}
READ ONLY
  • problem
  • console
No problems have been detected so far.
Ln 1, Col 1, Sel 0
Markdown