MidiPlayerTK.MidiStreamPlayer

The prefab MidiStreamPlayer is useful for playing real time music in relation with user actions or an algo.
Any Midi file is necessary, the notes are generated by your scripts from your own algorithm. Thank to the API of this class.
The main function MPTK_PlayEvent() and the class MPTKEvent are able to create all kind of midi events as note-on.
All the values must be set in MPTKEvent, command, note value, duration ... for more details look at the class MPTKEvent.
A note-on must also be stopped, : if duration = -1 the note is infinite, it's the goal of MPTK_StopEvent() to stop the note with a note-off.
On top of that, the Pro version adds playing chords with MPTK_PlayChordFromRange() and MPTK_PlayChordFromLib().
For playing scales, have a look to the class MPTKRangeLib
For more information see here https://paxstellar.fr/midi-file-player-detailed-view-2-2/
and look at the demo TestMidiStream with the source code TestMidiStream.cs.

On top of that, this class inherits from MidiSynth
All properties, event, methods from MidiSynth are available in the class MidiStreamPlayer.

More...

+ Inheritance diagram for MidiPlayerTK.MidiStreamPlayer:

Public Member Functions

MPTKChordBuilder MPTK_PlayChordFromLib (MPTKChordBuilder chord)
 [MPTK PRO] Play a chord from the chord library. See file ChordLib.csv in folder Resources/GeneratorTemplate.
The Tonic is used to buid the chord More...
 
MPTKChordBuilder MPTK_PlayChordFromRange (MPTKChordBuilder chord)
 [MPTK PRO] Play a chord from the current selected range (MPTK_RangeSelected), Tonic and Degree are defined in parameter MPTKChord chord.
Major range is selected if no range defined.
See file GammeDefinition.csv in folder Resources/GeneratorTemplate More...
 
void MPTK_PlayEvent (List< MPTKEvent > events)
 Play a list of midi events with a thread so the call return immediately. More...
 
void MPTK_PlayEvent (MPTKEvent evnt)
 Play one midi event with a thread so the call return immediately. More...
 
void MPTK_PlayPitchWheelChange (int channel, float pitchWheel)
 [MPTK PRO] V2.88.2 Play a Midi pitch change event for all notes on the channel More...
 
void MPTK_PlayPitchWheelSensitivity (int channel, int sensitivity)
 [MPTK PRO] V2.88.2 Play a midi pitch sensitivity change for all notes on the channel. More...
 
void MPTK_StartMidiStream ()
 Force starting of the MidiStream. May be usefull from another gamecomponent which want send midi command at the start of the application. More...
 
void MPTK_StopChord (MPTKChordBuilder chord)
 Stop playing the chord. All samples associated to the chord are stopped by sending a noteoff. More...
 
void MPTK_StopEvent (MPTKEvent pnote)
 Stop playing the note. All waves associated to the note are stop by sending a noteoff. More...
 
- Public Member Functions inherited from MidiPlayerTK.MidiSynth
string MPTK_Channel (int channel)
 Build an info string about the channel. Moreover a good pretext to display an example of Channel API. More...
 
int MPTK_ChannelBankGetIndex (int channel)
 Get channel bank. More...
 
int MPTK_ChannelCount ()
 Get channel count. The midi norm is 16, but MPTK can manage up to 32 channels. More...
 
bool MPTK_ChannelEnableGet (int channel)
 Is channel is enabled or disabled. More...
 
void MPTK_ChannelEnableSet (int channel, bool enable)
 Enable or disable a channel. More...
 
int MPTK_ChannelForcedPresetGet (int channel)
 Set forced preset on the channel. Midi will allways playing with this preset even if a Midi Preset Change message is received.
Set to -1 to disable this behavior. More...
 
bool MPTK_ChannelForcedPresetSet (int channel, int preset)
 Set forced preset on the channel. Midi will allways playing with this preset even if a Midi Preset Change message is received.
Set to -1 to disable this behavior. More...
 
int MPTK_ChannelNoteCount (int channel)
 Get count of notes played since the start of the Midi. More...
 
bool MPTK_ChannelPresetChange (int channel, int preset, int newbank=-1)
 Change the preset and bank for the channel.
