MusIT

Script Only!


One strength of Maestro is playing Music without any scripting by adding prefab in your hierarchy with the Unity editor. But sometimes you want to use Maestro Prefab only by script. This is also possible!

Unity Tips: assets added by script will be visible in the hierarchy only when application runs.

So in this case, all prefab properties must be set by script. Below in the example, the minimum properties to be set are defined. Feel free to use the full API available for each Maestro Prefab and Class!!!

Look at the Maestro API Reference.

Want a demonstration? Add these scripts to a gameObject in your hierarchy … and run.

The simplest MIDI loader:

// ------------------------------------------------------------------------ // Load a MIDI file and process each MIDI events // this script is provided in this folder: // Assets\MidiPlayer\Demo\FreeDemos\Script\TheSimplestMidiLoader.cs // ------------------------------------------------------------------------ using System.Collections; using System.Collections.Generic; using UnityEngine; using MidiPlayerTK; namespace DemoMPTK { /// <summary> /// This demo is able to load all events from a MIDI file only by script.\n /// There is nothing to create in the Unity editor, just add this script to a GameObject in your scene and run! /// </summary> public class TheSimplestMidiLoader : MonoBehaviour { MidiFileLoader midiFileLoader; private void Awake() { Debug.Log("Awake: dynamically add MidiFileLoader component"); // MidiPlayerGlobal is a singleton: only one instance can be created. if (MidiPlayerGlobal.Instance == null) gameObject.AddComponent<MidiPlayerGlobal>(); // When running, this component will be added to this gameObject midiFileLoader = gameObject.AddComponent<MidiFileLoader>(); } public void Start() { Debug.Log("Start: select a MIDI file and load MIDI events. Any sequencer and synth are instanciated"); // Select a MIDI from the MIDI DB (with exact name) midiFileLoader.MPTK_MidiName = "Bach - Fugue"; // Load the MIDI file if (midiFileLoader.MPTK_Load()) { // Read all MIDI events List<MPTKEvent> sequence = midiFileLoader.MPTK_ReadMidiEvents(); Debug.Log($"Loading '{midiFileLoader.MPTK_MidiName}', MIDI events count:{sequence.Count}"); } else Debug.Log($"Loading '{midiFileLoader.MPTK_MidiName}' - Error"); } } }
Code language: C# (cs)

The simplest MIDI player:

// ------------------------------------------------------------------------ // Load a MIDI file and Play // this script is provided in this folder: // Assets\MidiPlayer\Demo\FreeDemos\Script\TheSimplestMidiPlayer.cs // ------------------------------------------------------------------------ using System.Collections; using System.Collections.Generic; using UnityEngine; using MidiPlayerTK; namespace DemoMPTK { /// <summary> /// This demo is able to play a MIDI file only by script.\n /// There is nothing to create in the Unity editor, just add this script to a GameObject in your scene and run! /// </summary> public class TheSimplestMidiPlayer : MonoBehaviour { // MidiPlayerGlobal is a singleton: only one instance can be created. Making static to have only one reference. MidiFilePlayer midiFilePlayer; private void Awake() { Debug.Log("Awake: dynamically add MidiFilePlayer component"); // MidiPlayerGlobal is a singleton: only one instance can be created. if (MidiPlayerGlobal.Instance == null) gameObject.AddComponent<MidiPlayerGlobal>(); // When running, this component will be added to this gameObject. Set essential parameters. midiFilePlayer = gameObject.AddComponent<MidiFilePlayer>(); midiFilePlayer.MPTK_CorePlayer = true; midiFilePlayer.MPTK_DirectSendToPlayer = true; } public void Start() { Debug.Log("Start: select a MIDI file from the MPTK DB and play"); // Select a MIDI from the MIDI DB (with exact name) midiFilePlayer.MPTK_MidiName = "Bach - Fugue"; // Play the MIDI file midiFilePlayer.MPTK_Play(); } } }
Code language: HTML, XML (xml)