示例#1
0
        /// <summary>
        /// Stop playing the note. All waves associated to the note are stop by sending a noteoff.
        /// </summary>
        /// <param name="pnote"></param>
        public void MPTK_StopEvent(MPTKEvent pnote)
        {
            if (!MPTK_CorePlayer)
            {
                StopEvent(pnote);
            }
            else
            {
                QueueSynthCommand.Enqueue(new SynthCommand()
                {
                    Command = SynthCommand.enCmd.StopEvent, MidiEvent = pnote
                });
            }

            //try
            //{
            //    if (pnote != null && pnote.Voices != null)
            //    {
            //        foreach (fluid_voice voice in pnote.Voices)
            //            if (voice.volenv_section != fluid_voice_envelope_index.FLUID_VOICE_ENVRELEASE &&
            //                voice.status != fluid_voice_status.FLUID_VOICE_OFF)
            //                voice.fluid_voice_noteoff();
            //    }
            //}
            //catch (System.Exception ex)
            //{
            //    MidiPlayerGlobal.ErrorDetail(ex);
            //}
        }
示例#2
0
        private IEnumerator <float> TheadPlay(MPTKEvent note)
        {
            if (note != null)
            {
                try
                {
                    if (!MPTK_PauseOnDistance || MidiPlayerGlobal.MPTK_DistanceToListener(this.transform) <= VoiceTemplate.Audiosource.maxDistance)
                    {
#if DEBUGPERF
                        DebugPerf("-----> Init perf:", 0);
#endif
                        PlayEvent(note);
                        //Debug.Log("where is it");
#if DEBUGPERF
                        DebugPerf("<---- ClosePerf perf:", 2);
#endif
                    }
                }
                catch (System.Exception ex)
                {
                    MidiPlayerGlobal.ErrorDetail(ex);
                }
            }
            yield return(0);
        }
示例#3
0
 /// <summary>
 /// Play one midi event with a thread so the call return immediately.
 ///! @snippet MusicView.cs Example PlayNote
 /// </summary>
 public void MPTK_PlayEvent(MPTKEvent evnt)
 {
     try
     {
         if (MidiPlayerGlobal.MPTK_SoundFontLoaded)
         {
             if (!MPTK_CorePlayer)
             {
                 Timing.RunCoroutine(TheadPlay(evnt));
             }
             else
             {
                 QueueSynthCommand.Enqueue(new SynthCommand()
                 {
                     Command = SynthCommand.enCmd.StartEvent, MidiEvent = evnt
                 });
             }
         }
         else
         {
             Debug.LogWarningFormat("SoundFont not yet loaded, Midi Event cannot be processed Code:{0} Channel:{1}", evnt.Command, evnt.Channel);
         }
     }
     catch (System.Exception ex)
     {
         MidiPlayerGlobal.ErrorDetail(ex);
     }
 }
