Search Unity

Maestro Midi Player Tool Kit - Good news for your rhythm game !

Discussion in 'Made With Unity' started by BachmannT, Apr 14, 2018.

  1. franMx

    franMx

    Joined:
    May 27, 2013
    Posts:
    30
    Thanks, unfortunately still having problems. I didn't include library, but it's the free version (seems 2.88). Scene name is test and I have 2 gameObjects, test1 and test2. on test2. when key 1 is pressed should call play, but not workign (at least missing object is now gone, made a new unity project).
     

    Attached Files:

  2. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    Hello
    I have an answer ;-)
    void Update()
    {
    if (Input.GetKeyDown(KeyCode.Alpha1))
    {
    Debug.Log("playing note");
    // Start playing a new note
    MPTKEvent NotePlaying = new MPTKEvent()
    {
    Command = MPTKCommand.NoteOn,
    Value = CurrentNote,
    Channel = StreamChannel,
    Duration = 9999999, // 9999 seconds but stop by the new note. See before.
    Velocity = Velocity // Sound can vary depending on the velocity
    };
    midiStreamPlayer.MPTK_PlayEvent(NotePlaying);


    if (NotePlaying != null)
    {
    // Stop the note (method to simulate a real human on a keyboard : duration is not known when note is triggered)
    midiStreamPlayer.MPTK_StopEvent(NotePlaying);
    NotePlaying = null;
    }

    }
    }

    It's because you cancel the event (MPTK_StopEvent) just after creating it!

    Thierry
     
    franMx likes this.
  3. franMx

    franMx

    Joined:
    May 27, 2013
    Posts:
    30
    Thanks a lot!
     
  4. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    I always like talking, It's good for stimulate idea :)
    I added to the demo TestMidiStream this short script: able to play the blues notes with a few keys. Playing chord is also possible.
    Code (CSharp):
    1.    
    2.         private MPTKEvent[] eventsMidi;
    3.         // blues en C minor: C, D#, F, F#, G, A#
    4.         private int[] keysToNote = { 60, 63, 65, 66, 67, 70, 72, 75, 77 };
    5.  
    6.         void Update()
    7.         {
    8.             // Check that SoundFont is loaded
    9.             if (!MidiPlayerGlobal.MPTK_IsReady())
    10.                 return;
    11.  
    12.             // Better in Start(), it's here only for demo clarity
    13.             if (eventsMidi == null)
    14.                 eventsMidi = new MPTKEvent[10];
    15.             for (int key = 0; key < 9; key++)
    16.             {
    17.                 // Check if key 1 to 9 is down (top alpha keyboard)
    18.                 if (Input.GetKeyDown(KeyCode.Alpha1 + key))
    19.                 {
    20.                     // Create a new note and play
    21.                     eventsMidi[key] = new MPTKEvent()
    22.                           { Command = MPTKCommand.NoteOn,
    23.                             Channel = StreamChannel,
    24.                             Duration = -1,
    25.                             Value = keysToNote[key], Velocity = 100 };
    26.                     midiStreamPlayer.MPTK_PlayEvent(eventsMidi[key]);
    27.                 }
    28.  
    29.                 // Check if key 1 to 9 is up to stop the notes if corresponding note is playing
    30.                 if (Input.GetKeyUp(KeyCode.Alpha1 + key) && eventsMidi[key] != null)
    31.                 {
    32.                     midiStreamPlayer.MPTK_StopEvent(eventsMidi[key]);
    33.                     eventsMidi[key] = null;
    34.                 }
    35.             }
    36.     }
     
    franMx likes this.
  5. standardcombo

    standardcombo

    Joined:
    Jun 28, 2012
    Posts:
    19
    Hey folks. This MIDI player looks very promising. However, I imported it into a blank project and no sound comes out. Any ideas? Unity v2019.4.x

    I looked at all the setup, quickstart, readme and nothing. No errors or warnings in console.

    I've tried all the demo scenes that come with the import, as well as a blank scene with the player prefab added. It looks like it's playing and if I enable logging, I get info about notes. There is just, no sound. If I select the source wav assets and hit play I can hear them fine and I'm not having any other audio issues elsewhere.

    Thanks.
     
  6. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    Hello
    Does the audio setting look like this ?
    upload_2021-11-30_8-28-42.png
     
  7. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    Have you find a solution ?
     
  8. standardcombo

    standardcombo

    Joined:
    Jun 28, 2012
    Posts:
    19
    DPC Buffer Size was set to "Best Performance". I've changed it to "Default" and nothing improved.
    Here's the Hierarchy + Inspector.
    This time I tried the "ScenesDemonstration" and the choice "Simply Play MIDI"
    upload_2021-12-1_20-30-10.png
     
  9. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    I don't think that the issue comes from Maestro, all the setup are OK.

    Sometimes there is an issue with Unity and Audio depending on the audio card.
    There is a lot of issues describe on the web. Search "unity no sound" .
    It's tricky to correct, the more simple is to reinstall a newer version of Unity.
    What is your current Unity version and which OS ?
     
  10. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    there is also a common mistake.
    Check you don't have "Mute Audio" enabled:
     
  11. GiovanniHDZ

    GiovanniHDZ

    Joined:
    Jan 31, 2019
    Posts:
    57
    Hi!
    I'm testing your asset, and works like a charm!
    Be perfect, however, I would like to try something by adjusting the speed of the midi tempo.

    I export it from ableton live, where the session has a tempo of 103, and when importing it the "initial tempo" is 120, how can I change those values?

    Edit:
    I've modified the speed value, considering that 120 bpm = speed 1
    So 103 bpm = 0.858333 ...
    And it works, but the note generation is a little faster each time and the beat/rythm is lost, so I would like to try with the bpm set in the midi file to keep the velocity at 1 and use the 103 bpm along with the track of audio (song) just like how it was created.

    ------
    Explanation: What am I doing?

    In a project of ableton live I have motado a song
    I set the bpm in the project according to the song.
    I use a keyboard to play the song and generate the midi.
    I export the midi.


    In unity, I put the song and the midi
    I play the song with the midi event - OnEventStartPlayMidi
    Everything works, I always have the same rhythm at the beginning
    But it always gets lost because the notes are generated a little faster, it is as if the midi has a little more bpm.

    I hope you can help me :)
    If not I'll try it on my own, great asset!
     
    Last edited: Dec 3, 2021
  12. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    Hello
    Perhaps the change tempo event has not been inserted in the Midi by ableton ?
    You can check with the MIDI importer:
    upload_2021-12-3_12-13-17.png

    Regards
    Thierry
     
  13. GiovanniHDZ

    GiovanniHDZ

    Joined:
    Jan 31, 2019
    Posts:
    57
    Hi, thanks for the awnser!

    I have looked at the midi file and it does not contain that set tempo line.
    is not visible in the midi file configuration window
     
  14. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    you have to open a case to ableton ;-)
     
    GiovanniHDZ likes this.
  15. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    Im' lost, it's a newer question in your edit ?
     
  16. Game-Whiz

    Game-Whiz

    Joined:
    Nov 10, 2011
    Posts:
    122
    Hi,

    I just bought Maestro, quickly tested in on my Mac and it works beautifully.

    However when I deploy any of the examples (for example, TestMidiFilePlayer) to my iPhone, there's no sound. Is there any additional step I must take to deploy to an iPhone?

    Thanks!
     
  17. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    Hello, thank you for your purchase!
    No, nothing more to do for deploying to an iPhone.
    Is the app run ? can you see the midi bar moving ?
    Have you sound from Unity app with classical Audio Source playing ?
     
  18. HEATH3N

    HEATH3N

    Joined:
    Jul 19, 2014
    Posts:
    17
    Hello,

    I'm having an odd situation with the following code:

    Code (CSharp):
    1. private void Start() {
    2.     if (!midiFileLoader.MPTK_Load()) throw new Exception("Could not load MIDI file");
    3.  
    4.     Debug.Log(midiFileLoader.MPTK_TrackCount);
    5. }
    In the MidiFilePlayer component I have MIDI file #30 selected, which has 4 tracks. But when I run the game, the log statement outputs 21 tracks, which is the number that MIDI file #1 has. No matter which file I use, it always gives me the track count for the first file.

    I recently upgraded to the latest version of the asset so I'm wondering if the MIDI database got messed up in the process and it's defaulting to the first file because it can't find the others. I've tried reselecting/deleting and re-adding files but it hasn't helped.
     
  19. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    Hello, hard to understand your concern, but what I can said is that: don't forget that with Unity the order of start of each gameObject is not obvious. So, perhaps, in your start() method, the MidiFileplayer gameobject is not yet full started. It's why there is method in Maestro like OnEventStartPlayMidi.
     
  20. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    Try this also:
    Code (CSharp):
    1.             MidiLoad midiLoaded = midiFilePlayer.MPTK_Load();
    2.             if (midiLoaded == null) throw new Exception("Could not load MIDI file");
    3.             Debug.Log(midiLoaded.MPTK_TrackCount);
    4.  
    MidiLoad class is only for reading MIDI events not playing.

    Regards
    Thierry
     
  21. cihadturhan_unity

    cihadturhan_unity

    Joined:
    Apr 19, 2021
    Posts:
    65
    Hey @BachmannT
    Your midiplayer asset is amazing. I'm surprised how much you give free!

    Please let me solve this issue:
    When I load a midi file with
    Code (CSharp):
    1. midiFilePlayer.MPTK_Load()
    , I can't see channel and preset info, all presets are 0 before playing. During play I can see preset is set a value such as 67.

    I want to see this information before I start playing. Is it possible?
     
  22. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    Hello
    I'm happy if you like it :)
    For your question: presets by channel can be changed by MIDI events all along of a MIDI playing. So there is not real "initial" state.
    Nevertheless, often all presets change are defined at the start of the MIDI and never change after.
    Bellow for this MIDI, all presets are defined at the start.
    upload_2022-1-21_18-10-31.png

    One solution for you, woule be to use the function midifileplayer.MPTK_ReadMidiEvents()
    https://mptkapi.paxstellar.com/d7/d...player.html#a9de67ba378c573bd84a24edb3b7a2fd7

    and search MIDI event PatchChange in the MPTKEvent object
    https://mptkapi.paxstellar.com/da/d...er_t_k.html#a7a4f887433d0dc42a02060d2ed63e6c0

    Regards
    Thierry
     
  23. cihadturhan_unity

    cihadturhan_unity

    Joined:
    Apr 19, 2021
    Posts:
    65
    Thank you for a quick answer. Now I understand how midi works and it makes sense.
    I think I will try this solution and implement a function to read channels and presets before play.

    Relevant question: Is there a way to update channel preset before Play? Or, should I play and pause immediately after meta events to update presets?
    Thanks

    Edit: Never mind, I figured it out. I change patch value from event so it works!
     
    Last edited: Jan 22, 2022
  24. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    For sure, I often have questions about MIDI norm and how it works. There is a lot of sites which explains MIDI but sometimes too much complex or time consuming to read. Say me if you found something cool!
     
    Last edited: Jan 22, 2022
  25. cihadturhan_unity

    cihadturhan_unity

    Joined:
    Apr 19, 2021
    Posts:
    65
    Will do for sure!

    I have another question if you don't mind. How can I make midi player respect timescale? Right now, when timescale is 0.5, it still plays in full-speed. I see you're using `RealTimeUpdate` enum, is it enough to change that?

    Code (CSharp):
    1. Routine.RunCoroutine(midiFilePlayer.ThreadInternalMidiPlaying(midiFilePlayer.MPTK_MidiName, 0, 0)
    2.             .CancelWith(midiFilePlayer), Segment.RealtimeUpdate);
     
    BachmannT likes this.
  26. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    Hello
    Easy, use this method in your MidiFilePlayer objet (or Speed from the inspector)
    https://mptkapi.paxstellar.com/d7/d...player.html#aa0e5778d510851909db48e717dc401c4

    Regards
    Thierry
     
    cihadturhan_unity likes this.
  27. Hunkofsteel

    Hunkofsteel

    Joined:
    May 13, 2015
    Posts:
    26
    Hi, this package is very nice, but I seem to have encountered pop/click sounds when velocity is one. Furthermore, the volume difference between the NoteOn events with a velocity of 2 compared to 127 is relatively small; is there a way to widen that difference?

    Is there a way to make lower velocities quieter than they already are?
     
    UnableImp likes this.
  28. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    Hi
    So, are you using MidiStreamPlayer to generate MIDI event ? I checked with the demo TestMidiStream I found the same issue with low velocity. Expression and Attenuation can be used to lower the volume.
    upload_2022-1-28_1-59-9.png
     
  29. Hunkofsteel

    Hunkofsteel

    Joined:
    May 13, 2015
    Posts:
    26
    Hi,
    Yep, I'm using MidiStreamPlayer to generate MIDI events. The expression and attenuation are exactly what I need for the volume difference! I figure for now I'll remove the case when velocity =1, but my friend suspects it has to do with the synth.
     
  30. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    and your friend is right! There is something strange when velocity=1, I will inspect this point ASAP, but not before next week, I'm overwhelmed :(

    Regards
     
  31. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    767
    I have a suggestion:

    You can a script importer that import midi files Midifile, and you would make MidiFile extend ScriptableObject.
    This has several advantages:
    * You completely avoid the parsing overhead at runtime. All parsing is done at compile time.
    * Midis keep their .mid extension
    * No need for midiDB at all, you would just drag a midi straight into MidiFilePlayer
     
  32. brunoofvr

    brunoofvr

    Joined:
    Sep 3, 2021
    Posts:
    1
    Hello, and thank you for developing this wonderful package.
    Unfortunately, no matter the demo I try, I'm getting a IndexOutOfRangeException at

    MidiPlayerTK.MidiSynth.OnAudioFilterRead (System.Single[] data, System.Int32 channels) (at Assets/MidiPlayer/Scripts/MPTKGameObject/MidiSynth.cs:3049)

    even in a fresh new project.
    Any idea on the cause of it?

    Regards,
     
    wedgiebee likes this.
  33. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    Hello, no sorry, I have any idea !
    Have you load another SoudFont or other MIDI than the demo ?
    Maestro pro & free are tested on MacOS and Windows with all Unity versions. Nothing like this has been detected.
    Could you try to download Maestro from Unity with a new project ?
     
    Last edited: Feb 25, 2022
  34. cihadturhan_unity

    cihadturhan_unity

    Joined:
    Apr 19, 2021
    Posts:
    65
    Hi @BachmannT,
    I really like your asset and considering to buy pro plugin for one feature: in-game midi editor/sequencer.
    I see there is a demo scene in pro version for midi sequencer:
    upload_2022-3-15_16-15-19.png
    Any chance to share a video or screenshots of the sequencer.
    Thanks!
     
  35. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    Hello, Thank for your interest. I'm sorry to be disappointing, but there is no feature "in-game midi editor/sequencer"
    It's only a demonstration how to use Maestro API to generate MIDI music.
    There is so good sequencer for free, why you should build it?

    Below a screenshot.
    upload_2022-3-16_7-6-41.png
    Regards
    Thierry
     

    Attached Files:

    cihadturhan_unity likes this.
  36. rmacgillis

    rmacgillis

    Joined:
    Feb 5, 2014
    Posts:
    15
    Hi, I'm using the StreamPlayer for an Android App. It's a guitar app where users can play notes, scales, chords, and phrases.

    So I use the Play method. There's 2 scenes. If one is for phrases, the other for all else. If either scene is loaded on start, the MidiStreamer works fine. However, if I go from one scene to the next, audio won't play.

    I have an app that allows me to view warnings/errors in the Android App. No errors. So I'm not sure what's wrong.

    I do get this warning "Could not find a part of the path "data/app/com/MGSoft/GRefP-t
    Cm3B_OAEBcC63qN*nQJ1Q==/base.apk/MidiPlayer/Resources/MidiSet.txt" - However this warning shows up and it will still play (unless switching scenes)

    Only other warning I saw once was about the SoundFont not being loaded after switching scenes and playing a note. But it doesn't always show up.
    I put a method in to check if it was loaded after several seconds and if false, to load the SF. But had no effect.

    ?
     
  37. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    Hello, have you also the case when switching scene in the Unity editor ?
     
  38. rmacgillis

    rmacgillis

    Joined:
    Feb 5, 2014
    Posts:
    15
    No, only in the app
     
    Last edited: Mar 17, 2022
  39. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    Have you the possibility to share with me a light project to reproduce this issue?
     
  40. rmacgillis

    rmacgillis

    Joined:
    Feb 5, 2014
    Posts:
    15
    Well, I created a new basic project last night: 2 scenes. Each with a button to play a note through the Streamer. A button to load each level. And a few buttons to load Soundfonts. Had no audio issues.

    If switching Soundfonts I do get the warning "Could not find a part of the path "data/app/com/MGSoft/GRefP-t
    Cm3B_OAEBcC63qN*nQJ1Q==/base.apk/MidiPlayer/Resources/MidiSet.txt" in the Android App, but the SF still plays.

    I guess I'll try some process of elimination with the main project, see if there's a script that's interfering. Unless, you know of some way to force the audio to play.
     
    Last edited: Mar 17, 2022
  41. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    Try something like that when switching: midiFilePlayer.MPTK_InitSynth()
    With no garantee!
    Share your basic project (if you can reproduce the issue), I could have a look.
     
    Last edited: Mar 18, 2022
  42. cihadturhan_unity

    cihadturhan_unity

    Joined:
    Apr 19, 2021
    Posts:
    65
    Thank you!
    I thought there was a timeline that you can add and edit notes (like GarageBand app)
    I’ll check how I can do that.
     
  43. rmacgillis

    rmacgillis

    Joined:
    Feb 5, 2014
    Posts:
    15
    Can't reproduce the issue in project yet, but will try. And I can only use the Streamer since there's note passages that can get up to 200bpm and the Streamer is the only thing that doesn't distort notes at that speed.
     
  44. CDAfonsoCl

    CDAfonsoCl

    Joined:
    May 18, 2021
    Posts:
    17
    Hello @BachmannT, thanks for this great library!

    I'm having this issue using the Midi External Player with some midi-files:

    MPTK_DurationsMS is not returning the correct value (see animated gif):

    Midi File used: https://we.tl/t-VB6yDeYeKd


    Thank you in advance.

    Best Regards.
     

    Attached Files:

  45. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    Hello,
    There is no doubt, this behavior is strange!
    The issue is also when playing with the MIDI from the MIDIDB
    It seems there is some weird tempo change at the end which affect the total duration time.
    upload_2022-3-23_20-46-28.png

    A simple solution is to disable tempo change. I'm not sure I can do better.
    upload_2022-3-23_20-52-18.png


    All my best
    Thierry
     
  46. CDAfonsoCl

    CDAfonsoCl

    Joined:
    May 18, 2021
    Posts:
    17
    That is the problem indeed. For other midi-files i need to keep the tempo-change active, but I've deleted the last part of the music sheet, after the tempo change (It wasn't really needed). Now the duration is 100% correct.

    Thank you for all your help Thierry!

    Wish you the best.
    C.A.
     
    Last edited: Mar 24, 2022
  47. Hunkofsteel

    Hunkofsteel

    Joined:
    May 13, 2015
    Posts:
    26
    Hi,
    I'd like to understand what is the MRTK Pulse Length is exactly. I'm trying to create a basic metronome from my MIDI file, and I'm playing a 120 tempo 4/4 music (Mary Had A Little Lamb).

    In this case, the Quaterters per Minute is 120, which means the quarter per second is 120 / 60. To get seconds per quarter I take the inverse, giving 60 / 120 = 0.5.

    Pulse length in this scenario is giving me 0.52xxxxxx

    I'm trying to understand if my math is wrong or pulse length is just not what I think it is.
     
  48. CDAfonsoCl

    CDAfonsoCl

    Joined:
    May 18, 2021
    Posts:
    17
    @BachmannT, quick question:
    Is it possible to play a midi file in the midi database trough the external midi player?

    Thank you!
     
  49. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    Yes!
    upload_2022-4-2_9-25-12.png

    But only for playing in the Unity editor. Don't forget, when compiling for execute out of Unity all resources will be packaged in a compressed file and the path are not valid ... it's why there is a MidiDB in Maestro.

    Regards
    Thierry
     
    Last edited: Apr 2, 2022
    CDAfonsoCl likes this.
  50. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    387
    https://en.wikipedia.org/wiki/Pulses_per_quarter_note
    There is an error in the Maestro documentation, MPTK_PulseLength is the length in millisecond of a tick. Obviously depends on the current tempo and the ticks per quarter.
    Calculation:
    MPTK_PulseLenght = tempo / DeltaTicksPerQuarterNote / 1000
    So the Pulse length is the minimum time in millisecond between two MIDI events, it's more a level of resolution: the MIDI sequencer will not be able to play two separate events in a time below this value.

    Also here some interesting information:
    https://sites.uci.edu/camp2014/2014/05/19/timing-in-midi-files/

    I will correct the doc asap and add this information in the "Play Midi With Script" demo:

    upload_2022-4-2_9-51-0.png


    Thank for remarks which help to make Maestro better.
    Regards
    Thierry
     

    Attached Files:

    Last edited: Apr 2, 2022
    Hunkofsteel likes this.