When playing a Midi file, the preset is set by channel with the Midi message Patch Change.
The bank is changed with a ControlChange Midi message.
The new value of the bank is local for the channel, the preset list is not updated.
To change globally the bank, use instead the global methods: MidiPlayerGlobal.MPTK_SelectBankInstrument or MidiPlayerGlobal.MPTK_SelectBankDrum More...
 
int MPTK_ChannelPresetGetIndex (int channel)
 Get channel preset indx. More...
 
string MPTK_ChannelPresetGetName (int channel)
 Get channel current preset name. More...
 
float MPTK_ChannelVolumeGet (int channel)
 Get the volume of the channel More...
 
void MPTK_ChannelVolumeSet (int channel, float volume)
 Set the volume for a channel as a percentage. More...
 
void MPTK_ChorusSetDefault ()
 [MPTK PRO] - Set Chorus Unity default value as defined with Unity. More...
 
void MPTK_ClearAllSound (bool destroyAudioSource=false, int _idSession=-1)
 Clear all sound by sending note off.
That could take some seconds because release time for sample need to be played. More...
 
void MPTK_InitSynth (int channelCount=16)
 Initialize the synthetizer: channel, voices, modulator.
It's not usefull to call this method if you are using prefabs (MidiFilePlayer, MidiStreamPlayer, ...).
Each gameObjects created from these prefabs have their own, autonomous and isolated synth. More...
 
void MPTK_ResetStat ()
 Reset voices statistics More...
 
void MPTK_ReverbSetDefault ()
 [MPTK PRO] - Set Reverb Unity default value as defined with Unity. More...
 
void MPTK_SFChorusSetDefault ()
 [MPTK PRO] - Set Chrous SoundFont default value as defined in fluidsynth.
FLUID_CHORUS_DEFAULT_N 3
FLUID_CHORUS_DEFAULT_LEVEL 2.0 but set to 0.9 (thank John)
FLUID_CHORUS_DEFAULT_SPEED 0.3
FLUID_CHORUS_DEFAULT_DEPTH 8.0
FLUID_CHORUS_DEFAULT_TYPE FLUID_CHORUS_MOD_SINE
WIDTH 10 More...
 
void MPTK_SFFilterSetDefault ()
 [MPTK PRO] - Set Filter SoundFont default value as defined in fluidsynth.
More...
 
void MPTK_SFReverbSetDefault ()
 [MPTK PRO] - Set Reverb SoundFont default value as defined in fluidsynth.
FLUID_REVERB_DEFAULT_ROOMSIZE 0.2f
FLUID_REVERB_DEFAULT_DAMP 0.0f
FLUID_REVERB_DEFAULT_WIDTH 0.5f
FLUID_REVERB_DEFAULT_LEVEL 0.9f
More...
 
void MPTK_StartSequencerMidi ()
 Start the Midi sequencer: each midi events are read and play in a dedicated thread.
This thread is automatically started by prefabs MidiFilePlayer, MidiListPlayer, MidiExternalPlayer. More...
 
void MPTK_StopSynth ()
 Stop processing samples by the synth and the Midi sequencer. More...
 
IEnumerator MPTK_WaitAllNotesOff (int _idSession=-1)
 Wait until all notes are off.
That could take some seconds due to the samples release time.
Therefore, the method exit after a timeout of 3 seconds.
*** Use this method only as a coroutine *** More...
 
delegate void OnAudioFrameStartHandler (double synthTime)
 Delegate for the event OnAudioFrameStartHandler. see OnAudioFrameStart More...
 

Properties

string? MPTK_RangeName [get]
 [MPTK PRO] Name of range selected (musical scale) More...
 
int MPTK_RangeSelected [get, set]
 [MPTK PRO] Current selected range (musical scale) More...
 
- Properties inherited from MidiPlayerTK.MidiSynth
bool MPTK_ApplyUnityChorus [get, set]
 [MPTK PRO] - Apply Chorus Unity effect to the AudioSource. The effect is applied to all voices. More...
 
bool MPTK_ApplyUnityReverb [get, set]
 [MPTK PRO] - Apply Reverb Unity effect to the AudioSource. The effect is applied to all voices. More...
 
float MPTK_ChorusDelay [get, set]
 [MPTK PRO] - Chorus delay in ms.
