예제 #1
0
        short MixPcm16()
        {
            short pcm16 = 0;

            if (mChannelCount == 1)
            {
                if (mBitDepth == 16)
                {
                    pcm16 = ReadPcm16();
                }
                else
                {
                    Waveform.Pcm8toPcm16(ReadPcm8(), out pcm16);
                }
            }
            else
            {
                if (mBitDepth == 16)
                {
                    var left  = ReadPcm16();
                    var right = ReadPcm16();

                    pcm16 = MixStereoPcm16(left, right);
                }
                else
                {
                    var left  = ReadPcm8();
                    var right = ReadPcm8();

                    Waveform.Pcm8toPcm16(MixStereoPcm8(left, right), out pcm16);
                }
            }

            return(pcm16);
        }
예제 #2
0
        public override void ReadPcm16(int sample, out short pcm16)
        {
            if (sample < 0 || sample >= mSampleCount)
            {
                throw new ArgumentOutOfRangeException("sample");
            }

            pcm16 = 0;

            switch (mFormat)
            {
            case WaveFormat.Pcm8: {
                mReader.Goto(sample);
                Waveform.Pcm8toPcm16(mReader.ReadS8(), out pcm16);
                break;
            }

            case WaveFormat.Pcm16: {
                mReader.Goto(sample * 2);
                pcm16 = mReader.ReadS16();
                break;
            }

            case WaveFormat.Adpcm2: {
                int last = 0, penult = 0;
                var _pcm16 = new short[16];
                for (var i = 0; i < mSampleCount; i += 16)
                {
                    Waveform.Adpcm2toPcm16(mReader.Read8s(5), _pcm16, ref last, ref penult);
                    if (i <= sample && sample < (i + 16))
                    {
                        pcm16 = _pcm16[sample - i];
                        break;
                    }
                }
                break;
            }

            case WaveFormat.Adpcm4: {
                int last = 0, penult = 0;
                var _pcm16 = new short[16];
                for (var i = 0; i < mSampleCount; i += 16)
                {
                    Waveform.Adpcm4toPcm16(mReader.Read8s(9), _pcm16, ref last, ref penult);
                    if (i <= sample && sample < (i + 16))
                    {
                        pcm16 = _pcm16[sample - i];
                        break;
                    }
                }
                break;
            }
            }
        }
예제 #3
0
        protected override void WritePcm16(aBinaryWriter writer)
        {
            mReader.Goto(0);

            switch (mFormat)
            {
            case WaveFormat.Pcm8: {
                for (var i = 0; i < mSampleCount; ++i)
                {
                    short pcm16;
                    Waveform.Pcm8toPcm16(mReader.ReadS8(), out pcm16);
                    writer.WriteS16(pcm16);
                }
                break;
            }

            case WaveFormat.Pcm16: {
                writer.WriteS16s(mReader.ReadS16s(mSampleCount));
                break;
            }

            case WaveFormat.Adpcm2: {
                var pcm16 = new short[16];
                int last = 0, penult = 0;
                for (var i = 0; i < mSampleCount; i += 16)
                {
                    Waveform.Adpcm2toPcm16(mReader.Read8s(5), pcm16, ref last, ref penult);
                    writer.WriteS16s(pcm16, System.Math.Min(16, (mSampleCount - i)));
                }
                break;
            }

            case WaveFormat.Adpcm4: {
                var pcm16 = new short[16];
                int last = 0, penult = 0;
                for (var i = 0; i < mSampleCount; i += 16)
                {
                    Waveform.Adpcm4toPcm16(mReader.Read8s(9), pcm16, ref last, ref penult);
                    writer.WriteS16s(pcm16, System.Math.Min(16, (mSampleCount - i)));
                }
                break;
            }
            }
        }
예제 #4
0
        public override void ReadPcm16(int sample, out short left, out short right)
        {
            if (sample < 0 || sample >= mSampleCount)
            {
                throw new ArgumentOutOfRangeException("sample");
            }

            GotoSample(sample);

            if (mChannelCount == 1)
            {
                short pcm16;

                if (mBitDepth == 16)
                {
                    pcm16 = ReadPcm16();
                }
                else
                {
                    Waveform.Pcm8toPcm16(ReadPcm8(), out pcm16);
                }

                left  = pcm16;
                right = pcm16;
            }
            else
            {
                if (mBitDepth == 16)
                {
                    left  = ReadPcm16();
                    right = ReadPcm16();
                }
                else
                {
                    Waveform.Pcm8toPcm16(ReadPcm8(), out left);
                    Waveform.Pcm8toPcm16(ReadPcm8(), out right);
                }
            }
        }
