Пример #1
0
        void ParseEvent(Data.Buffer data)
        {
            byte status = data.PeekByte();

            if (status == 0xff) // meta event
            {
                data.Pop(1);
                ParseMetaEvent(data);
                return;
            }

            int channel   = status & 0xf;
            int eventType = status >> 4;

            if (eventType < 0x8)
            {
                uint ticks = data.Pop <byte>();
                currentTime += ConvertTicksToTime(ticks);
                currentTick += ticks;
                return;
            }
            else if (eventType < 0xF)
            {
                data.Pop(1);
            }

            switch (eventType)
            {
            case 0x9:     // Note on
                // Note: In XMI it has 3 parameters (last for duration).
                // But we create two events (note on and off).
            {
                byte note     = data.Pop <byte>();
                byte velocity = data.Pop <byte>();
                uint length   = ParseDeltaTime(data);

                if (velocity != 0)
                {
                    var onEvent  = new PlayNoteEvent((byte)channel, note, velocity, currentTime, currentTick);
                    var offEvent = new StopNoteEvent((byte)channel, note, currentTime + ConvertTicksToTime(length), currentTick + length);

                    events.Add(onEvent);
                    events.Add(offEvent);
                }
            }
            break;

            case 0xB:     // Control change
            {
                byte controller = (byte)(data.Pop <byte>() & 0x7f);
                byte value      = (byte)(data.Pop <byte>() & 0x7f);

                if (controller < 120)         // ignore reserved controller events >= 120
                {
                    events.Add(new SetControllerValueEvent((byte)channel, controller, value, currentTime, currentTick));
                }
            }
            break;

            case 0x8:
            case 0xA:
            case 0xE:
                data.Pop(2);
                break;

            case 0xC:
                events.Add(new SetInstrumentEvent((byte)channel, data.Pop <byte>(), currentTime, currentTick));
                break;

            case 0xD:
                data.Pop(1);
                break;

            default:
                throw new ExceptionAudio("Unsupported xmi/midi event type.");
            }
        }
Пример #2
0
        void ParseEvent(Data.Buffer data)
        {
            byte status = data.PeekByte();

            if (status == 0xff) // meta event
            {
                data.Pop(1);
                ParseMetaEvent(data);
                return;
            }

            int channel   = status & 0xf;
            int eventType = status >> 4;

            if (eventType < 0x8)
            {
                currentTime += ConvertTicksToTime(data.Pop <byte>());
                return;
            }
            else if (eventType < 0xF)
            {
                data.Pop(1);
            }

            switch (eventType)
            {
            case 0x9:     // Note on
                // Note: In XMI it has 3 parameters (last for duration).
                // But we create two events (note on and off).
            {
                byte note     = data.Pop <byte>();
                byte velocity = data.Pop <byte>();
                uint length   = ParseDeltaTime(data);

                if (velocity != 0)
                {
                    var onEvent  = new PlayNoteEvent((byte)channel, note, currentTime);
                    var offEvent = new StopNoteEvent((byte)channel, note, currentTime + ConvertTicksToTime(length));

                    events.Add(onEvent);
                    events.Add(offEvent);
                }
            }
            break;

            case 0x8:
            case 0xA:
            case 0xB:
            case 0xE:
                data.Pop(2);
                break;

            case 0xC:
            {
                var patchEvent = new SetInstrumentEvent((byte)channel, data.Pop <byte>(), currentTime);

                events.Add(patchEvent);
            }
            break;

            case 0xD:
                data.Pop(1);
                break;

            default:
                throw new ExceptionAudio("Unsupported xmi/midi event type.");
            }
        }