示例#1
0
        public static Stream Decompress(string fileName)
        {
            if (!IsCompressed(fileName))
            {
                return(File.OpenRead(fileName));
            }

            byte[] bytesToDecompress = File.ReadAllBytes(fileName);

            using (ABinaryReader codeReader = new ABinaryReader(bytesToDecompress.ToMemoryStream(false, true), Endianness.Big, Encoding.GetEncoding(932)),
                   countReader = new ABinaryReader(bytesToDecompress.ToMemoryStream(false, true), Endianness.Big, Encoding.GetEncoding(932)),
                   dataReader = new ABinaryReader(bytesToDecompress.ToMemoryStream(false, true), Endianness.Big, Encoding.GetEncoding(932)))
            {
                // Parse the header.
                string headerIdentifier = codeReader.ReadRawString(4);
                int    uncompressedSize = codeReader.ReadS32();
                countReader.Position = codeReader.ReadS32();
                dataReader.Position  = codeReader.ReadS32();

                // This is the buffer where we will put our decompressed data.
                // I would use an ABinaryWriter for this, but we need to read/write from the same buffer for the RLE parts.
                byte[] outputArray = new byte[uncompressedSize];

                int  outPosition     = 0;                       // Our position in the destination buffer.
                uint validBitsCount  = 0;                       // The number of valid bits left in the code byte.
                byte currentCodeByte = 0;                       // Our current code byte.

                // Begin decompression.
                while (outPosition < uncompressedSize)
                {
                    // If the current code byte is used, get a new one.
                    if (validBitsCount <= 0)
                    {
                        currentCodeByte = codeReader.Read8();
                        validBitsCount  = 8;
                    }

                    // If the next bit in the code byte is a 1, do a direct, 1:1 copy of the next data byte; otherwise, uncompress a chunk of data.
                    if ((currentCodeByte & 0x80) == 0x80)
                    {
                        outputArray[outPosition++] = dataReader.Read8();
                    }
                    else
                    {
                        // Read the count data.
                        ushort count = countReader.Read16();

                        // The last three nybbles represent the distance in the buffer to go back.
                        int distance = (count & 0xFFF);

                        // Calculate the position from which we start.
                        int startOffset = (outPosition - (distance + 1));

                        // The upper nybble of count; if zero, read a third byte and add 0x10.
                        int byteCount = ((count >> 12) & 0xF);

                        // If the upper nybble of the count is equal to zero, that means the number of bytes is too large for a nybble and is actually in the next whole byte.
                        // Add 0x10 to this byte's value, possibly to account for the original nybble. (How does Thakis figure this crap out?)
                        if (byteCount == 0)
                        {
                            byteCount = (dataReader.Read8() + 0x10);
                        }

                        // Take into consideration the two bytes for the count by adding two to the byte count.
                        byteCount += 2;

                        // Copy the run data.
                        Repeater.Repeat(byteCount, () => outputArray[outPosition++] = outputArray[startOffset++]);
                    }

                    // Get the next bit in the code byte.
                    currentCodeByte <<= 1;
                    validBitsCount--;
                }

                // Return the uncompressed data.
                return(outputArray.ToMemoryStream(false, true));
            }
        }
