Beispiel #1
0
        static int read_itcompr16(ITPACK status, ModuleReader reader, short[] buffer, ushort count, ref ushort incnt)
        {
            int    x, y, needbits, havebits, new_count = 0;
            ushort bits    = status.bits;
            ushort bufbits = status.bufbits;
            short  last    = status.last;
            byte   buf     = status.buf;

            int place = 0;


            while (place < count)
            {
                needbits = new_count != 0 ? 4 : bits;
                x        = havebits = 0;

                while (needbits != 0)
                {
                    /* feed buffer */
                    if (bufbits == 0)
                    {
                        if (incnt-- > 0)
                        {
                            // Turns out the normal mikmod implementation just sorta ignores EOF when reading samples...
                            // so in this situation I've changed the mikMod test application to fail in these situations
                            // and going to get sharpMik todo the same.
                            buf = reader.Read_byte();

                            if (reader.isEOF())
                            {
                                throw new Exception("EOF while reading the samples!");
                            }
                        }
                        else
                        {
                            buf = 0;
                        }
                        bufbits = 8;
                    }


                    /* get as many bits as necessary */
                    y         = needbits < bufbits ? needbits : bufbits;
                    x        |= (buf & ((1 << y) - 1)) << havebits;
                    buf     >>= y;
                    bufbits   = (ushort)(bufbits - (ushort)y);
                    needbits -= y;
                    havebits += y;
                }



                if (new_count != 0)
                {
                    new_count = 0;
                    if (++x >= bits)
                    {
                        x++;
                    }
                    bits = (ushort)x;
                    continue;
                }

                if (bits < 7)
                {
                    if (x == (1 << (bits - 1)))
                    {
                        new_count = 1;
                        continue;
                    }
                }
                else if (bits < 17)
                {
                    y = (0xffff >> (17 - bits)) - 8;

                    if ((x > y) && (x <= y + 16))
                    {
                        if ((x -= y) >= bits)
                        {
                            x++;
                        }

                        bits = (ushort)(x);
                        continue;
                    }
                }
                else if (bits < 18)
                {
                    if (x >= 0x10000)
                    {
                        bits = (ushort)(x - 0x10000 + 1);
                        continue;
                    }
                }
                else
                {
                    /* error in compressed data... */
                    throw new Exception("Invalid data");
                }

                if (bits < 16) /* extend sign */
                {
                    x = ((short)(x << (16 - bits))) >> (16 - bits);
                }

                buffer[place++] = (last += (short)x);
            }
            status.bits    = bits;
            status.bufbits = bufbits;
            status.last    = last;
            status.buf     = buf;
            return(place);
        }
Beispiel #2
0
        static int read_itcompr8(ITPACK status, ModuleReader reader, short[] buffer, ushort count, ref ushort incnt)
        {
            ushort x;
            int    y, needbits, havebits, new_count = 0;
            ushort bits    = status.bits;
            ushort bufbits = status.bufbits;
            sbyte  last    = (sbyte)status.last;
            byte   buf     = status.buf;

            int place = 0;

            while (place < count)
            {
                needbits = (new_count != 0 ? 3 : bits);
                x        = 0;
                havebits = 0;

                while (needbits != 0)
                {
                    /* feed buffer */
                    if (bufbits == 0)
                    {
                        if (incnt-- > 0)
                        {
                            buf = reader.Read_byte();
                        }
                        else
                        {
                            buf = 0;
                        }
                        bufbits = 8;
                    }
                    /* get as many bits as necessary */
                    y         = needbits < bufbits ? needbits : bufbits;
                    x        |= (ushort)((buf & ((1 << y) - 1)) << havebits);
                    buf     >>= y;
                    bufbits  -= (ushort)y;
                    needbits -= y;
                    havebits += y;
                }

                if (new_count != 0)
                {
                    new_count = 0;
                    if (++x >= bits)
                    {
                        x++;
                    }
                    bits = (ushort)x;
                    continue;
                }
                if (bits < 7)
                {
                    if (x == (1 << (bits - 1)))
                    {
                        new_count = 1;
                        continue;
                    }
                }
                else if (bits < 9)
                {
                    y = (0xff >> (9 - bits)) - 4;
                    if ((x > y) && (x <= y + 8))
                    {
                        if ((x -= (ushort)y) >= bits)
                        {
                            x++;
                        }
                        bits = (ushort)x;
                        continue;
                    }
                }
                else if (bits < 10)
                {
                    if (x >= 0x100)
                    {
                        bits = (ushort)(x - 0x100 + 1);
                        continue;
                    }
                }
                else
                {
                    /* error in compressed data... */
                    throw new Exception("Invalid Data");
                }

                if (bits < 8) /* extend sign */
                {
                    x = (ushort)(((sbyte)(x << (8 - bits))) >> (8 - bits));
                }

                short val = (short)((last += (sbyte)x) << 8);
                buffer[place++] = (short)val;
            }

            status.bits    = bits;
            status.bufbits = bufbits;
            status.last    = last;
            status.buf     = buf;

            return(place);
        }