Exemple #1
0
        public static void Main(string[] args)
        {
            XmlConfigurator.Configure();
            Log.Info("Starting...");
            var url     = args.Any() ? args[0] : DefaultUrl;
            var request = (HttpWebRequest)WebRequest.Create(url);

            HttpWebResponse response = null;

            try
            {
                response = (HttpWebResponse)request.GetResponse();
                Log.Info("Connected");
            }
            catch (WebException e)
            {
                Log.Error($"Could not read stream: {e.Message}");
                Environment.Exit(1);
            }

            using (var responseStream = response.GetResponseStream())
            {
                var sampleBuffer         = new float[SampleBufferSize];
                var recorders            = new List <DispatchMessageRecorder>();
                var byteBuffer           = new byte[16384 * 4];
                var readFullyStream      = new ReadFullyStream(responseStream);
                var decompressor         = CreateDecompressor(readFullyStream);
                var bufferedWaveProvider = CreateBufferedWaveProvider(decompressor);
                var toneDetector         = new TonePatternDetector(TargetFrequency1, TargetFrequency2,
                                                                   bufferedWaveProvider.WaveFormat.SampleRate);
                var sampleProvider = bufferedWaveProvider.ToSampleProvider();

                while (true)
                {
                    var frame          = Mp3Frame.LoadFromStream(readFullyStream);
                    var bytesReadCount = decompressor.DecompressFrame(frame, byteBuffer, 0);
                    bufferedWaveProvider.AddSamples(byteBuffer, 0, bytesReadCount);
                    var sampleCount = sampleProvider.Read(sampleBuffer, 0, sampleBuffer.Length);

                    if (EndOfSamples(sampleCount, sampleBuffer))
                    {
                        break;
                    }

                    if (toneDetector.Detected(sampleBuffer))
                    {
                        Log.Info($"Tone detected at {DateTime.UtcNow}");
                        recorders.Add(new DispatchMessageRecorder(bufferedWaveProvider.WaveFormat));
                        toneDetector.Reset();
                    }

                    foreach (var recorder in recorders)
                    {
                        recorder.Record(byteBuffer, bytesReadCount, sampleBuffer, sampleCount);
                    }

                    recorders = RefreshRecorderList(recorders).ToList();
                }

                recorders.ForEach(r => r.Dispose());

                var    cli     = new WebClient();
                string apicall = cli.DownloadString("http://api.citizensmedical.ca/ping.php?file=dispatch_" + { DateTime.Now: ddMMyyyy_HHmmssff } +".wav");
                Log.Info("API Results: " + apicall);
                Log.Info("End of stream");
            }
        }
Exemple #2
0
        public static void Main(string[] args)
        {
            _container = CreateContainer();
            XmlConfigurator.Configure();
            Log.Info("Starting...");
            var request  = (HttpWebRequest)WebRequest.Create(_container.Resolve <IConfigurationReader>().ReadStreamUrl());
            var response = GetHttpWebResponse(request);

            using (var responseStream = response.GetResponseStream())
            {
                var sampleBuffer         = new float[SampleBufferSize];
                var recorders            = new List <IDispatchMessageRecorder>();
                var byteBuffer           = new byte[16384 * 4];
                var readFullyStream      = new ReadFullyStream(responseStream);
                var decompressor         = CreateDecompressor(readFullyStream);
                var bufferedWaveProvider = CreateBufferedWaveProvider(decompressor);
                var toneDetector         = _container.Resolve <ITonePatternDetector>();
                var sampleProvider       = bufferedWaveProvider.ToSampleProvider();
                var sampleRate           = bufferedWaveProvider.WaveFormat.SampleRate;
                var silenceDetector      = _container.Resolve <ISilenceDetector>();

                while (true)
                {
                    var frame          = ReadFrame(readFullyStream);
                    var bytesReadCount = decompressor.DecompressFrame(frame, byteBuffer, 0);
                    bufferedWaveProvider.AddSamples(byteBuffer, 0, bytesReadCount);
                    var sampleCount = sampleProvider.Read(sampleBuffer, 0, sampleBuffer.Length);

                    if (EndOfSamples(sampleCount, sampleBuffer))
                    {
                        break;
                    }

                    if (toneDetector.Detected(sampleBuffer, sampleRate))
                    {
                        Log.Info($"Tone detected at {DateTime.UtcNow}");
                        recorders.Add(new DispatchMessageRecorder(bufferedWaveProvider.WaveFormat));
                        toneDetector.Reset();
                    }

                    foreach (var recorder in recorders)
                    {
                        recorder.Record(byteBuffer, bytesReadCount, sampleBuffer, sampleCount);
                    }

                    if (silenceDetector.IsRecordingComplete(sampleBuffer, sampleRate))
                    {
                        recorders.ForEach(r =>
                        {
                            r.Close();
                            r.Dispose();
                        });

                        recorders.Clear();
                    }
                }

                recorders.ForEach(r => r.Dispose());

                Log.Info("End of stream");
            }
        }