示例#2
0
        // Token: 0x06000008 RID: 8 RVA: 0x00002124 File Offset: 0x00000324
        private static MIDIEvent FromStream(ABinaryReader binaryReader, ulong deltaTime, byte statusByte)
        {
            if (statusByte == 255)
            {
                MetaEventType metaEventType = (MetaEventType)binaryReader.Read8();
                int           num           = (int)binaryReader.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 (num != 2)
                        {
                            throw new InvalidDataException("Invalid size in sequence-number event.");
                        }
                        return(new SequenceNumberEvent(deltaTime, (int)binaryReader.Read16()));

                    case MetaEventType.Text:
                        return(new TextEvent(deltaTime, binaryReader.ReadRawString(num)));

                    case MetaEventType.CopyrightNotice:
                        return(new CopyrightNoticeEvent(deltaTime, binaryReader.ReadRawString(num)));

                    case MetaEventType.Name:
                        return(new TrackNameEvent(deltaTime, binaryReader.ReadRawString(num)));

                    case MetaEventType.InstrumentName:
                        return(new InstrumentNameEvent(deltaTime, binaryReader.ReadRawString(num)));

                    case MetaEventType.Lyrics:
                        return(new LyricsEvent(deltaTime, binaryReader.ReadRawString(num)));

                    case MetaEventType.Marker:
                        return(new MarkerEvent(deltaTime, binaryReader.ReadRawString(num)));

                    case MetaEventType.CuePoint:
                        return(new CuePointEvent(deltaTime, binaryReader.ReadRawString(num)));

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

                    case MetaEventType.KeySignature:
                        if (num != 2)
                        {
                            throw new InvalidDataException("Invalid size in key-signature event.");
                        }
                        return(new KeySignatureEvent(deltaTime, (int)binaryReader.ReadS8(), (Scale)binaryReader.Read8()));

                    default:
                        if (metaEventType2 == MetaEventType.SequencerSpecific)
                        {
                            byte[] array = binaryReader.Read8s(num);
                            if (num == 0 || num < ((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]);
                            return(new SequencerSpecificEvent(deltaTime, manufacturerID, flag, array.Duplicate(flag ? 3 : 1, array.Length - (flag ? 3 : 1))));
                        }
                        break;
                    }
                }
                else
                {
                    if (num != 3)
                    {
                        throw new InvalidDataException("Invalid size in tempo-change event.");
                    }
                    return(new TempoChangeEvent(deltaTime, binaryReader.Read24()));
                }
                throw new NotImplementedException(string.Format("Encountered unimplemented meta event type {0}.", metaEventType));
            }
            else if (statusByte == 240 || statusByte == 247)
            {
                byte[] array2 = binaryReader.Read8s((int)binaryReader.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           = statusByte == 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);
                }
                return(new SystemExclusiveEvent(deltaTime, type, manufacturerID2, flag4, array2));
            }
            else
            {
                ChannelEventType channelEventType = (ChannelEventType)(statusByte & 240);
                byte             channelNumber    = (byte)(statusByte & 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)
                    {
                        return(new NoteOffEvent(deltaTime, (int)channelNumber, binaryReader.Read8(), (int)binaryReader.Read8()));
                    }
                    if (channelEventType2 == ChannelEventType.NoteOn)
                    {
                        return(new NoteOnEvent(deltaTime, (int)channelNumber, (int)binaryReader.Read8(), (int)binaryReader.Read8()));
                    }
                    if (channelEventType2 == ChannelEventType.NoteAftertouch)
                    {
                        return(new NoteAftertouchEvent(deltaTime, (int)channelNumber, (int)binaryReader.Read8(), (int)binaryReader.Read8()));
                    }
                }
                else if (channelEventType2 <= ChannelEventType.ProgramChange)
                {
                    if (channelEventType2 == ChannelEventType.Controller)
                    {
                        return(new ControllerEvent(deltaTime, (int)channelNumber, (int)binaryReader.Read8(), (int)binaryReader.Read8()));
                    }
                    if (channelEventType2 == ChannelEventType.ProgramChange)
                    {
                        return(new ProgramChangeEvent(deltaTime, (int)channelNumber, (int)binaryReader.Read8()));
                    }
                }
                else
                {
                    if (channelEventType2 == ChannelEventType.ChannelAftertouch)
                    {
                        return(new ChannelAftertouchEvent(deltaTime, (int)channelNumber, (int)binaryReader.Read8()));
                    }
                    if (channelEventType2 == ChannelEventType.PitchBend)
                    {
                        return(new PitchBendEvent(deltaTime, (int)channelNumber, (int)(binaryReader.Read8() & 127) | (int)(binaryReader.Read8() & 127) << 7));
                    }
                }
                throw new NotImplementedException(string.Format("Encountered unimplemented channel event type {0}.", channelEventType));
            }
        }
示例#3
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);
        }
示例#4
0
        public static Stream Decompress(string fileName)
        {
            if (!IsCompressed(fileName))
            {
                return File.OpenRead(fileName);
            }

            byte[] bytesToDecompress = File.ReadAllBytes(fileName);

            using (ABinaryReader codeReader = new ABinaryReader(bytesToDecompress.ToMemoryStream(false, true), Endianness.Big, Encoding.GetEncoding(932)),
                    countReader = new ABinaryReader(bytesToDecompress.ToMemoryStream(false, true), Endianness.Big, Encoding.GetEncoding(932)),
                    dataReader = new ABinaryReader(bytesToDecompress.ToMemoryStream(false, true), Endianness.Big, Encoding.GetEncoding(932)))
            {
                // Parse the header.
                string headerIdentifier = codeReader.ReadRawString(4);
                int uncompressedSize = codeReader.ReadS32();
                countReader.Position = codeReader.ReadS32();
                dataReader.Position = codeReader.ReadS32();

                // This is the buffer where we will put our decompressed data.
                // I would use an ABinaryWriter for this, but we need to read/write from the same buffer for the RLE parts.
                byte[] outputArray = new byte[uncompressedSize];

                int outPosition = 0;		// Our position in the destination buffer.
                uint validBitsCount = 0;	// The number of valid bits left in the code byte.
                byte currentCodeByte = 0;	// Our current code byte.

                // Begin decompression.
                while (outPosition < uncompressedSize)
                {
                    // If the current code byte is used, get a new one.
                    if (validBitsCount <= 0)
                    {
                        currentCodeByte = codeReader.Read8();
                        validBitsCount = 8;
                    }

                    // If the next bit in the code byte is a 1, do a direct, 1:1 copy of the next data byte; otherwise, uncompress a chunk of data.
                    if ((currentCodeByte & 0x80) == 0x80)
                    {
                        outputArray[outPosition++] = dataReader.Read8();
                    }
                    else
                    {
                        // Read the count data.
                        ushort count = countReader.Read16();

                        // The last three nybbles represent the distance in the buffer to go back.
                        int distance = (count & 0xFFF);

                        // Calculate the position from which we start.
                        int startOffset = (outPosition - (distance + 1));

                        // The upper nybble of count; if zero, read a third byte and add 0x10.
                        int byteCount = ((count >> 12) & 0xF);

                        // If the upper nybble of the count is equal to zero, that means the number of bytes is too large for a nybble and is actually in the next whole byte.
                        // Add 0x10 to this byte's value, possibly to account for the original nybble. (How does Thakis figure this crap out?)
                        if (byteCount == 0)
                        {
                            byteCount = (dataReader.Read8() + 0x10);
                        }

                        // Take into consideration the two bytes for the count by adding two to the byte count.
                        byteCount += 2;

                        // Copy the run data.
                        Repeater.Repeat(byteCount, () => outputArray[outPosition++] = outputArray[startOffset++]);
                    }

                    // Get the next bit in the code byte.
                    currentCodeByte <<= 1;
                    validBitsCount--;
                }

                // Return the uncompressed data.
                return outputArray.ToMemoryStream(false, true);
            }
        }