void InstrumentSwitch(object obj, InstrumentEvent evt)
 {
     currentInstrument = evt.instrument;
     foreach (NoteObject noteObj in notes.Values)
     {
         noteObj.fade = noteObj.note.instrument != evt.instrument;
     }
 }
Пример #2
0
        public static Midi Create(Mid mid)
        {
            Midi midi = new Midi();

            midi.Division = mid.Division as Mid.TicksPerBeatDivision;
            List <NoteEvent> OpenNotes = new List <NoteEvent>();

            foreach (Mid.Track t in mid.Tracks)
            {
                Track track = new Track();
                ulong time  = 0;
                foreach (Mid.Event e in t.Events)
                {
                    time += e.DeltaTime;
                    if (e is Mid.MetaEvent)
                    {
                        Mid.MetaEvent meta = e as Mid.MetaEvent;
                        if (meta.Type == 0x3)
                        {
                            track.Name = Util.Encoding.GetString(meta.Data);
                            if (midi.Name == null)
                            {
                                midi.Name = track.Name;
                            }
                        }
                        else if (meta.Type == 0x1)
                        {
                            track.Comments.Add(new TextEvent(time, Util.Encoding.GetString(meta.Data)));
                        }
                        else if (meta.Type == 0x5)
                        {
                            track.Lyrics.Add(new TextEvent(time, Util.Encoding.GetString(meta.Data)));
                        }
                        else if (meta.Type == 0x6)
                        {
                            track.Markers.Add(new TextEvent(time, Util.Encoding.GetString(meta.Data)));
                        }
                        else if (meta.Type == 0x51)
                        {
                            byte[] data = new byte[4];
                            meta.Data.CopyTo(data, 1);
                            midi.BPM.Add(new TempoEvent(time, BigEndianConverter.ToUInt32(data)));
                        }
                        else if (meta.Type == 0x58)
                        {
                            midi.Signature.Add(new TimeSignatureEvent(time, meta.Data[0], meta.Data[1], meta.Data[2], meta.Data[3]));
                        }
                        else if (meta.Type == 0x2F)
                        {
                        }                         //else
                                                  //throw new Exception("OHSHIT");
                    }
                    else if (e is Mid.ChannelEvent)
                    {
                        Mid.ChannelEvent c = e as Mid.ChannelEvent;
                        if (c.Type == 0x8 || (c.Type == 0x9 && c.Parameter2 == 0))                           // Note off
                        // ( A note on with velocity 0 acts as a note off )
                        {
                            NoteEvent note = OpenNotes.Find(n => n.Note == c.Parameter1);
                            if (note != null)
                            {
                                note.Duration        = time - note.Time;
                                note.ReleaseVelocity = c.Parameter2;
                                OpenNotes.Remove(note);
                            }
                        }
                        else if (c.Type == 0x9)                             // Note on
                        {
                            NoteEvent note = new NoteEvent(time, c.Channel, c.Parameter1, c.Parameter2, 0);
                            OpenNotes.Add(note);
                            track.Notes.Add(note);
                        }
                        else if (c.Type == 0xC)                             // Program Change
                        {
                            InstrumentEvent change = new InstrumentEvent(time, c.Channel, c.Parameter1, c.Parameter2);
                            track.Instruments.Add(change);
                        }
                        else if (c.Type == 0xB)                             // Controller
                        {
                            switch (c.Parameter1)
                            {
                            case 0x00:                                     // Bank Select
                                BankEvent bank = new BankEvent(time, c.Channel, c.Parameter2);
                                track.Banks.Add(bank);
                                break;
                            }
                        }                         //else
                                                  //throw new Exception("OHSHIT");
                    }
                }

                if (track.Notes.Count > 0 || track.Comments.Count > 0 || track.Lyrics.Count > 0)
                {
                    midi.Tracks.Add(track);
                }
            }

            return(midi);
        }