Use case: you want to generate music with an instrument (preset, patch, program, there is a lot of synonyms!) other than Piano at the startup of your application.
By default when the synthesizer is loaded, all channels are set with the preset 0. Generally, preset 0 is associated with Piano moreover when it’s a General Midi SoundFont.
The method MPTK_ChannelPresetChange
must be used for changing preset with a prefab MidiPlayerStream
.
But, the method must be called just after the synthesizer is initialized. So, we used the event OnEventSynthStarted
to call EndLoadingSynth
(or the name that you want!) in the script.
This process can be applied for a lot of synthesizer parameters, for example Kill By Exclusive Class , or Release Same Note (If the same note is hit twice on the same channel, then the older voice process is advanced to the release stage).
Look here for detailed information and a script: how to change SoundFont Bank and Preset when application is started.
using MidiPlayerTK; // Add a reference to the MPTK namespace at the top of your script
using UnityEngine;
public class YourClass : MonoBehaviour
{
MidiFilePlayer midiFilePlayer;
void Start()
{
// Get a reference to the prefab MidiFilePlayer from the hierarchy in the scene
midiFilePlayer = FindObjectOfType<MidiFilePlayer>();
// Add a listener on the MIDI File Player.
// NotesToPlay will be called for each new group of notes read by the MIDI sequencer from the MIDI file.
midiFilePlayer.OnEventStartPlayMidi.AddListener(StartPlay);
}
public void StartPlay(string midiname)
{
Debug.LogFormat($"Start playing midi {midiname}");
// Disable MIDI channel 9 (generally drums)
midiFilePlayer.MPTK_ChannelEnableSet(9, false);
// Set start tick
midiFilePlayer.MPTK_TickCurrent = 500;
}
}
Don’t forget to initialize your prefab in your script, see link here.