Пример #1
0
        /// <summary>
        /// Takes an ogg file and splits it down to a list of pcms
        /// </summary>
        /// <param name="fileName">The full file name</param>
        /// <returns>A list of byte arrays (pcms) that represent the full audio file</returns>
        public static List <byte[]> Extract(string fileName)
        {
            const int     CHANNELS = 2;
            List <byte[]> samples  = new List <byte[]>();

            using (FileStream stream = File.Open($"./audio_files/{fileName}", FileMode.Open))
            {
                OpusEncoder       encoder = new OpusEncoder(48000, CHANNELS, Concentus.Enums.OpusApplication.OPUS_APPLICATION_AUDIO);
                OpusDecoder       decoder = new OpusDecoder(48000, CHANNELS);
                OpusOggReadStream oggIn   = new OpusOggReadStream(decoder, stream);
                while (oggIn.HasNextPacket)
                {
                    short[] pcm = oggIn.DecodeNextPacket();
                    if (pcm != null)
                    {
                        byte[] outputBytes       = new byte[1920 * 4];
                        int    outputBytesLength = encoder.Encode(pcm, 0, 1920, outputBytes, 0, outputBytes.Length);
                        outputBytes = outputBytes.Take(outputBytesLength).ToArray();
                        samples.Add(outputBytes);
                    }
                }
            }

            return(samples);
        }
Пример #2
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);
        }
Пример #3
0
        static void _waveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            var recorderBytes  = e.Buffer.AsSpan(0, e.BytesRecorded);
            var recordedShorts = MemoryMarshal.Cast <byte, short>(recorderBytes);
            //_playBuffer.AddSamples(MemoryMarshal.Cast<short, byte>(recordedShorts).ToArray(), 0, e.BytesRecorded);
            //return;

            var soundBuffer = new short[recordedShorts.Length + _notEncodedBuffer.Length];

            _notEncodedBuffer.CopyTo(soundBuffer, 0);
            recordedShorts.CopyTo(soundBuffer.AsSpan(_notEncodedBuffer.Length));

            int segmentCount = soundBuffer.Length / ShortsPerSegment;

            var willNotEncoded = soundBuffer.AsSpan(segmentCount * ShortsPerSegment);

            _notEncodedBuffer = willNotEncoded.ToArray();

            for (int i = 0; i < segmentCount; i++)
            {
                var segment = soundBuffer.AsSpan(i * ShortsPerSegment, ShortsPerSegment);
                var buff    = _encoder.Encode(segment, segment.Length);
                _bytesSent += (ulong)buff.Length;
                var dec = _decoder.Decode(buff, segment.Length);
                _playBuffer.AddSamples(MemoryMarshal.Cast <short, byte>(dec).ToArray(), 0, dec.Length * 2);
            }
        }
Пример #4
0
        public static void Main(string[] args)
        {
            FileStream  fileIn  = new FileStream("C:\\Users\\lostromb\\Documents\\Visual Studio 2015\\Projects\\Concentus-git\\AudioData\\repro.raw", FileMode.Open);
            OpusEncoder encoder = new OpusEncoder(16000, 1, OpusApplication.OPUS_APPLICATION_AUDIO);

            encoder.Bitrate    = (96000);
            encoder.ForceMode  = (OpusMode.MODE_CELT_ONLY);
            encoder.SignalType = (OpusSignal.OPUS_SIGNAL_MUSIC);
            encoder.Complexity = (0);

            OpusDecoder decoder = new OpusDecoder(16000, 1);

            FileStream fileOut       = new FileStream("C:\\Users\\lostromb\\Documents\\Visual Studio 2015\\Projects\\Concentus-git\\AudioData\\out_c.raw", FileMode.Create);
            int        packetSamples = 960;

            byte[] inBuf       = new byte[packetSamples * 2];
            byte[] data_packet = new byte[1275];
            while (fileIn.Length - fileIn.Position >= inBuf.Length)
            {
                int     bytesRead    = fileIn.Read(inBuf, 0, inBuf.Length);
                short[] pcm          = BytesToShorts(inBuf, 0, inBuf.Length);
                int     bytesEncoded = encoder.Encode(pcm, 0, packetSamples, data_packet, 0, 1275);
                //System.out.println(bytesEncoded + " bytes encoded");

                int samplesDecoded = decoder.Decode(data_packet, 0, bytesEncoded, pcm, 0, packetSamples, false);
                //System.out.println(samplesDecoded + " samples decoded");
                byte[] bytesOut = ShortsToBytes(pcm);
                fileOut.Write(bytesOut, 0, bytesOut.Length);
            }

            fileIn.Close();
            fileOut.Close();
        }
