MidiPlayerTK.MidiInReader

Read MIDI events from a MIDI keyboard connected to your Windows or Mac desktop. This class must be used with the prefab MidiInReader.
There is no need to writing a script. For a simple usage, all the job can be done in the prefab inspector. More information here https://paxstellar.fr/prefab-midiinreader/
More...

Inheritance diagram for MidiPlayerTK.MidiInReader:
MidiPlayerTK.MidiSynth

Public Attributes

bool MPTK_ReadMidiInput
 
EventMidiClass OnEventInputMidi
 
- 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
 

Properties

MPTKChannels MPTK_Channels [get]
 Description and list of MIDI Channels associated to the MIDI synth.
Each MIDI synth has 16 channels that carry all the relevant MIDI information. More...
 
- 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]
 

Additional Inherited Members

- Public Types inherited from MidiPlayerTK.MidiSynth
enum  ModeSpatializer { Channel , Track }
 
- 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)
 
- Static Public Attributes inherited from MidiPlayerTK.MidiSynth
static List< MidiFilePlayerSpatialSynths
 
- Protected Attributes inherited from MidiPlayerTK.MidiSynth
string instrumentPlayed
 
- Events inherited from MidiPlayerTK.MidiSynth
OnAudioFrameStartHandler OnAudioFrameStart
 

Detailed Description

Read MIDI events from a MIDI keyboard connected to your Windows or Mac desktop. This class must be used with the prefab MidiInReader.
There is no need to writing a script. For a simple usage, all the job can be done in the prefab inspector. More information here https://paxstellar.fr/prefab-midiinreader/

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

Example of script. See TestMidiInputScripting.cs for a more detailed usage.
Display each MIDI events from a MIDI device connected Need for a reference to the Prefab (can also be set from the hierarchy)

MidiInReader midiIn = FindFirstObjectByType<MidiInReader>();
if (midiIn == null)
Debug.LogWarning("Can't find a MidiInReader Prefab in the current Scene Hierarchy. Add it with the MPTK menu.");
// There is two methods to trigger event: in inpector from the Unity editor or by script
midiIn.OnEventInputMidi.AddListener((MPTKEvent evt) =>
{
// your processing here
Debug.Log(evt.ToString());
});
Description of a MIDI Event. It's the heart of MPTK! Essential to handling MIDI by script from all ot...
Definition: MPTKEvent.cs:45
override string ToString()
Extract value 2 from a double int build with BuildIntFromBytes
Definition: MPTKEvent.cs:456
Read MIDI events from a MIDI keyboard connected to your Windows or Mac desktop. This class must be us...
Definition: MidiInReader.cs:35
EventMidiClass OnEventInputMidi
Definition: MidiInReader.cs:58

Member Data Documentation

◆ MPTK_ReadMidiInput

bool MidiPlayerTK.MidiInReader.MPTK_ReadMidiInput

Read Midi input

◆ OnEventInputMidi

EventMidiClass MidiPlayerTK.MidiInReader.OnEventInputMidi

Define unity event to trigger when note available from the Midi file.

MidiInReader midiFilePlayer = FindFirstObjectByType<MidiInReader>();
...
if (!midiFilePlayer.OnEventInputMidi.HasEvent())
{
// No listener defined, set now by script. NotesToPlay will be called for each new notes read from Midi file
midiFilePlayer.OnEventInputMidi.AddListener(NotesToPlay);
}
...
public void NotesToPlay(MPTKEvent notes)
{
Debug.Log(notes.Value);
foreach (MPTKEvent midievent in notes)
{
...
}
}
int Value
Definition: MPTKEvent.cs:153

Property Documentation

◆ MPTK_Channels

MPTKChannels MidiPlayerTK.MidiInReader.MPTK_Channels
get

Description and list of MIDI Channels associated to the MIDI synth.
Each MIDI synth has 16 channels that carry all the relevant MIDI information.

  • Current instrument / bank
  • Volume
  • Mute / Unmute (see Enable)
  • Pitch bend ...

