/// <inheritdoc /> public int Read(float[] buffer, int offset, int count) { if (!_samples.Read(new ArraySegment <float>(buffer, offset, count))) { return(0); } return(count); }
private void SendReliableDataPackets() { ArraySegment <byte> buffer; while (_queuedReliableTransmissions.Read(out buffer)) { SentTraffic.Update(buffer.Count); SendReliable(buffer); _byteBufferPool.Put(buffer.Array); } }
internal static void WriteMultithreadedLogs() { if (_main == null) { _main = Thread.CurrentThread; } LogMessage msg; while (LogsFromOtherThreads.Read(out msg)) { msg.Log(); } }
/// <summary> /// Flush the transfer buffer to the encoded audio buffer. /// /// THIS IS NOT THREAD SAFE! Don't call this once playback has started. /// </summary> public void FlushTransferBuffer() { // empty the transfer buffer into the decoder buffer VoicePacket frame; while (_inputBuffer.Read(out frame)) _source.Push(frame); // set the complete flag after flushing the transfer buffer if (_complete && !_sourceClosed) { _sourceClosed = true; _source.Stop(); } }
private void FlushTransferBuffer() { // empty the transfer buffer into the decoder buffer EncodedAudio frame; while (_inputBuffer.Read(out frame)) { _source.Push(frame); } // set the complete flag after flushing the transfer buffer if (_complete && !_sourceClosed) { _sourceClosed = true; _source.Stop(); } }
private void ThreadEntry() { try { while (_runThread) { //Wait for an event(s) to arrive which we need to process. //Max wait time is the size of the smallest frame size so it should wake up with no work to do // a lot of the time (ensuring minimal processing latency when there is work to do). if (_inputQueue.EstimatedUnreadCount == 0) { _threadEvent.WaitOne(10); } //Apply a pipeline reset if one has been requested, but one has not yet been applied if (Interlocked.Exchange(ref _resetRequested, 0) == 1 && !_resetApplied) { ApplyReset(); } //If there are no items in the buffer skip back to sleep var countInBuffer = _inputQueue.EstimatedUnreadCount; if (countInBuffer == 0) { continue; } //We're about to process some audio, meaning the pipeline is no longer in a reset state _resetApplied = false; //Reset the external dropped frames counter and store it locally, we're about to process the packet before the drop var missed = Interlocked.Exchange(ref _droppedFrames, 0); for (var i = 0; i < countInBuffer; i++) { float[] buffer; if (!_inputQueue.Read(out buffer)) { Log.Warn("Attempting to drain '{0}' frames from preprocessor input queue, but failed to read frame '{1}'", countInBuffer, i); missed++; } else if (buffer == null) { Log.Warn("Attempting to drain '{0}' frames from preprocessor input queue, but read a null frame for frame '{1}'", countInBuffer, i); missed++; } else { //Run through the preprocessor (rewrite the buffer in place) var preSpeech = VadIsSpeechDetected; ProcessInputAudio(buffer); var postSpeech = VadIsSpeechDetected; //Measure the amplitude of the preprocessed signal _arv.Update(new ArraySegment <float>(buffer)); //Update the VAD subscribers if necessary if (preSpeech ^ postSpeech) { if (preSpeech) { SendStoppedTalking(); } else { SendStartedTalking(); } } _inputBufferSource.Put(buffer); } } //Now submit as many empty frames as necessary to make up for lost frames for (var i = 0; i < missed; i++) { //We can't get a buffer from the input queue, submit silent frames to make up for lost data Log.Debug("Sending a silent frame to compensate for lost audio from the microphone", missed); Array.Clear(_emptyInputFrame, 0, _emptyInputFrame.Length); ProcessInputAudio(_emptyInputFrame); } } } catch (Exception e) { Log.Error(Log.PossibleBugMessage("Unhandled exception killed the microphone capture thread: " + e, "02EB75C0-1E12-4109-BFD2-64645C14BD5F")); } }