Пример #5
0
        private void CaptureCallback(IAsyncResult ar)
        {
            if (!_capture.CanRead)
            {
                return;
            }
            if (!IsConnected)
            {
                return;
            }

            var read = _capture.EndRead(ar);

            if (SelectedVoiceMode == VoiceMode.VoiceActivation)
            {
                HandleVoiceActivation();
            }

            if (IsSpeaking)
            {
#if DEBUG
                Console.WriteLine(_readBuffer.Length);
#endif

                int    length;
                byte[] encoded = _encoder.Encode(_readBuffer, _readBuffer.Length, out length);

                _packets.Add(new VoicePacket(encoded, length));

                _capture.BeginRead(_readBuffer, 0, _readBuffer.Length, CaptureCallback, null);
                return;
            }

            _capture.BeginRead(_readBuffer, 0, _readBuffer.Length, CaptureCallback, null);
        }
Пример #6
0
        public void waveIn_DataAvailableEvent(object sender, WaveInEventArgs e)
        {
            sum = 0;
            for (int i = 0; i < 8; i++)
            {
                sum += Math.Abs(BitConverter.ToInt16(e.Buffer, 200 * i));
            }
            sum      /= 8;
            volume_in = sum;
            if (sum > lastmax * 0.2)
            {
                soundBuffer = new byte[e.BytesRecorded + notEncodedBuffer.Length]; //Legnht = new data + old data
                for (int i = 0; i < notEncodedBuffer.Length; i++)                  //First we try encode as much as we can from old data
                {
                    soundBuffer[i] = notEncodedBuffer[i];
                }
                for (int i = 0; i < e.BytesRecorded; i++)
                {
                    soundBuffer[i + notEncodedBuffer.Length] = e.Buffer[i];
                }
            }
            else
            {
                soundBuffer = new byte[notEncodedBuffer.Length];
                for (int i = 0; i < notEncodedBuffer.Length; i++)   //First we try encode as much as we can from old data
                {
                    soundBuffer[i] = notEncodedBuffer[i];
                }
            }

            if (soundBuffer.Length != 0)
            {
                int byteCap         = bytesPerSegment;
                int segmentCount    = (int)Math.Floor((decimal)soundBuffer.Length / byteCap);
                int segmentsEnd     = segmentCount * byteCap;
                int notEncodedCount = soundBuffer.Length - segmentsEnd;
                notEncodedBuffer = new byte[notEncodedCount];
                for (int i = 0; i < notEncodedCount; i++)
                {
                    notEncodedBuffer[i] = soundBuffer[segmentsEnd + i];
                }
                int len;

                for (int i = 0; i < segmentCount; i++)
                {
                    byte[] segment = new byte[byteCap];
                    for (int j = 0; j < segment.Length; j++)
                    {
                        segment[j] = soundBuffer[(i * byteCap) + j];
                    }
                    byte[] EncodedBuffer = encoder.Encode(segment, segment.Length, out len);
                    bytesSent += (ulong)len;
                    byte[] CutEncoded = new byte[len];
                    Array.Copy(EncodedBuffer, CutEncoded, len);
                    //send
                    //decode(CutEncoded, CutEncoded.Length);
                    network.Datasend(CutEncoded);
                }
            }
        }
Пример #7
0
        public byte[] Encode(float[] samples)
        {
            var opusPacket     = new byte[samples.Length];
            int opusPacketSize = _encoder.Encode(samples, 0, samples.Length / Channels, opusPacket, 0, opusPacket.Length);

            return(opusPacket.Take(opusPacketSize).ToArray());
        }
