Exemplo n.º 1
0
 /// <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);
     }
 }
Exemplo n.º 2
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 <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();
            }
        }
Exemplo n.º 3
0
        public void AddSoundFont(ImSoundFont imsf)
        {
            SoundFontInfo sfi = new SoundFontInfo();

            sfi.Name = imsf.SoundFontName;
            SoundFonts.Add(sfi);
        }
Exemplo n.º 4
0
        public static void LoadBanks(ImSoundFont imsf)
        {
            // BankSelected is XMLIgnore, build bank selected from string
            imsf.BankSelected = new bool[MAXBANKPRESET];
            for (int b = 0; b < imsf.BankSelected.Length; b++)
            {
                imsf.BankSelected[b] = false;
            }

            if (imsf.StrBankSelected != null)
            {
                string[] sbanks = imsf.StrBankSelected.Split(',');
                if (sbanks != null)
                {
                    foreach (string sbank in sbanks)
                    {
                        if (!string.IsNullOrEmpty(sbank))
                        {
                            int ibank = Convert.ToInt32(sbank);
                            if (ibank >= 0 && ibank < MAXBANKPRESET)
                            {
                                imsf.BankSelected[ibank] = true;
                            }
                        }
                    }
                }
            }

            // Build bank content
            imsf.Banks = new ImBank[MAXBANKPRESET];
            foreach (HiPreset p in imsf.HiSf.preset)
            {
                if (p != null)
                {
                    if (imsf.Banks[p.Bank] == null)
                    {
                        // New bank, create it
                        imsf.Banks[p.Bank] = new ImBank()
                        {
                            BankNumber = p.Bank,
                            defpresets = new HiPreset[MAXBANKPRESET]
                        };
                    }
                    imsf.Banks[p.Bank].defpresets[p.Num] = p;
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Load an ImSoundFont from a string
        /// </summary>
        /// <param name="path"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static ImSoundFont Load(string data)
        {
            ImSoundFont loaded = null;

            try
            {
                if (!string.IsNullOrEmpty(data))
                {
                    var serializer = new XmlSerializer(typeof(ImSoundFont));
                    using (TextReader reader = new StringReader(data))
                    {
                        loaded = serializer.Deserialize(reader) as ImSoundFont;
                    }
                }
            }
            catch (System.Exception ex)
            {
                MidiPlayerGlobal.ErrorDetail(ex);
            }
            return(loaded);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Load an ImSoundFont from a desktop file
        /// </summary>
        /// <param name="path"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static ImSoundFont Load(string path, string name)
        {
            ImSoundFont loaded = null;

            try
            {
                string Filepath = Path.Combine(path, name);
                Filepath += MidiPlayerGlobal.ExtensionSoundFileFile;

                if (File.Exists(Filepath))
                {
                    var serializer = new XmlSerializer(typeof(ImSoundFont));
                    using (var stream = new FileStream(Filepath, FileMode.Open))
                    {
                        loaded = serializer.Deserialize(stream) as ImSoundFont;
                    }
                }
            }
            catch (System.Exception ex)
            {
                MidiPlayerGlobal.ErrorDetail(ex);
            }
            return(loaded);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Load an ImSoundFont from a TextAsset resource
        /// </summary>
        /// <param name="path"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static ImSoundFont Load(string path, string name)
        {
            ImSoundFont loaded = null;

            try
            {
                // Path to the XML soundfonts file for this SF
                TextAsset sfxml = Resources.Load <TextAsset>(path + "/" + name);
                if (sfxml == null || sfxml.bytes.Length == 0)
                {
                    Debug.LogWarningFormat("SoundFont {0} not found in Unity resource {1}", name, path);
                }
                else
                {
                    if (sfxml != null && !string.IsNullOrEmpty(sfxml.text))
                    {
                        // Load XML description of the SF
                        var serializer = new XmlSerializer(typeof(ImSoundFont));
                        using (TextReader reader = new StringReader(sfxml.text))
                        {
                            loaded = serializer.Deserialize(reader) as ImSoundFont;
                        }

                        if (loaded == null)
                        {
                            Debug.LogWarningFormat("Error when reading SoundFont {0} from {1}", name, path);
                        }
                        else
                        {
                            //Debug.LogFormat("XML SF loaded {0} Bank instrument:{1} Bank drum:{2}", loaded.SoundFontName, loaded.DefaultBankNumber, loaded.DrumKitBankNumber);

                            // Load binary data of the SF
                            TextAsset sfbin = Resources.Load <TextAsset>(path + "/" + name + "_data");
                            if (sfbin == null)
                            {
                                Debug.LogWarningFormat("Error when reading SoundFont data {0} from {1}", name, path);
                            }
                            else
                            {
                                // Create sf from binaray data
                                SFLoad load = new SFLoad(sfbin.bytes, SFFile.SfSource.MPTK);
                                if (load == null || load.SfData == null)
                                {
                                    Debug.LogWarningFormat("Error when decoding SoundFont data {0} from {1}", name, path);
                                }
                                else
                                {
                                    loaded.HiSf = load.SfData;
                                    LoadBanks(loaded);
                                    //SFFile.DumpSFToFile(loaded.hisf, @"c:\temp\" + name + "_dump.txt");
                                }
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                MidiPlayerGlobal.ErrorDetail(ex);
            }
            return(loaded);
        }
Exemplo n.º 8
0
        //! @cond NODOC

        /// <summary>
        /// Core function to load a SF when playing or from editor from the Unity asset
        /// </summary>
        public static void LoadCurrentSF()
        {
            MPTK_SoundFontLoaded = false;
            // Load simplfied soundfont
            try
            {
                DateTime start = DateTime.Now;
                if (CurrentMidiSet == null)
                {
                    Debug.Log(MidiPlayerGlobal.ErrorNoSoundFont);
                }
                else
                {
                    SoundFontInfo sfi = CurrentMidiSet.ActiveSounFontInfo;
                    if (sfi == null)
                    {
                        Debug.Log(MidiPlayerGlobal.ErrorNoSoundFont);
                    }
                    else
                    {
                        //Debug.Log("Start loading " + sfi.Name);

                        // Path to the soundfonts directory for this SF, start from resource folder
                        string pathToImSF = Path.Combine(SoundfontsDB + "/", sfi.Name);

                        WavePath = Path.Combine(pathToImSF + "/", PathToWave);
                        // Load all presets defined in the sf
                        ImSFCurrent = ImSoundFont.Load(pathToImSF, sfi.Name);

                        // Add
                        if (ImSFCurrent == null)
                        {
                            Debug.LogWarning("Error loading " + sfi.Name ?? "name not defined");
                        }
                        else
                        {
                            BuildBankList();
                            BuildPresetList(true);
                            BuildPresetList(false);
                            //BuildDrumList();
                            timeToLoadSoundFont = DateTime.Now - start;

                            //Debug.Log("End loading SoundFont " + timeToLoadSoundFont.TotalSeconds + " seconds");
                        }
                    }
                }
            }
            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;
                    System.Diagnostics.Stopwatch watchLoadWave = new System.Diagnostics.Stopwatch(); // High resolution time
                    watchLoadWave.Start();
                    if (MPTK_LoadWaveAtStartup)
                    {
                        LoadAudioClip();
                    }
                    LoadWave();
                    timeToLoadWave = watchLoadWave.Elapsed;
                    //Debug.Log("End loading Waves " + timeToLoadWave.TotalSeconds + " seconds" + " count:" + MPTK_CountWaveLoaded);
                }
                catch (System.Exception ex)
                {
                    MidiPlayerGlobal.ErrorDetail(ex);
                }
            }
            if (ImSFCurrent != null)
            {
                MPTK_SoundFontLoaded = true;
            }

            if (OnEventPresetLoaded != null)
            {
                OnEventPresetLoaded.Invoke();
            }
        }
Exemplo n.º 9
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();
            }
        }
Exemplo n.º 10
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;
                }
            }
        }