Prefab MidiStreamPlayer

Please, see our overview page for general information about MPTK. In fact, with MidiStreamPlayer you don’t have to add Midi to MPTK but a SoudFont is always needed … to play sound!

MidiStreamPlayer is a Prefab that you can use to play a Midi  music without Midi file: you generate Midi events from your code and send them to MidiStreamPlayer.

The main functions are MPTK_PlayEvent(MPTKEvent evnt)and MPTK_StopEvent(MPTKEvent evnt). See a short script below:

// Play a C4 for 1 seconds NotePlaying = new MPTKEvent() { Command = MPTKCommand.NoteOn, // midi command Value = 48, // from 0 to 127, 48 for C4, 60 for C5, ... Channel = 0, // from 0 to 15, 9 reserved for drum Duration = 1000, // note duration in millisecond, -1 to play undefinitely, MPTK_StopChord to stop Velocity = 100, // from 0 to 127, sound can vary depending on the velocity Delay = 0, // delay in millisecond before playing the note }; midiStreamPlayer.MPTK_PlayEvent(NotePlaying);

See an example with source code at the end of this page.

Inspector parameters

  • Volume: set the default volume of the playing.
  • Transpose: transpose the music by half tone.
  • Show Spatialization Parameters: See here
  • Show Synth Parameters: See here
  • Show Effect Parameters: See here (available with Pro version, disabled by default)
  • Default Editor: See here

And always, you can modify all these parameters by script with the MPTK API.

Integration of MidiStreamPlayer in your script

See TestMidiStream.cs from MPTK asset (available also in free version) for the full functional source code.

See also here for a guide how to write script with MPTK.

using MidiPlayerTK; ... // MPTK component able to play a stream of midi events public MidiStreamPlayer midiStreamPlayer; ... if (midiStreamPlayer != null) { if (!midiStreamPlayer.OnEventSynthStarted.HasEvent()) midiStreamPlayer.OnEventSynthStarted.AddListener(EndLoadingSynth); } else Debug.LogWarning("No Stream Midi Player associed to this game object"); public void EndLoadingSynth(string name) { midiStreamPlayer.MPTK_PlayEvent(new MPTKEvent() { Command = MPTKCommand.PatchChange, Value = CurrentPatchInstrument, Channel = StreamChannel, }); } private void BankPatchChanged(object tag, int index) { switch ((string)tag) { case "BANK_INST": midiStreamPlayer.MPTK_PlayEvent(new MPTKEvent() { Command = MPTKCommand.ControlChange, Controller = MPTKController.BankSelect, Value = index, Channel = StreamChannel, }); break; case "PATCH_INST": midiStreamPlayer.MPTK_PlayEvent(new MPTKEvent() { Command = MPTKCommand.PatchChange, Value = index, Channel = StreamChannel, }); break; } void Update() { ... // Start playint a new note NotePlaying = new MPTKEvent() { Command = MPTKCommand.NoteOn, Value = CurrentNote, Channel = StreamChannel, Duration = 9999999, // 9999 seconds but stop by the new note. See before. Velocity = Velocity // Sound can vary depending on the velocity }; midiStreamPlayer.MPTK_PlayEvent(NotePlaying); ... if (NotePlaying != null) { // Stop the note (method to simulate a real human on a keyboard : duration is not known when note is triggered) midiStreamPlayer.MPTK_StopEvent(NotePlaying); NotePlaying = null; } }

Have Fun!