Пример #8
0
        public override OpusFormat EncodeFromPcm16(Pcm16Format pcm16, OpusParameters config)
        {
            const int frameSize = 960;
            var       encoder   = new OpusEncoder(pcm16.SampleRate, pcm16.ChannelCount, OpusApplication.OPUS_APPLICATION_AUDIO);

            if (config.Bitrate > 0)
            {
                encoder.Bitrate = config.Bitrate;
            }

            int inPos     = 0;
            int remaining = pcm16.SampleCount;

            short[] pcmData = pcm16.Channels.Interleave(1);

            // Encoded data shouldn't be larger than the input pcm
            var buffer = new byte[frameSize * pcm16.ChannelCount];
            var frames = new List <OpusFrame>();

            short[] encodeInput = pcmData;

            while (remaining >= 0)
            {
                int encodeCount = Math.Min(frameSize, remaining);

                if (remaining < frameSize)
                {
                    encodeInput = new short[frameSize * pcm16.ChannelCount];
                    Array.Copy(pcmData, inPos, encodeInput, 0, encodeCount);
                    inPos = 0;
                }

                int frameLength = encoder.Encode(encodeInput, inPos, frameSize, buffer, 0, buffer.Length);

                var frame = new OpusFrame();
                frame.Length      = frameLength;
                frame.Data        = new byte[frameLength];
                frame.FinalRange  = encoder.FinalRange;
                frame.SampleCount = frameSize;

                Array.Copy(buffer, frame.Data, frameLength);

                frames.Add(frame);

                if (remaining == 0)
                {
                    break;
                }

                remaining -= encodeCount;
                inPos     += encodeCount * pcm16.ChannelCount;
            }

            OpusFormat format = new OpusFormatBuilder(pcm16.SampleCount, pcm16.SampleRate, pcm16.ChannelCount, encoder.Lookahead, frames)
                                .WithLoop(pcm16.Looping, pcm16.LoopStart, pcm16.LoopEnd)
                                .Build();

            return(format);
        }
Пример #9
0
        private static void PlayWorker()
        {
            Stopwatch time = new Stopwatch();

            time.Start();
            int sample     = 0;
            int sampleTime = 960 / (read.WaveFormat.SampleRate / 1000);

            PlayerState = PlayerState.Running;

            while (true)
            {
                ct.ThrowIfCancellationRequested();

                if (paused)
                {
                    SpinWait.SpinUntil(() => { return(!paused); });
                }

                float[] buff = new float[960];
                int     byteNum;
                try
                {
                    byteNum = read.Read(buff, 0, 960);
                }
                catch (Exception e)
                {
                    break;
                }

                try
                {
                    byte[] encoded = new byte[1275];
                    int    len     = encoder.Encode(buff, 0, byteNum, encoded, 0, 1275);

                    sample++;

                    while (!(time.ElapsedMilliseconds >= sample * sampleTime))
                    {
                        ;
                    }
                    ct.ThrowIfCancellationRequested();
                    client.SendAudio(encoded, len, Codec.OpusVoice);
                }
                catch (Exception e)
                {
                    //Console.WriteLine(e);
                    break;
                }
            }

            PlayerState = PlayerState.Stopped;
            if (State != State.Stopped)
            {
                ShiftCue();
                State = State.Playing;
            }
        }
Пример #10
0
        private void _waveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            var soundBuffer = new byte[e.BytesRecorded + _notEncodedBuffer.Length];

            for (var i = 0; i < _notEncodedBuffer.Length; i++)
            {
                soundBuffer[i] = _notEncodedBuffer[i];
            }

            for (var i = 0; i < e.BytesRecorded; i++)
            {
                soundBuffer[i + _notEncodedBuffer.Length] = e.Buffer[i];
            }

            var byteCap = _bytesPerSegment;
            //      Console.WriteLine("{0} ByteCao", byteCap);
            var segmentCount    = (int)Math.Floor((decimal)soundBuffer.Length / byteCap);
            var segmentsEnd     = segmentCount * byteCap;
            var notEncodedCount = soundBuffer.Length - segmentsEnd;

            _notEncodedBuffer = new byte[notEncodedCount];
            for (var i = 0; i < notEncodedCount; i++)
            {
                _notEncodedBuffer[i] = soundBuffer[segmentsEnd + i];
            }

            for (var i = 0; i < segmentCount; i++)
            {
                //create segment of audio
                var segment = new byte[byteCap];
                for (var j = 0; j < segment.Length; j++)
                {
                    segment[j] = soundBuffer[i * byteCap + j];
                }

                //boost microphone volume if needed
                if (MicBoost != 1.0f)
                {
                    for (var n = 0; n < segment.Length; n += 2)
                    {
                        var sample = (short)((segment[n + 1] << 8) | segment[n + 0]);
                        // n.b. no clipping test going on here // FROM NAUDIO SOURCE !
                        sample         = (short)(sample * MicBoost);
                        segment[n]     = (byte)(sample & 0xFF);
                        segment[n + 1] = (byte)(sample >> 8);
                    }
                }

                //encode as opus bytes
                int len;
                var buff = _encoder.Encode(segment, segment.Length, out len);

                if (_udpVoiceHandler != null && buff != null && len > 0)
                {
                    _udpVoiceHandler.Send(buff, len);
                }
            }
        }
