Exemple #1
0
        /// <summary>
        /// Creates a MidiEvent from a raw message received using
        /// the MME MIDI In APIs
        /// </summary>
        /// <param name="rawMessage">The short MIDI message</param>
        /// <returns>A new MIDI Event</returns>
        public static MidiEvent FromRawMessage(int rawMessage)
        {
            long absoluteTime = 0;
            int b = rawMessage & 0xFF;
            int data1 = (rawMessage >> 8) & 0xFF;
            int data2 = (rawMessage >> 16) & 0xFF;
            MidiCommandCode commandCode;
            int channel = 1;

            if ((b & 0xF0) == 0xF0)
            {
                // both bytes are used for command code in this case
                commandCode = (MidiCommandCode)b;
            }
            else
            {
                commandCode = (MidiCommandCode)(b & 0xF0);
                channel = (b & 0x0F) + 1;
            }

            MidiEvent me;
            switch (commandCode)
            {
                case MidiCommandCode.NoteOn:
                case MidiCommandCode.NoteOff:
                case MidiCommandCode.KeyAfterTouch:
                    if (data2 > 0 && commandCode == MidiCommandCode.NoteOn)
                    {
                        me = new NoteOnEvent(absoluteTime, channel, data1, data2, 0);
                    }
                    else
                    {
                        me = new NoteEvent(absoluteTime, channel, commandCode, data1, data2);
                    }
                    break;
                //case MidiCommandCode.ControlChange:
               //     me = new ControlChangeEvent(absoluteTime,channel,(MidiController)data1,data2);
                //    break;
                case MidiCommandCode.PatchChange:
                    me = new PatchChangeEvent(absoluteTime,channel,data1);
                    break;
                case MidiCommandCode.ChannelAfterTouch:
                    me = new ChannelAfterTouchEvent(absoluteTime,channel,data1);
                    break;
                case MidiCommandCode.PitchWheelChange:
                    me = new PitchWheelChangeEvent(absoluteTime, channel, data1 + (data2 << 7));
                    break;
                case MidiCommandCode.TimingClock:
                case MidiCommandCode.StartSequence:
                case MidiCommandCode.ContinueSequence:
                case MidiCommandCode.StopSequence:
                case MidiCommandCode.AutoSensing:
                    me = new MidiEvent(absoluteTime,channel,commandCode);
                    break;
                case MidiCommandCode.MetaEvent:
                case MidiCommandCode.Sysex:
                default:
                    throw new FormatException(String.Format("Unsupported MIDI Command Code for Raw Message {0}", commandCode));
            }
            return me;
        }
