/// <summary>
 /// Read the list of midi events available in the Midi from a ticks position to an end position.
 /// </summary>
 /// <param name="fromTicks">ticks start</param>
 /// <param name="toTicks">ticks end</param>
 /// <returns></returns>
 public List <MPTKEvent> MPTK_ReadMidiEvents(long fromTicks = 0, long toTicks = long.MaxValue)
 {
     if (miditoload == null)
     {
         NoMidiLoaded("MPTK_ReadMidiEvents");
         return(null);
     }
     miditoload.LogEvents   = MPTK_LogEvents;
     miditoload.KeepNoteOff = MPTK_KeepNoteOff;
     return(miditoload.MPTK_ReadMidiEvents(fromTicks, toTicks));
 }
        /// <summary>
        /// It's an optional action before playing a midi file witk MPTK_Play.
        /// Useful to get information (duration, ...) before playing the Midi.
        /// </summary>
        private void GetMidiInfo()
        {
            MidiLoad midiloaded = midiFilePlayer.MPTK_Load();

            if (midiloaded != null)
            {
                infoMidi  = "Duration: " + midiloaded.MPTK_Duration.TotalSeconds + " seconds\n";
                infoMidi += "Tempo: " + midiloaded.MPTK_InitialTempo + "\n";
                List <MPTKEvent> listEvents = midiloaded.MPTK_ReadMidiEvents();
                infoMidi += "Count Midi Events: " + listEvents.Count + "\n";
                Debug.Log(infoMidi);
            }
        }
        /// <summary>
        /// It's an optional action before playing a midi file witk MPTK_Play.
        /// Useful to get information (duration, ...) before playing the Midi.
        /// </summary>
        private void GetMidiInfo()
        {
            MidiLoad midiloaded = midiFilePlayer.MPTK_Load();

            if (midiloaded != null)
            {
                infoMidi  = string.Format("Duration       : {0}\n", midiloaded.MPTK_Duration);
                infoMidi += string.Format("Real Duration: {0}\n", midiloaded.MPTK_RealDuration);
                infoMidi += "Tempo: " + Mathf.RoundToInt((float)midiloaded.MPTK_InitialTempo) + "\n";
                List <MPTKEvent> listEvents = midiloaded.MPTK_ReadMidiEvents();
                infoMidi += "Count Midi Events: " + listEvents.Count + "\n";
                //Debug.Log(infoMidi);
            }
        }