Range from 0.1 to 100. Default = 40 ms. More...
 
float MPTK_ChorusDepth [get, set]
 [MPTK PRO] - Chorus modulation depth.
Range from 0 to 1. Default = 0.03. More...
 
float MPTK_ChorusDryMix [get, set]
 [MPTK PRO] - Volume of original signal to pass to output.
Range from 0 to 1. Default = 0.5. More...
 
float MPTK_ChorusRate [get, set]
 [MPTK PRO] - Chorus modulation rate in hz.
Range from 0 to 20. Default = 0.8 hz. More...
 
float MPTK_ChorusWetMix1 [get, set]
 [MPTK PRO] - Volume of 1st chorus tap.
Range from 0 to 1. Default = 0.5. More...
 
float MPTK_ChorusWetMix2 [get, set]
 [MPTK PRO] - Volume of 2nd chorus tap. This tap is 90 degrees out of phase of the first tap.
Range from 0 to 1. Default = 0.5. More...
 
float MPTK_ChorusWetMix3 [get, set]
 [MPTK PRO] - Volume of 3rd chorus tap. This tap is 90 degrees out of phase of the second tap.
Range from 0 to 1. Default = 0.5. More...
 
int MPTK_IndexSynthBuffSize [get, set]
 Set or Get sample rate output of the synth. -1:default, 0:24000, 1:36000, 2:48000, 3:60000, 4:72000, 5:84000, 6:96000.
It's better to stop playing before changing on fly to avoid bad noise. More...
 
int MPTK_IndexSynthRate [get, set]
 Set or Get sample rate output of the synth. -1:default, 0:24000, 1:36000, 2:48000, 3:60000, 4:72000, 5:84000, 6:96000.
It's better to stop playing before changing on fly to avoid bad noise. More...
 
string? MPTK_InstrumentPlayed [get]
 If spatialization is track mode, contains the last instrument played on this track More...
 
bool MPTK_IsSpatialSynthMaster [get]
 Trus if this MidiSynth is the master synth responsible to read midi events and to dispatch to other MidiSynths More...
 
float MPTK_MaxDistance [get, set]
 If MPTK_Spatialize is enabled, the volume of the audio source depends on the distance between the audio source and the listener.
Beyong this distance, the volume is set to 0 and the midi player is paused. No effect if MPTK_Spatialize is disabled. More...
 
bool MPTK_PauseOnDistance [get, set]
 [obsolete] replaced by MPTK_Spatialize"); V2.83 More...
 
float MPTK_ReverbDecayHFRatio [get, set]
 [MPTK PRO] - Decay HF Ratio : High-frequency to low-frequency decay time ratio.
Ranges from 0.1 to 2.0. More...
 
float MPTK_ReverbDecayTime [get, set]
 [MPTK PRO] - Reverberation decay time at low-frequencies in seconds.
Ranges from 0.1 to 20. Default is 1. More...
 
float MPTK_ReverbDelay [get, set]
 [MPTK PRO] - Late reverberation delay time relative to first reflection in seconds.
Ranges from 0 to 0.1. Default is 0.04 More...
 
float MPTK_ReverbDensity [get, set]
 [MPTK PRO] - Reverberation density (modal density) in percent.
Ranges from 0 to 1. More...
 
float MPTK_ReverbDiffusion [get, set]
 [MPTK PRO] - Reverberation diffusion (echo density) in percent.
Ranges from 0 to 1. Default is 1. More...
 
float MPTK_ReverbDryLevel [get, set]
 [MPTK PRO] - Mix level of dry signal in output.
Ranges from 0 to 1. More...
 
float MPTK_ReverbHFReference [get, set]
 [MPTK PRO] - Reference high frequency in Hz.
Ranges from 1000 to 20000. Default is 5000 More...
 
float MPTK_ReverbLevel [get, set]
 [MPTK PRO] - Late reverberation level relative to room effect.
Ranges from 0 to 1. More...
 
float MPTK_ReverbLFReference [get, set]
 [MPTK PRO] - Reference low-frequency in Hz.
Ranges from 20 to 1000. Default is 250 More...
 
float MPTK_ReverbReflectionDelay [get, set]
 [MPTK PRO] - Late reverberation level relative to room effect.
