/// <summary>
 /// Load Simplified SoundFont from OS (edit mode)
 /// </summary>
 /// <param name="soundPath"></param>
 /// <param name="soundFontName"></param>
 public static void LoadImSF(string soundPath = null, string soundFontName = null)
 {
     try
     {
         if (soundPath == null || soundFontName == null)
         //Load active SF
         {
             if (MidiPlayerGlobal.CurrentMidiSet.ActiveSounFontInfo != null)
             {
                 soundPath     = Path.Combine(Application.dataPath + "/", MidiPlayerGlobal.PathToSoundfonts);
                 soundPath     = Path.Combine(soundPath + "/", MidiPlayerGlobal.CurrentMidiSet.ActiveSounFontInfo.Name);
                 soundFontName = MidiPlayerGlobal.CurrentMidiSet.ActiveSounFontInfo.Name;
             }
         }
         if (soundPath != null && soundFontName != null)
         {
             Debug.Log("SoundFont loading " + soundFontName);
             MidiPlayerGlobal.ImSFCurrent = ImSoundFont.Load(soundPath, soundFontName);
             MidiPlayerGlobal.ImSFCurrent.CreateBankDescription();
             MidiPlayerGlobal.BuildPresetList();
             MidiPlayerGlobal.BuildDrumList();
         }
         else
         {
             Debug.Log("LoadImSF : no sf defined");
         }
     }
     catch (System.Exception ex)
     {
         MidiPlayerGlobal.ErrorDetail(ex);
     }
 }
Exemple #2
0
        public static void LoadCurrentSF()
        {
            // Load simplfied soundfont
            try
            {
                DateTime start = DateTime.Now;
                if (CurrentMidiSet == null)
                {
                    Debug.LogWarning("No SoundFont defined, go to Unity menu Tools to add a Soundfont");
                }
                else
                {
                    SoundFontInfo sfi = CurrentMidiSet.ActiveSounFontInfo;
                    if (sfi == null)
                    {
                        Debug.LogWarning("No SoundFont defined, go to Unity menu Tools to add a Soundfont");
                    }
                    else
                    {
                        // Path to the soundfonts directory for this SF, start from resource folder
                        string pathToImSF = Path.Combine(SoundfontsDB + "/", sfi.Name);
                        // Path to the soundfonts file for this SF
                        TextAsset sf = Resources.Load <TextAsset>(Path.Combine(pathToImSF + "/", sfi.Name));
                        if (sf == null)
                        {
                            Debug.LogWarning("No SoundFont found " + pathToImSF);
                        }
                        else
                        {
                            WavePath = Path.Combine(pathToImSF + "/", PathToWave);
                            // Load all presets defined in the XML sf
                            ImSFCurrent         = ImSoundFont.Load(sf.text);
                            timeToLoadSoundFont = DateTime.Now - start;
                            BuildPresetList();
                            BuildDrumList();
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                MidiPlayerGlobal.ErrorDetail(ex);
            }

            if (ImSFCurrent == null)
            {
                Debug.LogWarning("SoundFont not loaded.");
                return;
            }

            // Load samples only in run mode
            if (Application.isPlaying)
            {
                try
                {
                    MPTK_CountWaveLoaded   = 0;
                    MPTK_CountPresetLoaded = 0;
                    DateTime start = DateTime.Now;
                    for (int ibank = 0; ibank < ImSFCurrent.Banks.Length; ibank++)
                    {
                        if (ImSFCurrent.Banks[ibank] != null)
                        {
                            for (int ipreset = 0; ipreset < ImSFCurrent.Banks[ibank].Presets.Length; ipreset++)
                            {
                                MPTK_CountPresetLoaded++;
                                if (ImSFCurrent.Banks[ibank].Presets[ipreset] != null)
                                {
                                    LoadSamples(ibank, ipreset);
                                }
                            }
                        }
                    }
                    timeToLoadWave = DateTime.Now - start;
                }
                catch (System.Exception ex)
                {
                    MidiPlayerGlobal.ErrorDetail(ex);
                }
            }
            if (OnEventPresetLoaded != null)
            {
                OnEventPresetLoaded.Invoke();
            }
        }
Exemple #3
0
        /// <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;
                }
            }
        }