Esempio n. 1
0
        public AudioPlayer(string filename)
        {
            whiteNoiseSound = CodecFactory.Instance.GetCodec(filename);
            whiteNoiseSound = whiteNoiseSound.Loop();

            soundOut = GetSoundOut();
            soundOut.Initialize(whiteNoiseSound);
        }
Esempio n. 2
0
        public AlarmSystem()
        {
            //_loggingEnabled = Trace.Listeners.Count > 1;
            Trace.TraceInformation("Alarm system init");
            Trace.Indent();
            state = States.Running;
            using (MMDeviceEnumerator enumerator = new MMDeviceEnumerator())
            {
                using (MMDevice device = enumerator.GetDefaultAudioEndpoint(DataFlow.Capture, Role.Communications))
                {
                    _meter        = AudioMeterInformation.FromDevice(device);
                    _soundCapture = new WasapiCapture(true, AudioClientShareMode.Shared, 250)
                    {
                        Device = device
                    };
                    _soundCapture.Initialize();
                    _soundCapture.Start();
                    Trace.TraceInformation("Sound Capture OK");
                }
            }
            IWaveSource soundSource = GetSoundSource();

            soundSource = soundSource.Loop();
            _soundOut   = GetSoundOut();
            _soundOut.Initialize(soundSource);
            Trace.TraceInformation("Sound Out OK");

            captureMultiplier  = Properties.Settings.Default.Boost;
            delayBeforeAlarm   = Properties.Settings.Default.SafeScreamZone;
            delayBeforeOverlay = Properties.Settings.Default.AlertOverlayDelay;
            AlarmVolume        = (float)Properties.Settings.Default.Volume / 100;

            _systemSimpleAudioVolume = GetSimpleAudioVolume();
            SystemVolume             = (Properties.Settings.Default.VolumeSystem / 100f).Clamp(0, 1);
            KeepSystemVolume();

            _bgInputListener.WorkerSupportsCancellation = true;
            _bgInputListener.DoWork             += bgInputListener_DoWork;
            _bgInputListener.RunWorkerCompleted += bgInputListener_RunWorkerCompleted;

            _bgInputListener.RunWorkerAsync();
            Trace.TraceInformation("Background worker running");

            #region Timers

            _timerAlarmDelay       = new DispatcherTimer();
            _timerAlarmDelay.Tick += (s, args) =>
            {
                if (_timerAlarmDelayArgs.ElapsedTime.Seconds >= delayBeforeAlarm)
                {
                    _timerAlarmDelayArgs.alarmActive = true;
                    _timerAlarmDelay.Stop();
                    PlayAlarm();
                }

                OnUpdateTimerAlarmDelay(this, _timerAlarmDelayArgs);
                if (_timerAlarmDelay.Dispatcher.HasShutdownStarted)
                {
                    _timerAlarmDelayArgs = null;
                }
            };

            _timerOverlayShow       = new DispatcherTimer();
            _timerOverlayShow.Tick += (s, args) =>
            {
                if (_timerOverlayDelayArgs.ElapsedTime.Seconds >= delayBeforeOverlay)
                {
                    _timerOverlayDelayArgs.alarmActive = true;
                    _timerOverlayShow.Stop();
                    ShowAlertWindow();
                }
                OnUpdateTimerOverlayDelay(this, _timerOverlayDelayArgs);
                if (_timerOverlayShow.Dispatcher.HasShutdownStarted)
                {
                    _timerOverlayDelayArgs = null;
                }
            };

            _timerOverlayUpdate          = new DispatcherTimer();
            _timerOverlayUpdate.Interval = TimeSpan.FromMilliseconds(10);
            _timerOverlayUpdate.Tick    += (s, args) =>
            {
                _alertOverlay.Update();
                if (!_isMessageAlarmEnabled)
                {
                    _overlayWorking = false;
                }
                if (!_overlayWorking)
                {
                    _timerOverlayUpdate.Stop();
                    _alertOverlay.Dispose();
                    _alertOverlay = null;
                }
            };
            #endregion

            Trace.TraceInformation("Timers initialized");
            Trace.TraceInformation("Alarm System up and running!");
            Trace.Unindent();
        }