Пример #11
0
        private void waveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            byte[] liveBytes = AdjustAudioLevelDB(e.Buffer, sendGain);
            liveInput.AddSamples(liveBytes, 0, e.BytesRecorded);
            byte[] mixed = new byte[e.BytesRecorded];
            MixingSampleProvider.ToWaveProvider16().Read(mixed, 0, e.BytesRecorded);
            byte[] soundBuffer = new byte[e.BytesRecorded + notEncodedBuffer.Length];
            for (int i = 0; i < notEncodedBuffer.Length; i++)
            {
                soundBuffer[i] = notEncodedBuffer[i];
            }
            for (int i = 0; i < e.BytesRecorded; i++)
            {
                soundBuffer[i + notEncodedBuffer.Length] = mixed[i];
            }

            sendAudiolevel = AudioLevelDB(soundBuffer);
            int byteCap         = bytesPerSegment;
            int segmentCount    = (int)Math.Floor((decimal)soundBuffer.Length / byteCap);
            int segmentsEnd     = segmentCount * byteCap;
            int notEncodedCount = soundBuffer.Length - segmentsEnd;

            notEncodedBuffer = new byte[notEncodedCount];

            for (int i = 0; i < notEncodedCount; i++)
            {
                notEncodedBuffer[i] = soundBuffer[segmentsEnd + i];
            }
            for (int i = 0; i < segmentCount; i++)
            {
                byte[] segment = new byte[byteCap];
                for (int j = 0; j < segment.Length; j++)
                {
                    segment[j] = soundBuffer[(i * byteCap) + j];
                }
                int    len;
                byte[] buff    = encoder.Encode(segment, segment.Length, out len);
                byte[] newbuff = new byte[++len];
                newbuff[0] = 16;
                for (int j = 1; j < newbuff.Length; j++)
                {
                    newbuff[j] = buff[j - 1];
                }
                session.SendAudioFrame((uint)segmentFrames, 21, newbuff);
                playersForm.IsEncoding = true;
            }
            if (playBuffer.BufferedDuration.TotalMilliseconds > 10)
            {
                button1.ForeColor = Color.Black;
                button1.BackColor = Color.Green;
            }
            else
            {
                button1.ForeColor = Color.White;
                button1.BackColor = Color.Red;
            }
        }
Пример #12
0
    public override void FromAudioDataToPacket(float[] audioData, int audioDataOffset, int audioDataCount, ref VoicePacketInfo info, BytePacket output)
    {
        int startIndex = output.CurrentSeek + (sizeof(int) * 2);
        int frameSize  = Mathf.Min(audioDataCount / info.Channels, (info.Frequency / 100) * 6);
        int n          = encoder.Encode(audioData, audioDataOffset, frameSize, output.Data, startIndex, output.MaxCapacity - startIndex);

        output.Write(n);
        output.Write(frameSize);
        output.CurrentSeek += n;
    }
Пример #13
0
        public override byte[] Encode(short[] data)
        {
            byte[] outputBuffer = new byte[1000];

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

            byte[] buffer = new byte[thisPacketSize];
            Buffer.BlockCopy(outputBuffer, 0, buffer, 0, thisPacketSize);

            return(buffer);
        }
Пример #14
0
        private void SoundInSourceOnDataAvailable(object sender,
                                                  DataAvailableEventArgs dataAvailableEventArgs)
        {
            int read;
            var buffer = new byte[dataAvailableEventArgs.ByteCount];

            while ((read = _captureSource.Read(buffer, 0, buffer.Length)) > 0)
            {
                var notEncodedLength = _notEncodedBuffer?.Length ?? 0;
                var soundBuffer      = new byte[read + notEncodedLength];

                //Fill the soundbuffer with _notEncodedBuffer
                if (notEncodedLength > 0)
                {
                    Buffer.BlockCopy(_notEncodedBuffer, 0, soundBuffer, 0, notEncodedLength);
                }

                //Fill the soundbuffer with the data
                Buffer.BlockCopy(buffer, 0, soundBuffer, notEncodedLength, read);

                var segmentCount = (int)Math.Floor((double)soundBuffer.Length / _bytesPerSegment);

                var segmentsEnd     = segmentCount * _bytesPerSegment;
                var notEncodedCount = soundBuffer.Length - segmentsEnd;
                _notEncodedBuffer = new byte[notEncodedCount];

                Buffer.BlockCopy(soundBuffer, segmentsEnd, _notEncodedBuffer, 0, notEncodedCount);

                if (segmentCount == 0)
                {
                    return;
                }

                var dataBuffers       = new byte[segmentCount][];
                var dataBufferLengths = new int[segmentCount];

                unsafe
                {
                    fixed(byte *soundBufferPtr = soundBuffer)
                    for (int i = 0; i < segmentCount; i++)
                    {
                        int len;

                        dataBuffers[i] = _opusEncoder.Encode(soundBufferPtr + _bytesPerSegment * i, _bytesPerSegment,
                                                             out len);
                        dataBufferLengths[i] = len;
                    }
                }

                DataAvailable?.Invoke(this,
                                      new DataInfoAvailableEventArgs(new VoiceChatDataInfo(dataBuffers, dataBufferLengths)));
            }
        }