예제 #5
0
        protected override void WriteAdpcm4(aBinaryWriter writer)
        {
            mReader.Goto(0);

            switch (mFormat)
            {
            case WaveFormat.Pcm8: {
                var pcm16 = new short[16];
                var adpcm4 = new byte[9];
                int last = 0, penult = 0;
                for (var i = 0; i < mSampleCount; i += 16)
                {
                    for (var j = 0; j < 16; ++j)
                    {
                        pcm16[j] = 0;

                        if (i + j < mSampleCount)
                        {
                            Waveform.Pcm8toPcm16(mReader.ReadS8(), out pcm16[j]);
                        }
                    }
                    Waveform.Pcm16toAdpcm4(pcm16, adpcm4, ref last, ref penult);
                    writer.Write8s(adpcm4);
                }
                break;
            }

            case WaveFormat.Pcm16: {
                var pcm16 = new short[16];
                var adpcm4 = new byte[9];
                int _last = 0, _penult = 0;
                for (var i = 0; i < mSampleCount; i += 16)
                {
                    for (var j = 0; j < 16; ++j)
                    {
                        pcm16[j] = (short)(i + j < mSampleCount ? mReader.ReadS16() : 0);
                    }
                    Waveform.Pcm16toAdpcm4(pcm16, adpcm4, ref _last, ref _penult);
                    writer.Write8s(adpcm4);
                }
                break;
            }

            case WaveFormat.Adpcm2: {
                var pcm16 = new short[16];
                var adpcm4 = new byte[9];
                int last1 = 0, penult1 = 0;
                int last2 = 0, penult2 = 0;
                for (var i = 0; i < mSampleCount; i += 16)
                {
                    Waveform.Adpcm2toPcm16(mReader.Read8s(5), pcm16, ref last1, ref penult1);
                    Waveform.Pcm16toAdpcm4(pcm16, adpcm4, ref last2, ref penult2);
                    writer.Write8s(adpcm4);
                }
                break;
            }

            case WaveFormat.Adpcm4: {
                for (var i = 0; i < mSampleCount; i += 16)
                {
                    writer.Write8s(mReader.Read8s(9));
                }
                break;
            }
            }
        }
예제 #6
0
        protected override void CalculateHistoryAdpcm4(int sample, out int last, out int penult)
        {
            last   = 0;
            penult = 0;

            var frame = (sample / 16);

            if (frame == 0)
            {
                return;
            }

            switch (mFormat)
            {
            case WaveFormat.Pcm8: {
                var adpcm4 = new byte[9];
                var pcm16 = new short[16];
                int _last = 0, _penult = 0;

                mReader.Goto(0);

                for (var i = 0; i < frame; ++i)
                {
                    for (var j = 0; j < 16; ++j)
                    {
                        pcm16[j] = 0;

                        if ((i * 16 + j) < mSampleCount)
                        {
                            Waveform.Pcm8toPcm16(mReader.ReadS8(), out pcm16[j]);
                        }
                    }

                    Waveform.Pcm16toAdpcm4(pcm16, adpcm4, ref _last, ref _penult);
                    Waveform.Adpcm4toPcm16(adpcm4, pcm16, ref last, ref penult);
                }

                break;
            }

            case WaveFormat.Pcm16: {
                var adpcm4 = new byte[9];
                var pcm16 = new short[16];
                int _last = 0, _penult = 0;

                mReader.Goto(0);

                for (var i = 0; i < frame; ++i)
                {
                    for (var j = 0; j < 16; ++j)
                    {
                        pcm16[j] = 0;

                        if ((i + j) < mSampleCount)
                        {
                            pcm16[j] = mReader.ReadS16();
                        }
                    }

                    Waveform.Pcm16toAdpcm4(pcm16, adpcm4, ref _last, ref _penult);
                    Waveform.Adpcm4toPcm16(adpcm4, pcm16, ref last, ref penult);
                }

                break;
            }

            case WaveFormat.Adpcm2: {
                var adpcm4 = new byte[9];
                var pcm16 = new short[16];
                int last1 = 0, penult1 = 0;
                int last2 = 0, penult2 = 0;

                mReader.Goto(0);

                for (var i = 0; i < frame; ++i)
                {
                    Waveform.Adpcm2toPcm16(mReader.Read8s(5), pcm16, ref last1, ref penult1);
                    Waveform.Pcm16toAdpcm4(pcm16, adpcm4, ref last2, ref penult2);
                    Waveform.Adpcm4toPcm16(adpcm4, pcm16, ref last, ref penult);
                }

                break;
            }

            case WaveFormat.Adpcm4: {
                var pcm16 = new short[16];

                mReader.Goto(0);

                for (var i = 0; i < frame; ++i)
                {
                    Waveform.Adpcm4toPcm16(mReader.Read8s(9), pcm16, ref last, ref penult);
                }

                break;
            }
            }
        }