Hello. I’ve been using the Maestro Midi Player Tool Kit asset’s stream player to play midi notes when keys are pressed on the keyboard, but all of a sudden, calling the event to play notes no longer makes a sound.
The example scene still works as expected, nothing is muted in the game window, scripts or player settings, and I’ve tries restarting the project and my computer.
The code is very simple - it just takes listens for key presses and plays the assigned notes:
using MidiPlayerTK;
using UnityEngine;
public class InstrumentControls : MonoBehaviour
{
public MidiStreamPlayer midiStreamPlayer;
private MPTKEvent NotePlaying;
public int Velocity = 100;
public KeyNotes[] keyNotes;
public int instrument;
void Update()
{
for (int i = 0; i < keyNotes.Length; i++)
{
if (Input.GetKeyDown(keyNotes[i].k))
{
PlayNote(keyNotes[i].n, NotePlaying);
}
}
if (Input.GetKeyDown(KeyCode.RightShift))
{
midiStreamPlayer.MPTK_PlayEvent(
new MPTKEvent()
{
Command = MPTKCommand.PatchChange,
Value = instrument,
Channel = 0,
});
instrument += 1;
}
}
private void PlayNote(int note, MPTKEvent nEvent)
{
//THE CODE GETS TO HERE FINE, BUT BELOW IS THE SECTION THAT DOESN'T WORK
// Start playing a new note
nEvent = new MPTKEvent()
{
Command = MPTKCommand.NoteOn,
Value = note,
Channel = 0,
Duration = 4,
Velocity = Velocity // Sound can vary depending on the velocity
};
midiStreamPlayer.MPTK_PlayEvent(nEvent);
}
}
The code in the demo scene is much larger to create all the GUI, but the function to play a note is pretty much the same:
//! [ExampleMPTK_PlayEvent]
/// <summary>@brief
/// Send the note to the player. Notes are plays in a thread, so call returns immediately.
/// The note is stopped automatically after the Duration defined.
/// </summary>
private void MaestroPlayOneNote()
{
//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(CurrentDuration * 1000f), // millisecond, -1 to play indefinitely
Velocity = CurrentVelocity, // Sound can vary depending on the velocity
Delay = Convert.ToInt64(CurrentDelay * 1000f),
};
midiStreamPlayer.MPTK_PlayEvent(NotePlaying);
}
Sometimes, if the key is pressed right after toggling the mute option in the game window off and on, or straight after hitting play, one note will actually play, but no more.
Is this an issue anyone else has had or knows a fix for?
My code:
Inspector view:
Demo scene (works fine):