示例#1
0
        static public void BuildDrumList()
        {
            List <string> drums = new List <string>();

            try
            {
                //Debug.Log(">>> Load Preset - b:" + ibank + " p:" + ipatch);
                if (ImSFCurrent != null && CurrentMidiSet != null)
                {
                    if (ImSFCurrent.DrumKitBankNumber >= 0 && ImSFCurrent.DrumKitBankNumber < ImSFCurrent.Banks.Length)
                    {
                        int ibank = ImSFCurrent.DrumKitBankNumber;
                        if (ImSFCurrent.Banks[ibank] != null && ImSFCurrent.Banks[ibank].Presets[0] != null)
                        {
                            for (int key = 0; key < 127; key++)
                            {
                                string   waveName;
                                ImSample sample = GetImSample(ibank, 0, key, 127);
                                if (sample != null)
                                {
                                    waveName = Path.GetFileNameWithoutExtension(sample.WaveFile);
                                }
                                else
                                {
                                    waveName = "";
                                }
                                drums.Add(waveName);
                            }
                        }
                        else
                        {
                            //Debug.LogWarning("Bank don't exists.");
                        }
                    }
                    else
                    {
                        //Debug.LogWarning("No bank defined for instrument.");
                    }
                }
                else
                {
                    Debug.LogWarning("BuildDrumList: No MidiSet defined, go to menu 'Tools/MPTK - Midi File Setup' or alt-m");
                }
            }
            catch (System.Exception ex)
            {
                MidiPlayerGlobal.ErrorDetail(ex);
            }
            listDrum = drums.AsReadOnly();
        }
示例#2
0
        private void LogInfoSample(MidiNote note, ImSample sample, string info = null)
        {
            //TimeSpan playtime = TimeSpan.Zero;
            //if (miditoplay != null) playtime = miditoplay.CurrentMidiTime();

            //string time = string.Format("[{0:00}:{1:00}:{2:00}:{3:000}]", playtime.Hours, playtime.Minutes, playtime.Seconds, playtime.Milliseconds);
            string time = "";

            if (sample != null)
#if DEBUGPITCHNOTE
            { Debug.Log(string.Format("{0} C:{1,2} P:{2,3:000} D:{3} Note:{4,3:000} OriginalPitch:{5} PitchCorrection:{6} FineTune:{7} CoarseTune:{8} Duration:{9,4} sec. Velocity:{10} Wave:{11}",
                                      time, note.Chanel, note.Patch, note.Drum, note.Midi,
                                      sample.OriginalPitch, sample.PitchCorrection, sample.FineTune, sample.CoarseTune, Math.Round(note.Duration / 1000d, 2), note.Velocity, sample.WaveFile)); }
#else
            { Debug.Log(string.Format("{0} C:{1,2} P:{2,3:000} D:{3} Note:{4,3:000} {5,-3} Duration:{6,4} sec. Velocity:{7} Pan:{8} Wave:{9}",
                                      time, note.Channel, note.Patch, note.Drum, note.Midi, HelperNoteLabel.LabelFromMidi(note.Midi),
                                      Math.Round(note.Duration / 1000d, 2), note.Velocity, sample.Pan, sample.WaveFile)); }
示例#3
0
        /// <summary>
        /// Play one note with this AudioSource
        /// </summary>
        /// <param name="audio">AudioSource</param>
        /// <param name="loop">Sound with loop</param>
        /// <param name="note"></param>
        /// <returns></returns>
        protected IEnumerator PlayNote(AudioSource audio, bool drum, ImSample sample, MidiNote note, float vTransitionTime)
        {
            if (note.Delay > 0f)
            {
                float endDelay = Time.realtimeSinceStartup + note.Delay / 1000f;
                while (Time.realtimeSinceStartup < endDelay && note.Delay > 0f)
                {
                    yield return(new WaitForSeconds(0.01f));
                }
            }

            try
            {
                audio.pitch = note.Pitch;
                if (drum)
                {
                    audio.loop = false;
                }
                else
                {
                    audio.loop = sample.IsLoop;
                }
                // Attenuation removed from current version
                //audio.volume = Mathf.Lerp(0f, 1f, note.Velocity * sample.Attenuation / 127f) * MPTK_Volume;
                audio.volume = Mathf.Lerp(0f, 1f, note.Velocity / 127f) * MPTK_Volume;

                // Pan from the SoundFont
                if (MidiPlayerGlobal.CurrentMidiSet.ActiveSounFontInfo.Panoramic)
                {
                    audio.panStereo = Mathf.Lerp(-1f, 1f, (sample.Pan + 500) / 1000f);
                }
                else
                {
                    audio.panStereo = 0f;
                }

                // Pan from the midi file or from a midi generated event
                if (note.Pan >= 0)
                {
                    audio.panStereo = Mathf.Lerp(-1f, 1f, note.Pan / 127f);
                }
                //Debug.Log(string.Format("   Pan  - note:{0,3} sample:{1,3} --> audio pan:{2,3}" , note.Pan , sample.Pan,Math.Round( audio.panStereo,2)));
                //Debug.Log(string.Format("   Vel  - note:{0,3} sample:{1,3} --> audio vel:{2,3}", note.Velocity , Math.Round(sample.Attenuation, 2), Math.Round(audio.volume, 2)));
                //Debug.Log(string.Format("   Loop - drum:{0} sample:{1}     --> audio loop:{2}" , drum , sample.IsLoop , audio.loop));

                audio.Play();
            }
            catch (Exception)
            {
            }

            // Attack & Decay taken in account by the wave, for drum (loop=false) play the wave one shot (no duration)
            if (audio.loop)
            {
                // Sustain phase until key release, constant amplitude
                float end = Time.realtimeSinceStartup + (float)(note.Duration / 1000d);
                while (note.Duration > 0f)
                {
                    try
                    {
                        if (Time.realtimeSinceStartup >= end || !audio.isPlaying)
                        {
                            break;
                        }
                    }
                    catch (Exception)
                    {
                    }
                    yield return(new WaitForSeconds(0.01f));
                }
                //Debug.Log("stop " + sample.WaveFile + " " + note.Duration);
                // Release phase
                if (vTransitionTime > 0f)
                {
                    float dtime  = 0f;
                    float volume = 0;

                    try
                    {
                        volume = audio.volume;
                        end    = Time.realtimeSinceStartup + vTransitionTime;
                    }
                    catch (Exception)
                    {
                    }

                    do
                    {
                        dtime = end - Time.realtimeSinceStartup;
                        try
                        {
                            audio.volume = Mathf.Lerp(0f, volume, dtime / vTransitionTime);
                            if (dtime < 0f || !audio.isPlaying)
                            {
                                break;
                            }
                        }
                        catch (Exception)
                        {
                            break;
                        }
                        yield return(new WaitForSeconds(0.01f));
                    }while (true);
                }

                try
                {
                    audio.Stop();
                }
                catch (Exception)
                {
                }
            }
            //else
            //{
            //    // play with no loop (drum)
            //    while (audio.isPlaying)
            //    {
            //        yield return new WaitForSeconds(0.01f);
            //    }
            //}
        }