Esempio n. 3
0
        public AlarmSystem(bool isController          = false,
                           int micCaptureBoost        = 100,
                           float delayBeforeAlarm     = 0,
                           float delayBeforeOverlay   = 0,
                           float alarmVolume          = 0,
                           float systemVolume         = 0,
                           float threshold            = 100,
                           bool isSoundAlertEnabled   = false,
                           bool isOverlayAlertEnabled = false)
        {
            //_loggingEnabled = Trace.Listeners.Count > 1;
            try
            {
                Trace.TraceInformation("Alarm system init");
                Trace.Indent();
                state = States.Running;
                this._isControllerMode = isController;

                MMNotificationClient deviceNotification = new MMNotificationClient();
                deviceNotification.DefaultDeviceChanged += DeviceNotification_DefaultDeviceChanged;
                deviceNotification.DeviceAdded          += DeviceNotification_DeviceAdded;
                deviceNotification.DeviceRemoved        += DeviceNotification_DeviceRemoved;

                GetCapture(isController);
                Trace.TraceInformation("Sound Capture OK");

                _soundSource = GetSoundSource();
                _soundSource = _soundSource.Loop();
                GetOutputSound();
                Trace.TraceInformation("Sound Out OK");


                this._isSoundAlertEnabled   = isSoundAlertEnabled;
                this._isOverlayAlertEnabled = isOverlayAlertEnabled;
                this._micCaptureBoost       = micCaptureBoost;
                this._delayBeforeAlarm      = delayBeforeAlarm;
                this._delayBeforeOverlay    = delayBeforeOverlay;
                this.AlarmVolume            = alarmVolume;
                this._alarmThreshold        = threshold;

                _systemSimpleAudioVolume = GetSimpleAudioVolume();
                this.SystemVolume        = systemVolume;

                if (!isController)
                {
                    _bgInputListener.WorkerSupportsCancellation = true;
                    _bgInputListener.DoWork             += bgInputListener_DoWork;
                    _bgInputListener.RunWorkerCompleted += bgInputListener_RunWorkerCompleted;

                    _bgInputListener.RunWorkerAsync();
                }
                Trace.TraceInformation("Background worker running");

                #region Timers

                _timerAlarmDelay          = new System.Timers.Timer();
                _timerAlarmDelay.Elapsed += (s, args) =>
                {
                    if (_timerAlarmDelayArgs.ElapsedTime.Seconds >= _delayBeforeAlarm)
                    {
                        _timerAlarmDelayArgs.alarmActive = true;

                        _timerAlarmDelay.Stop();
                        PlayAlarm(!_isControllerMode);
                    }

                    OnUpdateTimerAlarmDelay(this, _timerAlarmDelayArgs);
                };

                _timerOverlayShow          = new System.Timers.Timer();
                _timerOverlayShow.Elapsed += (s, args) =>
                {
                    if (_timerOverlayDelayArgs.ElapsedTime.Seconds >= _delayBeforeOverlay)
                    {
                        _timerOverlayDelayArgs.alarmActive = true;
                        _timerOverlayShow.Stop();
                        Application.Current.Dispatcher.Invoke((Action) delegate { ShowAlertWindow(!_isControllerMode); });
                    }
                    OnUpdateTimerOverlayDelay(this, _timerOverlayDelayArgs);
                    //if (_timerOverlayShow.Dispatcher.HasShutdownStarted)
                    //    _timerOverlayDelayArgs = null;
                };

                _timerOverlayUpdate          = new System.Timers.Timer();
                _timerOverlayUpdate.Interval = 10;
                _timerOverlayUpdate.Elapsed += (s, args) =>
                {
                    _alertOverlay.Update();
                    if (!_overlayWorking)
                    {
                        _timerOverlayUpdate.Stop();
                        if (_alertOverlay != null)
                        {
                            _alertOverlay.Dispose();
                            _alertOverlay = null;
                        }
                    }
                };

                _timerZeroVolumeDelay           = new System.Timers.Timer(10000);
                _timerZeroVolumeDelay.AutoReset = false;
                _timerZeroVolumeDelay.Elapsed  += (s, args) =>
                {
                    _currentCaptureRole -= 1;
                    if (_currentCaptureRole < 0)
                    {
                        _currentCaptureRole = Role.Communications;
                    }
                    GetCapture(isController);
                };
                _timerZeroVolumeDelay.Start();
                #endregion

                Trace.TraceInformation("Timers initialized");
                Trace.TraceInformation("Alarm System up and running!");
                Trace.Unindent();
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                Trace.TraceError(e.StackTrace);
                Application.Current.Shutdown();
            }
        }