public AudioProcessor(Publisher publisher, string outlet = null) { using WasapiCapture capture = new WasapiLoopbackCapture(CAPTURE_LATENCY); capture.Initialize(); channelNum = capture.WaveFormat.Channels; systemSampleRate = capture.WaveFormat.SampleRate; using SoundInSource captureSource = new SoundInSource(capture) { FillWithZeros = false }; using SimpleNotificationSource notificationSource = new SimpleNotificationSource(FluentExtensions.ToSampleSource(captureSource)) { Interval = PROCESS_WINDOW_LENGTH }; InitializeMonoBuffers(monoBuffers, channelNum, notificationSource.BlockCount); blockBuffer = new float[notificationSource.BlockCount * channelNum]; lpf = new LowpassFilter(systemSampleRate, LFE_CUTOFF); MonoPulseDetector = new SimplePulseDetector(monoBuffers, lfeProvided: false, biQuadFilter: lpf); localisationer = new Localisationer(monoBuffers); if (channelNum > 2) { LFEPulseDetector = new SimplePulseDetector(monoBuffers, lfeProvided: true); } capture.DataAvailable += (s, e) => { while (notificationSource.Read(blockBuffer, 0, notificationSource.BlockCount * channelNum) > 0) { monoBuffers = Deinterlacing(monoBuffers, blockBuffer, channelNum); if (LFEPulseDetector != null) { bool m = MonoPulseDetector.Predict(); bool l = LFEPulseDetector.Predict(); if (m || l) { double angle = localisationer.GetLoudestAngle(); #if DEBUG Console.Clear(); Console.WriteLine($"LFE Level: {LFEPulseDetector.CurrentReading:F3}, LFE Threshold: {LFEPulseDetector.CurrentThreshold:F3}"); Console.WriteLine($"Mixed Level: {MonoPulseDetector.CurrentReading:F3}, Mixed Threshold: {MonoPulseDetector.CurrentThreshold:F3}"); Console.WriteLine($"Impulse Detected - Mono:{m}, LFE:{l}, Angle: {angle:F3}, Hit Count:{hitCount}"); #endif if (publisher != null && outlet != null) { publisher.Publish(outlet, $"{m}|{l}|{angle:F3}"); } hitCount++; } } else { if (MonoPulseDetector.Predict()) { double angle = localisationer.GetLoudestAngle(); #if DEBUG Console.Clear(); Console.WriteLine($"Level: {MonoPulseDetector.CurrentReading:F3}, Threshold: {MonoPulseDetector.CurrentThreshold:F3}"); Console.WriteLine($"Impulse Detected - Mono, Angle:{angle:F3}, Hit Count:{hitCount}"); #endif if (publisher != null && outlet != null) { publisher.Publish(outlet, $"True|False|{angle:F3}"); } hitCount++; } } } }; StartCapturingAndHold(capture); }
public AudioProcessor(Publisher publisher, string outlet = null) { WasapiCapture capture = new WasapiLoopbackCapture(CAPTURE_LATENCY); capture.Initialize(); channelNum = capture.WaveFormat.Channels; systemSampleRate = capture.WaveFormat.SampleRate; SoundInSource captureSource = new SoundInSource(capture) { FillWithZeros = false }; SimpleNotificationSource notificationSource = new SimpleNotificationSource(FluentExtensions.ToSampleSource(captureSource)) { Interval = PROCESS_WINDOW_LENGTH }; InitializeMonoBuffers(pcmBuffers, channelNum, notificationSource.BlockCount); blockBuffer = new float[notificationSource.BlockCount * channelNum]; capture.DataAvailable += (s, e) => { while (notificationSource.Read(blockBuffer, 0, notificationSource.BlockCount * channelNum) > 0) { // Extracted audio signal pcmBuffers = Deinterlacing(pcmBuffers, blockBuffer, channelNum); CSCore.Utils.Complex[] data = new CSCore.Utils.Complex[pcmBuffers[0].Length]; for (int i = 0; i < pcmBuffers.Count; i++) { data[i].Real = pcmBuffers[0][i]; data[i].Imaginary = 0; } //DetectPitch(pcmBuffers); counter++; if (counter % 30 == 0) { Console.Clear(); FastFourierTransformation.Fft(data, 8); //Console.WriteLine(data.Length); foreach (var d in data) { Console.Write(Math.Round(d.Value, 4) + " "); } Console.WriteLine(); } // TODO: Implement your model //publisher.Publish(outlet, "OUTPUT MESSAGE TO NEXT OUTLET"); } }; StartCapturingAndHold(capture); }