Пример #15
0
 void SendData()
 {
     // take pieces of buffer and send data
     while (micBuffer.Count > packageSize)
     {
         // ここでエンコードをやってる。
         byte[] encodedData = encoder.Encode(micBuffer.GetRange(0, packageSize).ToArray());
         Debug.Log("enc length:" + encodedData.Length);
         SendEncodedData(encodedData);
         micBuffer.RemoveRange(0, packageSize);
     }
 }
Пример #16
0
        void AddSection(int start, int asamps)
        {
            byte[] tbuf = new byte[asamps * 2];
            Array.Copy(tempbuf, tbuf, tbuf.Length);
            byte[] dat = Encoder.Encode(tbuf);
            stat_bytes2 += dat.Length;
            byte[] decdat = Decoder.Decode(dat, dat.Length);
            int    buf    = AL.GenBuffer();

            AL.BufferData(buf, ALFormat.Mono16, decdat, decdat.Length, SampleRate);
            AL.SourceQueueBuffer(PlaybackSrc, buf);
            AL.Source(PlaybackSrc, ALSourcef.Gain, Volume);
        }
Пример #17
0
        public byte[] OpusEncoding(short[] inputAudioSamples)
        {
            OpusEncoder encoder = OpusEncoder.Create(48000, 1, OpusApplication.OPUS_APPLICATION_AUDIO);

            encoder.Bitrate = 12000;

            // Encoding loop
            byte[] outputBuffer   = new byte[1000];
            int    frameSize      = 960;
            int    thisPacketSize = encoder.Encode(inputAudioSamples, 0, frameSize, outputBuffer, 0, outputBuffer.Length); // this throws OpusException on a failure, rather than returning a negative number

            return(outputBuffer);
        }
Пример #18
0
        private void DataAvailable(object sender, WaveInEventArgs e)
        {
            // WaveBuffer is broken.
            //var buffer = new WaveBuffer(e.Buffer);

            var buffer = new float[e.BytesRecorded / 4];

            for (int i = 0; i < e.BytesRecorded; i += 4)
            {
                buffer[i / 4] = BitConverter.ToSingle(e.Buffer, i) * SpeakersVolumeSlider.Volume;
            }
            // This peak detection doesn't work correctly
            {
                float peakL = 0, peakR = 0;
                for (int i = 0; i < buffer.Length; i += 2)
                {
                    peakL += Math.Abs(buffer[i]);
                }
                for (int i = 1; i < buffer.Length; i += 2)
                {
                    peakR += Math.Abs(buffer[i]);
                }
                SpeakersLeftChannel.Amplitude  = peakL / (buffer.Length / 2);
                SpeakersRightChannel.Amplitude = peakR / (buffer.Length / 2);
            }
            int framesAvailable = e.BytesRecorded * 8 / capture.WaveFormat.BitsPerSample / 2;

            //Console.WriteLine("Got {0} samples per channel", framesAvailable);
            float[] inBuffer;
            int     inBufferOffset;
            int     inNeeded = resampler.ResamplePrepare(framesAvailable, capture.WaveFormat.Channels, out inBuffer, out inBufferOffset);

            Array.Copy(buffer, 0, inBuffer, inBufferOffset, inNeeded * 2);
            float[] outBuffer    = new float[framesAvailable * 4];
            int     outAvailable = resampler.ResampleOut(outBuffer, 0, inNeeded, framesAvailable * 2, outFormat.Channels);

            //Console.WriteLine("Got {0} samples after resample", outAvailable);

            for (int i = 0; i < framesAvailable * 2; i += 960)
            {
                var resampledDataTemp   = outBuffer.Skip(i).Take(Math.Min(960, (outAvailable * 2 - i))).ToArray();
                var resampledPaddedData = new float[960];
                var output = new byte[1276];
                Array.Copy(resampledDataTemp, resampledPaddedData, resampledDataTemp.Length);
                int encoded = encoder.Encode(resampledPaddedData, 0, 480, output, 0, 1276);
                SendData(output.Take(encoded).ToArray());
                //Console.WriteLine("Encoded packet of {0} bytes", encoded);

                // TODO: Send encoded data to the client
            }
        }
