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
bool MPTK_ApplyModLfo
 
bool MPTK_ApplyRealTimeModulator
 
bool MPTK_ApplyVibLfo
 
bool MPTK_AudioSettingFromUnity
 
bool MPTK_AutoBuffer = true
 
int MPTK_AutoCleanVoiceLimit
 
bool MPTK_CorePlayer
 
float MPTK_CutOffVolume = 0.0001f
 
bool MPTK_DirectSendToPlayer
 
MPTKEffectSoundFont MPTK_EffectSoundFont
 
MPTKEffectUnity MPTK_EffectUnity
 Unlike SoundFont effects, they applied to the whole player. On the other hand, the Unity effects parameters are rich and, obviously based on Uniy algo!
https://docs.unity3d.com/Manual/class-AudioEffectMixer.html
Only most important effect are integrated in Maestro: Reverb and Chorus. On need, others effects could be added. More...
 
bool MPTK_EnableChangeTempo
 
bool MPTK_EnableFreeSynthRate = false
 
bool MPTK_EnablePanChange
 
bool MPTK_EnablePresetDrum
 
int MPTK_InstrumentNum
 
bool MPTK_KillByExclusiveClass = true
 
float MPTK_LeanSynthStarting = 0.05f
 
bool MPTK_LogEvents
 
bool MPTK_LogWave
 
bool MPTK_ReleaseSameNote = true
 
uint MPTK_ReleaseTimeMin = 500000
 
float MPTK_ReleaseTimeMod = 1f
 
bool MPTK_SpatialSynthEnabled
 
int MPTK_StatVoiceCountActive
 
int MPTK_StatVoiceCountFree
 
int MPTK_StatVoiceCountPlaying
 
int MPTK_StatVoiceCountReused
 
int MPTK_StatVoicePlayed
 
int MPTK_StatVoiceRatioReused
 
int MPTK_ThreadMidiWait = 10
 
bool MPTK_WeakDevice
 
Action< int, long, int, int > OnBeatEvent
 Action is executed at each beat and received these parameters: More...
 
EventSynthClass OnEventSynthAwake
 
EventSynthClass OnEventSynthStarted
 
Func< MPTKEvent, bool > OnMidiEvent
 
bool playOnlyFirstWave
 

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_IndexSynthBuffSize [get, set]
 
int MPTK_IndexSynthRate [get, set]
 
string MPTK_InstrumentPlayed [get]
 
bool MPTK_IsSpatialSynthMaster [get]
 
bool MPTK_KeepPlayingNonLooped [get, set]
 
float MPTK_MaxDistance [get, set]
 
bool MPTK_PauseOnDistance [get, set]
 
bool MPTK_Spatialize [get, set]
 
int MPTK_SpatialSynthIndex [get]
 
int MPTK_SynthRate [get, set]
 
int MPTK_ThreadMidiPriority [get, set]
 
string MPTK_TrackName [get]
 
int MPTK_TransExcludedChannel [get, set]
 
int MPTK_Transpose [get, set]
 
float MPTK_Volume [get, set]
 

Additional Inherited Members

- Public Types inherited from MidiPlayerTK.MidiSynth
enum  ModeSpatializer { Channel , Track }
 
- Public Member Functions inherited from MidiPlayerTK.MidiSynth
void MPTK_ClearAllSound (bool destroyAudioSource=false, int _idSession=-1)
 
void MPTK_InitSynth (int channelCount=16, bool preserveChannelInfo=false)
 
void MPTK_ResetStat ()
 
void MPTK_StartSequencerMidi ()
 
void MPTK_StartSynth ()
 
void MPTK_StopSynth ()
 
IEnumerator MPTK_WaitAllNotesOff (int _idSession=-1)
 
delegate void OnAudioFrameStartHandler (double synthTime)
 
- Static Public Member Functions inherited from MidiPlayerTK.MidiSynth
static StringBuilder MPTK_BuildInfoSynth (MidiSynth synth)
 Build a string with performance and information about the MIDI reader and the MIDI synthesizer. DEBUG_PERF_AUDIO DEBUG_HISTO_DSPSIZE DEBUG_PERF_MIDI More...
 
- Static Public Attributes inherited from MidiPlayerTK.MidiSynth
const float FLUID_CHORUS_DEFAULT_DEPTH = 4.25f
 
const float FLUID_CHORUS_DEFAULT_LEVEL = 0.6f
 
const int FLUID_CHORUS_DEFAULT_N = 3
 
const float FLUID_CHORUS_DEFAULT_SPEED = 0.2f
 
const float FLUID_CHORUS_DEFAULT_WIDTH = 10f
 
const float FLUID_REVERB_DEFAULT_DAMP = 0.3f
 
const float FLUID_REVERB_DEFAULT_LEVEL = 0.7f
 
const float FLUID_REVERB_DEFAULT_ROOMSIZE = 0.5f
 
const float FLUID_REVERB_DEFAULT_WIDTH = 0.8f
 
static List< MidiFilePlayerSpatialSynths
 
- 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 = FindObjectOfType<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:431
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:128

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 = FindObjectOfType<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);
}