Read() public static method

public static Read ( Stream s ) : Chunk
s Stream
return Chunk
Beispiel #1
0
        public static bool LoadSound(Stream s, out byte[] rawData, out int sampleRate)
        {
            rawData = null;

            sampleRate = s.ReadUInt16();
            var dataSize   = s.ReadInt32();
            var outputSize = s.ReadInt32();

            var readFlag = s.ReadByte();

            if (!Enum.IsDefined(typeof(SoundFlags), readFlag))
            {
                return(false);
            }

            var readFormat = s.ReadByte();

            if (!Enum.IsDefined(typeof(SoundFormat), readFormat))
            {
                return(false);
            }

            var output        = new byte[outputSize];
            var offset        = 0;
            var index         = 0;
            var currentSample = 0;

            while (dataSize > 0)
            {
                var chunk = Chunk.Read(s);
                for (var n = 0; n < chunk.CompressedSize; n++)
                {
                    var b = s.ReadUInt8();

                    var t = DecodeSample(b, ref index, ref currentSample);
                    output[offset++] = (byte)t;
                    output[offset++] = (byte)(t >> 8);

                    if (offset < outputSize)
                    {
                        /* possible that only half of the final byte is used! */
                        t = DecodeSample((byte)(b >> 4), ref index, ref currentSample);
                        output[offset++] = (byte)t;
                        output[offset++] = (byte)(t >> 8);
                    }
                }

                dataSize -= 8 + chunk.CompressedSize;
            }

            rawData = output;
            return(true);
        }
Beispiel #2
0
        public static byte[] LoadSound(Stream s)
        {
            var br = new BinaryReader(s);

            /*var sampleRate =*/ br.ReadUInt16();
            var dataSize   = br.ReadInt32();
            var outputSize = br.ReadInt32();

            /*var flags = (SoundFlags)*/ br.ReadByte();
            /*var format = (SoundFormat)*/ br.ReadByte();

            var output        = new byte[outputSize];
            var offset        = 0;
            var index         = 0;
            var currentSample = 0;

            while (dataSize > 0)
            {
                var chunk = Chunk.Read(br);
                for (int n = 0; n < chunk.CompressedSize; n++)
                {
                    var b = br.ReadByte();

                    var t = DecodeSample(b, ref index, ref currentSample);
                    output[offset++] = (byte)t;
                    output[offset++] = (byte)(t >> 8);

                    if (offset < outputSize)
                    {
                        /* possible that only half of the final byte is used! */
                        t = DecodeSample((byte)(b >> 4), ref index, ref currentSample);
                        output[offset++] = (byte)t;
                        output[offset++] = (byte)(t >> 8);
                    }
                }

                dataSize -= 8 + chunk.CompressedSize;
            }

            return(output);
        }