MidiPlayerTK.MidiExternalPlayer

Play a Local MIDI file or from a Web site. This class must be used with the prefab MidiExternalPlayer
More...

Inheritance diagram for MidiPlayerTK.MidiExternalPlayer:
MidiPlayerTK.MidiFilePlayer MidiPlayerTK.MidiSynth MidiPlayerTK.MidiSynth

Public Member Functions

override void MPTK_Play (bool alreadyLoaded=false)
 Play the midi file defined with MPTK_MidiName or MPTK_MidiIndex. More...
 
- Public Member Functions inherited from MidiPlayerTK.MidiFilePlayer
void MPTK_Stop ()
 
void MPTK_Stop (bool stopAllSound=true, float wait=-1f)
 
void MPTK_RePlay ()
 
void MPTK_Pause (float timeToPauseMS=-1f)
 
void MPTK_UnPause ()
 
void MPTK_Next ()
 
void MPTK_Previous ()
 
bool MPTK_SearchMidiToPlay (string searchPartOfName)
 
void MPTK_PlayNextOrPrevious (int offset)
 
void MPTK_SwitchMidiWithDelay (int index, string name, float volume, float delayToStopMillisecond, float delayToStartMillisecond)
 
virtual void MPTK_Play (float delayRampUp, float startDelay=0)
 
void MPTK_Play (byte[] data)
 
void MPTK_Play (MPTKWriter mfw2, float delayRampUp=0f, float fromPosition=0, float toPosition=0, long fromTick=0, long toTick=0, bool timePosition=true)
 
virtual void MPTK_Stop (float delayRampDown, float stopDelay=0)
 
void MPTK_ResetTranspose ()
 
MPTKEvent.EnumLength MPTK_NoteLength (MPTKEvent note)
 
MidiLoad MPTK_Load ()
 
List< MPTKEventMPTK_ReadMidiEvents (long fromTicks=0, long toTicks=long.MaxValue)
 
void MPTK_SortEvents ()
 
MidiLoad MPTK_Load (string uri)
 
- Public Member Functions inherited from MidiPlayerTK.MidiSynth
void MPTK_InitSynth (int channelCount=16, bool preserveChannelInfo=false, bool preserveActivVoice=true)
 
void MPTK_InitModulators ()
 
void MPTK_ClearAllSound (bool destroyAudioSource=false, int _idSession=-1)
 
void MPTK_DebugActiveVoice ()
 
void MPTK_DebugFreeVoice ()
 
void MPTK_ResetStat ()
 
delegate void OnAudioFrameStartHandler (double synthTime)
 
void MPTK_StartSequencerMidi ()
 
void MPTK_StartSynth ()
 
void MPTK_StopSynth ()
 
IEnumerator MPTK_WaitAllNotesOff (int _idSession=-1)
 
void MPTK_PlayDirectEvent (MPTKEvent midiEvent, bool playNoteOff=true)
 
void MPTK_StopDirectEvent (MPTKEvent midiEvent)
 
void MPTK_PauseVoices (float transitionDuration=30f)
 
void MPTK_ResumeVoices (float transitionDuration=30f)
 

Play and Stop MIDI

new string MPTK_MidiName [get, set]
 

Additional Inherited Members

- Public Types inherited from MidiPlayerTK.MidiFilePlayer
enum  ModeStopPlay { StopNoWaiting , StopWhenAllVoicesReleased , StopWhenAllVoicesEnded }
 Defined the behavior of the MIDI player when playback is stopped with MPTK_Stop or restarted when the last MIDI events is reached and MPTK_MidiAutoRestart is set to true. See also MPTK_ModeStopVoice More...
 
- Public Types inherited from MidiPlayerTK.MidiSynth
enum  ModeSpatializer { Channel , Track }
 
- Public Attributes inherited from MidiPlayerTK.MidiFilePlayer
bool MPTK_StartPlayAtFirstNote
 
bool MPTK_StopPlayOnLastNote
 
ModeStopPlay MPTK_ModeStopVoice
 
bool MPTK_PauseOnFocusLoss
 
bool MPTK_LogLoadEvents
 
LoadingStatusMidiEnum MPTK_StatusLastMidiLoaded
 
string MPTK_WebRequestError
 
