Maestro - Midi Player Tool Kit for Unity Version 2.19.0
Loading...
Searching...
No Matches

Collection of MIDI channels managed by a MIDI synth.
A standard MIDI synth uses 16 channels (0 to 15). Each channel keeps an independent state, including: More...

Public Member Functions

 MPTKChannels (MidiSynth psynth, int countChannel=16)
 Creates a channel collection for a synth.
 MPTKChannels ()
 Parameterless constructor kept for serialization and code stripping preservation.
void ResetExtension (int channelNum=-1)
 Resets Maestro channel extension fields.

Public Attributes

bool EnableResetChannel = true
 Enables channel-state reset when MIDI playback starts.
If true (default), the channel list is reinitialized when the synth is reinitialized. Setting this to false can produce unexpected behavior with MidiFilePlayer, but may be useful with MidiStreamPlayer.

Properties

int Length [get]
 Number of channels in this collection. Usually 16 when reading a standard MIDI file.
Higher values are possible for internal usage, but this is not MIDI-file compliant.
bool EnableAll [set]
 Enables or disables playback for all channels.
float VolumeAll [set]
 Sets the volume for all channels, from 0.0 to 1.0.
MPTKChannel this[int channel] [get, set]
 Indexer access to a channel in the synth. Channels are addressed from 0 to 15 in standard MIDI usage.

Detailed Description

Collection of MIDI channels managed by a MIDI synth.
A standard MIDI synth uses 16 channels (0 to 15). Each channel keeps an independent state, including:

  • Instrument (preset) and bank
  • Volume
  • Mute state (see Enable)
  • Pitch bend and controller values

MIDI messages target one channel at a time, allowing different instruments and settings to play simultaneously. In General MIDI, channel 9 (10 in 1-based MIDI notation) is conventionally used for percussion.

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

Constructor & Destructor Documentation

◆ MPTKChannels()

MidiPlayerTK.MPTKChannels.MPTKChannels ( MidiSynth psynth,
int countChannel = 16 )

Creates a channel collection for a synth.

Parameters
psynthTarget synth that owns these channels.
countChannelNumber of channels to create. Default is 16.

Member Function Documentation

◆ ResetExtension()

void MidiPlayerTK.MPTKChannels.ResetExtension ( int channelNum = -1)

Resets Maestro channel extension fields.

  • LastBank is set to -1 (no last bank feature when reset)
  • ForcedBank is disabled
  • ForcedPreset is disabled
  • Enable is set to true
  • Volume is set to maximum (1) Other channel state, such as current preset, bank, and controllers, is not reset.
    Version
    2.10.1
    Parameters
    channelNumChannel index to reset. Use -1 (default) to reset all channels.

Member Data Documentation

◆ EnableResetChannel

bool MidiPlayerTK.MPTKChannels.EnableResetChannel = true

Enables channel-state reset when MIDI playback starts.
If true (default), the channel list is reinitialized when the synth is reinitialized. Setting this to false can produce unexpected behavior with MidiFilePlayer, but may be useful with MidiStreamPlayer.

Version
2.10.1

Property Documentation

◆ EnableAll

bool MidiPlayerTK.MPTKChannels.EnableAll
set

Enables or disables playback for all channels.

// Also available for MidiStreamPlayer, MidiInReader, MidiExternalPlayer.
if (GUILayout.Button("Enable All", GUILayout.Width(100)))
midiFilePlayer.MPTK_Channels.EnableAll = true;
if (GUILayout.Button("Disable All", GUILayout.Width(100)))
midiFilePlayer.MPTK_Channels.EnableAll = false;

◆ this[int channel]

MPTKChannel MidiPlayerTK.MPTKChannels.this[int channel]
getset

Indexer access to a channel in the synth. Channels are addressed from 0 to 15 in standard MIDI usage.

// Force a random preset between 0 and 127 for each channels
// midiFilePlayer.MPTK_Channels.ResetExtension(); to return to origin preset
foreach (MPTKChannel mptkChannel in midiFilePlayer.MPTK_Channels)
mptkChannel.ForcedPreset = UnityEngine.Random.Range(0, 127);
Parameters
channelZero-based channel index (typically 0 to 15).
Returns
The channel at the requested index, or null if out of range.