Ranges from -10000.0 to 2000.0. Default is 0.0. More...
 
float MPTK_ReverbReflectionLevel [get, set]
 [MPTK PRO] - Early reflections level relative to room effect.
Ranges from 0 to 1. More...
 
float MPTK_ReverbRoom [get, set]
 [MPTK PRO] - Room effect level at low frequencies.
Ranges from 0 to 1. More...
 
float MPTK_ReverbRoomHF [get, set]
 [MPTK PRO] - Room effect high-frequency level.
Ranges from 0 to 1. More...
 
float MPTK_ReverbRoomLF [get, set]
 [MPTK PRO] - Room effect low-frequency level.
Ranges from 0 to 1. More...
 
float MPTK_SFChorusDepth [get, set]
 [MPTK PRO] - Set the SoundFont chorus effect depth
Chorus depth [0, 256]
V2.88.2 More...
 
float MPTK_SFChorusLevel [get, set]
 [MPTK PRO] - Set the SoundFont chorus effect level [0, 10]
V2.88.2 - becomes a parameter and default value set to 0.9 (was 2f, thank John) More...
 
float MPTK_SFChorusSpeed [get, set]
 [MPTK PRO] - Set the SoundFont chorus effect speed
Chorus speed in Hz [0.1, 5]
V2.88.2 More...
 
float MPTK_SFChorusWidth [get, set]
 [MPTK PRO] - Set the SoundFont chorus effect width
The chorus unit process a monophonic input signal and produces stereo output controlled by WIDTH macro.
Width allows to get a gradually stereo effect from minimum (monophonic) to maximum stereo effect. [0, 10]
V2.88.2 More...
 
float MPTK_SFFilterQModOffset [get, set]
 [MPTK PRO] - Quality Factor is defined in the SoundFont for each notes.
This parameter increase or decrease the default SoundFont value. Range: -96 to 96. More...
 
float MPTK_SFReverbDamp [get, set]
 [MPTK PRO] - Set the SoundFont reverb effect damp [0,1].
Controls the reverb time frequency dependency. This controls the reverb time for the frequency sample rate/2
When 0, the reverb time for high frequencies is the same as for DC frequency.
When > 0, high frequencies have less reverb time than lower frequencies.
V2.88.2
More...
 
float MPTK_SFReverbLevel [get, set]
 [MPTK PRO] - Set the SoundFont reverb effect level
V2.88.2 More...
 
float MPTK_SFReverbRoomSize [get, set]
 [MPTK PRO] - Set the SoundFont reverb effect room size. Controls concave reverb time between 0 (0.7 s) and 1 (12.5 s)
V2.88.2
More...
 
float MPTK_SFReverbWidth [get, set]
 [MPTK PRO] - Set the SoundFont reverb effect width [0,100].
Controls the left/right output separation.
When 0, there are no separation and the signal on left and right output is the same.This sounds like a monophonic signal.
When 100, the separation between left and right is maximum.
V2.88.2
More...
 
bool MPTK_Spatialize [get, set]
 Should the Spatialization effect must be enabled?
See here how to setup spatialization with Unity https://paxstellar.fr/midi-file-player-detailed-view-2/#Foldout-Spatialization-Parameters More...
 
int MPTK_SpatialSynthIndex [get]
 Index of the MidiSynth for the dedicated Channel or Track when the prefab MidiSpatializer is used.
The MidiSynth reader (from a midi file) has no channel because no voice is played, so DedicatedChannel is set to -1 More...
 
int MPTK_SynthRate [get, set]
 Current synth rate defined. More...
 
int MPTK_TransExcludedChannel [get, set]
 V2.88.3 Transpose will apply to all channels except this one. Set to -1 to apply to all channel.
Default is 9 because generally we don't want to transpose drum channel. More...
 
int MPTK_Transpose [get, set]
 Transpose note from -24 to 24 More...
 
float MPTK_Volume [get, set]
 Volume of midi playing.
Must be >=0 and <= 1 More...
 

Additional Inherited Members

- Public Attributes inherited from MidiPlayerTK.MidiSynth
bool MPTK_ApplyModLfo
 Apply LFO effect defined in the SoundFont More...
 
