protected IEnumerator <float> ThreadPlay(byte[] midiBytesToPlay = null)
        {
            midiIsPlaying = true;
            stopMidi      = false;
            replayMidi    = false;
            bool   first           = true;
            string currentMidiName = "";

            //Debug.Log("Start play");
            try
            {
                miditoplay = new MidiLoad();

                // No midi byte array, try to load from MidiFilesDN from resource
                if (midiBytesToPlay == null || midiBytesToPlay.Length == 0)
                {
                    currentMidiName = MPTK_MidiName;
                    TextAsset mididata = Resources.Load <TextAsset>(Path.Combine(MidiPlayerGlobal.MidiFilesDB, currentMidiName));
                    midiBytesToPlay = mididata.bytes;
                }

                miditoplay.KeepNoteOff = MPTK_KeepNoteOff;
                miditoplay.MPTK_Load(midiBytesToPlay);
            }
            catch (System.Exception ex)
            {
                MidiPlayerGlobal.ErrorDetail(ex);
            }

            if (miditoplay != null)
            {
                yield return(Timing.WaitUntilDone(Timing.RunCoroutine(ThreadClearAllSound(true)), false));

                try
                {
                    OnEventStartPlayMidi.Invoke(currentMidiName);
                }
                catch (System.Exception ex)
                {
                    MidiPlayerGlobal.ErrorDetail(ex);
                }

                try
                {
                    miditoplay.ChangeSpeed(MPTK_Speed);
                    miditoplay.ChangeQuantization(MPTK_Quantization);
                }
                catch (System.Exception ex)
                {
                    MidiPlayerGlobal.ErrorDetail(ex);
                }

                lastTimePlay      = Time.realtimeSinceStartup;
                timeFromStartPlay = 0d;
                // Loop on each events midi
                do
                {
                    miditoplay.LogEvents = MPTK_LogEvents;

                    if (MPTK_PauseOnDistance)
                    {
                        distanceEditorModeOnly = MidiPlayerGlobal.MPTK_DistanceToListener(this.transform);
                        if (distanceEditorModeOnly > VoiceTemplate.Audiosource.maxDistance)
                        {
                            lastTimePlay = Time.realtimeSinceStartup;
                            yield return(Timing.WaitForSeconds(0.2f));

                            continue;
                        }
                    }

                    if (playPause)
                    {
                        lastTimePlay = Time.realtimeSinceStartup;
                        yield return(Timing.WaitForSeconds(0.2f));

                        if (miditoplay.EndMidiEvent || replayMidi || stopMidi)
                        {
                            break;
                        }
                        if (timeToPauseMilliSeconde > -1f)
                        {
                            timeToPauseMilliSeconde -= 0.2f;
                            if (timeToPauseMilliSeconde <= 0f)
                            {
                                playPause = false;
                            }
                        }
                        continue;
                    }

                    if (!first)
                    {
                        timeFromStartPlay += (Time.realtimeSinceStartup - lastTimePlay) * 1000d;
                    }
                    else
                    {
                        timeFromStartPlay = 0d;
                        first             = false;
                    }

                    //Debug.Log("---------------- " + timeFromStartPlay );
                    // Read midi events until this time
                    List <MPTKEvent> midievents = miditoplay.ReadMidiEvents(timeFromStartPlay);

                    if (miditoplay.EndMidiEvent || replayMidi || stopMidi)
                    {
                        break;
                    }

                    // Play notes read
                    if (midievents != null && midievents.Count > 0)
                    {
                        try
                        {
                            if (OnEventNotesMidi != null)
                            {
                                OnEventNotesMidi.Invoke(midievents);
                            }
                        }
                        catch (System.Exception ex)
                        {
                            MidiPlayerGlobal.ErrorDetail(ex);
                        }

                        //float beforePLay = Time.realtimeSinceStartup;
                        //Debug.Log("---------------- play count:" + midievents.Count + " Start:" + timeFromStartPlay + " Delta:" + (Time.realtimeSinceStartup - lastTimePlay) * 1000f);
                        if (MPTK_DirectSendToPlayer)
                        {
                            foreach (MPTKEvent midievent in midievents)
                            {
                                if (midievent.Command == MPTKCommand.MetaEvent && midievent.Meta == MPTKMeta.SetTempo && MPTK_EnableChangeTempo)
                                {
                                    miditoplay.ChangeTempo(midievent.Duration);
                                    //Debug.Log(BuildInfoTrack(trackEvent) + string.Format("SetTempo   {0} MicrosecondsPerQuarterNote:{1}", tempo.Tempo, tempo.MicrosecondsPerQuarterNote));
                                }
                                else
                                {
                                    PlayEvent(midievent);
                                }
                            }
                        }
                        //Debug.Log("---------------- played count:" + notes.Count + " Start:" + timeFromStartPlay + " Delta:" + (Time.realtimeSinceStartup - lastTimePlay) * 1000f + " Elapsed:" + (Time.realtimeSinceStartup - beforePLay) * 1000f);
                    }

                    lastTimePlay = Time.realtimeSinceStartup;

                    if (Application.isEditor)
                    {
                        TimeSpan times = TimeSpan.FromMilliseconds(timeFromStartPlay);
                        playTimeEditorModeOnly = string.Format("{0:00}:{1:00}:{2:00}:{3:000}", times.Hours, times.Minutes, times.Seconds, times.Milliseconds);
                        durationEditorModeOnly = string.Format("{0:00}:{1:00}:{2:00}:{3:000}", MPTK_Duration.Hours, MPTK_Duration.Minutes, MPTK_Duration.Seconds, MPTK_Duration.Milliseconds);
                    }

                    yield return(-1);// new WaitForSeconds(delayMilliSeconde / 1000f);// 0.01f);
                }while (true);
            }
            else
            {
                Debug.LogWarning("MidiFilePlayer/ThreadPlay - Midi Load error");
            }

            midiIsPlaying = false;

            try
            {
                EventEndMidiEnum reason = EventEndMidiEnum.MidiEnd;
                if (nextMidi)
                {
                    reason   = EventEndMidiEnum.Next;
                    nextMidi = false;
                }
                else if (prevMidi)
                {
                    reason   = EventEndMidiEnum.Previous;
                    prevMidi = false;
                }
                else if (stopMidi)
                {
                    reason = EventEndMidiEnum.ApiStop;
                }
                else if (replayMidi)
                {
                    reason = EventEndMidiEnum.Replay;
                }
                OnEventEndPlayMidi.Invoke(currentMidiName, reason);

                if ((MPTK_Loop || replayMidi) && !stopMidi)
                {
                    MPTK_Play();
                }
                //stopMidiToPlay = false;
            }
            catch (System.Exception ex)
            {
                MidiPlayerGlobal.ErrorDetail(ex);
            }
            //Debug.Log("Stop play");
        }