Example #4
0
        void OnGUI()
        {
            if (!HelperDemo.CheckSFExists())
            {
                return;
            }

            // Set custom Style. Good for background color 3E619800
            if (myStyle == null)
            {
                myStyle = new CustomStyle();
            }

            if (butCentered == null)
            {
                butCentered           = new GUIStyle("Button");
                butCentered.alignment = TextAnchor.MiddleCenter;
                butCentered.fontSize  = 16;
            }

            if (labCentered == null)
            {
                labCentered           = new GUIStyle("Label");
                labCentered.alignment = TextAnchor.MiddleCenter;
                labCentered.fontSize  = 16;
            }

            if (MidiLoaded != null)
            {
                scrollerWindow = GUILayout.BeginScrollView(scrollerWindow, false, false, GUILayout.Width(Screen.width));

                // Display popup in first to avoid activate other layout behind
                PopMidi.Draw(MidiPlayerGlobal.MPTK_ListMidi, MidiIndex, myStyle);

                MainMenu.Display("Test Midi File Loader - Demonstrate how to use the MPTK API to load a Midi file", myStyle);

                //
                // Left column: Midi action and info
                // ---------------------------------

                GUILayout.BeginHorizontal();
                GUILayout.BeginVertical(myStyle.BacgDemos, GUILayout.Width(450));

                GUILayout.BeginHorizontal();
                // Open the popup to select a midi
                if (GUILayout.Button("Select And Load Midi File", GUILayout.Height(40)))
                {
                    PopMidi.Show = !PopMidi.Show;
                }
                PopMidi.Position(ref scrollerWindow);

                if (GUILayout.Button(new GUIContent("Read Events", ""), GUILayout.Height(40)))
                {
                    infoEvents = new List <string>();
                    List <MPTKEvent> events = MidiLoaded.MPTK_ReadMidiEvents(StartTicks, EndTicks);
                    foreach (MPTKEvent evt in events)
                    {
                        switch (evt.Command)
                        {
                        case MPTKCommand.NoteOn:
                            infoEvents.Add(string.Format("NoteOn\tChannel:{0:00} \tTick:{1}\tNote:{2}\tDuration:{3,2}\tVelocity:{4}\n",
                                                         evt.Channel, evt.Tick, evt.Value, Math.Round(evt.Duration, 2), evt.Velocity));
                            break;

                        case MPTKCommand.NoteOff:
                            infoEvents.Add(string.Format("NoteOff\tChannel:{0:00} \tTick:{1}\tNote:{2}\tDuration:{3,2}\tVelocity:{4}\n",
                                                         evt.Channel, evt.Tick, evt.Value, Math.Round(evt.Duration, 2), evt.Velocity));
                            break;

                        case MPTKCommand.PatchChange:
                            infoEvents.Add(string.Format("Patch\tChannel:{0:00} \tTick:{1}\tPatch:{2}\n",
                                                         evt.Channel, evt.Tick, evt.Value));
                            break;

                        case MPTKCommand.ControlChange:
                            infoEvents.Add(string.Format("Control\tChannel:{0:00} \tTick:{1}\tValue:{2}\tControler:{3}\n",
                                                         evt.Channel, evt.Tick, evt.Value, evt.Controller));
                            break;

                        case MPTKCommand.MetaEvent:
                            if (!string.IsNullOrEmpty(evt.Info))
                            {
                                infoEvents.Add(string.Format("Text\tChannel:{0:00} \tTick:{1}\tValue:{2}\n",
                                                             evt.Channel, evt.Tick, evt.Info));
                            }
                            break;
                        }
                    }
                }
                GUILayout.EndHorizontal();

                string midiname = "no midi defined";
                if (MidiIndex >= 0 && MidiPlayerGlobal.MPTK_ListMidi != null && MidiIndex < MidiPlayerGlobal.MPTK_ListMidi.Count)
                {
                    midiname = MidiPlayerGlobal.MPTK_ListMidi[MidiIndex].Label;
                }
                GUILayout.Label("Current Midi file: " + midiname, myStyle.TitleLabel3);

                GUILayout.Space(10);
                GUILayout.BeginHorizontal();
                GUILayout.Label("Read Midi Events from: " + StartTicks, myStyle.TitleLabel3, GUILayout.Width(220));
                StartTicks = (long)GUILayout.HorizontalSlider((float)StartTicks, 0f, (float)MidiLoaded.MPTK_TickLast, GUILayout.Width(buttonWidth));
                GUILayout.EndHorizontal();

                GUILayout.Space(10);
                GUILayout.BeginHorizontal();
                GUILayout.Label("Read Midi Events to: " + EndTicks, myStyle.TitleLabel3, GUILayout.Width(220));
                EndTicks = (long)GUILayout.HorizontalSlider((float)EndTicks, 0f, (float)MidiLoaded.MPTK_TickLast, GUILayout.Width(buttonWidth));
                GUILayout.EndHorizontal();

                GUILayout.Space(10);
                GUILayout.BeginHorizontal();
                GUILayout.Label("Duration in seconds: ", myStyle.TitleLabel3, GUILayout.Width(220));
                GUILayout.Label(MidiLoaded.MPTK_Duration.TotalSeconds.ToString(), myStyle.TitleLabel3);
                GUILayout.EndHorizontal();

                GUILayout.Space(10);
                GUILayout.BeginHorizontal();
                GUILayout.Label("Beat per Measure: ", myStyle.TitleLabel3, GUILayout.Width(220));
                GUILayout.Label(MidiLoaded.MPTK_NumberBeatsMeasure.ToString());
                GUILayout.EndHorizontal();

                GUILayout.Space(10);
                GUILayout.BeginHorizontal();
                GUILayout.Label("Quarter per Beat: ", myStyle.TitleLabel3, GUILayout.Width(220));
                GUILayout.Label(MidiLoaded.MPTK_NumberQuarterBeat.ToString(), myStyle.TitleLabel3);
                GUILayout.EndHorizontal();

                GUILayout.Space(10);
                GUILayout.BeginHorizontal();
                GUILayout.Label("MPTK_InitialTempo: ", myStyle.TitleLabel3, GUILayout.Width(220));
                GUILayout.Label(Convert.ToInt32(MidiLoaded.MPTK_InitialTempo).ToString(), myStyle.TitleLabel3);
                GUILayout.EndHorizontal();

                // End left column
                GUILayout.EndVertical();

                if (infoEvents != null && infoEvents.Count > 0)
                {
                    GUILayout.BeginVertical(myStyle.BacgDemos, GUILayout.Width(650));

                    //
                    // Right Column: midi infomation, lyrics, ...
                    // ------------------------------------------
                    GUILayout.BeginHorizontal(myStyle.BacgDemos);

                    if (GUILayout.Button("<<", butCentered, GUILayout.Height(40)))
                    {
                        PageToDisplay = 0;
                    }
                    if (GUILayout.Button("<", butCentered, GUILayout.Height(40)))
                    {
                        PageToDisplay--;
                    }
                    GUILayout.Label("page " + (PageToDisplay + 1).ToString() + " / " + (infoEvents.Count / MAXLINEPAGE + 1).ToString(), labCentered, GUILayout.Width(150), GUILayout.Height(40));
                    if (GUILayout.Button(">", butCentered, GUILayout.Height(40)))
                    {
                        PageToDisplay++;
                    }
                    if (GUILayout.Button(">>", butCentered, GUILayout.Height(40)))
                    {
                        PageToDisplay = infoEvents.Count / MAXLINEPAGE;
                    }

                    GUILayout.EndHorizontal();

                    if (PageToDisplay < 0)
                    {
                        PageToDisplay = 0;
                    }
                    if (PageToDisplay * MAXLINEPAGE > infoEvents.Count)
                    {
                        PageToDisplay = infoEvents.Count / MAXLINEPAGE;
                    }

                    string infoToDisplay = "";
                    for (int i = PageToDisplay * MAXLINEPAGE; i < (PageToDisplay + 1) * MAXLINEPAGE; i++)
                    {
                        if (i < infoEvents.Count)
                        {
                            infoToDisplay += infoEvents[i];
                        }
                    }

                    GUILayout.BeginHorizontal();

                    scrollPos = GUILayout.BeginScrollView(scrollPos, false, false);//, GUILayout.Height(heightLyrics));
                    GUILayout.Label(infoToDisplay, myStyle.TextFieldMultiLine);
                    GUILayout.EndScrollView();

                    GUILayout.EndVertical();
                }
                GUILayout.EndHorizontal();

                GUILayout.EndScrollView();
            }
        }