Beispiel #1
0
        /**
         * FORMAT 3, 4, 5
         * OPUS 64kbit/s, 32 kbit/s, 16 kbit/s (mono)
         **/
        private byte[] EncodeOpus(float[] data)
        {
            // STORE UP DATA UNTIL WE HAVE ENOUGH TO ENCODE A FRAME
            format3data.AddRange(Converters.floats2shorts(data, true));

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

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

                format3data.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);

                format3compdata.AddRange(truncArray);
            }

            if (format3compdata.Count > 0)
            {
                return(format3compdata.ToArray());
            }
            return(null);
        }
Beispiel #2
0
        /**
         * FORMAT 2
         * G722 16kHz mono
         **/
        private byte[] EncodeFormat2(float[] data)
        {
            byte[]  rs = codecDownSampler.inputResample(Converters.floats2bytes(data), data.Length);
            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);
        }
Beispiel #3
0
        /**
         * FORMAT 1
         * G722 48kHz stereo
         **/
        private byte[] EncodeFormat1(float[] data)
        {
            short[] shorts = Converters.floats2shorts(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);
        }