EventNotesMidiClass OnEventNotesMidi
 
EventStartMidiClass OnEventStartPlayMidi
 
EventEndMidiClass OnEventEndPlayMidi
 
MPTKInnerLoop MPTK_InnerLoop
 
- Public Attributes inherited from MidiPlayerTK.MidiSynth
ModeSpatializer MPTK_ModeSpatializer
 
int MPTK_MaxSpatialSynth
 
bool MPTK_SpatialSynthEnabled
 
int MPTK_InstrumentNum
 
MPTKSoundFont MPTK_SoundFont = null
 
bool MPTK_EnablePresetDrum
 
bool MPTK_ReleaseSameNote = true
 
bool MPTK_KillByExclusiveClass = true
 
float MPTK_ReleaseTimeMod = 1f
 
float MPTK_CutOffVolume = 0.0001f
 
float MPTK_LeanSynthStarting = 0.05f
 
bool MPTK_ApplyRealTimeModulator
 
bool MPTK_ApplyModLfo
 
bool MPTK_ApplyVibLfo
 
bool MPTK_DirectSendToPlayer
 
bool MPTK_EnableChangeTempo
 
bool MPTK_EnablePanChange
 
bool MPTK_WeakDevice
 
uint MPTK_ReleaseTimeMin = 500000
 
float MPTK_OrientationToListener
 
bool MPTK_PauseOnMaxDistance = true
 
int MPTK_ThreadMidiWait = 10
 
bool MPTK_AutoBuffer = true
 
int MPTK_AutoCleanVoiceLimit
 
float MPTK_AutoCleanVoiceTime
 
MPTKEffectSoundFont MPTK_EffectSoundFont
 
MPTKEffectUnity MPTK_EffectUnity
 Unlike SoundFont effects, they are applied to the whole player. On the other hand, Unity effect parameters are richer and are obviously based on Unity algorithms!
https://docs.unity3d.com/Manual/class-AudioEffectMixer.html
Only the most important effects are integrated in Maestro: Reverb and Chorus. If needed, other effects could be added. More...
 
bool MPTK_AudioSettingFromUnity
 
bool MPTK_EnableFreeSynthRate = false
 
bool MPTK_LogEvents
 
bool MPTK_LogWave
 
int MPTK_StatVoiceCountPlaying
 
int MPTK_StatVoiceCountActive
 
int MPTK_StatVoiceCountReused
 
int MPTK_StatVoiceCountFree
 
int MPTK_StatVoiceRatioReused
 
int MPTK_StatVoicePlayed
 
EventSynthClass OnEventSynthAwake
 
EventSynthClass OnEventSynthStarted
 
Func< MPTKEvent, bool > OnMidiEvent
 
Action< int, long, int, int > OnBeatEvent
 
- Static Public Attributes inherited from MidiPlayerTK.MidiFilePlayer
static string[] ModeStopPlayLabel = { "Stop No Waiting", "Stop When All Voices Are Released", "Stop When All Voices Are Ended" }
 Defined the behavior of the MIDI player when playback is stopped with MPTK_Stop or restarted when the last MIDI events is reached and MPTK_MidiAutoRestart is set to true. See also MPTK_ModeStopVoice More...
 
- Static Public Attributes inherited from MidiPlayerTK.MidiSynth
static List< MidiFilePlayerSpatialSynths
 
- Protected Attributes inherited from MidiPlayerTK.MidiFilePlayer
string midiNameToPlay
 Defined the behavior of the MIDI player when playback is stopped with MPTK_Stop or restarted when the last MIDI events is reached and MPTK_MidiAutoRestart is set to true. See also MPTK_ModeStopVoice More...
 
- Protected Attributes inherited from MidiPlayerTK.MidiSynth
string instrumentPlayed
 
- Properties inherited from MidiPlayerTK.MidiFilePlayer
virtual string MPTK_MidiName [get, set]
 
int MPTK_MidiIndex [get, set]
 
bool MPTK_PlayOnStart [get, set]
 
bool MPTK_MidiAutoRestart [get, set]
 
bool MPTK_IsPaused [get]
 
bool MPTK_IsPlaying [get]
 
MPTKChannels MPTK_Channels [get]
 
double MPTK_Position [get, set]
 
