/// <summary> /// Call by the first MidiPlayer awake /// </summary> //public static void Init() //{ // Instance.StartCoroutine(Instance.InitThread()); //} /// <summary> /// Call by the first MidiPlayer awake /// </summary> private IEnumerator <float> InitThread() { if (!Initialized) { //Debug.Log("MidiPlayerGlobal InitThread"); Initialized = true; ImSFCurrent = null; try { AudioListener = Component.FindObjectOfType <AudioListener>(); if (AudioListener == null) { Debug.LogWarning("No audio listener found. Add one and only one AudioListener component to your hierarchy."); //return; } } catch (System.Exception ex) { MidiPlayerGlobal.ErrorDetail(ex); } try { AudioListener[] listeners = Component.FindObjectsOfType <AudioListener>(); if (listeners != null && listeners.Length > 1) { Debug.LogWarning("More than one audio listener found. Some unexpected behaviors could happen."); } } catch (System.Exception ex) { MidiPlayerGlobal.ErrorDetail(ex); } try { LoadMidiSetFromRsc(); DicAudioClip.Init(); } catch (System.Exception ex) { MidiPlayerGlobal.ErrorDetail(ex); } if (CurrentMidiSet == null) { Debug.LogWarning(MidiPlayerGlobal.ErrorNoMidiFile); yield return(Timing.WaitForOneFrame); } else if (CurrentMidiSet.ActiveSounFontInfo == null) { Debug.Log(MidiPlayerGlobal.ErrorNoSoundFont); yield return(Timing.WaitForOneFrame); } BuildMidiList(); LoadCurrentSF(); } }
/// <summary> /// Loading of a SoundFontt when playing using a thread /// </summary> /// <param name="restartPlayer"></param> /// <returns></returns> private static IEnumerator <float> LoadSoundFontThread(bool restartPlayer = true) { //if (MidiPlayerGlobal.ImSFCurrent != null) { //Debug.Log("Load MidiPlayerGlobal.ImSFCurrent: " + MidiPlayerGlobal.ImSFCurrent.SoundFontName); //Debug.Log("Load CurrentMidiSet.ActiveSounFontInfo: " + CurrentMidiSet.ActiveSounFontInfo.Name); MidiSynth[] synths = FindObjectsOfType <MidiSynth>(); List <MidiFilePlayer> playerToRestart = new List <MidiFilePlayer>(); MPTK_SoundFontLoaded = false; if (Application.isPlaying) { if (synths != null) { foreach (MidiSynth synth in synths) { if (synth is MidiFilePlayer) { MidiFilePlayer player = (MidiFilePlayer)synth; if (player.MPTK_IsPlaying) { playerToRestart.Add(player); player.MPTK_Stop(); // stop and clear all sound } } //synth.MPTK_ClearAllSound(); yield return(Routine.WaitUntilDone(Routine.RunCoroutine(synth.ThreadWaitAllStop(), Segment.RealtimeUpdate), false)); synth.MPTK_StopSynth(); } } DicAudioClip.Init(); DicAudioWave.Init(); } LoadCurrentSF(); //Debug.Log(" Time To Load SoundFont: " + Math.Round(MidiPlayerGlobal.MPTK_TimeToLoadSoundFont.TotalSeconds, 3).ToString() + " second"); //Debug.Log(" Time To Load Waves: " + Math.Round(MidiPlayerGlobal.MPTK_TimeToLoadWave.TotalSeconds, 3).ToString() + " second"); if (synths != null) { foreach (MidiSynth synth in synths) { synth.MPTK_InitSynth(); if (synth is MidiFilePlayer) { synth.MPTK_StartSequencerMidi(); } } if (restartPlayer) { foreach (MidiFilePlayer player in playerToRestart) { player.MPTK_RePlay(); } } } } }
/// <summary> /// Loading of a SoundFonttwhen playing using a thread /// </summary> /// <returns></returns> private static IEnumerator <float> LoadSoundFontThread() { if (MidiPlayerGlobal.ImSFCurrent != null) { //Debug.Log("Load MidiPlayerGlobal.ImSFCurrent: " + MidiPlayerGlobal.ImSFCurrent.SoundFontName); //Debug.Log("Load CurrentMidiSet.ActiveSounFontInfo: " + CurrentMidiSet.ActiveSounFontInfo.Name); MidiSynth[] midiplayers = FindObjectsOfType <MidiSynth>(); MPTK_SoundFontLoaded = false; if (Application.isPlaying) { if (midiplayers != null) { foreach (MidiSynth mp in midiplayers) { if (mp is MidiFilePlayer) { MidiFilePlayer mfp = (MidiFilePlayer)mp; if (!mfp.MPTK_IsPaused) { mfp.MPTK_Pause(); } yield return(Timing.WaitUntilDone(Timing.RunCoroutine(mp.ThreadClearAllSound(true)))); } } } DicAudioClip.Init(); DicAudioWave.Init(); } LoadCurrentSF(); //Debug.Log(" Time To Load SoundFont: " + Math.Round(MidiPlayerGlobal.MPTK_TimeToLoadSoundFont.TotalSeconds, 3).ToString() + " second"); //Debug.Log(" Time To Load Waves: " + Math.Round(MidiPlayerGlobal.MPTK_TimeToLoadWave.TotalSeconds, 3).ToString() + " second"); if (midiplayers != null) { foreach (MidiSynth mp in midiplayers) { if (mp is MidiFilePlayer) { MidiFilePlayer mfp = (MidiFilePlayer)mp; if (mfp.MPTK_IsPaused) { mfp.MPTK_InitSynth(); mfp.MPTK_RePlay(); } } } } } }
private static IEnumerator LoadSoundFontThread() { if (MidiPlayerGlobal.ImSFCurrent != null) { Debug.Log("Load MidiPlayerGlobal.ImSFCurrent: " + MidiPlayerGlobal.ImSFCurrent.SoundFontName); Debug.Log("Load CurrentMidiSet.ActiveSounFontInfo: " + CurrentMidiSet.ActiveSounFontInfo.Name); MidiPlayerGlobal.SoundFontLoaded = false; MidiPlayer[] midiplayers = null; if (Application.isPlaying) { midiplayers = GameObject.FindObjectsOfType <MidiPlayer>(); if (midiplayers != null) { foreach (MidiPlayer mp in midiplayers) { //if (mp is MidiFilePlayer) ((MidiFilePlayer)mp).MPTK_Pause(); yield return(Instance.StartCoroutine(mp.MPTK_ClearAllSound(true))); } } //return; DicAudioClip.Init(); } MidiPlayerGlobal.LoadCurrentSF(); Debug.Log(" Time To Load SoundFont: " + Math.Round(MidiPlayerGlobal.MPTK_TimeToLoadSoundFont.TotalSeconds, 3).ToString() + " second"); Debug.Log(" Time To Load Waves: " + Math.Round(MidiPlayerGlobal.MPTK_TimeToLoadWave.TotalSeconds, 3).ToString() + " second"); MidiPlayerGlobal.SoundFontLoaded = true; if (Application.isPlaying) { if (midiplayers != null) { foreach (MidiPlayer mp in midiplayers) { if (mp is MidiFilePlayer) { ((MidiFilePlayer)mp).MPTK_ReSyncTime(); } } } } } }
/// <summary> /// Load samples associated to a patch /// </summary> /// <param name="ibank"></param> /// <param name="ipatch"></param> private static void LoadSamples(int ibank, int ipatch) { try { float start = Time.realtimeSinceStartup; //Debug.Log(">>> Load Preset - b:" + ibank + " p:" + ipatch); if (ImSFCurrent != null) { ImPreset preset = ImSFCurrent.Banks[ibank].Presets[ipatch]; //Debug.Log("Loading Preset - " + index + " '" + preset.Name + "'"); // Load each sample associated with this preset in DicAudioClip foreach (ImInstrument inst in preset.Instruments) { foreach (ImSample smpl in inst.Samples) { if (smpl.WaveFile != null) { if (!DicAudioClip.Exist(smpl.WaveFile)) { AudioClip ac = Resources.Load <AudioClip>(WavePath + "/" + Path.GetFileNameWithoutExtension(smpl.WaveFile)); if (ac != null) { DicAudioClip.Add(smpl.WaveFile, ac); MPTK_CountWaveLoaded++; } //else Debug.LogWarning("Wave " + smpl.WaveFile + " not found"); } } } } //Debug.Log("--- Loaded Preset - " + ipatch + " '" + preset.Name + "' " + count + " samples loaded"); } else { Debug.Log("Presets not loaded "); } } catch (System.Exception ex) { MidiPlayerGlobal.ErrorDetail(ex); } }
/// <summary> /// Load samples associated to a patch for High SF /// </summary> private static void LoadAudioClip() { try { float start = Time.realtimeSinceStartup; //Debug.Log(">>> Load Sample"); //int count = 0; if (ImSFCurrent != null) { foreach (HiSample smpl in ImSFCurrent.HiSf.Samples) { if (smpl.Name != null) { if (!DicAudioClip.Exist(smpl.Name)) { string path = WavePath + "/" + Path.GetFileNameWithoutExtension(smpl.Name);// + ".wav"; AudioClip ac = Resources.Load <AudioClip>(path); if (ac != null) { //Debug.Log("Sample load " + path); DicAudioClip.Add(smpl.Name, ac); MPTK_CountWaveLoaded++; } //else Debug.LogWarning("Sample " + smpl.WaveFile + " not found"); } } } } else { Debug.Log("SoundFont not loaded "); } } catch (System.Exception ex) { MidiPlayerGlobal.ErrorDetail(ex); } }
/// <summary> /// Play one note - [New 1.7] /// </summary> /// <param name="note"></param> public void MPTK_PlayNote(MidiNote note) { try { // Search sample associated to the preset, midi note and velocity int selectedBank = note.Drum ? MidiPlayerGlobal.CurrentMidiSet.ActiveSounFontInfo.DrumKitBankNumber : selectedBank = MidiPlayerGlobal.CurrentMidiSet.ActiveSounFontInfo.DefaultBankNumber; int noteMidi = note.Midi; if (!note.Drum) { noteMidi += MPTK_Transpose; } //ImSample smpl = MidiPlayerGlobal.GetImSample(selectedBank, note.Patch, noteMidi, note.Velocity); //if (smpl != null) { List <ImSample> samples = MidiPlayerGlobal.GetImMultiSample(selectedBank, note.Patch, noteMidi, note.Velocity); //LogInfoSample(note, null, " Found " + samples.Count + " samples"); foreach (ImSample smpl in samples) { note.Pitch = Mathf.Pow(_ratioHalfTone, (float)(noteMidi - smpl.OriginalPitch + smpl.CoarseTune) + (float)smpl.FineTune / 100f); // Load wave from audioclip AudioClip clip = DicAudioClip.Get(smpl.WaveFile); if (clip != null && clip.loadState == AudioDataLoadState.Loaded) { if (MPTK_LogWaves) { LogInfoSample(note, smpl); } AudioSource audioSelected = null; // Search audioclip not playing with the same wave try { foreach (AudioSource audio in audiosources) { //Debug.Log(audio.isPlaying + " " + audio.clip.name + " " + clip.name); if (!audio.isPlaying && audio.clip.name == clip.name) { audioSelected = audio; break; } } } catch (System.Exception ex) { MidiPlayerGlobal.ErrorDetail(ex); } if (audioSelected == null) { // No audiosource available, create a new audiosource audioSelected = Instantiate <AudioSource>(AudioSourceTemplate); audioSelected.Stop(); audioSelected.transform.position = AudioSourceTemplate.transform.position; audioSelected.transform.SetParent(this.transform); audiosources.Add(audioSelected); // Assign sound to audioclip audioSelected.clip = clip; } // Play note StartCoroutine(PlayNote(audioSelected, note.Drum, smpl, note, timeToRelease)); } else { if (MPTK_LogWaves) { LogInfoSample(note, null, smpl.WaveFile + " ******** Clip not ready to play or not found ******"); } } } //else if (samples.Count == 0) { if (MPTK_LogWaves) { LogInfoSample(note, null, " ********* Sample not found *********"); } } } } catch (System.Exception ex) { MidiPlayerGlobal.ErrorDetail(ex); } }
/// <summary> /// Call by the first MidiPlayer awake /// </summary> //public static void Init() //{ // Instance.StartCoroutine(Instance.InitThread()); //} /// <summary> /// Call by the first MidiPlayer awake /// </summary> private IEnumerator InitThread() { if (!Initialized) { //Debug.Log("MidiPlayerGlobal InitThread"); SoundFontLoaded = false; Initialized = true; ImSFCurrent = null; try { AudioListener = Component.FindObjectOfType <AudioListener>(); if (AudioListener == null) { Debug.LogWarning("No audio listener found. Add one and only one AudioListener component to your hierarchy."); //return; } } catch (System.Exception ex) { MidiPlayerGlobal.ErrorDetail(ex); } try { AudioListener[] listeners = Component.FindObjectsOfType <AudioListener>(); if (listeners != null && listeners.Length > 1) { Debug.LogWarning("More than one audio listener found. Some unexpected behaviors could happen."); } } catch (System.Exception ex) { MidiPlayerGlobal.ErrorDetail(ex); } try { LoadMidiSetFromRsc(); DicAudioClip.Init(); } catch (System.Exception ex) { MidiPlayerGlobal.ErrorDetail(ex); } if (CurrentMidiSet == null) { Debug.LogWarning("No Midi defined, go to menu 'Tools/MPTK - Midi File Setup' or alt-m"); yield return(0); } else if (CurrentMidiSet.ActiveSounFontInfo == null) { Debug.LogWarning("No Active SoundFont found. Define SoundFont from the menu 'Tools/MPTK - SoundFont Setup' or alt-f"); yield return(0); } LoadCurrentSF(); //Debug.Log(""); if (ImSFCurrent != null) { SoundFontLoaded = true; } } }