Ejemplo n.º 1
0
        // Token: 0x0600340E RID: 13326 RVA: 0x001488A0 File Offset: 0x00146AA0
        public static MidiFile Read(Stream stream, ReadingSettings settings = null)
        {
            if (settings == null)
            {
                settings = new ReadingSettings
                {
                    InvalidChannelEventParameterValuePolicy = InvalidChannelEventParameterValuePolicy.SnapToLimits,
                    InvalidChunkSizePolicy = InvalidChunkSizePolicy.Ignore,
                    NotEnoughBytesPolicy   = NotEnoughBytesPolicy.Ignore,
                    InvalidMetaEventParameterValuePolicy = InvalidMetaEventParameterValuePolicy.SnapToLimits,
                    UnexpectedTrackChunksCountPolicy     = UnexpectedTrackChunksCountPolicy.Ignore,
                    TextEncoding = Encoding.UTF8
                };
            }
            MidiFile midiFile = new MidiFile();
            int?     num      = null;
            int      num2     = 0;
            bool     flag     = false;

            try
            {
                using (MidiReader midiReader = new MidiReader(stream))
                {
                    long?  num3 = null;
                    string text = midiReader.ReadString("RIFF".Length);
                    if (text == "RIFF")
                    {
                        midiReader.Position += 12L;
                        uint num4 = midiReader.ReadDword();
                        num3 = new long?(midiReader.Position + (long)((ulong)num4));
                    }
                    else
                    {
                        midiReader.Position -= (long)text.Length;
                    }
                    while (!midiReader.EndReached)
                    {
                        if (num3 != null)
                        {
                            long position = midiReader.Position;
                            long?num5     = num3;
                            if (!(position < num5.GetValueOrDefault() & num5 != null))
                            {
                                break;
                            }
                        }
                        MidiChunk midiChunk = MidiFile.ReadChunk(midiReader, settings, num2, num);
                        if (midiChunk != null)
                        {
                            HeaderChunk headerChunk = midiChunk as HeaderChunk;
                            if (headerChunk != null)
                            {
                                if (!flag)
                                {
                                    num = new int?((int)headerChunk.TracksNumber);
                                    midiFile.TimeDivision    = headerChunk.TimeDivision;
                                    midiFile._originalFormat = new ushort?(headerChunk.FileFormat);
                                }
                                flag = true;
                            }
                            else
                            {
                                if (midiChunk is TrackChunk)
                                {
                                    num2++;
                                }
                                midiFile.Chunks.Add(midiChunk);
                            }
                        }
                    }
                    if (num != null)
                    {
                        int num6 = num2;
                        int?num7 = num;
                        if (!(num6 == num7.GetValueOrDefault() & num7 != null))
                        {
                            MidiFile.ReactOnUnexpectedTrackChunksCount(settings.UnexpectedTrackChunksCountPolicy, num2, num.Value);
                        }
                    }
                    if (!flag)
                    {
                        midiFile.TimeDivision = null;
                        if (settings.NoHeaderChunkPolicy == NoHeaderChunkPolicy.Abort)
                        {
                            throw new NoHeaderChunkException();
                        }
                    }
                }
            }
            catch (NotEnoughBytesException exception)
            {
                MidiFile.ReactOnNotEnoughBytes(settings.NotEnoughBytesPolicy, exception);
            }
            catch (EndOfStreamException exception2)
            {
                MidiFile.ReactOnNotEnoughBytes(settings.NotEnoughBytesPolicy, exception2);
            }
            return(midiFile);
        }
Ejemplo n.º 2
0
        // Token: 0x06003410 RID: 13328 RVA: 0x00148C28 File Offset: 0x00146E28
        private static MidiChunk ReadChunk(MidiReader reader, ReadingSettings settings, int actualTrackChunksCount, int?expectedTrackChunksCount)
        {
            MidiChunk midiChunk = null;

            try
            {
                string text = reader.ReadString(4);
                if (text.Length < 4)
                {
                    NotEnoughBytesPolicy notEnoughBytesPolicy = settings.NotEnoughBytesPolicy;
                    if (notEnoughBytesPolicy == NotEnoughBytesPolicy.Abort)
                    {
                        throw new NotEnoughBytesException("Chunk ID cannot be read since the reader's underlying stream doesn't have enough bytes.", 4L, (long)text.Length);
                    }
                    if (notEnoughBytesPolicy == NotEnoughBytesPolicy.Ignore)
                    {
                        return(null);
                    }
                }
                if (!(text == "MThd"))
                {
                    if (!(text == "MTrk"))
                    {
                        midiChunk = MidiFile.TryCreateChunk(text, settings.CustomChunkTypes);
                    }
                    else
                    {
                        midiChunk = new TrackChunk();
                    }
                }
                else
                {
                    midiChunk = new HeaderChunk();
                }
                if (midiChunk == null)
                {
                    switch (settings.UnknownChunkIdPolicy)
                    {
                    case UnknownChunkIdPolicy.ReadAsUnknownChunk:
                        midiChunk = new UnknownChunk(text);
                        break;

                    case UnknownChunkIdPolicy.Skip:
                    {
                        uint num = reader.ReadDword();
                        reader.Position += (long)((ulong)num);
                        return(null);
                    }

                    case UnknownChunkIdPolicy.Abort:
                        throw new UnknownChunkException("'" + text + "' chunk ID is unknown.", text);
                    }
                }
                if (midiChunk is TrackChunk && expectedTrackChunksCount != null)
                {
                    int?num2 = expectedTrackChunksCount;
                    if (actualTrackChunksCount >= num2.GetValueOrDefault() & num2 != null)
                    {
                        MidiFile.ReactOnUnexpectedTrackChunksCount(settings.UnexpectedTrackChunksCountPolicy, actualTrackChunksCount, expectedTrackChunksCount.Value);
                        ExtraTrackChunkPolicy extraTrackChunkPolicy = settings.ExtraTrackChunkPolicy;
                        if (extraTrackChunkPolicy != ExtraTrackChunkPolicy.Read && extraTrackChunkPolicy == ExtraTrackChunkPolicy.Skip)
                        {
                            uint num3 = reader.ReadDword();
                            reader.Position += (long)((ulong)num3);
                            return(null);
                        }
                    }
                }
                midiChunk.Read(reader, settings);
            }
            catch (NotEnoughBytesException exception)
            {
                MidiFile.ReactOnNotEnoughBytes(settings.NotEnoughBytesPolicy, exception);
            }
            catch (EndOfStreamException exception2)
            {
                MidiFile.ReactOnNotEnoughBytes(settings.NotEnoughBytesPolicy, exception2);
            }
            return(midiChunk);
        }