Maestro Stream Player stopped playing audio

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:

Demo code:
Inspector view:
Demo scene (works fine):

I have found a fix to the issue, although I am not quite sure what the problem actually was. Based on some more of the demo code, this is the updated and now working code:

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))
            {
                // Create a new note and play
                keyNotes[i].e = new MPTKEvent()
                {
                    Command = MPTKCommand.NoteOn,
                    Channel = 0, // From 0 to 15
                    Duration = -1, // Infinite, note-off when key is released, see bellow.
                    Value = keyNotes[i].n,
                    Velocity = 100
                };

                // Send the note-on MIDI event to the MIDI synth
                midiStreamPlayer.MPTK_PlayEvent(keyNotes[i].e);
            }
            // If the note is active and the corresponding key is released then stop the note
            if (keyNotes[i].e != null && Input.GetKeyUp(keyNotes[i].k))
            {
                midiStreamPlayer.MPTK_StopEvent(keyNotes[i].e);
                keyNotes[i].e = null;
            }
        }


        if (Input.GetKeyDown(KeyCode.RightShift))
        {
            midiStreamPlayer.MPTK_PlayEvent(
            new MPTKEvent()
            {
                Command = MPTKCommand.PatchChange,
                Value = instrument,
                Channel = 0,
            });
            instrument += 1;
        }

    }

So was the unspecified duration the problem?

The problem seems to be to do with the duration, yes, but even setting it to be something like 4 seconds still doesn’t make a sound.

I wonder if it may be to do with how it is being called if it takes a certain amount of time to run?

You should reach to the package owner to clarify this. I’m subscribed to his stuff and updates are periodically coming in, he’s definitely actively maintaining it.