Beispiel #1
0
        public MidiPlayer(MidiMusic music, IMidiOutput output, IMidiPlayerTimeManager timeManager)
        {
            if (music == null)
            {
                throw new ArgumentNullException("music");
            }
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }
            if (timeManager == null)
            {
                throw new ArgumentNullException("timeManager");
            }

            this.music  = music;
            this.output = output;

            messages         = SmfTrackMerger.Merge(music).Tracks [0].Messages;
            player           = new MidiEventLooper(messages, timeManager, music.DeltaTimeSpec);
            player.Starting += () => {
                // all control reset on all channels.
                for (int i = 0; i < 16; i++)
                {
                    buffer [0] = (byte)(i + 0xB0);
                    buffer [1] = 0x79;
                    buffer [2] = 0;
                    output.Send(buffer, 0, 3, 0);
                }
            };
            EventReceived += (m) => {
                switch (m.EventType)
                {
                case MidiEvent.NoteOn:
                case MidiEvent.NoteOff:
                    if (channel_mask != null && channel_mask [m.Channel])
                    {
                        return;                         // ignore messages for the masked channel.
                    }
                    goto default;

                case MidiEvent.SysEx1:
                case MidiEvent.SysEx2:
                    if (buffer.Length <= m.Data.Length)
                    {
                        buffer = new byte [buffer.Length * 2];
                    }
                    buffer [0] = m.StatusByte;
                    Array.Copy(m.Data, 0, buffer, 1, m.Data.Length);
                    output.Send(buffer, 0, m.Data.Length + 1, 0);
                    break;

                case MidiEvent.Meta:
                    // do nothing.
                    break;

                default:
                    var size = MidiEvent.FixedDataSize(m.StatusByte);
                    buffer [0] = m.StatusByte;
                    buffer [1] = m.Msb;
                    buffer [2] = m.Lsb;
                    output.Send(buffer, 0, size + 1, 0);
                    break;
                }
            };
        }
 public MidiMessage(int deltaTime, MidiEvent evt)
 {
     DeltaTime = deltaTime;
     Event     = evt;
 }
Beispiel #3
0
        public virtual void ProcessEvent(MidiEvent evt)
        {
            switch (evt.EventType)
            {
            case MidiEvent.NoteOn:
                Channels [evt.Channel].NoteVelocity [evt.Msb] = evt.Lsb;
                break;

            case MidiEvent.NoteOff:
                Channels [evt.Channel].NoteVelocity [evt.Msb] = 0;
                break;

            case MidiEvent.PAf:
                Channels [evt.Channel].PAfVelocity [evt.Msb] = evt.Lsb;
                break;

            case MidiEvent.CC:
                // FIXME: handle RPNs and NRPNs by DTE
                switch (evt.Msb)
                {
                case MidiCC.NrpnMsb:
                case MidiCC.NrpnLsb:
                    Channels [evt.Channel].DteTarget = DteTarget.Nrpn;
                    break;

                case MidiCC.RpnMsb:
                case MidiCC.RpnLsb:
                    Channels [evt.Channel].DteTarget = DteTarget.Rpn;
                    break;

                case MidiCC.DteMsb:
                    Channels [evt.Channel].ProcessDte(evt.Lsb, true);
                    break;

                case MidiCC.DteLsb:
                    Channels [evt.Channel].ProcessDte(evt.Lsb, false);
                    break;

                case MidiCC.DteIncrement:
                    Channels [evt.Channel].ProcessDteIncrement();
                    break;

                case MidiCC.DteDecrement:
                    Channels [evt.Channel].ProcessDteDecrement();
                    break;
                }
                Channels [evt.Channel].Controls [evt.Msb] = evt.Lsb;
                break;

            case MidiEvent.Program:
                Channels [evt.Channel].Program = evt.Msb;
                break;

            case MidiEvent.CAf:
                Channels [evt.Channel].CAf = evt.Msb;
                break;

            case MidiEvent.Pitch:
                Channels [evt.Channel].PitchBend = (short)((evt.Msb << 7) + evt.Lsb);
                break;
            }
            if (EventReceived != null)
            {
                EventReceived(evt);
            }
        }