MidiPlayerTK.MPTKChannel

Description of a MIDI Channel associated to the MIDI synth.
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. More...

Public Member Functions

int Controller (MPTKController numController, int valueController=-1)
 
override string ToString ()
 

Public Attributes

byte[] key_pressure
 
int LastBank
 
int LastPreset
 

Properties

int BankNum [get, set]
 
int Channel [get]
 
bool Enable [get, set]
 
int ForcedBank [get, set]
 
int ForcedPreset [get, set]
 
int NoteCount [get, set]
 
string PresetName [get]
 
int PresetNum [get, set]
 
float Volume [get, set]
 

Detailed Description

Description of a MIDI Channel associated to the MIDI synth.
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.

  • Current instrument / bank
  • Volume
  • Mute / Unmute (see Enable)
  • Pitch bend ...
// 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;

Member Function Documentation

◆ Controller()

int MidiPlayerTK.MPTKChannel.Controller ( MPTKController  numController,
int  valueController = -1 
)

Read or write the value of the controller.

Returns
controller value if read, previous value if write, -1 if error
GUILayout.Label("Sustain switch", myStyle.LabelRight, GUILayout.Width(120), GUILayout.Height(25));
// Is sustain enabled on the current channel?
bool sustain = midiStreamPlayer.MPTK_Channels[StreamChannel].Controller(MPTKController.Sustain) < 64 ? false : true;
// Switch sustain state
if (GUILayout.Button(sustain ? "Sustain On" : "Sustain Off", sustain ? myStyle.BtSelected : myStyle.BtStandard, GUILayout.Width(buttonTinyWidth)))
// Send a MIDI event control change for sustain
midiStreamPlayer.MPTK_PlayEvent(new MPTKEvent()
{
Command = MPTKCommand.ControlChange,
Value = sustain ? 0 : 64, // enable / disable sustain
Channel = StreamChannel
});
int Channel
Definition: MPTKChannels.cs:247
int Controller(MPTKController numController, int valueController=-1)
Definition: MPTKChannels.cs:595
Description of a MIDI Event. It's the heart of MPTK! Essential to handling MIDI by script from all ot...
Definition: MPTKEvent.cs:45
MPTKController
Definition: MPTKEnum.cs:131
MPTKCommand
Definition: MPTKEnum.cs:12
Parameters
numControllerController to read or write
valueControllerValue to set, default is -1 for only reading the controller value (no write)
  • SontFont 2.01 NRPN Message (Sect. 9.6, p. 74) *‍/

◆ ToString()

override string MidiPlayerTK.MPTKChannel.ToString ( )

Build an information string about the channel. It's also a good pretext to display an example of Channel API.
Exemple of return

  • Channel:2 Enabled [Preset:18, Bank:0] 'Rock Organ' Count:1 Volume:1
  • Channel:4 Muted [Preset:F44, Bank:0] 'Stereo Strings Trem' Count:33 Volume:0,50
Parameters
channelindex channel
Returns
Information string

Member Data Documentation

◆ key_pressure

byte [] MidiPlayerTK.MPTKChannel.key_pressure

MIDI polyphonic key pressure from [0;127]

◆ LastBank

int MidiPlayerTK.MPTKChannel.LastBank

Last bank used for this channel, useful when a forced preset has been set.

◆ LastPreset

int MidiPlayerTK.MPTKChannel.LastPreset

Last preset used for this channel, useful when a forced preset has been set.

Property Documentation

◆ BankNum

int MidiPlayerTK.MPTKChannel.BankNum
getset

Get or Set the current bank number associated to the channel.
Each MIDI channel can play a different preset and bank.
You can find bank and preset number with the menu Maestro / SoundFont Setup in Right Panel,
click on buttons with an eye icon to get all the presets for each bank.

