Beispiel #1
0
        private byte[] Format2(byte[] data)
        {
            short[] output = new short[data.Length * 2];
            decode2.Decode(decode2State, output, data, data.Length);

            float[] f = Converters.shorts2floats(output, true);
            float[] s = Converters.mono2stereo(f);

            byte[] o = format2upSampler.inputResample(Converters.floats2bytes(s), s.Length);

            return(o);
        }
Beispiel #2
0
        private byte[] Format1(byte[] data)
        {
            // SPLIT INTO TWO PARTS
            byte[] L = new byte[data.Length / 2];
            byte[] R = new byte[data.Length / 2];

            Array.Copy(data, L, L.Length);
            Array.Copy(data, L.Length, R, 0, R.Length);


            short[] outputL = new short[L.Length * 2];
            short[] outputR = new short[R.Length * 2];
            decode1L.Decode(decode1Lstate, outputL, L, L.Length);
            decode1R.Decode(decode1Rstate, outputR, R, R.Length);

            short[] output = Converters.MuxDualMono(outputL, outputR);

            return(Converters.floats2bytes(Converters.shorts2floats(output, true)));
        }
Beispiel #3
0
        private byte[] DecodeOpus(byte[] data, int packetSize)
        {
            // Decoding loop
            int frames    = data.Length / packetSize;
            int frameSize = 960;             // must be same as framesize used in input, you can use OpusPacketInfo.GetNumSamples() to determine this dynamically

            short[]      outputBuffer;
            List <float> outData = new List <float>();

            for (int i = 0; i < frames; i++)
            {
                outputBuffer = new short[frameSize * 2];

                int thisFrameSize = opus_decoder.Decode(data, i * packetSize, packetSize, outputBuffer, 0, frameSize, false);

                outData.AddRange(Converters.shorts2floats(outputBuffer, true));
            }

            return(Converters.floats2bytes(outData.ToArray()));
        }
Beispiel #4
0
        private void MixAudio()
        {
            List <float> output;
            List <float> pfl;
            int          currentSamples   = 0;
            int          connectedClients = 0;

            // MIX CLIENTS AUDIO
            lock (clientsLock)
            {
                foreach (KeyValuePair <string, Client> kvp in clients)
                {
                    Client c = kvp.Value;
                    if (c.IsConnected())
                    {
                        connectedClients++;
                        output = new List <float>();
                        for (int i = 0; i < c.currentSamples.Length / 4; i++)
                        {
                            output.Add(0f);
                        }

                        lock (c.sourcesLock)
                        {
                            foreach (int loop in c.GetSources())
                            {
                                Loop l = loops[loop];
                                lock (l.talkersLock)
                                {
                                    foreach (string talker in l.talkers)
                                    {
                                        if (clients[talker].IsConnected() && (c.guid != talker || NminusOne == false))
                                        {
                                            output = mixSamples(output, clients[talker]);
                                        }
                                    }
                                }
                            }
                        }

                        c.outgoingSamples = Converters.floats2bytes(output.ToArray());
                    }
                    else
                    {
                        c.outgoingSamples = null;
                    }
                    currentSamples = c.currentSamples.Length;
                }

                // MIX LOOP AUDIO FOR METERING AND SERVER PFL
                pfl = new List <float>();
                for (int i = 0; i < currentSamples / 4; i++)
                {
                    pfl.Add(0f);
                }

                foreach (Loop loop in loops)
                {
                    output = new List <float>();
                    for (int i = 0; i < currentSamples / 4; i++)
                    {
                        output.Add(0f);
                    }

                    foreach (string talker in loop.talkers)
                    {
                        output = mixSamples(output, clients[talker]);
                    }
                    loop.outgoingSamples = Converters.floats2bytes(output.ToArray());

                    if (loop.pfl)
                    {
                        pfl = mixSamples(pfl, output);
                    }
                }
                byte[] bytes = Converters.floats2bytes(pfl.ToArray());

                // If no clients are connected, ignore pfl
                if (connectedClients > 0)
                {
                    pflBuffer.AddSamples(bytes, 0, bytes.Length);

                    // Keep pflBuffer below 200ms
                    if (pflBuffer.BufferedDuration.TotalMilliseconds > 200)
                    {
                        double bytes_to_read = (sampleRate / 1000) * channels * (pflBuffer.BufferedDuration.TotalMilliseconds - 200);
                        byte[] void_array    = new byte[(int)bytes_to_read];
                        pflBuffer.Read(void_array, 0, (int)bytes_to_read);

                        //Logger.WriteLine("Trew away " + bytes_to_read.ToString() + " bytes from pflBuffer");
                    }
                }
            }
        }