bool MPTK_ApplyRealTimeModulator
 Apply real time modulatoreffect defined in the SoundFont: pitch bend, control change, enveloppe modulation More...
 
bool MPTK_ApplySFChorus
 [MPTK PRO] - Apply chorus effect as defined in the SoundFont.
This effect is processed with the fluidsynth algo independently on each voices but with a small decrease of performace (10%). More...
 
bool MPTK_ApplySFFilter
 [MPTK PRO] - Apply frequency low-pass filter as defined in the SoundFont.
This effect is processed with the fluidsynth algo independently on each voices but with a small decrease of performace (40%). More...
 
bool MPTK_ApplySFReverb
 [MPTK PRO] - Apply reverberation effect as defined in the SoundFont.
This effect is processed with the fluidsynth algo independently on each voices but with a small decrease of performace (40%). More...
 
bool MPTK_ApplyVibLfo
 Apply vibrato effect defined in the SoundFont More...
 
bool MPTK_AutoBuffer = true
 Voice buffering is important to get better performance. But you can disable this fonction with this parameter. More...
 
int MPTK_AutoCleanVoiceLimit
 Free voices older than MPTK_AutoCleanVoiceLimit are removed when count is over than MPTK_AutoCleanVoiceTime More...
 
bool MPTK_CorePlayer
 If true then Midi events are read and play from a dedicated thread.
If false, MidiSynth will use AudioSource gameobjects to play sound.
This properties must be defined before running the application from the inspector.
The default is true.
Warning: The non core mode player (MPTK_CorePlayer=false) will be removed with the next major version (V3) More...
 
float MPTK_CutOffVolume = 0.05f
 When amplitude is below this value the playing of sample is stopped (voice_off).
Can be increase for better performance but with degraded quality because sample could be stopped earlier. Remember: Amplitude can varying between 0 and 1. More...
 
bool MPTK_DirectSendToPlayer
 If true (default) then Midi events are sent automatically to the midi player.
Set to false if you want to process events without playing sound.
OnEventNotesMidi Unity Event can be used to process each notes. More...
 
bool MPTK_EnableChangeTempo
 Should accept change tempo from Midi Events ? More...
 
bool MPTK_EnableFreeSynthRate = false
 Allow direct setting of the Synth Rate More...
 
bool MPTK_EnablePanChange
 Should change pan from Midi Events or from SoundFont ?
Pan is disabled when Spatialization is activated. More...
 
bool MPTK_EnablePresetDrum
 Should accept change Preset for Drum canal 10 ?
Disabled by default. Could sometimes create bad sound with midi files not really compliant with the Midi norm. More...
 
bool MPTK_KillByExclusiveClass = true
 V2.83 Find the exclusive class of this voice. If set, kill all voices that match the exclusive class
and are younger than the first voice process created by this noteon event. More...
 
float MPTK_LeanSynthStarting = 0.05f
 V2.873 - A lean startup of the volume of the synth is useful to avoid weird sound at the beguining of the application (in some cases).
This parameter sets the speed of the increase of the volume of the audio source.
Set to 1 for an immediate full volume at start. More...
 
bool MPTK_LogWave
 Log for each wave to be played More...
 
bool MPTK_ReleaseSameNote = true
 V2.83. If the same note is hit twice on the same channel, then the older voice process is advanced to the release stage.
It's the default Midi processing. More...
 
uint MPTK_ReleaseTimeMin = 500000
 [Only when CorePlayer=False] Define a minimum release time at noteoff in 100 iem nanoseconds.
Default 50 ms is a good tradeoff. Below some unpleasant sound could be heard. Useless when MPTK_CorePlayer is true. More...
 
float MPTK_ReleaseTimeMod = 1f
 When a note is stopped with a noteoff or when the duration is over, note continue to play for a short time depending the instrument.

This parameter is a multiplier to increase or decrease the default release time defined in the SoundFont for each instrument.
Recommended values between 0.1 and 10. Default is 1 (no modification of the release time).
Performance issue: the longer it lasts the more CPU is used after the noteon. With a long release time, a lot of samples will be played simultaneously. More...
 
float MPTK_SFChorusAmplify
 [MPTK PRO] - Chorus level is defined in the SoundFont in the range [0, 1].
