示例#1
0
        // Token: 0x06000155 RID: 341 RVA: 0x00004F50 File Offset: 0x00003150
        public static MIDI FromStream(Stream stream)
        {
            ABinaryReader abinaryReader = new ABinaryReader(stream, Endianness.Big);
            MIDI          midi          = new MIDI();

            if (abinaryReader.ReadRawString(4) != "MThd")
            {
                throw new InvalidDataException("Missing header chunk in MIDI file.");
            }
            if (abinaryReader.Read32() != 6U)
            {
                throw new InvalidDataException("Invalid header size in MIDI file.");
            }
            MIDIFormat enumValue = (MIDIFormat)abinaryReader.Read16();

            if (!enumValue.IsDefined <MIDIFormat>())
            {
                throw new InvalidDataException("Invalid format in MIDI file.");
            }
            abinaryReader.Read16();
            int num = (int)abinaryReader.Read16();

            if ((num & 32768) != 0)
            {
                midi.division = new SMPTEDivision((SMPTEFrameRate)(-(int)(num >> 8)), num & 255);                 // TODO: Cannot infer type, so: (ModularType) -> (int)
            }
            else
            {
                midi.division = new TicksPerBeatDivision(num);
            }
            int num2 = 0;

            while (!abinaryReader.IsAtEndOfStream)
            {
                if (abinaryReader.ReadRawString(4) != "MTrk")
                {
                    throw new InvalidDataException("Missing track chunk in MIDI file.");
                }
                ulong num3 = 0UL;
                uint  num4 = abinaryReader.Read32();
                abinaryReader.SetAnchor();
                Track track = new Track();
                byte  b     = 0;
                while (abinaryReader.Position < (long)((ulong)num4))
                {
                    ulong num5 = abinaryReader.ReadUIntVar();
                    byte  b2   = abinaryReader.Read8();
                    if (b2 < 128)
                    {
                        if (b == 0)
                        {
                            throw new InvalidDataException("Encountered running status event with no previous status available.");
                        }
                        b2 = b;
                        abinaryReader.Position -= 1L;
                    }
                    if (b2 >= 240 && b2 <= 247)
                    {
                        b = 0;
                    }
                    else if (b2 >= 128 && b2 <= 239)
                    {
                        b = b2;
                    }
                    num3 += num5;
                    if (b2 == 255)
                    {
                        MetaEventType metaEventType = (MetaEventType)abinaryReader.Read8();
                        int           num6          = (int)abinaryReader.ReadUIntVar();
                        if (!metaEventType.IsDefined <MetaEventType>())
                        {
                            throw new InvalidDataException(string.Format("Encountered unsupported meta event type {0}.", metaEventType));
                        }
                        MetaEventType metaEventType2 = metaEventType;
                        if (metaEventType2 <= MetaEventType.EndOfTrack)
                        {
                            switch (metaEventType2)
                            {
                            case MetaEventType.SequenceNumber:
                                if (num6 != 2)
                                {
                                    throw new InvalidDataException("Invalid size in sequence-number event.");
                                }
                                track.Add(new SequenceNumberEvent(num5, (int)abinaryReader.Read16()), num3);
                                continue;

                            case MetaEventType.Text:
                                track.Add(new TextEvent(num5, abinaryReader.ReadRawString(num6)), num3);
                                continue;

                            case MetaEventType.CopyrightNotice:
                                track.Add(new CopyrightNoticeEvent(num5, abinaryReader.ReadRawString(num6)), num3);
                                continue;

                            case MetaEventType.Name:
                                track.Add(new TrackNameEvent(num5, abinaryReader.ReadRawString(num6)), num3);
                                continue;

                            case MetaEventType.InstrumentName:
                                track.Add(new InstrumentNameEvent(num5, abinaryReader.ReadRawString(num6)), num3);
                                continue;

                            case MetaEventType.Lyrics:
                                track.Add(new LyricsEvent(num5, abinaryReader.ReadRawString(num6)), num3);
                                continue;

                            case MetaEventType.Marker:
                                track.Add(new MarkerEvent(num5, abinaryReader.ReadRawString(num6)), num3);
                                continue;

                            case MetaEventType.CuePoint:
                                track.Add(new CuePointEvent(num5, abinaryReader.ReadRawString(num6)), num3);
                                continue;

                            default:
                                if (metaEventType2 != MetaEventType.ChannelPrefix)
                                {
                                    if (metaEventType2 == MetaEventType.EndOfTrack)
                                    {
                                        if (num6 != 0)
                                        {
                                            throw new InvalidDataException("Invalid size in end-of-track event.");
                                        }
                                        track.Add(new EndOfTrackEvent(num5), num3);
                                        continue;
                                    }
                                }
                                else
                                {
                                    if (num6 != 1)
                                    {
                                        throw new InvalidDataException("Invalid size in channel-prefix event.");
                                    }
                                    track.Add(new ChannelPrefixEvent(num5, abinaryReader.Read8()), num3);
                                    continue;
                                }
                                break;
                            }
                        }
                        else if (metaEventType2 != MetaEventType.TempoChange)
                        {
                            switch (metaEventType2)
                            {
                            case MetaEventType.TimeSignature:
                                if (num6 != 4)
                                {
                                    throw new InvalidDataException("Invalid size in time-signature event.");
                                }
                                track.Add(new TimeSignatureEvent(num5, (int)abinaryReader.Read8(), (int)abinaryReader.Read8(), (int)abinaryReader.Read8(), (int)abinaryReader.Read8()), num3);
                                continue;

                            case MetaEventType.KeySignature:
                                if (num6 != 2)
                                {
                                    throw new InvalidDataException("Invalid size in key-signature event.");
                                }
                                track.Add(new KeySignatureEvent(num5, (int)abinaryReader.ReadS8(), (Scale)abinaryReader.Read8()), num3);
                                continue;

                            default:
                                if (metaEventType2 == MetaEventType.SequencerSpecific)
                                {
                                    byte[] array = abinaryReader.Read8s(num6);
                                    if (num6 == 0 || num6 < ((array[0] == 0) ? 3 : 1))
                                    {
                                        throw new InvalidDataException("Invalid size in sequencer-specific event.");
                                    }
                                    bool flag           = array[0] == 0;
                                    int  manufacturerID = flag ? ((int)array[1] << 8 | (int)array[2]) : ((int)array[0]);
                                    track.Add(new SequencerSpecificEvent(num5, manufacturerID, flag, array.Duplicate(flag ? 3 : 1, array.Length - (flag ? 3 : 1))), num3);
                                    continue;
                                }
                                break;
                            }
                        }
                        else
                        {
                            if (num6 != 3)
                            {
                                throw new InvalidDataException("Invalid size in tempo-change event.");
                            }
                            track.Add(new TempoChangeEvent(num5, abinaryReader.Read24()), num3);
                            continue;
                        }
                        throw new NotImplementedException(string.Format("Encountered unimplemented meta event type {0}.", metaEventType));
                    }
                    else if (b2 == 240 || b2 == 247)
                    {
                        byte[] array2 = abinaryReader.Read8s((int)abinaryReader.ReadUIntVar());
                        if (array2.Length == 0 || array2.Length < ((array2[0] == 0) ? 3 : 1))
                        {
                            throw new InvalidDataException("Encountered a SysEx event with an invalid size.");
                        }
                        SystemExclusiveEventType type = SystemExclusiveEventType.Normal;
                        bool flag2           = b2 == 247;
                        bool flag3           = array2.Last <byte>() == 247;
                        bool flag4           = array2[0] == 0;
                        int  manufacturerID2 = flag4 ? ((int)array2[1] << 8 | (int)array2[2]) : ((int)array2[0]);
                        if (flag2)
                        {
                            type = (flag3 ? SystemExclusiveEventType.Terminating : SystemExclusiveEventType.Continuation);
                        }
                        if (flag3)
                        {
                            array2 = array2.Duplicate(array2.Length - 1);
                        }
                        track.Add(new SystemExclusiveEvent(num5, type, manufacturerID2, flag4, array2), num3);
                    }
                    else
                    {
                        ChannelEventType channelEventType = (ChannelEventType)(b2 & 240);
                        byte             channelNumber    = (byte)(b2 & 15);
                        if (!channelEventType.IsDefined <ChannelEventType>())
                        {
                            throw new InvalidDataException(string.Format("Encountered undefined channel-event type {0}.", channelEventType));
                        }
                        ChannelEventType channelEventType2 = channelEventType;
                        if (channelEventType2 <= ChannelEventType.NoteAftertouch)
                        {
                            if (channelEventType2 == ChannelEventType.NoteOff)
                            {
                                track.Add(new NoteOffEvent(num5, (int)channelNumber, abinaryReader.Read8(), (int)abinaryReader.Read8()), num3);
                                continue;
                            }
                            if (channelEventType2 == ChannelEventType.NoteOn)
                            {
                                track.Add(new NoteOnEvent(num5, (int)channelNumber, (int)abinaryReader.Read8(), (int)abinaryReader.Read8()), num3);
                                continue;
                            }
                            if (channelEventType2 == ChannelEventType.NoteAftertouch)
                            {
                                track.Add(new NoteAftertouchEvent(num5, (int)channelNumber, (int)abinaryReader.Read8(), (int)abinaryReader.Read8()), num3);
                                continue;
                            }
                        }
                        else if (channelEventType2 <= ChannelEventType.ProgramChange)
                        {
                            if (channelEventType2 == ChannelEventType.Controller)
                            {
                                track.Add(new ControllerEvent(num5, (int)channelNumber, (int)abinaryReader.Read8(), (int)abinaryReader.Read8()), num3);
                                continue;
                            }
                            if (channelEventType2 == ChannelEventType.ProgramChange)
                            {
                                track.Add(new ProgramChangeEvent(num5, (int)channelNumber, (int)abinaryReader.Read8()), num3);
                                continue;
                            }
                        }
                        else
                        {
                            if (channelEventType2 == ChannelEventType.ChannelAftertouch)
                            {
                                track.Add(new ChannelAftertouchEvent(num5, (int)channelNumber, (int)abinaryReader.Read8()), num3);
                                continue;
                            }
                            if (channelEventType2 == ChannelEventType.PitchBend)
                            {
                                track.Add(new PitchBendEvent(num5, (int)channelNumber, (int)(abinaryReader.Read8() & 127) | (int)(abinaryReader.Read8() & 127) << 7), num3);
                                continue;
                            }
                        }
                        throw new NotImplementedException(string.Format("Encountered unimplemented channel event type {0}.", channelEventType));
                    }
                }
                midi.tracks.Add(track);
                abinaryReader.ResetAnchor();
                num2++;
            }
            return(midi);
        }