Пример #19
0
        private async void AudioDataAvailable(object sender, WaveInEventArgs e)
        {
            short[] shorts   = new short[e.BytesRecorded / 2];
            byte[]  outBytes = new byte[_frames];
            Buffer.BlockCopy(e.Buffer, 0, shorts, 0, e.BytesRecorded);
            int size = _encoder.Encode(shorts, 0, _frames, outBytes, 0, _frames);


            var bitbytes = BitConverter.GetBytes(_voiceId);

            outBytes = bitbytes.Concat(SubArray(outBytes, 0, size)).ToArray();

            await _udp.SendAsync(outBytes, outBytes.Length);
        }
Пример #20
0
 void SendData()
 {
     if (isLocalPlayer)
     {
         // take pieces of buffer and send data
         while (micBuffer.Count > packageSize)
         {
             byte[] encodedData = encoder.Encode(micBuffer.GetRange(0, packageSize).ToArray());
             Debug.Log("OpusNetworked.SendData: " + encodedData.Length);
             CmdDistributeData(encodedData);
             micBuffer.RemoveRange(0, packageSize);
         }
     }
 }
        public void addPCMSamples(byte[] pcm_data, int data_len)
        {
            //// エンディアンを反転
            //// 引数のデータを書き換えてしまう
            //EndianReverser.uint16_bytes_reverse(pcm_data);

            byte[] soundBuffer = new byte[data_len + _notEncodedBuffer.Length];
            for (int i = 0; i < _notEncodedBuffer.Length; i++)
            {
                soundBuffer[i] = _notEncodedBuffer[i];
            }
            for (int i = 0; i < data_len; i++)
            {
                soundBuffer[i + _notEncodedBuffer.Length] = pcm_data[i];
            }

            int byteCap         = _bytesPerSegment;
            int segmentCount    = (int)Math.Floor((decimal)soundBuffer.Length / byteCap);
            int segmentsEnd     = segmentCount * byteCap;
            int notEncodedCount = soundBuffer.Length - segmentsEnd;

            _notEncodedBuffer = new byte[notEncodedCount];
            for (int i = 0; i < notEncodedCount; i++)
            {
                _notEncodedBuffer[i] = soundBuffer[segmentsEnd + i];
            }

            if (segmentCount == 0)
            {
                return;
            }

            for (int i = 0; i < segmentCount; i++)
            {
                byte[] segment = new byte[byteCap];
                for (int j = 0; j < segment.Length; j++)
                {
                    segment[j] = soundBuffer[(i * byteCap) + j];
                }
                int    len;
                byte[] buf = mEncoder.Encode(segment, segment.Length, out len);

                byte[] encoded_buf = new byte[len];
                Array.Copy(buf, 0, encoded_buf, 0, len);
                Console.WriteLine("opus encode finished and get encoded data " + len.ToString() + " bytes. sent this data to client.");
                aout.handleDataWithTCP(encoded_buf);

                _bytesSent += (ulong)len;
            }
        }
Пример #22
0
        public async void SendVoiceData(float[] frame)
        {
            if (lastReady != null && frame.Length == 1920 && secretkey != null)
            {
                int encodedSize = encoder.Encode(frame, 0, FrameSamplesPerChannel, buffer, 0, FrameBytes);

                byte[] opus  = new byte[encodedSize + 12 + 16];
                byte[] nonce = makeHeader();
                Buffer.BlockCopy(nonce, 0, opus, 0, 12);
                Buffer.BlockCopy(buffer, 0, opus, 12, encodedSize);
                timestamp = unchecked (timestamp + 960);
                Cypher.encrypt(opus, 12, encodedSize, opus, 12, nonce, secretkey);
                await _udpSocket.SendBytesAsync(opus);
            }
        }
Пример #23
0
        void OnAudioCaptured(object sender, WaveInEventArgs e)
        {
            short[] audioData = new short[e.BytesRecorded];

            audioData[0] = BitConverter.ToInt16(e.Buffer, 0);
            for (int i = 2; i < e.BytesRecorded - 1; i += 2)
            {
                audioData[i / 2] = BitConverter.ToInt16(e.Buffer, i);
            }

            byte[] encoded = new byte[1000];

            var len = encoder.Encode(audioData, 0, 960, encoded, 0, encoded.Length);

            //Array.Copy(encoded, encoded, len);
            audioSender.Send(encoded);
        }
