private bool Run(Config config, Logger log) { System.Console.WriteLine($"Listening for Dispatches on Device: {config.InputDevice}"); audioStorage = new WatcherAudioStorage(log); evaluator = new AudioEvaluator(log); recorder = new AudioRecorder(evaluator, audioStorage); processor = new AudioProcessor(recorder, evaluator, audioStorage); com = new ComService(log, processor); com.CallCreatedEvent += Com_CallCreatedEvent; System.Console.WriteLine("Hooking into Events"); recorder.SampleAggregator.MaximumCalculated += SampleAggregator_MaximumCalculated; recorder.SampleAggregator.WaveformCalculated += SampleAggregator_WaveformCalculated; processor.TriggerProcessingStarted += Processor_TriggerProcessingStarted; processor.TriggerProcessingFinished += Processor_TriggerProcessingFinished; evaluator.WatcherTriggered += Evaluator_WatcherTriggered; ResgridV3ApiClient.Init(config.ApiUrl, config.Username, config.Password); System.Console.WriteLine( $"Config Loaded with {config.Watchers.Count} watchers ({config.Watchers.Count(x => x.Active)} active)"); System.Console.WriteLine("Initializing Processor"); processor.Init(config); System.Console.WriteLine("Starting Processor"); processor.Start(); System.Console.WriteLine("Starting Communication Service"); com.Init(config); System.Console.WriteLine("Communication Service: Validating API Connection"); if (com.IsConnectionValid()) { System.Console.WriteLine("Communication Service: API Connection is Valid"); } else { System.Console.WriteLine( "Communication Service: CANNOT TALK TO RESGRID API, CHECK YOUR CONFIG APIURL AND ENSURE YOUR COMPUTER CAN TALK TO THAT URL"); } System.Console.WriteLine("Ready, Listening to Audio. Press Ctrl+C to exit."); while (recorder.RecordingState == RecordingState.Monitoring || recorder.RecordingState == RecordingState.Recording) { Thread.Sleep(250); } return(true); }
public string Execute(MonitorArgs args) { Logger log = new LoggerConfiguration() .MinimumLevel.Error() .WriteTo.Console() .CreateLogger(); audioStorage = new WatcherAudioStorage(log); evaluator = new AudioEvaluator(log); recorder = new AudioRecorder(evaluator, audioStorage); processor = new AudioProcessor(recorder, evaluator, audioStorage); System.Console.WriteLine("Resgrid Audio"); System.Console.WriteLine("-----------------------------------------"); System.Console.WriteLine("Monitoring Audio on Device: " + args.Device); System.Console.WriteLine("Hooking into Events"); recorder.SampleAggregator.MaximumCalculated += SampleAggregator_MaximumCalculated; recorder.SampleAggregator.WaveformCalculated += SampleAggregator_WaveformCalculated; processor.TriggerProcessingStarted += Processor_TriggerProcessingStarted; processor.TriggerProcessingFinished += Processor_TriggerProcessingFinished; evaluator.WatcherTriggered += Evaluator_WatcherTriggered; System.Console.WriteLine("Loading Settings"); Config config = new Config(); config.InputDevice = args.Device; config.AudioLength = 10; System.Console.WriteLine("Initializing Processor"); processor.Init(config); System.Console.WriteLine("Starting Processor"); processor.Start(); System.Console.WriteLine("Ready, Monitoring Audio. Press Ctrl+C to exit."); System.Console.WriteLine($"Timestamp: Min Max dB"); System.Console.WriteLine($"---------------------------------------------------------------------------"); while (recorder.RecordingState == RecordingState.Monitoring || recorder.RecordingState == RecordingState.Recording) { Thread.Sleep(100); } return(""); }
//[Test] public void EvaluateAudioTrigger_BasicShouldReturnTrue() { var path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Replace("file:\\", ""); //System.IO.FileStream WaveFile = System.IO.File.OpenRead(path + "\\Data\\TestAudio.wav"); //byte[] data = new byte[WaveFile.Length]; int sampleRate; //double[] fileData = Functions.WaveFileDataPrepare(path + "\\Data\\TestAudio.wav", out sampleRate);'' List <double> fftData = new List <double>(); int BUFFERSIZE = (int)Math.Pow(2, 11); // must be a multiple of 2 WaveStream waveStream = new NAudio.Wave.WaveFileReader(path + "\\Data\\TestAudio.wav"); int bytesPerSample = (waveStream.WaveFormat.BitsPerSample / 8) * waveStream.WaveFormat.Channels; int bytesRead = 0; BufferedWaveProvider bwp = new BufferedWaveProvider(new WaveFormat(44100, 1)); bwp.BufferLength = BUFFERSIZE * 2; bwp.DiscardOnBufferOverflow = true; byte[] waveData = new byte[BUFFERSIZE * 2]; bytesRead = waveStream.Read(waveData, 0, 128 * bytesPerSample); bwp.AddSamples(waveData, 0, bytesRead); int frameSize = BUFFERSIZE; byte[] frames = new byte[frameSize]; bwp.Read(frames, 0, frameSize); var waveFormEventArgs = AudioFunctions.PrepareAudioData(frames, frameSize); fftData.AddRange(waveFormEventArgs.FastFourierTransform); while (bytesRead != 0) { bytesRead = waveStream.Read(waveData, bytesRead, 128 * bytesPerSample); bwp.AddSamples(waveData, 0, bytesRead); frameSize = BUFFERSIZE; frames = new byte[frameSize]; bwp.Read(frames, 0, frameSize); waveFormEventArgs = AudioFunctions.PrepareAudioData(frames, frameSize); fftData.AddRange(waveFormEventArgs.FastFourierTransform); } //WavFile wavFile = new WavFile(); //wavFile.ReadWav(path + "\\Data\\TestAudio.wav"); //WaveFileReader reader = new WaveFileReader(path + "\\Data\\TestAudio.wav"); //List<byte> testAudio1; // = File.ReadAllBytes(path + "\\Data\\TestAudio.wav").ToList(); //int BUFFERSIZE = (int)Math.Pow(2, 14); // must be a multiple of 2 //using (var reader = new AudioFileReader(path + "\\Data\\TestAudio.wav")) //{ // // find the max peak // byte[] buffer = new byte[BUFFERSIZE]; // int read; // do // { // read = reader.Read(buffer, 0, buffer.Length); // } while (read > 0); // testAudio1 = new List<byte>(buffer); //} Logger log = new LoggerConfiguration() .MinimumLevel.Error() .CreateLogger(); var audioEvaluator = new AudioEvaluator(log); //if (testAudio1.Count() % 2 != 0) //{ // testAudio1.RemoveAt(testAudio1.Count() - 1); //} var trigger = new Trigger(); trigger.Frequency1 = 645.7; trigger.Frequency2 = 716.10; trigger.Count = 2; trigger.Time1 = 1; //wavFile.FFT(false); //var fftData = Functions.FFT(fileData); //var fftData = wavFile._aSamples; //var result = audioEvaluator.EvaluateAudioTrigger(trigger, fftData.ToArray()); //result.Should().BeTrue(); }