private IEnumerator TheadPlay(List <MPTKNote> notes) { if (notes != null && notes.Count > 0) { try { if (MPTK_PauseOnDistance) { distanceEditorModeOnly = MidiPlayerGlobal.MPTK_DistanceToListener(this.transform); if (distanceEditorModeOnly > AudioSourceTemplate.maxDistance) { notes.Clear(); } } } catch (System.Exception ex) { MidiPlayerGlobal.ErrorDetail(ex); } try { List <MidiNote> midinotes = new List <MidiNote>(); foreach (MPTKNote note in notes) { midinotes.Add(note.ToMidiNote()); } MPTK_PlayNotes(midinotes); //Debug.Log("---------------- play count:" + notes.Count + " " + timeFromStartMS ); } catch (System.Exception ex) { MidiPlayerGlobal.ErrorDetail(ex); } } yield return(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"); }