Пример #24
0
        public void OpusEncoderTest()
        {
            using (Stream source = File.OpenRead(WavTestFile))
            {
                using (OpusEncoder encoder = new OpusEncoder(source))
                {
                    Stream encoded = encoder.Encode();

                    using (OpusEncoder decoder = new OpusEncoder(encoded))
                    {
                        Stream decoded = decoder.Decode();

                        //Ensure it can be read
                        using (WaveReader wavreader = new WaveReader(decoded)) { }
                    }
                }
            }
        }
Пример #25
0
        private static void _waveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            byte[] soundBuffer = new byte[e.BytesRecorded + _notEncodedBuffer.Length];
            for (int i = 0; i < _notEncodedBuffer.Length; i++)
            {
                soundBuffer[i] = _notEncodedBuffer[i];
            }
            for (int i = 0; i < e.BytesRecorded; i++)
            {
                soundBuffer[i + _notEncodedBuffer.Length] = e.Buffer[i];
            }

            int byteCap         = _bytesPerSegment;
            int segmentCount    = (int)Math.Floor((decimal)soundBuffer.Length / byteCap);
            int segmentsEnd     = segmentCount * byteCap;
            int notEncodedCount = soundBuffer.Length - segmentsEnd;

            _notEncodedBuffer = new byte[notEncodedCount];
            for (int i = 0; i < notEncodedCount; i++)
            {
                _notEncodedBuffer[i] = soundBuffer[segmentsEnd + i];
            }

            for (int i = 0; i < segmentCount; i++)
            {
                byte[] segment = new byte[byteCap];
                for (int j = 0; j < segment.Length; j++)
                {
                    segment[j] = soundBuffer[(i * byteCap) + j];
                }

                short[] inbuff = BytesToShorts(segment);

                try
                {
                    byte[] buff = new byte[1275];
                    int    len  = encoder.Encode(inbuff, 0, 960, buff, 0, 1275);
                    client.SendAudio(buff, len, Codec.OpusVoice);
                }catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
        }
Пример #26
0
        void _waveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            byte[] soundBuffer = new byte[e.BytesRecorded + _notEncodedBuffer.Length];
            for (int i = 0; i < _notEncodedBuffer.Length; i++)
            {
                soundBuffer[i] = _notEncodedBuffer[i];
            }
            for (int i = 0; i < e.BytesRecorded; i++)
            {
                soundBuffer[i + _notEncodedBuffer.Length] = e.Buffer[i];
            }

            int byteCap         = _bytesPerSegment;
            int segmentCount    = (int)System.Math.Floor((decimal)soundBuffer.Length / byteCap);
            int segmentsEnd     = segmentCount * byteCap;
            int notEncodedCount = soundBuffer.Length - segmentsEnd;

            _notEncodedBuffer = new byte[notEncodedCount];
            for (int i = 0; i < notEncodedCount; i++)
            {
                _notEncodedBuffer[i] = soundBuffer[segmentsEnd + i];
            }

            for (int i = 0; i < segmentCount; i++)
            {
                byte[] segment = new byte[byteCap];
                for (int j = 0; j < segment.Length; j++)
                {
                    segment[j] = soundBuffer[(i * byteCap) + j];
                }
                int    len;
                byte[] buff = _encoder.Encode(segment, segment.Length, out len);
                ClientStatistics.AudioBytesEncoded += len;
                //buff = _decoder.Decode(buff, len, out len);
                //_playBuffer.AddSamples(buff, 0, len);
                if (ptt & started)
                {
                    byte[] trimmedBuff = new byte[len];
                    Buffer.BlockCopy(buff, 0, trimmedBuff, 0, len);
                    audioPublishInputQueue.Add(trimmedBuff);
                }
            }
        }