TimeSpan MPTK_Duration [get]
 
float MPTK_DurationMS [get]
 
long MPTK_TickLast [get]
 
long MPTK_TickFirstNote [get]
 
long MPTK_TickLastNote [get]
 
double MPTK_PositionFirstNote [get]
 
double MPTK_PositionLastNote [get]
 
int MPTK_TrackCount [get]
 
long MPTK_TickCurrent [get, set]
 
bool MPTK_RawSeek [get, set]
 
MPTKEvent MPTK_LastEventPlayed [get]
 
double MPTK_Pulse [get]
 
double MPTK_PulseLenght [get]
 
TimeSpan MPTK_PlayTime [get]
 
double MPTK_RealTime [get]
 
float MPTK_Speed [get, set]
 
bool MPTK_Loop [get, set]
 
double MPTK_Tempo [get, set]
 
string MPTK_SequenceTrackName [get]
 
string MPTK_ProgramName [get]
 
string MPTK_TrackInstrumentName [get]
 
string MPTK_TextEvent [get]
 
string MPTK_Copyright [get]
 
List< MPTKEventMPTK_MidiEvents [get]
 
int MPTK_Quantization [get, set]
 
int MPTK_DeltaTicksPerQuarterNote [get]
 
MidiLoad MPTK_MidiLoaded [get]
 
bool MPTK_KeepNoteOff [get, set]
 
bool MPTK_KeepEndTrack [get, set]
 
bool MPTK_ExtendedText [get, set]
 
- Properties inherited from MidiPlayerTK.MidiSynth
int MPTK_SpatialSynthIndex [get]
 
bool MPTK_IsSpatialSynthMaster [get]
 
string MPTK_InstrumentPlayed [get]
 
string MPTK_TrackName [get]
 
bool MPTK_KeepPlayingNonLooped [get, set]
 
float MPTK_VelocityAttenuation [get, set]
 
float MPTK_Volume [get, set]
 
int MPTK_Transpose [get, set]
 
int MPTK_TransExcludedChannel [get, set]
 
bool MPTK_Dsp64 [get, set]
 
bool MPTK_Orientation [get, set]
 
bool MPTK_DistanceAttenuation [get, set]
 
float MPTK_MinDistance [get, set]
 
float MPTK_MaxDistance [get, set]
 
float MPTK_MinSoundAttenuation [get, set]
 
int MPTK_ThreadMidiPriority [get, set]
 
bool MPTK_CorePlayer [get, set]
 
int MPTK_SynthRate [get, set]
 
int MPTK_IndexSynthRate [get, set]
 
int MPTK_IndexSynthBuffSize [get, set]
 
int MPTK_StatVoiceCountSustained [get]
 
- Events inherited from MidiPlayerTK.MidiSynth
OnAudioFrameStartHandler OnAudioFrameStart
 

Detailed Description

Play a Local MIDI file or from a Web site. This class must be used with the prefab MidiExternalPlayer

There is no need to writing a script. For a simple usage, all the job can be done in the prefab inspector.

But a set of methods are available in this class to drive the music from your script.
This class inherits from MidiFilePlayer and MidiSynth, so all properties, event, methods from MidiFilePlayer and MidiSynth are available in this class.

More information here: https://paxstellar.fr/midi-external-player-v2/

Attention
MidiExternalPlayer inherits of classes MidiFilePlayer and MidiSynth. For clarity, only MidiExternalPlayer attibutes are provided here. Look at the classes MidiFilePlayer and MidiSynth to discover all attributes available.
Version
Maestro Pro

Exemple for loading and playing a MIDI file from a web site.

// Example of script. See TestMidiExternalPlayer.cs for a more detailed usage.
// Need for a reference to the Prefab (to be set in the hierarchy or can be done by script)
MidiExternalPlayer midiExternalPlayer;
if (midiExternalPlayer==null)
Debug.LogError("TestMidiExternalPlayer: there is no MidiExternalPlayer Prefab set in Inspector.");
// Load a MIDI from a web site
midiExternalPlayer.MPTK_MidiName = "http://www.midiworld.com/midis/other/c2/bolero.mid";
midiExternalPlayer.MPTK_Play();
// more later ... load from a local folder (MacOS here)
midiExternalPlayer.MPTK_MidiName = "file:///Users/thierry/Desktop/Nirvana.mid"
midiExternalPlayer.MPTK_Play();
Play a Local MIDI file or from a Web site. This class must be used with the prefab MidiExternalPlayer
Definition: MidiExternalPlayer.cs:45
override void MPTK_Play(bool alreadyLoaded=false)
Play the midi file defined with MPTK_MidiName or MPTK_MidiIndex.
Definition: MidiExternalPlayer.cs:105

