예제 #1
0
        public byte[] encode(byte[] pcmIn)
        {
            byte[] Output;
            Output = new byte[(pcmIn.Length) / 4 + 3];
            uint  i;
            short s;

            int  sign;          // Current adpcm sign bit
            int  delta;         // Current adpcm output value
            int  step;          // Stepsize
            int  valprev;       // virtual previous output value
            int  vpdiff;        // Current change to valprev
            int  index;         // Current step change index
            byte bitbuffer = 0; // place to keep previous 4-bit value

            valprev = this.s.valprev;
            index   = this.s.index;

            step = stepsizeTable[index];

            for (i = 0; i <= pcmIn.Length - 4; i += 4)
            {
                s = BitConverter.ToInt16(pcmIn, (int)i);

                //**** Step 1 - compute difference with previous value
                delta = s - valprev;
                sign  = (delta < 0) ? 8 : 0;
                if (sign == 8)
                {
                    delta = -delta;
                }

                //**** Step 2 - Divide and clamp
                delta = (int)Math.Round((double)(delta << 2) / (double)step);
                if (delta > 7)
                {
                    delta = 7;
                }
                vpdiff = (delta * step) >> 2;

                //**** Step 3 - Update previous value
                if (sign == 8)
                {
                    valprev -= vpdiff;
                }
                else
                {
                    valprev += vpdiff;
                }

                //**** Step 4 - Clamp previous value to 16 bits
                if (valprev > short.MaxValue)
                {
                    valprev = short.MaxValue;
                }
                else if (valprev < short.MinValue)
                {
                    valprev = short.MinValue;
                }

                //**** Step 5 - Assemble value, update index and step values
                delta  = delta | sign;
                index += indexTable[delta];
                if (index < 0)
                {
                    index = 0;
                }
                if (index > 88)
                {
                    index = 88;
                }
                step = stepsizeTable[index];
                //**** Step 6 - Writing values into the array
                bitbuffer = (byte)(delta << 4);

                //NEXT SAMPLE
                s = BitConverter.ToInt16(pcmIn, (int)i + 2);

                //**** Step 1 - compute difference with previous value
                delta = s - valprev;
                sign  = (delta < 0) ? 8 : 0;
                if (sign == 8)
                {
                    delta = -delta;
                }

                //**** Step 2 - Divide and clamp
                delta = (int)Math.Round((double)(delta << 2) / (double)step);
                if (delta > 7)
                {
                    delta = 7;
                }
                vpdiff = (delta * step) >> 2;

                //**** Step 3 - Update previous value
                if (sign == 8)
                {
                    valprev -= vpdiff;
                }
                else
                {
                    valprev += vpdiff;
                }

                //**** Step 4 - Clamp previous value to 16 bits
                if (valprev > short.MaxValue)
                {
                    valprev = short.MaxValue;
                }
                else if (valprev < short.MinValue)
                {
                    valprev = short.MinValue;
                }

                //**** Step 5 - Assemble value, update index and step values
                delta  = delta | sign;
                index += indexTable[delta];
                if (index < 0)
                {
                    index = 0;
                }
                if (index > 88)
                {
                    index = 88;
                }
                step = stepsizeTable[index];

                //**** Step 6 - Writing values into the array
                Output[i / 4 + 3] = (byte)(delta | bitbuffer);
            }

            //Splitting Values to Byte-Array

            Output[0] = BitConverter.GetBytes(this.s.valprev)[0];
            Output[1] = BitConverter.GetBytes(this.s.valprev)[1];
            Output[2] = this.s.index;

            //Update state
            this.s.valprev = (short)valprev;
            this.s.index   = (byte)index;

            //Output
            return(Output);
        }
예제 #2
0
 public void resetState()
 {
     s = new StateMono();
 }