Пример #27
0
        private void RTProcess(object sender, float[] e)
        {
            try
            {
                byte[] nonce = new byte[] { 128, 120, 192, 46, 6, 144, 172, 128, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };                                                          //Length of 24
                byte[] key   = new byte[] { 40, 221, 122, 207, 253, 63, 24, 97, 28, 168, 80, 250, 98, 165, 166, 32, 161, 61, 248, 51, 84, 26, 171, 14, 139, 17, 174, 121, 9, 74, 181, 33 }; //Length of 32

                byte[] opus        = new byte[1820 * sizeof(float) + 16];
                int    encodedSize = encoder.Encode(e, 0, 960, opus, 0, 1820 * sizeof(float));
                RuntimeComponent.Cypher.encrypt(opus, 0, encodedSize, opus, 0, nonce, key);

                RuntimeComponent.Cypher.decrypt(opus, 0, encodedSize + 16, opus, 0, nonce, key);

                float[] frame   = new float[e.Length];
                int     samples = decoder.Decode(opus, 0, encodedSize, frame, 0, (20 * 48 * 2));
                AudioManager.AddFrame(frame, (uint)samples);
            }
            catch (Exception exc)
            {
            }
        }
Пример #28
0
        public byte[] Compress(AudioChunk input)
        {
            int frameSize = GetFrameSize();

            if (input != null)
            {
                short[] newData = input.Data;
                _incomingSamples.Write(newData);
            }
            else
            {
                // If input is null, assume we are at end of stream and pad the output with zeroes
                int paddingNeeded = _incomingSamples.Available() % frameSize;
                if (paddingNeeded > 0)
                {
                    _incomingSamples.Write(new short[paddingNeeded]);
                }
            }
            
            int outCursor = 0;
            
            if (_incomingSamples.Available() >= frameSize)
            {
                _timer.Reset();
                _timer.Start();
                short[] nextFrameData = _incomingSamples.Read(frameSize);
                int thisPacketSize = _encoder.Encode(nextFrameData, 0, frameSize, scratchBuffer, outCursor, scratchBuffer.Length);
                outCursor += thisPacketSize;
                _timer.Stop();
            }
            
            if (outCursor > 0)
            {
                _statistics.EncodeSpeed = _frameSize / ((double)_timer.ElapsedTicks / Stopwatch.Frequency * 1000);
            }

            byte[] finalOutput = new byte[outCursor];
            Array.Copy(scratchBuffer, 0, finalOutput, 0, outCursor);
            return finalOutput;
        }
Пример #29
0
        private void CaptureCallback(IAsyncResult ar)
        {
            if (!_capture.CanRead)
            {
                return;
            }
            if (!IsConnected)
            {
                return;
            }

            var read = _capture.EndRead(ar);

            if (SelectedVoiceMode == VoiceMode.VoiceActivation)
            {
                HandleVoiceActivation();
            }

            if (IsSpeaking)
            {
#if DEBUG
                Console.WriteLine(_readBuffer.Length);
#endif

                int len;
                var encoded = _encoder.Encode(_readBuffer, _readBuffer.Length, out len);

                var message = _client.CreateMessage();
                message.Write((byte)0x01);
                //message.Write(_readBuffer.Length);
                //message.Write(_readBuffer);
                message.Write(len);
                message.Write(encoded);

                _client.SendMessage(message, NetDeliveryMethod.ReliableOrdered);
            }

            _capture.BeginRead(_readBuffer, 0, _readBuffer.Length, CaptureCallback, null);
        }
Пример #30
0
        void _waveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            byte[] soundBuffer = new byte[e.BytesRecorded + _notEncodedBuffer.Length];
            for (int i = 0; i < _notEncodedBuffer.Length; i++)
            {
                soundBuffer[i] = _notEncodedBuffer[i];
            }
            for (int i = 0; i < e.BytesRecorded; i++)
            {
                soundBuffer[i + _notEncodedBuffer.Length] = e.Buffer[i];
            }

            int byteCap         = _bytesPerSegment;
            int segmentCount    = (int)Math.Floor((decimal)soundBuffer.Length / byteCap);
            int segmentsEnd     = segmentCount * byteCap;
            int notEncodedCount = soundBuffer.Length - segmentsEnd;

            _notEncodedBuffer = new byte[notEncodedCount];
            for (int i = 0; i < notEncodedCount; i++)
            {
                _notEncodedBuffer[i] = soundBuffer[segmentsEnd + i];
            }

            for (int i = 0; i < segmentCount; i++)
            {
                byte[] segment = new byte[byteCap];
                for (int j = 0; j < segment.Length; j++)
                {
                    segment[j] = soundBuffer[(i * byteCap) + j];
                }
                int    len;
                byte[] buff = _encoder.Encode(segment, segment.Length, out len);
                _bytesSent += (ulong)len;
                buff        = _decoder.Decode(buff, len, out len);
                _playBuffer.AddSamples(buff, 0, len);
            }
        }