Ejemplo n.º 1
0
        private void ReduceBitsPerSample24Other(ConvertParams args, DataSubChunk toDsc)
        {
            uint mask           = 0xffffffff << (24 - args.newQuantizationBitrate);
            uint maskError      = ~mask;
            var  gen            = new RNGCryptoServiceProvider();
            var  gng            = new GaussianNoiseGenerator();
            var  randomNumber   = new byte[3];
            int  noiseMagnitude = (int)Math.Pow(2, (24 - args.newQuantizationBitrate)) / 2;

            Console.WriteLine("D: maskErr={0:X}", maskError);

            int bytesPerFrame = mFsc.numChannels * mFsc.bitsPerSample / 8;
            int numFrames     = toDsc.data.Length / bytesPerFrame;

            int pos = 0;

            for (int i = 0; i < numFrames; ++i)
            {
                for (int ch = 0; ch < mFsc.numChannels; ++ch)
                {
                    double sample = ReadSampleValue24(toDsc, pos);
                    uint   error  = (uint)sample & maskError;
                    sample -= error;

                    switch (args.ditherType)
                    {
                    case ConvertParams.DitherType.Truncate:
                        break;

                    case ConvertParams.DitherType.RpdfDither:
                        gen.GetBytes(randomNumber);
                        int randDither = (int)((randomNumber[0]) + (randomNumber[1] << 8) + (randomNumber[2] << 16) & ~mask);
                        sample += randDither;
                        break;

                    case ConvertParams.DitherType.GaussianDither:
                        float noise = gng.NextFloat();
                        noise  *= noiseMagnitude;
                        sample += (int)noise;
                        break;

                    default:
                        System.Diagnostics.Debug.Assert(false);
                        break;
                    }

                    if (0x7fffff < sample)
                    {
                        sample = 0x7fffff;
                    }
                    if (sample < -0x800000)
                    {
                        sample = -0x800000;
                    }

                    WriteSampleValue24(toDsc, pos, (int)sample);

                    pos += mFsc.bitsPerSample / 8;
                }
            }
        }
Ejemplo n.º 2
0
        private void ReduceBitsPerSample24Other(ConvertParams args, DataSubChunk toDsc)
        {
            uint mask = 0xffffffff << (24 - args.newQuantizationBitrate);
            uint maskError = ~mask;
            var gen = new RNGCryptoServiceProvider();
            var gng = new GaussianNoiseGenerator();
            var randomNumber = new byte[3];
            int noiseMagnitude = (int)Math.Pow(2, (24 - args.newQuantizationBitrate))/2;

            Console.WriteLine("D: maskErr={0:X}", maskError);

            int bytesPerFrame = mFsc.numChannels * mFsc.bitsPerSample / 8;
            int numFrames = toDsc.data.Length / bytesPerFrame;

            int pos = 0;
            for (int i=0; i < numFrames; ++i) {
                for (int ch=0; ch < mFsc.numChannels; ++ch) {
                    double sample = ReadSampleValue24(toDsc, pos);
                    uint error = (uint)sample & maskError;
                    sample -= error;

                    switch (args.ditherType) {
                    case ConvertParams.DitherType.Truncate:
                        break;
                    case ConvertParams.DitherType.RpdfDither:
                        gen.GetBytes(randomNumber);
                        int randDither = (int)((randomNumber[0]) + (randomNumber[1] << 8) + (randomNumber[2]<<16) & ~mask);
                        sample += randDither;
                        break;
                    case ConvertParams.DitherType.GaussianDither:
                        float noise = gng.NextFloat();
                        noise *= noiseMagnitude;
                        sample += (int)noise;
                        break;
                    default:
                        System.Diagnostics.Debug.Assert(false);
                        break;
                    }

                    if (0x7fffff < sample) {
                        sample = 0x7fffff;
                    }
                    if (sample < -0x800000) {
                        sample = -0x800000;
                    }

                    WriteSampleValue24(toDsc, pos, (int)sample);

                    pos += mFsc.bitsPerSample / 8;
                }
            }
        }