예제 #1
0
        private byte[] Format2(byte[] data)
        {
            byte[]  rs = format2DownSampler.inputResample(data, data.Length / 4);
            float[] f  = Converters.bytes2floats(rs);

            float[] mono = Converters.stereo2mono(f);

            short[] shorts = Converters.floats2shorts(mono, true);

            int bytes = (int)(shorts.Length / 2);

            byte[] output = new byte[bytes];

            encode2.Encode(encode2State, output, shorts, shorts.Length);
            return(output);
        }
예제 #2
0
        private byte[] Format1(byte[] data)
        {
            short[] shorts = Converters.floats2shorts(Converters.bytes2floats(data), true);

            // DeMux into Left and Right
            List <short[]> lr = Converters.deMuxStereo(shorts);

            // CALCULATE NUMBER OF BYTES IN OUTPUT
            int bytes = (int)(lr[0].Length / 2);

            byte[] outputL = new byte[bytes];
            byte[] outputR = new byte[bytes];

            encode1L.Encode(encode1Lstate, outputL, lr[0], lr[0].Length);
            encode1R.Encode(encode1Rstate, outputR, lr[1], lr[1].Length);

            byte[] output = new byte[outputL.Length + outputR.Length];
            Array.Copy(outputL, output, outputL.Length);
            Array.Copy(outputR, 0, output, outputL.Length, outputR.Length);
            return(output);
        }
예제 #3
0
        /**
         * FORMAT 3, 4, 5
         * OPUS 64kbit/s, 32 kbit/s, 16 kbit/s
         **/
        private byte[] EncodeOpus(byte[] data_b)
        {
            float[] data = Converters.bytes2floats(data_b);

            // STORE UP DATA UNTIL WE HAVE ENOUGH TO ENCODE A FRAME
            opus_data.AddRange(Converters.floats2shorts(data, true));

            // CLEAR THE COMPRESSED DATA
            opus_compdata.Clear();

            while (opus_data.Count > 960 * 2)
            {
                // Encoding loop
                short[] inputAudioSamples = opus_data.GetRange(0, 960 * 2).ToArray();

                opus_data.RemoveRange(0, 960 * 2);

                byte[] outputBuffer = new byte[1000];
                int    frameSize    = 960;

                int thisPacketSize = opus_encoder.Encode(inputAudioSamples, 0, frameSize, outputBuffer, 0, outputBuffer.Length);                 // this throws OpusException on a failure, rather than returning a negative number


                byte[] truncArray = new byte[thisPacketSize];

                Array.Copy(outputBuffer, truncArray, truncArray.Length);

                opus_compdata.AddRange(truncArray);
            }

            if (opus_compdata.Count > 0)
            {
                return(opus_compdata.ToArray());
            }
            return(null);
        }
예제 #4
0
        private void SetMeterLevel(byte[] samples, int meter, int scale)
        {
            float avg   = 0;
            float sum   = 0;
            float max   = 0;
            short count = 0;
            float val;

            float[] values = Converters.bytes2floats(samples);

            for (int i = 0; i < values.Length - 3; i += 4)
            {
                val = values[i];

                if (val == float.MinValue)
                {
                    val = float.MaxValue;
                }
                else
                {
                    val = Math.Abs(val);
                }
                sum += val;

                if (val > max)
                {
                    max = val;
                }

                count++;
            }

            if (sum == 0)
            {
                avg = 0.001f;
            }
            else
            {
                avg = sum / count;
            }

            double dbfs = Math.Log10(max) * 20;

            // Return Time



            if (scale == 1)
            {
                double currentDbFS = meters[meter].getValue1() - 18 - (returnPrSec * (mainServerInterval / 1000d));
                if (currentDbFS < dbfs)
                {
                    currentDbFS = dbfs;
                }
                double dbu = currentDbFS + 18;
                meters[meter].setValue1((float)dbu);
            }
            else
            {
                double currentDbFS = meters[meter].getValue2() - 18 - (returnPrSec * (mainServerInterval / 1000d));
                if (currentDbFS < dbfs)
                {
                    currentDbFS = dbfs;
                }
                double dbu = currentDbFS + 18;
                meters[meter].setValue2((float)dbu);
            }
        }