private void FrameInputNode_QuantumStarted( AudioFrameInputNode sender, FrameInputNodeQuantumStartedEventArgs args) { if (_audioDataCurrentPosition == 0) { _fileOutputNode.Start(); } // doesn't matter how many samples requested var frame = ProcessOutputFrame(_audioGraph.SamplesPerQuantum); _frameInputNode.AddFrame(frame); if (_finished) { _fileOutputNode?.Stop(); _audioGraph?.Stop(); } // to not report too many times if (_audioGraph == null) { return; } if (_audioGraph.CompletedQuantumCount % 100 == 0) { var dProgress = (double)100 * _audioDataCurrentPosition / _audioData.LengthSamples(); _ioProgress?.Report(dProgress); } }
//private unsafe void ProcessFrameOutput(AudioFrame frame) //{ // using (var buffer = frame.LockBuffer(AudioBufferAccessMode.Write)) // using (var reference = buffer.CreateReference()) // { // byte* dataInBytes; // uint capacityInBytes; // float* dataInFloat; // // Get the buffer from the AudioFrame // ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacityInBytes); // //cal level // dataInFloat = (float*)dataInBytes; // float max = 0; // for (int i = 0; i < _audioGraph.SamplesPerQuantum; i++) // { // max = Math.Max(Math.Abs(dataInFloat[i]), max); // } // ProzessEvent(_detector.Calculate(max)); // } //} #endregion private void ProzessEvent(MicrophoneInput a) { if (a.State == 1) { Debug.WriteLine(a.Value + " : " + a.State); } if (a.State == 1 && _isRecoding == false) { Debug.WriteLine("STARTED RECORDING!"); _fileOutputNode.Reset(); _fileOutputNode.Start(); _isRecoding = true; } a.IsRecording = _isRecoding; _interacor.OnNoiseDetected(a); if (_isRecoding) { _count++; _lastRecoring.Add(a.State); } //Record Noise for at least 5 secounds if (_count < FiveSecounds || _isRecoding != true) { return; } // if there was enough sound in this 5 secounds, keep recoring if (_lastRecoring.Fold((b) => b.Sum()) > BufferTol && _count < 2 * FiveSecounds) { return; } _count = 0; _isRecoding = false; _fileOutputNode.Stop(); SaveAndCleanGraph(); }