Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Bug Maestro Stream Player stopped playing audio

Discussion in 'Audio & Video' started by EpicCodez, Apr 24, 2024.

  1. EpicCodez

    EpicCodez

    Joined:
    Jan 30, 2020
    Posts:
    6
    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:

    Code (CSharp):
    1.  
    2. using MidiPlayerTK;
    3. using UnityEngine;
    4. public class InstrumentControls : MonoBehaviour
    5. {
    6.     public MidiStreamPlayer midiStreamPlayer;
    7.     private MPTKEvent NotePlaying;
    8.     public int Velocity = 100;
    9.  
    10.     public KeyNotes[] keyNotes;
    11.     public int instrument;
    12.  
    13.     void Update()
    14.     {
    15.  
    16.         for (int i = 0; i < keyNotes.Length; i++)
    17.         {
    18.             if (Input.GetKeyDown(keyNotes[i].k))
    19.             {
    20.                 PlayNote(keyNotes[i].n, NotePlaying);
    21.             }
    22.         }
    23.  
    24.  
    25.         if (Input.GetKeyDown(KeyCode.RightShift))
    26.         {
    27.             midiStreamPlayer.MPTK_PlayEvent(
    28.             new MPTKEvent()
    29.             {
    30.                 Command = MPTKCommand.PatchChange,
    31.                 Value = instrument,
    32.                 Channel = 0,
    33.             });
    34.             instrument += 1;
    35.         }
    36.  
    37.     }
    38.  
    39.     private void PlayNote(int note, MPTKEvent nEvent)
    40.     {
    41.         //THE CODE GETS TO HERE FINE, BUT BELOW IS THE SECTION THAT DOESN'T WORK
    42.         // Start playing a new note
    43.         nEvent = new MPTKEvent()
    44.         {
    45.             Command = MPTKCommand.NoteOn,
    46.             Value = note,
    47.             Channel = 0,
    48.             Duration = 4,
    49.             Velocity = Velocity // Sound can vary depending on the velocity
    50.         };
    51.         midiStreamPlayer.MPTK_PlayEvent(nEvent);
    52.     }
    53. }
    54.  

    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:

    Code (CSharp):
    1.    
    2. //! [ExampleMPTK_PlayEvent]
    3. /// <summary>@brief
    4. /// Send the note to the player. Notes are plays in a thread, so call returns immediately.
    5. /// The note is stopped automatically after the Duration defined.
    6. /// </summary>
    7. private void MaestroPlayOneNote()
    8.  {
    9.             //Debug.Log($"{StreamChannel} {midiStreamPlayer.MPTK_ChannelPresetGetName(StreamChannel)}");
    10.             // Start playing a new note
    11.             NotePlaying = new MPTKEvent()
    12.             {
    13.                 Command = MPTKCommand.NoteOn,
    14.                 Value = CurrentNote, // note to played, ex 60=C5. Use the method from class HelperNoteLabel to convert to string
    15.                 Channel = StreamChannel,
    16.                 Duration = Convert.ToInt64(CurrentDuration * 1000f), // millisecond, -1 to play indefinitely
    17.                 Velocity = CurrentVelocity, // Sound can vary depending on the velocity
    18.                 Delay = Convert.ToInt64(CurrentDelay * 1000f),
    19.             };
    20.             midiStreamPlayer.MPTK_PlayEvent(NotePlaying);
    21.         }
    22.  

    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:
    Screenshot 2024-04-24 at 07.48.35.png Demo code:
    Screenshot 2024-04-24 at 07.48.42.png Inspector view:
    Screenshot 2024-04-24 at 07.49.03.png Demo scene (works fine):
    Screenshot 2024-04-24 at 07.49.35.png
     
    Last edited: Apr 24, 2024
  2. EpicCodez

    EpicCodez

    Joined:
    Jan 30, 2020
    Posts:
    6
    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:
    Code (CSharp):
    1. public class InstrumentControls : MonoBehaviour
    2. {
    3.     public MidiStreamPlayer midiStreamPlayer;
    4.     private MPTKEvent NotePlaying;
    5.     public int Velocity = 100;
    6.  
    7.     public KeyNotes[] keyNotes;
    8.     public int instrument;
    9.  
    10.     void Update()
    11.     {
    12.  
    13.         for (int i = 0; i < keyNotes.Length; i++)
    14.         {
    15.             if (Input.GetKeyDown(keyNotes[i].k))
    16.             {
    17.                 // Create a new note and play
    18.                 keyNotes[i].e = new MPTKEvent()
    19.                 {
    20.                     Command = MPTKCommand.NoteOn,
    21.                     Channel = 0, // From 0 to 15
    22.                     Duration = -1, // Infinite, note-off when key is released, see bellow.
    23.                     Value = keyNotes[i].n,
    24.                     Velocity = 100
    25.                 };
    26.  
    27.                 // Send the note-on MIDI event to the MIDI synth
    28.                 midiStreamPlayer.MPTK_PlayEvent(keyNotes[i].e);
    29.             }
    30.             // If the note is active and the corresponding key is released then stop the note
    31.             if (keyNotes[i].e != null && Input.GetKeyUp(keyNotes[i].k))
    32.             {
    33.                 midiStreamPlayer.MPTK_StopEvent(keyNotes[i].e);
    34.                 keyNotes[i].e = null;
    35.             }
    36.         }
    37.  
    38.  
    39.         if (Input.GetKeyDown(KeyCode.RightShift))
    40.         {
    41.             midiStreamPlayer.MPTK_PlayEvent(
    42.             new MPTKEvent()
    43.             {
    44.                 Command = MPTKCommand.PatchChange,
    45.                 Value = instrument,
    46.                 Channel = 0,
    47.             });
    48.             instrument += 1;
    49.         }
    50.  
    51.     }
     
  3. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    454
    So was the unspecified duration the problem?
     
  4. EpicCodez

    EpicCodez

    Joined:
    Jan 30, 2020
    Posts:
    6
    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?
     
  5. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    454
    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.