Member Function Documentation

◆ MPTK_Play()

override void MidiPlayerTK.MidiExternalPlayer.MPTK_Play ( bool  alreadyLoaded = false)
virtual

Play the midi file defined with MPTK_MidiName or MPTK_MidiIndex.

Parameters
alreadyLoadedtrue: the MIDI has already been loaded (see MPTK_Load() v2.9.0

Reimplemented from MidiPlayerTK.MidiFilePlayer.

Property Documentation

◆ MPTK_MidiName

new string MidiPlayerTK.MidiExternalPlayer.MPTK_MidiName
getset

Set the full path to a MIDI file to a local file with file:// or http:// to a MIDI file on a Web site. The MIDI file will be loaded and played.
See https://en.wikipedia.org/wiki/File_URI_scheme for example of URI file

public class SimplestMidiExternalPlayer : MonoBehaviour
{
public MidiExternalPlayer midiExternalPlayer;
// Coroutine handle so we can stop it cleanly
private Coroutine statusCoroutine;
void Start()
{
if (midiExternalPlayer == null)
{
Debug.LogWarning("SimplestMidiExternalPlayer: midiExternalPlayer is not set in the inspector.");
return;
}
// Set the URL of the MIDI file to play or defined a default URL in the inspector.
midiExternalPlayer.MPTK_MidiName = "https://mptkapi.paxstellar.com/MIDI/Dreams.1.mid";
// Start playing the MIDI file immediately (no auto start defined in the MidiExternalPlayer inspector)
midiExternalPlayer.MPTK_Play();
statusCoroutine = StartCoroutine(DisplayMidiStatusCoroutine());
}
void OnDisable()
{
// Stop the coroutine when this component is disabled to avoid dangling coroutines
if (statusCoroutine != null)
{
StopCoroutine(statusCoroutine);
statusCoroutine = null;
}
}
void OnDestroy()
{
// Ensure coroutine is stopped on destroy as well
if (statusCoroutine != null)
{
StopCoroutine(statusCoroutine);
statusCoroutine = null;
}
}
void Update()
{
// Press the space bar to play the MIDI file.
if (Input.GetKeyDown(KeyCode.Space))
{
if (!midiExternalPlayer.MPTK_IsPlaying)
midiExternalPlayer.MPTK_Play();
else
midiExternalPlayer.MPTK_Stop();
}
}
// Coroutine that runs every second and logs MIDI status
private IEnumerator DisplayMidiStatusCoroutine()
{
while (true)
{
if (midiExternalPlayer != null)
{
string name = midiExternalPlayer.MPTK_MidiName ?? "(none)";
bool isPlaying = midiExternalPlayer.MPTK_IsPlaying;
bool isPaused = midiExternalPlayer.MPTK_IsPaused;
double positionMs = midiExternalPlayer.MPTK_Position;
string duration = midiExternalPlayer.MPTK_Duration != null ? midiExternalPlayer.MPTK_Duration.ToString() : "(unknown)";
Debug.Log($"[MIDI Status {midiExternalPlayer.MPTK_StatusLastMidiLoaded}] name:{name} playing:{isPlaying} paused:{isPaused} pos:{positionMs:F0} ms duration:{duration}");
}
else
{
Debug.Log("[MIDI Status] midiExternalPlayer reference is null");
}
yield return new WaitForSeconds(1f);
}
}
}
bool MPTK_IsPlaying
Definition: MidiFilePlayer.cs:275
void MPTK_Stop()
Definition: MidiFilePlayer.cs:358
bool MPTK_IsPaused
Definition: MidiFilePlayer.cs:270
double MPTK_Position
Definition: MidiFilePlayer.cs:602
TimeSpan MPTK_Duration
Definition: MidiFilePlayer.cs:636