This parameter is added to the the default SoundFont value.
Range must be [-1, 1] More...
 
float MPTK_SFFilterFreqOffset = 0f
 [MPTK PRO] - Frequency cutoff is defined in the SoundFont for each notes.
This parameter increase or decrease the default SoundFont value. Range: -2000 to 3000 More...
 
float MPTK_SFReverbAmplify
 [MPTK PRO] - Reverberation level is defined in the SoundFont in the range [0, 1].
This parameter is added to the the default SoundFont value.
Range must be [-1, 1] More...
 
bool MPTK_SpatialSynthEnabled
 In spatialization mode not all MidiSynths are enabled. More...
 
int MPTK_StatVoiceCountActive
 Count of the active voices (playing) - Readonly More...
 
int MPTK_StatVoiceCountFree
 Count of the free voices for reusing on need.
Voice older than AutoCleanVoiceTime are removed but only when count is over than AutoCleanVoiceLimit - Readonly More...
 
int MPTK_StatVoicePlayed
 Count of voice played since the start of the synth More...
 
float MPTK_StatVoiceRatioReused
 Percentage of voice reused during the synth life. 0: any reuse, 100:all voice reused (unattainable, of course!) More...
 
bool MPTK_WeakDevice
 Should play on a weak device (cheaper smartphone) ? Apply only with AudioSource mode (MPTK_CorePlayer=False).
Playing Midi files with WeakDevice activated could cause some bad interpretation of Midi Event, consequently bad sound. More...
 
EventSynthClass OnEventSynthAwake
 Unity event fired at awake of the synthesizer. Name of the gameobject component is passed as a parameter.
Seting this callback function by script (AddListener) is not recommended. It's better to set callback function from the inspector. More...
 
EventSynthClass OnEventSynthStarted
 Unity event fired at start of the synthesizer. Name of the gameobject component is passed as a parameter.
Seting this callback function by script (AddListener) is not recommended. It's better to set callback function from the inspector. More...
 
bool playOnlyFirstWave
 Preset are often composed with 2 or more samples. Classically for left and right channel. Check this to play only the first sample found More...
 
- Static Public Attributes inherited from MidiPlayerTK.MidiSynth
static List< MidiFilePlayerSpatialSynths
 Contains each MidiSynth for each channel or track when the prefab MidiSpatializer is used and IsMidiChannelSpace=true.
Warning: only one MidiSpatializer can be used in a hierarchy. More...
 
- Events inherited from MidiPlayerTK.MidiSynth
OnAudioFrameStartHandler OnAudioFrameStart
 [MPTK PRO] - this event is triggered at each start of a new audio frame from the audio engine.
The parameter (double) is the current synth time in milliseconds. See example of use.
The callbach function will not run on the Unity thread, so you can't call Unity API except Debug.Log. More...
 

Detailed Description

The prefab MidiStreamPlayer is useful for playing real time music in relation with user actions or an algo.
Any Midi file is necessary, the notes are generated by your scripts from your own algorithm. Thank to the API of this class.
The main function MPTK_PlayEvent() and the class MPTKEvent are able to create all kind of midi events as note-on.
All the values must be set in MPTKEvent, command, note value, duration ... for more details look at the class MPTKEvent.
A note-on must also be stopped, : if duration = -1 the note is infinite, it's the goal of MPTK_StopEvent() to stop the note with a note-off.
On top of that, the Pro version adds playing chords with MPTK_PlayChordFromRange() and MPTK_PlayChordFromLib().
For playing scales, have a look to the class MPTKRangeLib
For more information see here https://paxstellar.fr/midi-file-player-detailed-view-2-2/
and look at the demo TestMidiStream with the source code TestMidiStream.cs.

On top of that, this class inherits from MidiSynth
All properties, event, methods from MidiSynth are available in the class MidiStreamPlayer.

