void AudioStream_OnBroadcast(object sender, byte [] bytes) { var level = AudioFunctions.CalculateLevel(bytes); if (level > SilenceThreshold) //did we find a signal? { audioDetected = true; silenceTime = null; } else //no audio detected { //see if we've detected 'near' silence for more than <audioTimeout> if (StopRecordingOnSilence && silenceTime.HasValue) { if (DateTime.Now.Subtract(silenceTime.Value) > AudioSilenceTimeout) { Timeout("AudioRecorderService.AudioStream_OnBroadcast (): AudioSilenceTimeout exceeded, stopping recording"); return; } } else { silenceTime = DateTime.Now; } } if (StopRecordingAfterTimeout && DateTime.Now - startTime > TotalAudioTimeout) { Timeout("AudioRecorderService.AudioStream_OnBroadcast(): TotalAudioTimeout exceeded, stopping recording"); } }
void AudioStream_OnBroadcast(object sender, byte [] bytes) { var level = AudioFunctions.CalculateLevel(bytes); AudioLevelUpdated?.Invoke(this, level); if (level < NearZero && !audioDetected) // discard any initial 0s so we don't jump the gun on timing out { Debug.WriteLine("level == {0} && !audioDetected", level); return; } if (level > SilenceThreshold) // did we find a signal? { audioDetected = true; silenceTime = null; Debug.WriteLine("AudioStream_OnBroadcast :: {0} :: level > SilenceThreshold :: bytes: {1}; level: {2}", DateTime.Now, bytes.Length, level); } else // no audio detected { // see if we've detected 'near' silence for more than <audioTimeout> if (StopRecordingOnSilence && silenceTime.HasValue) { var currentTime = DateTime.Now; if (currentTime.Subtract(silenceTime.Value).TotalMilliseconds > AudioSilenceTimeout.TotalMilliseconds) { Timeout($"AudioStream_OnBroadcast :: {currentTime} :: AudioSilenceTimeout exceeded, stopping recording :: Near-silence detected at: {silenceTime}"); return; } } else { silenceTime = DateTime.Now; Debug.WriteLine("AudioStream_OnBroadcast :: {0} :: Near-silence detected :: bytes: {1}; level: {2}", silenceTime, bytes.Length, level); } } if (StopRecordingAfterTimeout && DateTime.Now - startTime > TotalAudioTimeout) { Timeout("AudioStream_OnBroadcast(): TotalAudioTimeout exceeded, stopping recording"); } }
async void AudioStream_OnBroadcast(object sender, byte [] bytes) { var level = AudioFunctions.CalculateLevel(bytes);//, bigEndian: true);//, bigEndian: true, signed: false); //var level = Decode(bytes).Select(Math.Abs).Average(x => x); //double level = Math.Sqrt(sum / (bytes.Length / 2)); System.Diagnostics.Debug.WriteLine("AudioStream_OnBroadcast :: calculateLevel == {0}", level); if (level > SilenceThreshold) //did we find a signal? { audioDetected = true; silenceTime = null; } else //no audio detected { //see if we've detected 'near' silence for more than <audioTimeout> if (StopRecordingOnSilence && silenceTime.HasValue) { if (DateTime.Now.Subtract(silenceTime.Value) > AudioSilenceTimeout) { System.Diagnostics.Debug.WriteLine("AudioRecorderService.AudioStream_OnBroadcast(): AudioSilenceTimeout exceeded, stopping recording"); await StopRecording(); return; } } else { silenceTime = DateTime.Now; } } if (StopRecordingAfterTimeout && DateTime.Now - startTime > TotalAudioTimeout) { System.Diagnostics.Debug.WriteLine("AudioRecorderService.AudioStream_OnBroadcast(): TotalAudioTimeout exceeded, stopping recording"); await StopRecording(); } }