示例#4
0
        /// <summary>
        /// Stop playing the note. All waves associated to the note are stop by sending a noteoff.
        /// </summary>
        /// <param name="pnote"></param>
        public virtual void MPTK_StopEvent(MPTKEvent pnote)
        {
            try
            {
                if (pnote != null && pnote.Voices != null)
                {
                    //Debug.Log(pnote.Value);
                    foreach (fluid_voice note in pnote.Voices)
                    {
                        //Debug.Log(note);

                        if (note.volenv_section != fluid_voice_envelope_index.FLUID_VOICE_ENVRELEASE &&
                            note.status != fluid_voice_status.FLUID_VOICE_OFF)
                        {
                            //&& note.key == val)
                            note.fluid_voice_noteoff();
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                MidiPlayerGlobal.ErrorDetail(ex);
            }
        }
示例#5
0
        private MPTKEvent CreateNoteOffForNote(List <MPTKEvent> mptkEvents, TrackMidiEvent trackEvent)
        {
            MPTKEvent midievent;
            NoteEvent noteoff = (NoteEvent)trackEvent.Event;

            //Debug.Log(string.Format("Track:{0} NoteNumber:{1,3:000} AbsoluteTime:{2,6:000000} NoteLength:{3,6:000000} OffDeltaTime:{4,6:000000} ", track, noteon.NoteNumber, noteon.AbsoluteTime, noteon.NoteLength, noteon.OffEvent.DeltaTime));
            midievent = new MPTKEvent()
            {
                Tick     = trackEvent.AbsoluteQuantize,
                Command  = MPTKCommand.NoteOff,
                Value    = noteoff.NoteNumber,
                Channel  = trackEvent.Event.Channel - 1,
                Velocity = noteoff.Velocity,
                Duration = 0,
                Length   = 0,
            };
            //mptkEvents.Add(midievent);
            // if (LogEvents)
            // {
            //      string notename = (midievent.Channel != 9) ?
            //          String.Format("{0}{1}", NoteNames[midievent.Value % 12], midievent.Value / 12) : "Drum";
            //      Debug.Log(BuildInfoTrack(trackEvent) + string.Format("NoteOff {0,3:000}\t{1,-4}\tLenght:{2}", midievent.Value, notename, " Note Off"));
            //  }

            if (midievent.Channel == selectedChannel)
            {
                Kbd_PKeyGen.instance.ShowMidiNoteOff(noteoff.NoteNumber);
            }

            return(midievent);
        }
示例#6
0
 /// <summary>
 /// Return note length as https://en.wikipedia.org/wiki/Note_value
 /// </summary>
 /// <param name="note"></param>
 /// <returns>MPTKEvent.EnumLength</returns>
 public MPTKEvent.EnumLength MPTK_NoteLength(MPTKEvent note)
 {
     if (miditoplay != null)
     {
         return(miditoplay.NoteLength(note));
     }
     return(MPTKEvent.EnumLength.Sixteenth);
 }
 //has to be called to stop the note after playing
 private void StopOneNote()
 {
     if (notePlaying != null)
     {
         //Debug.Log("Stop note");
         midiStreamPlayer.MPTK_StopEvent(notePlaying);
         notePlaying = null;
     }
 }
 private void StopOneNote()
 {
     if (NotePlaying != null)
     {
         //Debug.Log("Stop note");
         // Stop the note (method to simulate a real human on a keyboard : duration is not known when note is triggered)
         midiStreamPlayer.MPTK_StopEvent(NotePlaying);
         NotePlaying = null;
     }
 }
示例#9
0
        /// <summary>
        /// Helper to create a drum hit
        /// </summary>
        /// <returns></returns>
        private MPTKEvent CreateDrum(int key, float delay)
        {
            MPTKEvent note = new MPTKEvent()
            {
                Command  = MPTKCommand.NoteOn,
                Value    = key,
                Duration = 0,
                Velocity = Velocity,
            };

            return(note);
        }
示例#10
0
        /// <summary>
        /// Helper to create a random note (not yet used)
        /// </summary>
        /// <returns></returns>
        private MPTKEvent CreateRandomNote()
        {
            MPTKEvent note = new MPTKEvent()
            {
                Command  = MPTKCommand.NoteOn,
                Value    = 50 + UnityEngine.Random.Range(0, 4) * 2,
                Duration = UnityEngine.Random.Range(100, 1000),
                Velocity = Velocity,
            };

            return(note);
        }
示例#11
0
        /// <summary>
        /// Helper to create a note
        /// </summary>
        /// <returns></returns>
        private MPTKEvent CreateNote(int key, float delay)
        {
            MPTKEvent note = new MPTKEvent()
            {
                Command  = MPTKCommand.NoteOn,
                Value    = key,
                Duration = DelayTimeChange * 1000f,
                Velocity = Velocity,
            };

            return(note);
        }
示例#12
0
 /// <summary>
 /// Return note length as https://en.wikipedia.org/wiki/Note_value
 /// </summary>
 /// <param name="note"></param>
 /// <returns>MPTKEvent.EnumLength</returns>
 public MPTKEvent.EnumLength MPTK_NoteLength(MPTKEvent note)
 {
     if (miditoload != null)
     {
         return(miditoload.NoteLength(note));
     }
     else
     {
         NoMidiLoaded("MPTK_NoteLength");
     }
     return(MPTKEvent.EnumLength.Sixteenth);
 }
示例#13
0
 private void PlayOneNote(int val)
 {
     // Start playint a new note
     NotePlaying = new MPTKEvent()
     {
         Command  = MPTKCommand.NoteOn,
         Value    = val,
         Channel  = StreamChannel,
         Duration = 9999999, // 9999 seconds but stop by the new note. See before.
         Velocity = Velocity // Sound can vary depending on the velocity
     };
     midiStreamPlayer.MPTK_PlayEvent(NotePlaying);
 }
 //has to be called to compose the note
 private void PlayOneNote()
 {
     //Debug.Log($"{StreamChannel} {midiStreamPlayer.MPTK_ChannelPresetGetName(StreamChannel)}");
     // Start playing a new note
     notePlaying = new MPTKEvent()
     {
         Command  = MPTKCommand.NoteOn,         //gets triggerd when all components are active
         Value    = currentNote,                //tone frequency
         Channel  = streamChannel,              //stays the same
         Duration = -1,                         //plays until stoped or fades away
         Velocity = velocity,                   //sound volume
     };
     midiStreamPlayer.MPTK_PlayEvent(notePlaying);
 }
示例#15
0
        public void PlayOneNote()
        {
            StopOneNote();

            NotePlaying = new MPTKEvent()
            {
                Command  = MPTKCommand.NoteOn,
                Value    = CurrentNote,
                Channel  = StreamChannel,
                Duration = 9999999, // 9999 seconds but stop by the new note. See before.
                Velocity = Velocity // Sound can vary depending on the velocity
            };
            midiStreamPlayer.MPTK_PlayEvent(NotePlaying);
        }
示例#16
0
 //! [Example MPTK_PlayEvent]
 /// <summary>
 /// Send the note to the player. Notes are plays in a thread, so call returns immediately.
 /// The note is stopped automatically after the Duration defined.
 /// </summary>
 private void PlayOneNote()
 {
     // Start playint a new note
     NotePlaying = new MPTKEvent()
     {
         Command  = MPTKCommand.NoteOn,
         Value    = CurrentNote,
         Channel  = StreamChannel,
         Duration = Convert.ToInt64(NoteDuration * 1000f), // millisecond, -1 to play undefinitely
         Velocity = Velocity,                              // Sound can vary depending on the velocity
         Delay    = Convert.ToInt64(NoteDelay * 1000f),
     };
     midiStreamPlayer.MPTK_PlayEvent(NotePlaying);
 }
示例#17
0
        /// <summary>
        /// Play one Midi event
        /// @snippet MusicView.cs Example PlayNote
        /// </summary>
        /// <param name="midievent"></param>
        protected void PlayEvent(MPTKEvent midievent)
        {
            try
            {
                if (MidiPlayerGlobal.CurrentMidiSet == null || MidiPlayerGlobal.CurrentMidiSet.ActiveSounFontInfo == null)
                {
                    Debug.Log("No SoundFont selected for MPTK_PlayNote ");
                    return;
                }

                switch (midievent.Command)
                {
                case MPTKCommand.NoteOn:
                    if (midievent.Velocity != 0)
                    {
#if DEBUGNOTE
                        numberNote++;
                        if (numberNote < startNote || numberNote > startNote + countNote - 1)
                        {
                            return;
                        }
#endif
                        //if (note.Channel==4)
                        synth_noteon(midievent);
                    }
                    break;

                case MPTKCommand.ControlChange:
                    if (MPTK_ApplyRealTimeModulator)
                    {
                        Channels[midievent.Channel].fluid_channel_cc(midievent.Controller, midievent.Value);     // replace of fluid_synth_cc(note.Channel, note.Controller, (int)note.Value);
                    }
                    break;

                case MPTKCommand.PatchChange:
                    if (midievent.Channel != 9 || MPTK_EnablePresetDrum == true)
                    {
                        fluid_synth_program_change(midievent.Channel, midievent.Value);
                    }
                    break;
                }
            }
            catch (System.Exception ex)
            {
                MidiPlayerGlobal.ErrorDetail(ex);
            }
        }
示例#18
0
        public void PlayDiminishedChord(int inversion)
        {
            int bass  = CurrentNote;
            int third = CurrentNote + 3;
            int fifth = CurrentNote + 6;

            if (inversion == 1)
            {
                bass += 12;
            }
            else if (inversion == 2)
            {
                bass  += 12;
                third += 12;
            }

            StopChord();
            NotePlaying = new MPTKEvent()
            {
                Command  = MPTKCommand.NoteOn,
                Value    = bass,
                Channel  = StreamChannel,
                Duration = 9999999, // 9999 seconds but stop by the new note. See before.
                Velocity = Velocity // Sound can vary depending on the velocity
            };
            midiStreamPlayer.MPTK_PlayEvent(NotePlaying);

            Third = new MPTKEvent()
            {
                Command  = MPTKCommand.NoteOn,
                Value    = third,
                Channel  = StreamChannel,
                Duration = 9999999, // 9999 seconds but stop by the new note. See before.
                Velocity = Velocity // Sound can vary depending on the velocity
            };
            midiStreamPlayer.MPTK_PlayEvent(Third);

            Fifth = new MPTKEvent()
            {
                Command  = MPTKCommand.NoteOn,
                Value    = fifth,
                Channel  = StreamChannel,
                Duration = 9999999, // 9999 seconds but stop by the new note. See before.
                Velocity = Velocity // Sound can vary depending on the velocity
            };
            midiStreamPlayer.MPTK_PlayEvent(Fifth);
        }
示例#19
0
 /// <summary>
 /// Play one midi event with a thread so the call return immediately.
 ///! @snippet MusicView.cs Example PlayNote
 /// </summary>
 public virtual void MPTK_PlayEvent(MPTKEvent note)
 {
     try
     {
         if (MidiPlayerGlobal.MPTK_SoundFontLoaded)
         {
             Timing.RunCoroutine(TheadPlay(note));
         }
         else
         {
             Debug.LogWarningFormat("SoundFont not yet loaded, Midi Event cannot be processed Code:{0} Channel:{1}", note.Command, note.Channel);
         }
     }
     catch (System.Exception ex)
     {
         MidiPlayerGlobal.ErrorDetail(ex);
     }
 }
示例#20
0
 /// <summary>
 /// https://en.wikipedia.org/wiki/Note_value
 /// </summary>
 /// <param name="note"></param>
 /// <returns></returns>
 public MPTKEvent.EnumLength NoteLength(MPTKEvent note)
 {
     if (midifile != null)
     {
         if (note.Length >= midifile.DeltaTicksPerQuarterNote * 4)
         {
             return(MPTKEvent.EnumLength.Whole);
         }
         else if (note.Length >= midifile.DeltaTicksPerQuarterNote * 2)
         {
             return(MPTKEvent.EnumLength.Half);
         }
         else if (note.Length >= midifile.DeltaTicksPerQuarterNote)
         {
             return(MPTKEvent.EnumLength.Quarter);
         }
         else if (note.Length >= midifile.DeltaTicksPerQuarterNote / 2)
         {
             return(MPTKEvent.EnumLength.Eighth);
         }
     }
     return(MPTKEvent.EnumLength.Sixteenth);
 }
示例#21
0
        /// <summary>
        /// Add a TrackMidiEvent to a list of MPTKEvent.
        /// </summary>
        /// <param name="mptkEvents">Must be alloc before the call</param>
        /// <param name="trackEvent"></param>
        /// <returns></returns>
        private void ConvertToEvent(List <MPTKEvent> mptkEvents, TrackMidiEvent trackEvent)
        {
            MPTKEvent midievent = null;

            switch (trackEvent.Event.CommandCode)
            {
            case MidiCommandCode.NoteOn:
                //if (((NoteOnEvent)trackEvent.Event).OffEvent != null)
            {
                NoteOnEvent noteon = (NoteOnEvent)trackEvent.Event;
                //Debug.Log(string.Format("Track:{0} NoteNumber:{1,3:000} AbsoluteTime:{2,6:000000} NoteLength:{3,6:000000} OffDeltaTime:{4,6:000000} ", track, noteon.NoteNumber, noteon.AbsoluteTime, noteon.NoteLength, noteon.OffEvent.DeltaTime));
                if (noteon.OffEvent != null)
                {
                    midievent = new MPTKEvent()
                    {
                        Track    = trackEvent.IndexTrack,
                        Tick     = trackEvent.AbsoluteQuantize,
                        Command  = MPTKCommand.NoteOn,
                        Value    = noteon.NoteNumber,
                        Channel  = trackEvent.Event.Channel - 1,
                        Velocity = noteon.Velocity,
                        Duration = Convert.ToInt64(noteon.NoteLength * MPTK_PulseLenght),
                        Length   = noteon.NoteLength,
                    };
                    mptkEvents.Add(midievent);
                    if (LogEvents && seek_ticks < 0)
                    {
                        string notename = (midievent.Channel != 9) ?
                                          String.Format("{0}{1}", NoteNames[midievent.Value % 12], midievent.Value / 12) : "Drum";
                        Debug.Log(BuildInfoTrack(trackEvent) + string.Format("NoteOn  {0,3:000}\t{1,-4}\tLenght:{2,5}\t{3}\tVeloc:{4,3}",
                                                                             midievent.Value, notename, noteon.NoteLength, NoteLength(midievent), noteon.Velocity));
                    }
                }
                else         // It's a noteoff
                {
                    if (KeepNoteOff)
                    {
                        midievent = new MPTKEvent()
                        {
                            Track    = trackEvent.IndexTrack,
                            Tick     = trackEvent.AbsoluteQuantize,
                            Command  = MPTKCommand.NoteOff,
                            Value    = noteon.NoteNumber,
                            Channel  = trackEvent.Event.Channel - 1,
                            Velocity = noteon.Velocity,
                            Duration = Convert.ToInt64(noteon.NoteLength * MPTK_PulseLenght),
                            Length   = noteon.NoteLength,
                        };
                        mptkEvents.Add(midievent);

                        if (LogEvents && seek_ticks < 0)
                        {
                            string notename = (midievent.Channel != 9) ?
                                              String.Format("{0}{1}", NoteNames[midievent.Value % 12], midievent.Value / 12) : "Drum";
                            Debug.Log(BuildInfoTrack(trackEvent) + string.Format("NoteOff {0,3:000}\t{1,-4}\tLenght:{2}", midievent.Value, notename, " Note Off"));
                        }
                    }
                }
            }
            break;

            case MidiCommandCode.NoteOff:
                if (KeepNoteOff)
                {
                    NoteEvent noteoff = (NoteEvent)trackEvent.Event;
                    //Debug.Log(string.Format("Track:{0} NoteNumber:{1,3:000} AbsoluteTime:{2,6:000000} NoteLength:{3,6:000000} OffDeltaTime:{4,6:000000} ", track, noteon.NoteNumber, noteon.AbsoluteTime, noteon.NoteLength, noteon.OffEvent.DeltaTime));
                    midievent = new MPTKEvent()
                    {
                        Track    = trackEvent.IndexTrack,
                        Tick     = trackEvent.AbsoluteQuantize,
                        Command  = MPTKCommand.NoteOff,
                        Value    = noteoff.NoteNumber,
                        Channel  = trackEvent.Event.Channel - 1,
                        Velocity = noteoff.Velocity,
                        Duration = 0,
                        Length   = 0,
                    };

                    mptkEvents.Add(midievent);

                    if (LogEvents && seek_ticks < 0)
                    {
                        string notename = (midievent.Channel != 9) ?
                                          String.Format("{0}{1}", NoteNames[midievent.Value % 12], midievent.Value / 12) : "Drum";
                        Debug.Log(BuildInfoTrack(trackEvent) + string.Format("NoteOff {0,3:000}\t{1,-4}\tLenght:{2}", midievent.Value, notename, " Note Off"));
                    }
                }
                break;

            case MidiCommandCode.PitchWheelChange:
                PitchWheelChangeEvent pitch = (PitchWheelChangeEvent)trackEvent.Event;
                midievent = new MPTKEvent()
                {
                    Track   = trackEvent.IndexTrack,
                    Tick    = trackEvent.AbsoluteQuantize,
                    Command = MPTKCommand.PitchWheelChange,
                    Channel = trackEvent.Event.Channel - 1,
                    Value   = pitch.Pitch,    // Pitch Wheel Value 0 is minimum, 0x2000 (8192) is default, 0x3FFF (16383) is maximum
                };
                mptkEvents.Add(midievent);
                if (LogEvents && seek_ticks < 0)
                {
                    Debug.Log(BuildInfoTrack(trackEvent) + string.Format("PitchWheelChange {0}", pitch.Pitch));
                }
                break;

            case MidiCommandCode.ControlChange:
                ControlChangeEvent controlchange = (ControlChangeEvent)trackEvent.Event;
                midievent = new MPTKEvent()
                {
                    Track      = trackEvent.IndexTrack,
                    Tick       = trackEvent.AbsoluteQuantize,
                    Command    = MPTKCommand.ControlChange,
                    Channel    = trackEvent.Event.Channel - 1,
                    Controller = (MPTKController)controlchange.Controller,
                    Value      = controlchange.ControllerValue,
                };

                //if ((MPTKController)controlchange.Controller != MPTKController.Sustain)
                mptkEvents.Add(midievent);

                // Other midi event
                if (LogEvents && seek_ticks < 0)
                {
                    Debug.Log(BuildInfoTrack(trackEvent) + string.Format("Control {0} {1}", controlchange.Controller, controlchange.ControllerValue));
                }

                break;

            case MidiCommandCode.PatchChange:
                PatchChangeEvent change = (PatchChangeEvent)trackEvent.Event;
                midievent = new MPTKEvent()
                {
                    Track   = trackEvent.IndexTrack,
                    Tick    = trackEvent.AbsoluteQuantize,
                    Command = MPTKCommand.PatchChange,
                    Channel = trackEvent.Event.Channel - 1,
                    Value   = change.Patch,
                };
                mptkEvents.Add(midievent);
                if (LogEvents && seek_ticks < 0)
                {
                    Debug.Log(BuildInfoTrack(trackEvent) + string.Format("Patch   {0,3:000} {1}", change.Patch, PatchChangeEvent.GetPatchName(change.Patch)));
                }
                break;

            case MidiCommandCode.MetaEvent:
                MetaEvent meta = (MetaEvent)trackEvent.Event;
                midievent = new MPTKEvent()
                {
                    Track   = trackEvent.IndexTrack,
                    Tick    = trackEvent.AbsoluteQuantize,
                    Command = MPTKCommand.MetaEvent,
                    Channel = trackEvent.Event.Channel - 1,
                    Meta    = (MPTKMeta)meta.MetaEventType,
                };

                switch (meta.MetaEventType)
                {
                case MetaEventType.EndTrack:
                    midievent.Info = "End Track";
                    break;

                case MetaEventType.TimeSignature:
                    AnalyzeTimeSignature(meta, trackEvent);
                    break;

                case MetaEventType.SetTempo:
                    if (EnableChangeTempo)
                    {
                        TempoEvent tempo = (TempoEvent)meta;
                        // Tempo change will be done in MidiFilePlayer
                        midievent.Duration = (long)tempo.Tempo;
                        MPTK_MicrosecondsPerQuarterNote = tempo.MicrosecondsPerQuarterNote;
                        fluid_player_set_midi_tempo(tempo.MicrosecondsPerQuarterNote);

                        // Force exit loop
                        if (LogEvents && seek_ticks < 0)
                        {
                            Debug.Log(BuildInfoTrack(trackEvent) + string.Format("Meta     {0,-15} Tempo:{1} MicrosecondsPerQuarterNote:{2}", meta.MetaEventType, tempo.Tempo, tempo.MicrosecondsPerQuarterNote));
                        }
                    }
                    break;

                case MetaEventType.SequenceTrackName:
                    midievent.Info = ((TextEvent)meta).Text;
                    if (!string.IsNullOrEmpty(SequenceTrackName))
                    {
                        SequenceTrackName += "\n";
                    }
                    SequenceTrackName += string.Format("T{0,2:00} {1}", trackEvent.IndexTrack, midievent.Info);
                    break;

                case MetaEventType.ProgramName:
                    midievent.Info = ((TextEvent)meta).Text;
                    ProgramName   += midievent.Info + " ";
                    break;

                case MetaEventType.TrackInstrumentName:
                    midievent.Info = ((TextEvent)meta).Text;
                    if (!string.IsNullOrEmpty(TrackInstrumentName))
                    {
                        TrackInstrumentName += "\n";
                    }
                    TrackInstrumentName += string.Format("T{0,2:00} {1}", trackEvent.IndexTrack, midievent.Info);
                    break;

                case MetaEventType.TextEvent:
                    midievent.Info = ((TextEvent)meta).Text;
                    TextEvent     += midievent.Info + " ";
                    break;

                case MetaEventType.Copyright:
                    midievent.Info = ((TextEvent)meta).Text;
                    Copyright     += midievent.Info + " ";
                    break;

                case MetaEventType.Lyric:         // lyric
                    midievent.Info = ((TextEvent)meta).Text;
                    TextEvent     += midievent.Info + " ";
                    break;

                case MetaEventType.Marker:         // marker
                    midievent.Info = ((TextEvent)meta).Text;
                    TextEvent     += midievent.Info + " ";
                    break;

                case MetaEventType.CuePoint:         // cue point
                case MetaEventType.DeviceName:
                    break;
                }

                if (LogEvents && !string.IsNullOrEmpty(midievent.Info) && seek_ticks < 0)
                {
                    Debug.Log(BuildInfoTrack(trackEvent) + string.Format("Meta     {0,-15} '{1}'", midievent.Meta, midievent.Info));
                }

                mptkEvents.Add(midievent);
                //Debug.Log(BuildInfoTrack(trackEvent) + string.Format("Meta {0} {1}", meta.MetaEventType, meta.ToString()));
                break;

            default:
                // Other midi event
                if (LogEvents && seek_ticks < 0)
                {
                    Debug.Log(BuildInfoTrack(trackEvent) + string.Format("Other    {0,-15} Not handle by MPTK", trackEvent.Event.CommandCode));
                }
                break;
            }
        }
示例#22
0
        /// <summary>
        /// Read midi event Tempo and Patch change from start
        /// </summary>
        /// <param name="timeFromStartMS"></param>
        /// <returns></returns>
        public List <MPTKEvent> ReadChangeFromStart(int position)
        {
            List <MPTKEvent> midievents = new List <MPTKEvent>();;

            try
            {
                if (midifile != null)
                {
                    if (position < 0 || position >= MidiSorted.Count)
                    {
                        position = MidiSorted.Count - 1;
                    }

                    for (int currentPosEvent = 0; currentPosEvent < position; currentPosEvent++)
                    {
                        TrackMidiEvent trackEvent = MidiSorted[currentPosEvent];
                        MPTKEvent      midievent  = null;
                        switch (trackEvent.Event.CommandCode)
                        {
                        case MidiCommandCode.ControlChange:
                            ControlChangeEvent controlchange = (ControlChangeEvent)trackEvent.Event;
                            midievent = new MPTKEvent()
                            {
                                Tick       = trackEvent.AbsoluteQuantize,
                                Command    = MPTKCommand.ControlChange,
                                Channel    = trackEvent.Event.Channel - 1,
                                Controller = (MPTKController)controlchange.Controller,
                                Value      = controlchange.ControllerValue,
                            };
                            break;

                        case MidiCommandCode.PatchChange:
                            PatchChangeEvent change = (PatchChangeEvent)trackEvent.Event;
                            midievent = new MPTKEvent()
                            {
                                Tick    = trackEvent.AbsoluteQuantize,
                                Command = MPTKCommand.PatchChange,
                                Channel = trackEvent.Event.Channel - 1,
                                Value   = change.Patch,
                            };
                            break;

                        case MidiCommandCode.MetaEvent:
                            MetaEvent meta = (MetaEvent)trackEvent.Event;
                            if (meta.MetaEventType == MetaEventType.SetTempo)
                            {
                                TempoEvent tempo = (TempoEvent)meta;
                                midievent = new MPTKEvent()
                                {
                                    Tick     = trackEvent.AbsoluteQuantize,
                                    Command  = MPTKCommand.MetaEvent,
                                    Channel  = trackEvent.Event.Channel - 1,
                                    Meta     = (MPTKMeta)meta.MetaEventType,
                                    Duration = tempo.Tempo,
                                };
                            }
                            break;
                        }
                        if (midievent != null)
                        {
                            midievents.Add(midievent);
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                MidiPlayerGlobal.ErrorDetail(ex);
            }
            return(midievents);
        }
示例#23
0
        /// <summary>
        /// Add a TrackMidiEvent to a list of MPTKEvent.
        /// </summary>
        /// <param name="mptkEvents">Must be alloc before the call</param>
        /// <param name="trackEvent"></param>
        /// <returns></returns>

        private bool ConvertToEventForNote(List <MPTKEvent> mptkEvents, TrackMidiEvent trackEvent)
        {
            bool      exitLoop  = false;
            MPTKEvent midievent = null;

            switch (trackEvent.Event.CommandCode)
            {
            case MidiCommandCode.NoteOn:

                if (((NoteOnEvent)trackEvent.Event).OffEvent != null)
                {
                    NoteOnEvent noteon = (NoteOnEvent)trackEvent.Event;
                    //Debug.Log(string.Format("Track:{0} NoteNumber:{1,3:000} AbsoluteTime:{2,6:000000} NoteLength:{3,6:000000} OffDeltaTime:{4,6:000000} ", track, noteon.NoteNumber, noteon.AbsoluteTime, noteon.NoteLength, noteon.OffEvent.DeltaTime));
                    if (noteon.NoteLength > 0)
                    {
                        midievent = new MPTKEvent()
                        {
                            Tick     = trackEvent.AbsoluteQuantize,
                            Command  = MPTKCommand.NoteOn,
                            Value    = noteon.NoteNumber,
                            Channel  = trackEvent.Event.Channel - 1,
                            Velocity = noteon.Velocity,
                            Duration = noteon.NoteLength * TickLengthMs,
                            Length   = noteon.NoteLength,
                        };
                        //mptkEvents.Add(midievent);

                        // show playing note
                        //Debug.Log(midievent.Channel);
                        if (midievent.Channel == selectedChannel)
                        {
                            Kbd_PKeyGen.instance.ShowMidiNoteOn(noteon.NoteNumber);
                        }

                        if (LogEvents)
                        {
                            string notename = (midievent.Channel != 9) ?
                                              String.Format("{0}{1}", NoteNames[midievent.Value % 12], midievent.Value / 12) : "Drum";
                            Debug.Log(BuildInfoTrack(trackEvent) + string.Format("NoteOn  {0,3:000}\t{1,-4}\tLenght:{2,5}\t{3}\tVeloc:{4,3}",
                                                                                 midievent.Value, notename, noteon.NoteLength, NoteLength(midievent), noteon.Velocity));
                        }
                    }
                    else if (KeepNoteOff)
                    {
                        midievent = CreateNoteOffForNote(mptkEvents, trackEvent);
                    }
                }
                break;

            case MidiCommandCode.NoteOff:
                midievent = CreateNoteOffForNote(mptkEvents, trackEvent);
                break;

            case MidiCommandCode.ControlChange:

                ControlChangeEvent controlchange = (ControlChangeEvent)trackEvent.Event;
                midievent = new MPTKEvent()
                {
                    Tick       = trackEvent.AbsoluteQuantize,
                    Command    = MPTKCommand.ControlChange,
                    Channel    = trackEvent.Event.Channel - 1,
                    Controller = (MPTKController)controlchange.Controller,
                    Value      = controlchange.ControllerValue,
                };
                mptkEvents.Add(midievent);

                // Other midi event
                if (LogEvents)
                {
                    Debug.Log(BuildInfoTrack(trackEvent) + string.Format("Control {0} {1}", controlchange.Controller, controlchange.ControllerValue));
                }

                break;

            case MidiCommandCode.PatchChange:
                PatchChangeEvent change = (PatchChangeEvent)trackEvent.Event;
                midievent = new MPTKEvent()
                {
                    Tick    = trackEvent.AbsoluteQuantize,
                    Command = MPTKCommand.PatchChange,
                    Channel = trackEvent.Event.Channel - 1,
                    Value   = change.Patch,
                };
                mptkEvents.Add(midievent);
                if (LogEvents)
                {
                    Debug.Log(BuildInfoTrack(trackEvent) + string.Format("Patch   {0,3:000} {1}", change.Patch, PatchChangeEvent.GetPatchName(change.Patch)));
                }
                break;

            case MidiCommandCode.MetaEvent:
                MetaEvent meta = (MetaEvent)trackEvent.Event;
                midievent = new MPTKEvent()
                {
                    Tick    = trackEvent.AbsoluteQuantize,
                    Command = MPTKCommand.MetaEvent,
                    Channel = trackEvent.Event.Channel - 1,
                    Meta    = (MPTKMeta)meta.MetaEventType,
                };

                switch (meta.MetaEventType)
                {
                case MetaEventType.TimeSignature:
                    AnalyzeTimeSignature(meta);
                    break;

                case MetaEventType.SetTempo:
                    TempoEvent tempo = (TempoEvent)meta;
                    // Tempo change will be done in MidiFilePlayer
                    midievent.Duration = tempo.Tempo;
                    MPTK_MicrosecondsPerQuarterNote = tempo.MicrosecondsPerQuarterNote;
                    // Force exit loop
                    exitLoop = true;
                    break;

                case MetaEventType.SequenceTrackName:
                    midievent.Info = ((TextEvent)meta).Text;
                    if (!string.IsNullOrEmpty(SequenceTrackName))
                    {
                        SequenceTrackName += "\n";
                    }
                    SequenceTrackName += string.Format("T{0,2:00} {1}", trackEvent.IndexTrack, midievent.Info);
                    //if (LogEvents) Debug.Log(BuildInfoTrack(trackEvent) + string.Format("Sequence   '{0}'", note.Info));
                    break;

                case MetaEventType.ProgramName:
                    midievent.Info = ((TextEvent)meta).Text;
                    ProgramName   += midievent.Info + " ";
                    //if (LogEvents) Debug.Log(BuildInfoTrack(trackEvent) + string.Format("Program   '{0}'", note.Info));
                    break;

                case MetaEventType.TrackInstrumentName:
                    midievent.Info = ((TextEvent)meta).Text;
                    if (!string.IsNullOrEmpty(TrackInstrumentName))
                    {
                        TrackInstrumentName += "\n";
                    }
                    TrackInstrumentName += string.Format("T{0,2:00} {1}", trackEvent.IndexTrack, midievent.Info);
                    //if (LogEvents) Debug.Log(BuildInfoTrack(trackEvent) + string.Format("Text      '{0}'", ((TextEvent)meta).Text));
                    break;

                case MetaEventType.TextEvent:
                    midievent.Info = ((TextEvent)meta).Text;
                    TextEvent     += midievent.Info + " ";
                    //if (LogEvents) Debug.Log(BuildInfoTrack(trackEvent) + string.Format("Sequence  '{0}'", ((TextEvent)meta).Text));
                    break;

                case MetaEventType.Copyright:
                    midievent.Info = ((TextEvent)meta).Text;
                    Copyright     += midievent.Info + " ";
                    //if (LogEvents) Debug.Log(BuildInfoTrack(trackEvent) + string.Format("Copyright '{0}'", ((TextEvent)meta).Text));
                    break;

                case MetaEventType.Lyric:         // lyric
                    midievent.Info = ((TextEvent)meta).Text;
                    break;

                case MetaEventType.Marker:         // marker
                case MetaEventType.CuePoint:       // cue point
                case MetaEventType.DeviceName:
                    break;
                }

                if (LogEvents && !string.IsNullOrEmpty(midievent.Info))
                {
                    Debug.Log(BuildInfoTrack(trackEvent) + string.Format("Meta    {0,15} '{1}'", midievent.Meta, midievent.Info));
                }

                mptkEvents.Add(midievent);
                //Debug.Log(BuildInfoTrack(trackEvent) + string.Format("Meta {0} {1}", meta.MetaEventType, meta.ToString()));
                break;

            default:
                // Other midi event
                if (LogEvents)
                {
                    Debug.Log(BuildInfoTrack(trackEvent) + string.Format("Other   {0,15} Not handle by MPTK", trackEvent.Event.CommandCode));
                }
                break;
            }

            return(exitLoop);
        }