Exemplo n.º 1
0
        public static MetaEvent ReadMetaEvent(BinaryReader br)
        {
            MetaEventType metaEventType = (MetaEventType)br.ReadByte();
            int           num           = MidiEvent.ReadVarInt(br);
            MetaEvent     metaEvent     = new MetaEvent();

            if (metaEventType <= MetaEventType.SetTempo)
            {
                switch (metaEventType)
                {
                case MetaEventType.TrackSequenceNumber:
                    metaEvent = new TrackSequenceNumberEvent(br, num);
                    goto IL_E7;

                case MetaEventType.TextEvent:
                case MetaEventType.Copyright:
                case MetaEventType.SequenceTrackName:
                case MetaEventType.TrackInstrumentName:
                case MetaEventType.Lyric:
                case MetaEventType.Marker:
                case MetaEventType.CuePoint:
                case MetaEventType.ProgramName:
                case MetaEventType.DeviceName:
                    metaEvent = new TextEvent(br, num);
                    goto IL_E7;

                default:
                    if (metaEventType != MetaEventType.EndTrack)
                    {
                        if (metaEventType == MetaEventType.SetTempo)
                        {
                            metaEvent = new TempoEvent(br, num);
                            goto IL_E7;
                        }
                    }
                    else
                    {
                        if (num != 0)
                        {
                            throw new FormatException("End track length");
                        }
                        goto IL_E7;
                    }
                    break;
                }
            }
            else if (metaEventType <= MetaEventType.TimeSignature)
            {
                if (metaEventType == MetaEventType.SmpteOffset)
                {
                    metaEvent = new SmpteOffsetEvent(br, num);
                    goto IL_E7;
                }
                if (metaEventType == MetaEventType.TimeSignature)
                {
                    metaEvent = new TimeSignatureEvent(br, num);
                    goto IL_E7;
                }
            }
            else
            {
                if (metaEventType == MetaEventType.KeySignature)
                {
                    metaEvent = new KeySignatureEvent(br, num);
                    goto IL_E7;
                }
                if (metaEventType == MetaEventType.SequencerSpecific)
                {
                    metaEvent = new SequencerSpecificEvent(br, num);
                    goto IL_E7;
                }
            }
            byte[] array = br.ReadBytes(num);
            if (array.Length != num)
            {
                throw new FormatException("Failed to read metaevent's data fully");
            }
            return(new RawMetaEvent(metaEventType, 0L, array));

IL_E7:
            metaEvent.metaEvent      = metaEventType;
            metaEvent.metaDataLength = num;
            return(metaEvent);
        }
Exemplo n.º 2
0
 public override void Export(ref long absoluteTime, BinaryWriter writer)
 {
     base.Export(ref absoluteTime, writer);
     writer.Write((byte)this.metaEvent);
     MidiEvent.WriteVarInt(writer, this.metaDataLength);
 }
Exemplo n.º 3
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();
                }
            }
        }
Exemplo n.º 4
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));
        }
Exemplo n.º 5
0
 public static bool IsNoteOn(MidiEvent midiEvent)
 {
     return(midiEvent != null && midiEvent.CommandCode == MidiCommandCode.NoteOn && ((NoteEvent)midiEvent).Velocity > 0);
 }
Exemplo n.º 6
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);
        }