Beispiel #1
0
        private void FindNoteOn(NoteEvent offEvent, List <NoteOnEvent> outstandingNoteOns)
        {
            bool flag = false;

            foreach (NoteOnEvent current in outstandingNoteOns)
            {
                if (current.Channel == offEvent.Channel && current.NoteNumber == offEvent.NoteNumber)
                {
                    current.OffEvent = offEvent;
                    outstandingNoteOns.Remove(current);
                    flag = true;
                    break;
                }
            }
            if (!flag && this.strictChecking)
            {
                throw new FormatException(string.Format("Got an off without an on {0}", offEvent));
            }
        }
Beispiel #2
0
        private MidiFile(Stream inputStream, bool strictChecking, bool ownInputStream)
        {
            this.strictChecking = strictChecking;
            BinaryReader binaryReader = new BinaryReader(inputStream);

            try
            {
                if (Encoding.UTF8.GetString(binaryReader.ReadBytes(4)) != "MThd")
                {
                    throw new FormatException("Not a MIDI file - header chunk missing");
                }
                uint num = MidiFile.SwapUInt32(binaryReader.ReadUInt32());
                if (num != 6u)
                {
                    throw new FormatException("Unexpected header chunk length");
                }
                this.fileFormat = MidiFile.SwapUInt16(binaryReader.ReadUInt16());
                int num2 = (int)MidiFile.SwapUInt16(binaryReader.ReadUInt16());
                this.deltaTicksPerQuarterNote = MidiFile.SwapUInt16(binaryReader.ReadUInt16());
                this.events = new MidiEventCollection((this.fileFormat == 0) ? 0 : 1, (int)this.deltaTicksPerQuarterNote);
                for (int i = 0; i < num2; i++)
                {
                    this.events.AddTrack();
                }
                long num3 = 0L;
                for (int j = 0; j < num2; j++)
                {
                    if (this.fileFormat == 1)
                    {
                        num3 = 0L;
                    }
                    if (Encoding.UTF8.GetString(binaryReader.ReadBytes(4)) != "MTrk")
                    {
                        throw new FormatException("Invalid chunk header");
                    }
                    num = MidiFile.SwapUInt32(binaryReader.ReadUInt32());
                    long               position  = binaryReader.BaseStream.Position;
                    MidiEvent          midiEvent = null;
                    List <NoteOnEvent> list      = new List <NoteOnEvent>();
                    while (binaryReader.BaseStream.Position < position + (long)((ulong)num))
                    {
                        midiEvent = MidiEvent.ReadNextEvent(binaryReader, midiEvent);
                        num3     += (long)midiEvent.DeltaTime;
                        midiEvent.AbsoluteTime = num3;
                        this.events[j].Add(midiEvent);
                        if (midiEvent.CommandCode == MidiCommandCode.NoteOn)
                        {
                            NoteEvent noteEvent = (NoteEvent)midiEvent;
                            if (noteEvent.Velocity > 0)
                            {
                                list.Add((NoteOnEvent)noteEvent);
                            }
                            else
                            {
                                this.FindNoteOn(noteEvent, list);
                            }
                        }
                        else if (midiEvent.CommandCode == MidiCommandCode.NoteOff)
                        {
                            this.FindNoteOn((NoteEvent)midiEvent, list);
                        }
                        else if (midiEvent.CommandCode == MidiCommandCode.MetaEvent && ((MetaEvent)midiEvent).MetaEventType == MetaEventType.EndTrack && strictChecking && binaryReader.BaseStream.Position < position + (long)((ulong)num))
                        {
                            throw new FormatException(string.Format("End Track event was not the last MIDI event on track {0}", j));
                        }
                    }
                    if (list.Count > 0 && strictChecking)
                    {
                        throw new FormatException(string.Format("Note ons without note offs {0} (file format {1})", list.Count, this.fileFormat));
                    }
                    if (binaryReader.BaseStream.Position != position + (long)((ulong)num))
                    {
                        throw new FormatException(string.Format("Read too far {0}+{1}!={2}", num, position, binaryReader.BaseStream.Position));
                    }
                }
            }
            finally
            {
                if (ownInputStream)
                {
                    binaryReader.Close();
                }
            }
        }
