Base class wich contains all the stuff to build a Wave Table Synth. More...
Public Types | |
enum | ModeSpatializer { Channel , Track } |
Public Member Functions | |
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 | |
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 | |
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< MidiFilePlayer > | SpatialSynths |
Properties | |
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] |
Events | |
OnAudioFrameStartHandler | OnAudioFrameStart |
Base class wich contains all the stuff to build a Wave Table Synth.
Load SoundFont and samples, process midi event, play voices, controllers, generators ...
This class is inherited by others class to build these prefabs: MidiStreamPlayer, MidiFilePlayer, MidiInReader.
It is not recommended to instanciate directly this class, rather add prefabs to the hierarchy of your scene. and use attributs and methods from an instance of them in your script. Example:
class extension pro
Spatializer Mode for the prefab MidiSpatializer
Enumerator | |
---|---|
Channel | Spatial Synth are enabled to dispatch note-on by channels. |
Track | Spatial Synth are enabled to dispatch note-on by tracks defined in the MIDI. |
|
static |
Build a string with performance and information about the MIDI reader and the MIDI synthesizer. DEBUG_PERF_AUDIO DEBUG_HISTO_DSPSIZE DEBUG_PERF_MIDI
synth |
void MidiPlayerTK.MidiSynth.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.
destroyAudioSource | useful only in non core mode |
_idSession | clear only for sample playing with this session, -1 for all (default) |
void MidiPlayerTK.MidiSynth.MPTK_InitSynth | ( | int | channelCount = 16 , |
bool | preserveChannelInfo = false |
||
) |
Initialize the synthetizer:
channelCount | Number of channel to create. Default is 16. Any other values are experimental! |
preserveChannelInfo | if true, the channel information will not be reset, Default is false: reinit also channel information |
void MidiPlayerTK.MidiSynth.MPTK_ResetStat | ( | ) |
Reset voices statistics
void MidiPlayerTK.MidiSynth.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.
void MidiPlayerTK.MidiSynth.MPTK_StartSynth | ( | ) |
Start processing samples by the synth and the MIDI sequencer. Useful only if MPTK_StopSynth has been called and MidiStreamPlayer.
void MidiPlayerTK.MidiSynth.MPTK_StopSynth | ( | ) |
Stop processing samples by the synth and the MIDI sequencer. See also MPTK_StartSynth.
IEnumerator MidiPlayerTK.MidiSynth.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 ***
_idSession | clear only for samples playing with this session, -1 for all |
delegate void MidiPlayerTK.MidiSynth.OnAudioFrameStartHandler | ( | double | synthTime | ) |
Delegate for the event OnAudioFrameStartHandler. see OnAudioFrameStart.
synthTime |
|
static |
Default chorus depth
|
static |
Default chorus level
|
static |
Default chorus voice count
|
static |
Default chorus speed
|
static |
Default chorus width gradually stereo effect
|
static |
Default reverb damping
|
static |
Default reverb level
|
static |
Default reverb room size
|
static |
Default reverb width
bool MidiPlayerTK.MidiSynth.MPTK_ApplyModLfo |
Apply LFO effect defined in the SoundFont
bool MidiPlayerTK.MidiSynth.MPTK_ApplyRealTimeModulator |
Apply real time modulatoreffect defined in the SoundFont: pitch bend, control change, enveloppe modulation
bool MidiPlayerTK.MidiSynth.MPTK_ApplyVibLfo |
Apply vibrato effect defined in the SoundFont
bool MidiPlayerTK.MidiSynth.MPTK_AudioSettingFromUnity |
If true then rate synth and buffer size will be automatically defined by Unity in accordance of the capacity of the hardware. - V2.89.0 -
Look at Unity menu "Edit / Project Settings..." and select between best latency and best performance.
If false, then rate and buffer size can be defined manually ... but with the risk of bad audio quality. It's more an experimental capacities!
bool MidiPlayerTK.MidiSynth.MPTK_AutoBuffer = true |
Voice buffering is important to get better performance.
int MidiPlayerTK.MidiSynth.MPTK_AutoCleanVoiceLimit |
Free voices older than MPTK_AutoCleanVoiceLimit are removed when count is over than MPTK_AutoCleanVoiceTime
bool MidiPlayerTK.MidiSynth.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)
float MidiPlayerTK.MidiSynth.MPTK_CutOffVolume = 0.0001f |
When amplitude of a sample is below this value the playing of sample is stopped.
Can be increase for better performance (when a lot of samples are played concurrently) but with degraded quality because sample could be stopped too early. Remember: Amplitude can varying between 0 and 1.
bool MidiPlayerTK.MidiSynth.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.
MPTKEffectSoundFont MidiPlayerTK.MidiSynth.MPTK_EffectSoundFont |
A SoundFont contains parameters to apply three kinds of effects: low-pass filter, reverb, chorus.
These parameters can be specifics for each instruments and even each voices.
Maestro MPTK effects are based on FluidSynth algo effects modules. Furthermore, to get more liberty from SoundFont, Maestro Pro can increase or decrease the impact of effects (from the inspector or by script). To summarize:
MPTKEffectUnity MidiPlayerTK.MidiSynth.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.
bool MidiPlayerTK.MidiSynth.MPTK_EnableChangeTempo |
Enable MIDI events tempo change from the MIDI file when playing. If disabled, only the first tempo change found in the MIDI will be applied (or 120 if not tempo change). Disable it when you want to change tempo by your script.
bool MidiPlayerTK.MidiSynth.MPTK_EnableFreeSynthRate = false |
Allow direct setting of the Synth Rate
bool MidiPlayerTK.MidiSynth.MPTK_EnablePanChange |
Should change pan from MIDI Events or from SoundFont ?
Pan is disabled when Spatialization is activated.
bool MidiPlayerTK.MidiSynth.MPTK_EnablePresetDrum |
Should accept change Preset for Drum canal 9 (drum) ?
Could sometimes create unexpected music with MIDI files not compliant with the MIDI standard. Example that could produce unexpected music. Generally, default drum bank is set to 128. With these events on channel 9,
preset 0 of the bank 0 will be selected for the drum ... and that will be, in general, a piano
int MidiPlayerTK.MidiSynth.MPTK_InstrumentNum |
When spatialization is track mode, contains the last instrument (preset) played.
bool MidiPlayerTK.MidiSynth.MPTK_KillByExclusiveClass = true |
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.
float MidiPlayerTK.MidiSynth.MPTK_LeanSynthStarting = 0.05f |
A lean startup of the volume of the synth is useful to avoid weird sound at the beginning 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.
bool MidiPlayerTK.MidiSynth.MPTK_LogEvents |
Log MIDI events (v2.9.0 moved from MidiFilePlayer)
bool MidiPlayerTK.MidiSynth.MPTK_LogWave |
Log for each sample to be played
bool MidiPlayerTK.MidiSynth.MPTK_ReleaseSameNote = true |
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.
uint MidiPlayerTK.MidiSynth.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.
float MidiPlayerTK.MidiSynth.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.
bool MidiPlayerTK.MidiSynth.MPTK_SpatialSynthEnabled |
In spatialization mode not all MidiSynths are enabled.
int MidiPlayerTK.MidiSynth.MPTK_StatVoiceCountActive |
Count of the active voices (playing and releasing) - Readonly
int MidiPlayerTK.MidiSynth.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
int MidiPlayerTK.MidiSynth.MPTK_StatVoiceCountPlaying |
Count of the active voices (playing excluding voices in release step) - Readonly
int MidiPlayerTK.MidiSynth.MPTK_StatVoiceCountReused |
Count of the voices reused - Readonly
int MidiPlayerTK.MidiSynth.MPTK_StatVoicePlayed |
Count of voice played since the start of the synth
int MidiPlayerTK.MidiSynth.MPTK_StatVoiceRatioReused |
Percentage of voice reused during the synth life. 0: any reuse, 100:all voice reused (unattainable, of course!)
int MidiPlayerTK.MidiSynth.MPTK_ThreadMidiWait = 10 |
Delay in milliseconds the MIDI thread waits before reading each MIDI events groups.
bool MidiPlayerTK.MidiSynth.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.
Action<int, long, int, int> MidiPlayerTK.MidiSynth.OnBeatEvent |
Action is executed at each beat and received these parameters:
EventSynthClass MidiPlayerTK.MidiSynth.OnEventSynthAwake |
Unity event fired at awake of the synthesizer. Name of the gameobject component is passed as a parameter.
Setting this callback function by script (AddListener) is not recommended. It's better to set callback function from the inspector.
Example of script (but it's recommended to set callback function from the inspector).
EventSynthClass MidiPlayerTK.MidiSynth.OnEventSynthStarted |
Unity event fired at start of the synthesizer. Name of the gameobject component is passed as a parameter.
Setting this callback function by script (AddListener) is not recommended. It's better to set callback function from the inspector.
Example of script (it's recommended to set callback function from the inspector).
Func<MPTKEvent, bool> MidiPlayerTK.MidiSynth.OnMidiEvent |
This function is called by the MIDI sequencer before sending the MIDI message to the MIDI synthesizer.
From version 2.10.0 the callback must return a boolean (see example). true to keep the event, false to skip it.
It can be used like a MIDI events preprocessor: it's possible to change the value of the MIDI events and therefore change the playback of the song.
The callback function receives a MPTKEvent object by reference (normal, it's a C# class).
Look at https://mptkapi.paxstellar.com/d9/d50/class_midi_player_t_k_1_1_m_p_t_k_event.html
Many changes are possible on the MIDI event: change note, velocity, channel, skip ..., even changing the MIDI type of the message!!!
See below some examples of run-time changes.
bool MidiPlayerTK.MidiSynth.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
|
static |
Contains each Midi Synth for each channel or track when the prefab MidiSpatializer is used and IsMidiChannelSpace=true.
Warning: only one MidiSpatializer can be used in a hierarchy.
|
getset |
Set or Get synth buffer size -1:default, 0:64, 1;128, 2:256, 3:512, 4:1024, 5:2048.
The change is global for all prefab. It's better to stop playing for all prefab before changing on fly to avoid bad noise or crash.
|
getset |
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.
|
get |
If spatialization is track mode, contains the last instrument played on this track.
|
get |
True if this MidiSynth is the master synth responsible to read midi events and to dispatch to other MidiSynths
|
getset |
When the value is true, NoteOff and Duration for non-looped samples are ignored and the samples play through to the end.
|
getset |
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.
|
getset |
[obsolete] replaced by MPTK_Spatialize"); V2.83
|
getset |
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 if MPTK_Spatialize is true:
AudioSource.maxDistance = MPTK_MaxDistance
AudioSource.spatialBlend = 1
AudioSource.spatialize = true
AudioSource.spatializePostEffects = true
|
get |
Index of the MidiSynth for the dedicated Channel or Track when the prefab MidiSpatializer is used.
If MPTK_ModeSpatializer = Channel then represent the playing channel.
If MPTK_ModeSpatializer = Track then represent the playing track.
The value is -1 for the Midi reader because no voice is played.
|
getset |
Get the the current synth rate or set free value (only if MPTK_EnableFreeSynthRate is true).
|
getset |
The MIDI thread’s priority shows how frequently a thread gains the access to CPU. The default value is 0 (normal), you can increase the priority to 1 and 2 (higher).
This is useful when the hardware is weak, to get a more stable reading of the MIDI.
|
get |
If spatialization is track mode, contains the last name of the track .
|
getset |
Transpose will apply to all channels except this one. Set to -1 to apply to all channel. V2.89.0
Default is 9 because generally we don't want to transpose drum channel.
|
getset |
Transpose note from -24 to 24
|
getset |
Global Volume between 0 and 1.
OnAudioFrameStartHandler MidiPlayerTK.MidiSynth.OnAudioFrameStart |
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.