public void GetAsShortMessageReturnsCorrectValue()
        {
            int channel = 2;
            int pitch = 0x3FFF; // 0x2000 is the default
            PitchWheelChangeEvent p = new PitchWheelChangeEvent(0, channel, pitch);

            Assert.AreEqual(0x007F7FE1, p.GetAsShortMessage());
        }
Example #2
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;
        }