Exemple #2
0
        protected IEnumerator ThreadPlay(byte[] midibytestoplay = null)
        {
            midiIsPlaying  = true;
            stopMidiToPlay = false;
            newMidiToPlay  = false;
            bool first = true;

            //Debug.Log("Start play");
            try
            {
                miditoplay = new MidiLoad();

                // No midi byte array, try to load from MidiFilesDN from resource
                if (midibytestoplay == null || midibytestoplay.Length == 0)
                {
                    TextAsset mididata = Resources.Load <TextAsset>(Path.Combine(MidiPlayerGlobal.MidiFilesDB, MPTK_MidiName));
                    midibytestoplay = mididata.bytes;
                }

                miditoplay.KeepNoteOff = MPTK_KeepNoteOff;
                miditoplay.Load(midibytestoplay);
            }
            catch (System.Exception ex)
            {
                MidiPlayerGlobal.ErrorDetail(ex);
            }

            if (miditoplay != null)
            {
                yield return(StartCoroutine(MPTK_ClearAllSound(true)));

                try
                {
                    OnEventStartPlayMidi.Invoke();
                }
                catch (System.Exception ex)
                {
                    MidiPlayerGlobal.ErrorDetail(ex);
                }

                try
                {
                    miditoplay.ChangeSpeed(MPTK_Speed);
                    miditoplay.ChangeQuantization(MPTK_Quantization);
                }
                catch (System.Exception ex)
                {
                    MidiPlayerGlobal.ErrorDetail(ex);
                }

                lastTimePlay      = Time.realtimeSinceStartup;
                timeFromStartPlay = 0d;
                // Loop on each events midi
                do
                {
                    miditoplay.LogEvents         = MPTK_LogEvents;
                    miditoplay.EnableChangeTempo = MPTK_EnableChangeTempo;
                    miditoplay.EnablePanChange   = MPTK_EnablePanChange;

                    if (MPTK_PauseOnDistance)
                    {
                        distanceEditorModeOnly = MidiPlayerGlobal.MPTK_DistanceToListener(this.transform);
                        if (distanceEditorModeOnly > AudioSourceTemplate.maxDistance)
                        {
                            lastTimePlay = Time.realtimeSinceStartup;
                            yield return(new WaitForSeconds(0.2f));

                            continue;
                        }
                    }

                    if (playPause)
                    {
                        lastTimePlay = Time.realtimeSinceStartup;
                        yield return(new WaitForSeconds(0.2f));

                        if (miditoplay.EndMidiEvent || newMidiToPlay || stopMidiToPlay)
                        {
                            break;
                        }
                        if (timeToPauseMilliSeconde > -1f)
                        {
                            timeToPauseMilliSeconde -= 0.2f;
                            if (timeToPauseMilliSeconde <= 0f)
                            {
                                playPause = false;
                            }
                        }
                        continue;
                    }

                    if (!first)
                    {
                        timeFromStartPlay += (Time.realtimeSinceStartup - lastTimePlay) * 1000f;
                    }
                    else
                    {
                        timeFromStartPlay = 0d;
                        first             = false;
                    }
                    lastTimePlay = Time.realtimeSinceStartup;

                    //Debug.Log("---------------- " + timeFromStartPlay );
                    // Read midi events until this time
                    List <MidiNote> notes = miditoplay.ReadMidiEvents(timeFromStartPlay);

                    if (miditoplay.EndMidiEvent || newMidiToPlay || stopMidiToPlay)
                    {
                        break;
                    }

                    // Play notes read
                    if (notes != null && notes.Count > 0)
                    {
                        try
                        {
                            if (OnEventNotesMidi != null)
                            {
                                OnEventNotesMidi.Invoke(notes);
                            }
                        }
                        catch (System.Exception ex)
                        {
                            MidiPlayerGlobal.ErrorDetail(ex);
                        }

                        if (MPTK_DirectSendToPlayer)
                        {
                            MPTK_PlayNotes(notes);
                        }
                        //Debug.Log("---------------- play count:" + notes.Count + " " + timeFromStartMS );
                    }
                    if (Application.isEditor)
                    {
                        TimeSpan times = TimeSpan.FromMilliseconds(timeFromStartPlay);
                        playTimeEditorModeOnly = string.Format("{0:00}:{1:00}:{2:00}:{3:000}", times.Hours, times.Minutes, times.Seconds, times.Milliseconds);
                        durationEditorModeOnly = string.Format("{0:00}:{1:00}:{2:00}:{3:000}", MPTK_Duration.Hours, MPTK_Duration.Minutes, MPTK_Duration.Seconds, MPTK_Duration.Milliseconds);
                    }

                    yield return(new WaitForSeconds(delayMilliSeconde / 1000f));// 0.01f);
                }while (true);
            }

            midiIsPlaying = false;

            try
            {
                if (OnEventEndPlayMidi != null && !stopMidiToPlay && !newMidiToPlay)
                {
                    OnEventEndPlayMidi.Invoke();
                }

                if ((MPTK_Loop || newMidiToPlay) && !stopMidiToPlay)
                {
                    MPTK_Play();
                }
                //stopMidiToPlay = false;
            }
            catch (System.Exception ex)
            {
                MidiPlayerGlobal.ErrorDetail(ex);
            }
            //Debug.Log("Stop play");
        }