using MidiPlayerTK; // Add a reference to the MPTK namespace at the top of your script
using UnityEngine;
public class YourClass : MonoBehaviour
{
// Need a reference to the prefab MidiStreamPlayer you have added in your scene hierarchy.
public MidiStreamPlayer midiStreamPlayer;
// This object will be pass to the MPTK_PlayEvent for playing an event
MPTKEvent mptkEvent;
void Start()
{
// Find the MidiStreamPlayer. Could be also set directly from the inspector.
midiStreamPlayer = FindObjectOfType<MidiStreamPlayer>();
}
void Play()
{
// Pitch wheel change integrated in the play event
midiStreamPlayer.MPTK_PlayEvent(new MPTKEvent()
{
Command = MPTKCommand.PitchWheelChange,
Value = 10000, // Value between 0 and 16383, center at 8192 (no change)
});
// Play a note
mptkEvent = new MPTKEvent()
{
Channel = 0, // Between 0 and 15
Duration = -1, // Infinite
Value = 60, // Between 0 and 127, with 60 plays a C5
Velocity = 100, // Max 127
};
midiStreamPlayer.MPTK_PlayEvent(mptkEvent);
}
// more later .... stop the note
void Stop()
{
midiStreamPlayer.MPTK_StopEvent(mptkEvent);
}
}
Definition: MidiFileLoader.cs:11
MPTKCommand
MIDI command codes. Defined the action to be done with the message: note on/off, change instrument,...
Definition: MPTKEvent.cs:15

Member Function Documentation

◆ MPTK_PlayChordFromLib()

MPTKChordBuilder MidiPlayerTK.MidiStreamPlayer.MPTK_PlayChordFromLib ( MPTKChordBuilder  chord)

[MPTK PRO] Play a chord from the chord library. See file ChordLib.csv in folder Resources/GeneratorTemplate.
The Tonic is used to buid the chord

private void PlayOneChordFromLib()
{
// Start playing a new chord
ChordLibPlaying = new MPTKChordBuilder(true)
{
// Parameters to build the chord
Tonic = CurrentNote,
FromLib = CurrentChord,
// Midi Parameters how to play the chord
Channel = StreamChannel,
// delay in milliseconds between each notes of the chord
Arpeggio = ArpeggioPlayChord,
// millisecond, -1 to play indefinitely
Duration = Convert.ToInt64(NoteDuration * 1000f),
// Sound can vary depending on the velocity
Velocity = Velocity,
Delay = Convert.ToInt64(NoteDelay * 1000f),
};
midiStreamPlayer.MPTK_PlayChordFromLib(ChordLibPlaying);
}
Parameters
chordrequired: Tonic and FromLib on top of the classical Midi parameters
Returns

◆ MPTK_PlayChordFromRange()

MPTKChordBuilder MidiPlayerTK.MidiStreamPlayer.MPTK_PlayChordFromRange ( MPTKChordBuilder  chord)

[MPTK PRO] Play a chord from the current selected range (MPTK_RangeSelected), Tonic and Degree are defined in parameter MPTKChord chord.
Major range is selected if no range defined.
See file GammeDefinition.csv in folder Resources/GeneratorTemplate

using MidiPlayerTK; // Add a reference to the MPTK namespace at the top of your script
using UnityEngine;
public class YourClass : MonoBehaviour
{
// Need a reference to the prefab MidiStreamPlayer you have added in your scene hierarchy.
public MidiStreamPlayer midiStreamPlayer;
// This object will be pass to the MPTK_PlayEvent for playing an event
MPTKEvent mptkEvent;
void Start()
{
// Find the MidiStreamPlayer. Could be also set directly from the inspector.
midiStreamPlayer = FindObjectOfType<MidiStreamPlayer>();
}
private void PlayOneChordFromLib()
{
// Start playing a new chord
MPTKChordBuilder ChordLibPlaying = new MPTKChordBuilder(true)
{
// Parameters to build the chord
Tonic = 60,
FromLib = 2,
// Midi Parameters how to play the chord
Channel = 0,
// delay in milliseconds between each notes of the chord
Arpeggio = 100,
// millisecond, -1 to play indefinitely
Duration = 500,
// Sound can vary depending on the velocity
Velocity = 100,
Delay = 0,
};
midiStreamPlayer.MPTK_PlayChordFromLib(ChordLibPlaying);
}
}
Parameters
chordrequired: Tonic and Degree on top of the classical Midi parameters
Returns

◆ MPTK_PlayEvent() [1/2]

void MidiPlayerTK.MidiStreamPlayer.MPTK_PlayEvent ( List< MPTKEvent events)

