Prefab MidiStreamPlayer

Please, see our overview page for general information about Maestro Midi Player Tool Kit.

MidiStreamPlayer is a prefab which is perfect for creating MIDI music without MIDI file: you generate MIDI events from your code and send them to MidiStreamPlayer.

With MidiStreamPlayer you don’t have to add Midi to MPTK but a SoudFont is always needed … to play sound!

You need certain skills at least at beginners level:

  • Understand MIDI and MPTKEvent class,
  • write C# script,
  • musical composition …

See below how using this prefab in a few steps:

Firstly, add the prefab to your scene:

Add the prefab directly from the menu MPTK

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

Secondly, create in your script a MidiStreamPlayer variable:

using MidiPlayerTK; ... // MPTK component able to play a stream of midi events // Need to be public to be visible in the inspector public MidiStreamPlayer midiStreamPlayer;
Code language: PHP (php)

Thirdly, with the inspector, set the reference of the midiStreamPlayer variable created in your script from the added prefab. This reference can also be done in your script.

Now, in your script you can call method for playing notes!

The main functions are MPTK_PlayEvent(MPTKEvent evnt) and MPTK_StopEvent(MPTKEvent evnt).


// 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);
Code language: JavaScript (javascript)

Don’t miss the full description of the MPTKEvent class.

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

As usual, you can modify these parameters by script with the MPTK API.

Example of script with MidiStreamPlayer

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

See also this guide how to write script with MPTK.

using MidiPlayerTK; ... // MPTK component able to play a stream of midi events // Need to be public to be visible in the inspector public MidiStreamPlayer midiStreamPlayer; ... // The reference can be set with the inspector or in your script // Search a MidiStreamPlayer in the scene and initialize your variable: midiStreamPlayer = FindObjectOfType<MidiStreamPlayer>(); if (midiStreamPlayer == null) Debug.LogWarning("Can't find a MidiStreamPlayer Prefab in the current Scene Hierarchy. Add it with the MPTK menu."); if (midiStreamPlayer != null) { if (!midiStreamPlayer.OnEventSynthStarted.HasEvent()) // The method EndLoadingSynth will be called when the synth is ready midiStreamPlayer.OnEventSynthStarted.AddListener(EndLoadingSynth); } else Debug.LogWarning("No Stream Midi Player associed to this game object"); public void EndLoadingSynth(string name) { // Change the default instrument when synth is started. // It's done also with a MIDI event and the command = MPTKCommand.PatchChange 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 playing 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; } }
Code language: PHP (php)

Have Fun!