Beispiel #3
0
        public static MidiEvent FromRawMessage(int rawMessage)
        {
            long            num  = 0L;
            int             num2 = rawMessage & 255;
            int             num3 = rawMessage >> 8 & 255;
            int             num4 = rawMessage >> 16 & 255;
            int             num5 = 1;
            MidiCommandCode midiCommandCode;

            if ((num2 & 240) == 240)
            {
                midiCommandCode = (MidiCommandCode)num2;
            }
            else
            {
                midiCommandCode = (MidiCommandCode)(num2 & 240);
                num5            = (num2 & 15) + 1;
            }
            if (midiCommandCode <= MidiCommandCode.ControlChange)
            {
                MidiEvent result;
                if (midiCommandCode <= MidiCommandCode.NoteOn)
                {
                    if (midiCommandCode != MidiCommandCode.NoteOff && midiCommandCode != MidiCommandCode.NoteOn)
                    {
                        goto IL_173;
                    }
                }
                else if (midiCommandCode != MidiCommandCode.KeyAfterTouch)
                {
                    if (midiCommandCode != MidiCommandCode.ControlChange)
                    {
                        goto IL_173;
                    }
                    result = new ControlChangeEvent(num, num5, (MidiController)num3, num4);
                    return(result);
                }
                if (num4 > 0 && midiCommandCode == MidiCommandCode.NoteOn)
                {
                    result = new NoteOnEvent(num, num5, num3, num4, 0);
                    return(result);
                }
                result = new NoteEvent(num, num5, midiCommandCode, num3, num4);
                return(result);
            }
            else if (midiCommandCode <= MidiCommandCode.ChannelAfterTouch)
            {
                if (midiCommandCode == MidiCommandCode.PatchChange)
                {
                    MidiEvent result = new PatchChangeEvent(num, num5, num3);
                    return(result);
                }
                if (midiCommandCode == MidiCommandCode.ChannelAfterTouch)
                {
                    MidiEvent result = new ChannelAfterTouchEvent(num, num5, num3);
                    return(result);
                }
            }
            else
            {
                if (midiCommandCode == MidiCommandCode.PitchWheelChange)
                {
                    MidiEvent result = new PitchWheelChangeEvent(num, num5, num3 + (num4 << 7));
                    return(result);
                }
                if (midiCommandCode != MidiCommandCode.Sysex)
                {
                    switch (midiCommandCode)
                    {
                    case MidiCommandCode.TimingClock:
                    case MidiCommandCode.StartSequence:
                    case MidiCommandCode.ContinueSequence:
                    case MidiCommandCode.StopSequence:
                    case MidiCommandCode.AutoSensing:
                    {
                        MidiEvent result = new MidiEvent(num, num5, midiCommandCode);
                        return(result);
                    }
                    }
                }
            }
IL_173:
            throw new FormatException(string.Format("Unsupported MIDI Command Code for Raw Message {0}", midiCommandCode));
        }
Beispiel #4
0
        public static MidiEvent ReadNextEvent(BinaryReader br, MidiEvent previous)
        {
            int             num  = MidiEvent.ReadVarInt(br);
            int             num2 = 1;
            byte            b    = br.ReadByte();
            MidiCommandCode midiCommandCode;

            if ((b & 128) == 0)
            {
                midiCommandCode = previous.CommandCode;
                num2            = previous.Channel;
                Stream expr_2D  = br.BaseStream;
                long   position = expr_2D.Position;
                expr_2D.Position = position - 1L;
            }
            else if ((b & 240) == 240)
            {
                midiCommandCode = (MidiCommandCode)b;
            }
            else
            {
                midiCommandCode = (MidiCommandCode)(b & 240);
                num2            = (int)((b & 15) + 1);
            }
            MidiEvent midiEvent;

            if (midiCommandCode <= MidiCommandCode.ControlChange)
            {
                if (midiCommandCode <= MidiCommandCode.NoteOn)
                {
                    if (midiCommandCode != MidiCommandCode.NoteOff)
                    {
                        if (midiCommandCode != MidiCommandCode.NoteOn)
                        {
                            goto IL_154;
                        }
                        midiEvent = new NoteOnEvent(br);
                        goto IL_16A;
                    }
                }
                else if (midiCommandCode != MidiCommandCode.KeyAfterTouch)
                {
                    if (midiCommandCode != MidiCommandCode.ControlChange)
                    {
                        goto IL_154;
                    }
                    midiEvent = new ControlChangeEvent(br);
                    goto IL_16A;
                }
                midiEvent = new NoteEvent(br);
                goto IL_16A;
            }
            if (midiCommandCode <= MidiCommandCode.ChannelAfterTouch)
            {
                if (midiCommandCode == MidiCommandCode.PatchChange)
                {
                    midiEvent = new PatchChangeEvent(br);
                    goto IL_16A;
                }
                if (midiCommandCode == MidiCommandCode.ChannelAfterTouch)
                {
                    midiEvent = new ChannelAfterTouchEvent(br);
                    goto IL_16A;
                }
            }
            else
            {
                if (midiCommandCode == MidiCommandCode.PitchWheelChange)
                {
                    midiEvent = new PitchWheelChangeEvent(br);
                    goto IL_16A;
                }
                if (midiCommandCode == MidiCommandCode.Sysex)
                {
                    midiEvent = SysexEvent.ReadSysexEvent(br);
                    goto IL_16A;
                }
                switch (midiCommandCode)
                {
                case MidiCommandCode.TimingClock:
                case MidiCommandCode.StartSequence:
                case MidiCommandCode.ContinueSequence:
                case MidiCommandCode.StopSequence:
                    midiEvent = new MidiEvent();
                    goto IL_16A;

                case MidiCommandCode.MetaEvent:
                    midiEvent = MetaEvent.ReadMetaEvent(br);
                    goto IL_16A;
                }
            }
IL_154:
            throw new FormatException(string.Format("Unsupported MIDI Command Code {0:X2}", (byte)midiCommandCode));
IL_16A:
            midiEvent.channel     = num2;
            midiEvent.deltaTime   = num;
            midiEvent.commandCode = midiCommandCode;
            return(midiEvent);
        }