MusIT

Prefab MidiStreamPlayer

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

MidiStreamPlayer is a prefab that is perfect for creating MIDI music without a 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 will need at least beginner-level skills to:

  • Understand basic MIDI concept.
  • The MPTK class MPTKEvent is central to creating MIDI music.
  • Writing C# script.

See below how to use this prefab in a few steps:

First, add the prefab to your scene:

Add the prefab directly from the menu MPTK

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

Secondly, create a midiStreamPlayer variable in your script:

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;

Thirdly, use the Unity Inspector to set the reference to the midiStreamPlayer variable created in your script. In this demo, we drag the MidiStreamPlayer game component to the inspector “Midi Stream Player” variable.

That can also be done in your script with FindObjectOfType<MidiStreamPlayer>(). Look at the source code at the bottom.

Quick Video How to Set a MIDI Stream Player prefab

Now you can call a method in your script to play notes!

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

Example:

// 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);

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

Inspector parameters

MIDI Stream Player Inspector View
MIDI Stream Player Inspector View
  • 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;
}
}

Have Fun!

Get MPTK from the Unity store

If you like Midi Player Tool Kit, please leave a review on the Asset Store. It’s very appreciated!!!

Maestro MPTK on ChatGPT!

From various MPTK documentation sources, @DarkSky42 has created a custom LLM based on ChatGPT. You can now ask all the questions you want and get a good level of answer: request code example, verify your source code, explain the MPTK demo …

Contact

If you have questions, please don’t hesitate to contact us via the dedicated Unity forum or our Discord  channel.

Reach the Discord archive by topic.

We are always happy to discuss your projects!

Add MIDI Music With 3 Clicks for Free

Sound Spatialisation, MPTK is ready for Virtual Reality [Pro]

Sound Spatialisation, MPTK is ready for Virtual Reality [free]

Midi Synth : Real Time Voice Effect Change

Euclidean Rhythm demo

The Deezer playlist that helped me create Maestro