// Select any value in the range 0 and 16383 for the bank
int newBank = (int)HelperDemo.GUI_Slider("Free Bank", midiStreamPlayer.MPTK_Channels[StreamChannel].BankNum, 0, 128 * 128 - 1, alignCaptionRight: false, widthCaption: 100, widthSlider: 115, widthLabelValue: 30);
// Select any value in the range 0 and 127 for the preset
int newPreset = (int)HelperDemo.GUI_Slider("Free Preset", midiStreamPlayer.MPTK_Channels[StreamChannel].PresetNum, 0, 127, alignCaptionRight: true, widthCaption: 100, widthSlider: 115, widthLabelValue: 30);
// If user made change for bank or preset ...
if (newBank != midiStreamPlayer.MPTK_Channels[StreamChannel].BankNum ||
newPreset != midiStreamPlayer.MPTK_Channels[StreamChannel].PresetNum)
{
// ... apply the change to the MidiStreamPlayer for the current channel.
// If the bank or the preset doestn't exist
// - the method returns false
// - the bank and preset are still registered in the channel
// - when a note-on is received on this channel, the first preset of the first bank is used to play (usually piano).
midiStreamPlayer.MPTK_Channels[StreamChannel].BankNum = newBank;
if (midiStreamPlayer.MPTK_Channels[StreamChannel].BankNum != newBank)
Debug.Log($"Bank {newBank} not set ");
else
Debug.Log($"Bank set to:{newBank}");
midiStreamPlayer.MPTK_Channels[StreamChannel].PresetNum = newPreset;
if (midiStreamPlayer.MPTK_Channels[StreamChannel].PresetNum != newPreset)
Debug.Log($"Preset {newPreset} not set ");
else
Debug.Log($"Preset set to:{newPreset}");
// Read the current preset name
Debug.Log($"Current preset name '{midiStreamPlayer.MPTK_Channels[StreamChannel].PresetName}'");
}

◆ Channel

int MidiPlayerTK.MPTKChannel.Channel
get

Channel number between 0 and 15, channel 9 is set aside specifically for drum sounds.

◆ Enable

bool MidiPlayerTK.MPTKChannel.Enable
getset

Properties to enable (unmute) or disable (mute) a channel or get status. All channels are unmuted when MIDI start playing (MidiFilePlayer::MPTK_Play).
By the way, to mute channels just before the playing, use the MidiFilePlayer::OnEventStartPlayMidi.

Look to the demo: Assets\MidiPlayer\Demo\FreeMVP\MidiLoop.cs

// Start is called before the first frame update
void Start()
{
// Find existing MidiFilePlayer in the scene hierarchy
// ---------------------------------------------------
midiFilePlayer = FindFirstObjectByType<MidiFilePlayer>();
if (midiFilePlayer == null)
{
Debug.LogWarning("Can't find a MidiFilePlayer Prefab in the current Scene Hierarchy. Add it with the Maestro menu.");
return;
}
midiFilePlayer.MPTK_PlayOnStart = false;
// Set Listeners
// -------------
// triggered when MIDI starts playing (Indeed, will be triggered at every restart)
midiFilePlayer.OnEventStartPlayMidi.AddListener(StartPlay);
// triggered when MIDI ends playing (Indeed, will be triggered at every end of restart)
midiFilePlayer.OnEventEndPlayMidi.AddListener(EndPlay);
// triggered every time a group of MIDI events are ready to be played by the MIDI synth.
midiFilePlayer.OnEventNotesMidi.AddListener(MidiReadEvents);
LoadAndPlay();
}
public void StartPlay(string midiname)
{
// Enable or disable MIDI channel. it's not possible before because channel are not yet allocated
for (int channel = 0; channel < midiFilePlayer.MPTK_Channels.Length; channel++)
// Enable only ChannelSelected or all channels if ChannelSelected equal -1
if (channel == ChannelSelected || ChannelSelected == -1)
// v2.10.1 midiFilePlayer.MPTK_ChannelEnableSet(channel, true);
midiFilePlayer.MPTK_Channels[channel].Enable= true;
else
// Disable this channel
// v2.10.1 midiFilePlayer.MPTK_ChannelEnableSet(channel, false);
midiFilePlayer.MPTK_Channels[channel].Enable = false;
Debug.Log($"<color=green>Start at tick:{midiFilePlayer.MPTK_TickCurrent}</color>" +
$" MPTK_DeltaTicksPerQuarterNote:{midiFilePlayer.MPTK_MidiLoaded.MPTK_DeltaTicksPerQuarterNote}" +
$" MPTK_NumberBeatsMeasure:{midiFilePlayer.MPTK_MidiLoaded.MPTK_NumberBeatsMeasure}" +
$" MPTK_NumberQuarterBeat:{midiFilePlayer.MPTK_MidiLoaded.MPTK_NumberQuarterBeat}");
}
Returns
true if channel is enabled
// 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*/
}