Exemple #2
0
        /// <summary>
        /// Constructs a MidiEvent from a BinaryStream
        /// </summary>
        /// <param name="br">The binary stream of MIDI data</param>
        /// <param name="previous">The previous MIDI event (pass null for first event)</param>
        /// <returns>A new MidiEvent</returns>
        public static MidiEvent ReadNextEvent(BinaryReader br, MidiEvent previous)
        {
            int deltaTime = MidiEvent.ReadVarInt(br);
            MidiCommandCode commandCode;
            int channel = 1;
            byte b = br.ReadByte();
            if((b & 0x80) == 0)
            {
                // a running command - command & channel are same as previous
                commandCode = previous.CommandCode;
                channel = previous.Channel;
                br.BaseStream.Position--; // need to push this back
            }
            else
            {
                if((b & 0xF0) == 0xF0)
                {
                    // both bytes are used for command code in this case
                    commandCode = (MidiCommandCode) b;
                }
                else
                {
                    commandCode = (MidiCommandCode) (b & 0xF0);
                    channel = (b & 0x0F) + 1;
                }
            }

            MidiEvent me;
            switch(commandCode)
            {
            case MidiCommandCode.NoteOn:
                me = new NoteOnEvent(br);
                break;
            case MidiCommandCode.NoteOff:
            case MidiCommandCode.KeyAfterTouch:
                me = new NoteEvent(br);
                break;
            //case MidiCommandCode.ControlChange:
               //     me = new ControlChangeEvent(br);
               //     break;
            case MidiCommandCode.PatchChange:
                me = new PatchChangeEvent(br);
                break;
            case MidiCommandCode.ChannelAfterTouch:
                me = new ChannelAfterTouchEvent(br);
                break;
            case MidiCommandCode.PitchWheelChange:
                me = new PitchWheelChangeEvent(br);
                break;
            case MidiCommandCode.TimingClock:
            case MidiCommandCode.StartSequence:
            case MidiCommandCode.ContinueSequence:
            case MidiCommandCode.StopSequence:
                me = new MidiEvent();
                break;
            case MidiCommandCode.Sysex:
                me = SysexEvent.ReadSysexEvent(br);
                break;
            case MidiCommandCode.MetaEvent:
                try {
               		me = MetaEvent.ReadMetaEvent(br);
                }
                catch(FormatException) {
                    me = new MidiEvent();
                }
                break;
            default:
                me = new MidiEvent();
                break;
                //throw new FormatException(String.Format("Unsupported MIDI Command Code {0:X2}",(byte) commandCode));
            }
            me.channel = channel;
            me.deltaTime = deltaTime;
            me.commandCode = commandCode;
            return me;
        }
Exemple #3
0
        private void sr_OnTrackEvent(object sender, TrackEventArgs e)
        {
            long counter = 0;
            long notesBefore = TotalNotes;
            foreach (var ev in e.Events.Where(x => x is MidiUtils.IO.MidiEvent))
            {
                counter++;
                // Convert the event to NAudio's system so we can get a shortMessage
                var em = (MidiUtils.IO.MidiEvent)ev;
                if (ev.Type == EventType.NoteOn) TotalNotes++;
                MidiEvent me;
                try
                {
                    switch (ev.Type)
                    {
                        case EventType.NoteOn:
                        case EventType.NoteOff:
                        case EventType.PolyphonicKeyPressure:
                            if (em.Data2 > 0 && ev.Type == EventType.NoteOn)
                            {
                                me = new NoteOnEvent(ev.Tick, em.Channel + 1, em.Data1, em.Data2, 0);
                            }
                            else
                            {
                                me = new NoteEvent(ev.Tick, em.Channel + 1, (MidiCommandCode)(int)ev.Type, em.Data1, em.Data2);
                            }
                            break;
                        case EventType.ControlChange:
                            me = new ControlChangeEvent(ev.Tick, em.Channel + 1, (MidiController)em.Data1, em.Data2);
                            break;
                        case EventType.ProgramChange:
                            me = new PatchChangeEvent(ev.Tick, em.Channel + 1, em.Data1);
                            break;
                        case EventType.ChannelPressure:
                            me = new ChannelAfterTouchEvent(ev.Tick, em.Channel + 1, em.Data1);
                            break;
                        case EventType.Pitchbend:
                            me = new PitchWheelChangeEvent(ev.Tick, em.Channel + 1, em.Data1 + (em.Data2 << 7));
                            break;
                        default:
                            throw new InvalidOperationException("Unsupported MIDI event type: " + ev.Type);
                    }
                }
                catch (Exception ex)
                {
                    Exceptions++;
                    continue;
                }

                // Send the message
                try { _midi.Send(me.GetAsShortMessage()); }
                catch (MmException ex)
                {
                    if ((int)ex.Result == 67) Overloads++;
                    else Exceptions++;
                    continue;
                }
                if (ev.Type == EventType.NoteOn) SuccessNotes++;
            }
            PeakNotesPerInterval = Math.Max(PeakNotesPerInterval, TotalNotes - notesBefore);
            PeakEventsPerInterval = Math.Max(PeakEventsPerInterval, counter);
            TotalEvents += counter;
        }