Play a list of midi events with a thread so the call return immediately.

private void PlayOneNote()
{
//Debug.Log($"{StreamChannel} {midiStreamPlayer.MPTK_ChannelPresetGetName(StreamChannel)}");
// Start playing a new note
NotePlaying = new MPTKEvent()
{
Command = MPTKCommand.NoteOn,
Value = CurrentNote, // note to played, ex 60=C5. Use the method from class HelperNoteLabel to convert to string
Channel = StreamChannel,
Duration = Convert.ToInt64(NoteDuration * 1000f), // millisecond, -1 to play indefinitely
Velocity = Velocity, // Sound can vary depending on the velocity
Delay = Convert.ToInt64(NoteDelay * 1000f),
};
#if MPTK_PRO
// Applied to the current note playing all the real time generators defined
for (int i = 0; i < nbrGenerator; i++)
if (indexGenerator[i] >= 0)
NotePlaying.MTPK_ModifySynthParameter((fluid_gen_type)indexGenerator[i], valueGenerator[i] / 100f, MPTKModeGeneratorChange.Override);
#endif
midiStreamPlayer.MPTK_PlayEvent(NotePlaying);
}

◆ MPTK_PlayEvent() [2/2]

void MidiPlayerTK.MidiStreamPlayer.MPTK_PlayEvent ( MPTKEvent  evnt)

Play one midi event with a thread so the call return immediately.

midiStreamPlayer.MPTK_PlayEvent
(
new MPTKEvent()
{
Channel = 9,
Duration = 999999,
Value = 48,
Velocity = 100
}
);

◆ MPTK_PlayPitchWheelChange()

void MidiPlayerTK.MidiStreamPlayer.MPTK_PlayPitchWheelChange ( int  channel,
float  pitchWheel 
)

[MPTK PRO] V2.88.2 Play a Midi pitch change event for all notes on the channel

Parameters
channelChannel must be in the range 0-15
pitchWheelNormalized Pitch Wheel Value. Range 0 to 1. V2.88.2 range normalized from 0 to 1.
  • 0 minimum (equivalent to value 0 for midi standard event)
  • 0.5 centered (equivalent to value 8192 for midi standard event)
  • 1 maximum (equivalent to value 16383 for midi standard event)


◆ MPTK_PlayPitchWheelSensitivity()

void MidiPlayerTK.MidiStreamPlayer.MPTK_PlayPitchWheelSensitivity ( int  channel,
int  sensitivity 
)

[MPTK PRO] V2.88.2 Play a midi pitch sensitivity change for all notes on the channel.

Parameters
channelChannel must be in the range 0-15
sensitivityPitch change sensitivity from 0 to 24 semitones up and down. Default value 2.\ Example: 4, means semitons range is from -4 to 4 when MPTK_PlayPitchWheelChange change from 0 to 1.

◆ MPTK_StartMidiStream()

void MidiPlayerTK.MidiStreamPlayer.MPTK_StartMidiStream ( )

Force starting of the MidiStream. May be usefull from another gamecomponent which want send midi command at the start of the application.

!
! // Find a MidiStreamPlayer Prefab from the scene
! MidiStreamPlayer MidiStream = FindObjectOfType<MidiStreamPlayer>();
! MidiStream.MPTK_StartMidiStream();
!

///

◆ MPTK_StopChord()

void MidiPlayerTK.MidiStreamPlayer.MPTK_StopChord ( MPTKChordBuilder  chord)

Stop playing the chord. All samples associated to the chord are stopped by sending a noteoff.

Parameters
chord

◆ MPTK_StopEvent()

void MidiPlayerTK.MidiStreamPlayer.MPTK_StopEvent ( MPTKEvent  pnote)

Stop playing the note. All waves associated to the note are stop by sending a noteoff.

Parameters
pnote

Property Documentation

◆ MPTK_RangeName

string? MidiPlayerTK.MidiStreamPlayer.MPTK_RangeName
get

[MPTK PRO] Name of range selected (musical scale)

◆ MPTK_RangeSelected

int MidiPlayerTK.MidiStreamPlayer.MPTK_RangeSelected
getset

[MPTK PRO] Current selected range (musical scale)