◆ ForcedBank

int MidiPlayerTK.MPTKChannel.ForcedBank
getset

Get or Set forced bank on the channel from 0 to 65635. MIDI will allways using this bank even if a bank change message is received.
Set to -1 to disable this behavior.

Returns
preset index, -1 if not set
// 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;

◆ ForcedPreset

int MidiPlayerTK.MPTKChannel.ForcedPreset
getset

Get or 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.

Parameters
channel
Returns
preset index, -1 if not set
// 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;

◆ NoteCount

int MidiPlayerTK.MPTKChannel.NoteCount
getset

Get or Set the count of notes (NoteOn) played since the start of the MIDI. Set could be usefull to reset the value to 0.

◆ PresetName

string MidiPlayerTK.MPTKChannel.PresetName
get

Get the current preset name for the channel.
Each MIDI channel can play a different preset.

Parameters
channelMIDI channel must be between 0 and 15
Returns
channel preset name or "" if channel error
// Defined preset with CurrentPreset (value defined in inspector to 0).
midiStreamPlayer.MPTK_Channels[StreamChannel].PresetNum = CurrentPreset;
Debug.LogFormat($" Preset '{midiStreamPlayer.MPTK_Channels[StreamChannel].PresetName}' defined on channel {StreamChannel}");

◆ PresetNum

int MidiPlayerTK.MPTKChannel.PresetNum
getset

Get or Set current preset number associated to the channel. Value must be between 0 and 127 (if you have a full GM SoundFont).
Each MIDI channel can play a different preset and bank.
You can find bank and preset number with the menu Maestro / SoundFont Setup in Right Panel,
click on buttons with an eye icon to get all the presets for each bank.

// Select any value in the range 0 and 16383 for the bank
int newBank = (int)HelperDemo.GUI_Slider("Free Bank", midiStreamPlayer.MPTK_Channels[StreamChannel].BankNum, 0, 128 * 128 - 1, alignCaptionRight: false, widthCaption: 100, widthSlider: 115, widthLabelValue: 30);
// Select any value in the range 0 and 127 for the preset
int newPreset = (int)HelperDemo.GUI_Slider("Free Preset", midiStreamPlayer.MPTK_Channels[StreamChannel].PresetNum, 0, 127, alignCaptionRight: true, widthCaption: 100, widthSlider: 115, widthLabelValue: 30);
// If user made change for bank or preset ...
if (newBank != midiStreamPlayer.MPTK_Channels[StreamChannel].BankNum ||
newPreset != midiStreamPlayer.MPTK_Channels[StreamChannel].PresetNum)
{
// ... apply the change to the MidiStreamPlayer for the current channel.
// If the bank or the preset doestn't exist
// - the method returns false
// - the bank and preset are still registered in the channel
// - when a note-on is received on this channel, the first preset of the first bank is used to play (usually piano).
midiStreamPlayer.MPTK_Channels[StreamChannel].BankNum = newBank;
if (midiStreamPlayer.MPTK_Channels[StreamChannel].BankNum != newBank)
Debug.Log($"Bank {newBank} not set ");
else
Debug.Log($"Bank set to:{newBank}");
midiStreamPlayer.MPTK_Channels[StreamChannel].PresetNum = newPreset;
if (midiStreamPlayer.MPTK_Channels[StreamChannel].PresetNum != newPreset)
Debug.Log($"Preset {newPreset} not set ");
else
Debug.Log($"Preset set to:{newPreset}");
// Read the current preset name
Debug.Log($"Current preset name '{midiStreamPlayer.MPTK_Channels[StreamChannel].PresetName}'");
}

◆ Volume

float MidiPlayerTK.MPTKChannel.Volume
getset

Get or Set the volume for a channel as a percentage between 0 and 1.

// 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;