private static void LoadWave() { 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 (!DicAudioWave.Exist(smpl.Name)) { LoadWave(smpl); MPTK_CountWaveLoaded++; } } } } else { Debug.Log("SoundFont not loaded "); } } catch (System.Exception ex) { MidiPlayerGlobal.ErrorDetail(ex); } }
/// <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(); } } } } } }
public static void LoadWave(HiSample smpl) { //Debug.Log("-------------------- " + smpl.Name); string path = WavePath + "/" + Path.GetFileNameWithoutExtension(smpl.Name);// + ".wav"; AudioClip ac = Resources.Load <AudioClip>(path); if (ac != null) { float[] data = new float[ac.samples * ac.channels]; if (ac.GetData(data, 0)) { //Debug.Log(smpl.Name + " " + factor); smpl.Data = data; DicAudioWave.Add(smpl); //MPTK_CountWaveLoaded++; } } //else Debug.LogWarning("Sample " + smpl.WaveFile + " not found"); }
/// <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(); DicAudioWave.Init(); } catch (System.Exception ex) { MidiPlayerGlobal.ErrorDetail(ex); } if (CurrentMidiSet == null) { Debug.LogWarning(MidiPlayerGlobal.ErrorNoMidiFile); yield return(Routine.WaitForOneFrame); } else if (CurrentMidiSet.ActiveSounFontInfo == null) { Debug.Log(MidiPlayerGlobal.ErrorNoSoundFont); yield return(Routine.WaitForOneFrame); } BuildMidiList(); if (MPTK_LoadSoundFontAtStartup) { LoadCurrentSF(); } } }
private static void LoadWave() { 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 (!DicAudioWave.Exist(smpl.Name)) { //Debug.Log("-------------------- " + smpl.Name); string path = WavePath + "/" + Path.GetFileNameWithoutExtension(smpl.Name);// + ".wav"; AudioClip ac = Resources.Load <AudioClip>(path); if (ac != null) { //Debug.Log("Wave load " + path); //count++; //if (count % 50 == 0) //{ // System.Diagnostics.Process proc = System.Diagnostics.Process.GetCurrentProcess(); // long total = GC.GetTotalMemory(true); // GC.Collect(); // total = GC.GetTotalMemory(true); //} //GC.Collect(); //if (count > 1000) break; //// For test ... //float[] data = new float[ac.samples * ac.channels]; //if (!ac.GetData(data, 0)) // Debug.LogWarningFormat("LoadAudioClip - Clip {0} data not loaded", smpl.Name); //else // Debug.LogFormat("{0} {1} {2}", smpl.Name, ac.samples, ac.channels); //// end for test //HiSample newSample = new HiSample(); //newSample.Name = smpl.Name; //newSample.SampleRate = (uint)ac.frequency;// smpl.SampleRate; //newSample.OrigPitch = smpl.OrigPitch; //newSample.PitchAdj = smpl.PitchAdj; //newSample.SampleType = smpl.SampleType; //newSample.Start = 0; //newSample.End = smpl.End; //if (smpl.Name.Contains("Fd4")) // Debug.Log(""); float[] data = new float[ac.samples * ac.channels]; if (ac.GetData(data, 0)) { //Debug.Log(smpl.Name + " " + factor); smpl.Data = data; //newSample.LoopStart = (uint)(smpl.LoopStart * factor); //newSample.LoopEnd = (uint)(smpl.LoopEnd * factor); //if (newSample.LoopEnd >= newSample.Data.Length) // newSample.LoopEnd = (uint)newSample.Data.Length - 1; //Debug.Log(" OrigPitch:" + newSample.OrigPitch); //Debug.Log(" SampleRate:" + newSample.SampleRate); //Debug.Log(" Length:" + newSample.Data.Length); //Debug.Log(" LoopStart:" + newSample.LoopStart); //Debug.Log(" LoopEnd:" + newSample.LoopEnd); DicAudioWave.Add(smpl); //MPTK_CountWaveLoaded++; } } //else Debug.LogWarning("Wave " + smpl.WaveFile + " not found"); } } } } else { Debug.Log("SoundFont not loaded "); } } catch (System.Exception ex) { MidiPlayerGlobal.ErrorDetail(ex); } }