コード例 #1
0
        public int Read(byte[] buffer, int offset, int count)
        {
            int read = source.Read(buffer, offset, count);

            if (Settings.GlobalSettingsStore.Instance.ProfileSettingsStore.GetClientSettingBool(ProfileSettingsKeys.NATOTone))
            {
                var effectBytes = GetEffect(read / 2);

                //mix together
                for (int i = 0; i < read / 2; i++)
                {
                    short audio = ConversionHelpers.ToShort(buffer[(offset + i) * 2], buffer[((i + offset) * 2) + 1]);

                    audio = (short)(audio + _audioEffectShort[i]);

                    //buffer[i + offset] = effectBytes[i]+buffer[i + offset];

                    byte byte1;
                    byte byte2;
                    ConversionHelpers.FromShort(audio, out byte1, out byte2);

                    buffer[(offset + i) * 2]       = byte1;
                    buffer[((i + offset) * 2) + 1] = byte2;
                }
            }

            return(read);
        }
コード例 #2
0
        public static byte[] CreateStereoMix(byte[] pcmAudio)
        {
            var stereoMix = new byte[pcmAudio.Length * 2];

            for (var i = 0; i < pcmAudio.Length / 2; i++)
            {
                short audio = ConversionHelpers.ToShort(pcmAudio[i * 2], pcmAudio[i * 2 + 1]);

                //half audio to keep loudness the same
                if (audio != 0)
                {
                    audio = (short)(audio / 2);
                }

                byte byte1;
                byte byte2;
                ConversionHelpers.FromShort(audio, out byte1, out byte2);

                stereoMix[i * 4]     = byte1;
                stereoMix[i * 4 + 1] = byte2;

                stereoMix[i * 4 + 2] = byte1;
                stereoMix[i * 4 + 3] = byte2;
            }
            return(stereoMix);
        }
コード例 #3
0
        public void ConversionShortToBytes()
        {
            short[] shorts = new short[] { 1, short.MaxValue, short.MinValue, 0 };

            foreach (var shortTest in shorts)
            {
                byte byte1;
                byte byte2;
                ConversionHelpers.FromShort(shortTest, out byte1, out byte2);

                byte[] converted = BitConverter.GetBytes(shortTest);

                Assert.AreEqual(byte1, converted[0]);
                Assert.AreEqual(byte2, converted[1]);

                //convert back
                Assert.AreEqual(shortTest, ConversionHelpers.ToShort(byte1, byte2));
            }
        }
コード例 #4
0
        public static byte[] CreateBalancedMix(byte[] pcmAudio, float balance)
        {
            float left  = 1.0f;
            float right = 1.0f;

            //right
            if (balance > 0)
            {
                var leftBias  = 1 - Math.Abs(balance);
                var rightBias = Math.Abs(balance);
                //right
                left  = left * leftBias;
                right = right * rightBias;
            }
            else if (balance < 0)
            {
                var leftBias  = Math.Abs(balance);
                var rightBias = 1 - Math.Abs(balance);
                //left
                left  = left * leftBias;
                right = right * rightBias;
            }
            else
            {
                //equal balance
                left  = 0.5f;
                right = 0.5f;
            }

            if (left > 1f)
            {
                left = 1f;
            }
            if (right > 1f)
            {
                right = 1f;
            }

            var stereoMix = new byte[pcmAudio.Length * 2];

            for (var i = 0; i < pcmAudio.Length / 2; i++)
            {
                float audio = ConversionHelpers.ToShort(pcmAudio[i * 2], pcmAudio[i * 2 + 1]);

                short leftAudio  = 0;
                short rightAudio = 0;
                //half audio to keep loudness the same
                if (audio != 0)
                {
                    leftAudio  = Convert.ToInt16(audio * left);
                    rightAudio = Convert.ToInt16(audio * right);
                }

                byte byte1;
                byte byte2;
                ConversionHelpers.FromShort(leftAudio, out byte1, out byte2);

                byte byte3;
                byte byte4;
                ConversionHelpers.FromShort(rightAudio, out byte3, out byte4);

                stereoMix[i * 4]     = byte1;
                stereoMix[i * 4 + 1] = byte2;

                stereoMix[i * 4 + 2] = byte3;
                stereoMix[i * 4 + 3] = byte4;
            }
            return(stereoMix);
        }