They serve to distinguish between instruments and provide independent control over each one.
By transmitting MIDI messages on their respective channels, you can alter the instrument, volume, pitch, and other parameters.
Within the Maestro Midi Player Toolkit, MIDI channels are designated numerically from 0 to 15. Notably, channel 9 is set aside specifically for drum sounds.

HelperDemo.GUI_Horizontal(HelperDemo.Zone.BEGIN);
GUILayout.Label("Channel Preset Name Preset / Bank",
myStyle.TitleLabel3, GUILayout.Width(60 + 140 + 120 + 100 + 110));
GUILayout.Label(" Count Enabled Volume",
myStyle.TitleLabel3, GUILayout.Width(900));
HelperDemo.GUI_Horizontal(HelperDemo.Zone.END);
// Also available for MidiStreamPlayer, MidiInReader, MidiExternalPlayer.
for (int channel = 0; channel < midiFilePlayer.MPTK_Channels.Length; channel++)
{
HelperDemo.GUI_Horizontal(HelperDemo.Zone.BEGIN);
// Display channel number and log info
if (GUILayout.Button($" {channel:00}", myStyle.TitleLabel3, GUILayout.Width(60)))
Debug.Log(midiFilePlayer.MPTK_Channels[channel].ToString());
// Display preset name
GUILayout.Label(midiFilePlayer.MPTK_Channels[channel].PresetName ?? "not set", myStyle.TitleLabel3, GUILayout.Width(140));
// Display preset and bank index
int presetNum = midiFilePlayer.MPTK_Channels[channel].PresetNum;
int bankNum = midiFilePlayer.MPTK_Channels[channel].BankNum;
int presetForced = midiFilePlayer.MPTK_Channels[channel].ForcedPreset;
// Check if preset is forced and build a string info
string sPreset = presetForced == -1 ? $"{presetNum} / {bankNum}" : $"F{presetForced} / {bankNum}";
// Slider to change the preset on this channel from -1 (disable forced) to 127.
int forcePreset = (int)HelperDemo.GUI_Slider(sPreset, presetNum, -1f, 127f, alignCaptionRight: true, widthCaption: 120, widthSlider: 100, widthLabelValue: -1);
if (forcePreset != presetNum)
{
// Force a preset and a bank whatever the MIDI events from the MIDI file.
// set forcePreset to -1 to restore to the last preset and bank value known from the MIDI file.
// let forcebank to -1 to not force the bank.
// Before v2.10.1 midiFilePlayer.MPTK_ChannelForcedPresetSet(channel, forcePreset, forceBank);
midiFilePlayer.MPTK_Channels[channel].ForcedBank = forceBank;
midiFilePlayer.MPTK_Channels[channel].ForcedPreset = forcePreset;
}
// Display count note by channel
GUILayout.Label($"{midiFilePlayer.MPTK_Channels[channel].NoteCount,-5}", myStyle.LabelRight, GUILayout.Width(100));
// Toggle to enable or disable a channel
GUILayout.Label(" ", myStyle.TitleLabel3, GUILayout.Width(20));
bool state = GUILayout.Toggle(midiFilePlayer.MPTK_Channels[channel].Enable, "", GUILayout.MaxWidth(20));
if (state != midiFilePlayer.MPTK_Channels[channel].Enable)
{
midiFilePlayer.MPTK_Channels[channel].Enable = state;
Debug.LogFormat("Channel {0} state:{1}, preset:{2}", channel, state, midiFilePlayer.MPTK_Channels[channel].PresetName ?? "not set"); /*2.84*/
}
// Slider to change volume
float currentVolume = midiFilePlayer.MPTK_Channels[channel].Volume;
float volume = HelperDemo.GUI_Slider(null, currentVolume, 0f, 1f, alignCaptionRight: true, enableButton: false, widthCaption: -1, widthSlider: 100, widthLabelValue: 40);
if (volume != currentVolume)
midiFilePlayer.MPTK_Channels[channel].Volume = volume;
HelperDemo.GUI_Horizontal(HelperDemo.Zone.END);
}