private int[] keysToNote = { 60, 63, 65, 66, 67, 70, 72, 75, 77 };
void Update()
{
return;
if (eventsMidi == null)
for (int key = 0; key < 9; key++)
{
if (Input.GetKeyDown(KeyCode.Alpha1 + key))
{
{
Channel = StreamChannel,
Duration = -1,
Value = keysToNote[key],
Velocity = 100
};
midiStreamPlayer.MPTK_PlayEvent(eventsMidi[key]);
}
if (eventsMidi[key] != null && Input.GetKeyUp(KeyCode.Alpha1 + key))
{
midiStreamPlayer.MPTK_StopEvent(eventsMidi[key]);
eventsMidi[key] = null;
}
}
if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.UpArrow))
{
if (Input.GetKeyDown(KeyCode.DownArrow)) CurrentPreset--;
if (Input.GetKeyDown(KeyCode.UpArrow)) CurrentPreset++;
CurrentPreset = Mathf.Clamp(CurrentPreset, 0, 127);
midiStreamPlayer.MPTK_PlayEvent(new MPTKEvent()
{
Value = CurrentPreset,
Channel = StreamChannel,
});
}
#if MPTK_PRO
if (PitchChange != DEFAULT_PITCH)
{
if (Time.realtimeSinceStartup - LastTimePitchChange > 0.5f)
{
PitchChange = Mathf.SmoothDamp(PitchChange, DEFAULT_PITCH, ref currentVelocityPitch, 0.5f, 10000, Time.unscaledDeltaTime);
if (Mathf.Abs(PitchChange - DEFAULT_PITCH) < 0.001f)
PitchChange = DEFAULT_PITCH;
midiStreamPlayer.MPTK_PlayPitchWheelChange(StreamChannel, PitchChange);
}
}
#endif
if (midiStreamPlayer != null && (IsplayingLoopPresets || IsplayingLoopNotes))
{
float time = Time.realtimeSinceStartup - LastTimeChange;
if (time > LoopDelay)
{
LastTimeChange = Time.realtimeSinceStartup;
for (int indexNote = 0; indexNote < CountNoteToPlay; indexNote++)
{
if (IsplayingLoopPresets)
{
if (++CurrentPreset > EndLoopPreset) CurrentPreset = StartLoopPreset;
if (CurrentPreset < StartLoopPreset) CurrentPreset = StartLoopPreset;
midiStreamPlayer.MPTK_PlayEvent(new MPTKEvent()
{
Value = CurrentPreset,
Channel = StreamChannel,
});
}
if (IsplayingLoopNotes)
{
if (++CurrentNote > EndLoopingNote) CurrentNote = StartLoopingNote;
if (CurrentNote < StartLoopingNote) CurrentNote = StartLoopingNote;
}
MaestroPlay(false);
}
}
}
}
void MaestroPlay(bool stopCurrent)
{
if (RandomNote)
{
if (StartLoopingNote >= EndLoopingNote)
CurrentNote = StartLoopingNote;
else
CurrentNote = UnityEngine.Random.Range(StartLoopingNote, EndLoopingNote);
}
if (RandomDuration)
{
CurrentDuration = UnityEngine.Random.Range(0.1f, 2f);
if (!RandomDelay)
LoopDelay = CurrentDuration;
}
if (RandomDelay)
LoopDelay = UnityEngine.Random.Range(0.01f, 2f);
#if MPTK_PRO
if (FoldOutChord && (ChordPlay || ChordLibPlay || ScaleLibPlay))
{
if (RandomNote)
{
CountNoteChord = UnityEngine.Random.Range(3, 5);
DegreeChord = UnityEngine.Random.Range(1, 8);
CurrentChord = UnityEngine.Random.Range(StartLoopingNote, EndLoopingNote);
}
if (stopCurrent)
MaestroStopChord();
if (ChordPlay)
MaestroPlayOneChord();
if (ChordLibPlay)
MaestroPlayOneChordFromLib();
if (ScaleLibPlay)
MaestroPlayScale();
}
else
#endif
{
if (stopCurrent)
MaestroStopOneNote();
MaestroPlayOneNote();
}
}
#if MPTK_PRO
MPTKChordBuilder ChordPlaying;
MPTKChordBuilder ChordLibPlaying;
private void MaestroPlayScale()
{
MPTKScaleLib scale = MPTKScaleLib.CreateScale((
MPTKScaleName)CurrentScale,
true);
for (int ecart = 0; ecart < scale.Count; ecart++)
{
NotePlaying = new MPTKEvent()
{
Value = CurrentNote + scale[ecart],
Channel = StreamChannel,
Duration = DelayPlayScale,
Velocity = CurrentVelocity,
Delay = ecart * DelayPlayScale,
};
midiStreamPlayer.MPTK_PlayEvent(NotePlaying);
}
}
private void MaestroPlayOneChord()
{
ChordPlaying = new MPTKChordBuilder(true)
{
Tonic = CurrentNote,
Count = CountNoteChord,
Degree = DegreeChord,
Channel = StreamChannel,
Arpeggio = ArpeggioPlayChord,
Duration = Convert.ToInt64(CurrentDuration * 1000f),
Velocity = CurrentVelocity,
Delay = Convert.ToInt64(CurrentDelay * 1000f),
};
midiStreamPlayer.MPTK_PlayChordFromScale(ChordPlaying);
}
private void MaestroPlayOneChordFromLib()
{
ChordLibPlaying = new MPTKChordBuilder(true)
{
Tonic = CurrentNote,
FromLib = CurrentChord,
Channel = StreamChannel,
Arpeggio = ArpeggioPlayChord,
Duration = Convert.ToInt64(CurrentDuration * 1000f),
Velocity = CurrentVelocity,
Delay = Convert.ToInt64(CurrentDelay * 1000f),
};
midiStreamPlayer.MPTK_PlayChordFromLib(ChordLibPlaying);
}
private void MaestroStopChord()
{
if (ChordPlaying != null)
midiStreamPlayer.MPTK_StopChord(ChordPlaying);
if (ChordLibPlaying != null)
midiStreamPlayer.MPTK_StopChord(ChordLibPlaying);
}
#else
private void PlayScale() { }
private void PlayOneChord() { }
private void PlayOneChordFromLib() { }
private void StopChord() { }
#endif
private void MaestroPlayOneNote()
{
NotePlaying = new MPTKEvent()
{
Value = CurrentNote,
Channel = StreamChannel,
Duration = Convert.ToInt64(CurrentDuration * 1000f),
Velocity = CurrentVelocity,
Delay = Convert.ToInt64(CurrentDelay * 1000f),
};
#if MPTK_PRO
for (int i = 0; i < nbrGenerator; i++)
if (indexGenerator[i] >= 0)
NotePlaying.ModifySynthParameter((fluid_gen_type)indexGenerator[i], valueGenerator[i] / 100f, MPTKModeGeneratorChange.Override);
#endif
midiStreamPlayer.MPTK_PlayEvent(NotePlaying);
}
private void MaestroStopOneNote()
{
if (NotePlaying != null)
{
midiStreamPlayer.MPTK_StopEvent(NotePlaying);
NotePlaying = null;
}
}
Description of a MIDI Event. It's the heart of MPTK! Essential to handling MIDI by script from all ot...
Definition: MPTKEvent.cs:45
Singleton class to manage all globales MPTK features. More information here: https://paxstellar....
Definition: MidiPlayerGlobal.cs:16
static bool MPTK_IsReady(float delay=0.5f)
Definition: MidiPlayerGlobal.cs:525
MPTKCommand
Definition: MPTKEnum.cs:12
MPTKScaleName
List of ranges available
Definition: MPTKScaleLib.cs:254