예제 #1
0
        public void PlayAlarm(bool playAlarm = true)
        {
            if (_defaultDeviceChanged)
            {
                return;
            }
            _timerAlarmDelayArgs             = new TimerDelayArgs(DateTime.Now);
            _timerAlarmDelayArgs.alarmActive = true;
            OnUpdateTimerAlarmDelay(this, _timerAlarmDelayArgs);
            _isSoundAlertPlaying = true;
            if (!playAlarm)
            {
                return;
            }

            if (_soundOut.GetType() == typeof(WasapiOut))
            {
                if ((_soundOut as WasapiOut).Device.DeviceState != DeviceState.Active)
                {
                    GetNewSoundOut();
                }
            }

            if (_soundOut.PlaybackState == PlaybackState.Paused)
            {
                _soundOut.Resume();
            }
            else
            {
                _soundOut.Play();
            }
        }
예제 #2
0
        private void VolumeCheck(float volume)
        {
            VolumeCheckArgs vca;

            vca = new VolumeCheckArgs(VOLUME_OK);
            if (volume >= alarmThreshold)
            {
                KeepSystemVolume();
                vca.meterColor = VOLUME_HIGH;
                if (_isSoundAlarmEnabled)
                {
                    if (!_timerAlarmDelay.IsEnabled && _soundOut.PlaybackState != PlaybackState.Playing)
                    {
                        vca.resetSound       = true;
                        vca.resetLabelColor  = true;
                        _timerAlarmDelayArgs = new TimerDelayArgs(DateTime.Now);
                        _timerAlarmDelay.Start();
                    }
                }
                else
                {
                    vca.resetSound        = true;
                    vca.resetLabelContent = true;
                    StopTheAlarm(ref vca, true, false);
                }
                if (_isMessageAlarmEnabled)
                {
                    if (!_timerOverlayShow.IsEnabled && !_timerOverlayUpdate.IsEnabled)
                    {
                        vca.resetOverlay       = true;
                        vca.resetLabelColor    = true;
                        _timerOverlayDelayArgs = new TimerDelayArgs(DateTime.Now);
                        _timerOverlayShow.Start();
                    }
                }
                else
                {
                    vca.resetOverlay      = true;
                    vca.resetLabelContent = true;
                    StopTheAlarm(ref vca, false, true);
                }
            }
            else
            {
                vca.resetSound        = true;
                vca.resetOverlay      = true;
                vca.resetLabelContent = true;
                StopTheAlarm(ref vca, true, true);
            }
            OnVolumeCheck(this, vca);
        }
예제 #3
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();
        }
예제 #4
0
        private void VolumeCheck(float volume)
        {
            if (_defaultDeviceChanged)
            {
                return;
            }
            try
            {
                VolumeCheckArgs vca;
                vca = new VolumeCheckArgs(VOLUME_OK);
                if (volume >= _alarmThreshold)
                {
                    KeepSystemVolume();
                    vca.meterColor = VOLUME_HIGH;
                    if (_isSoundAlertEnabled)
                    {
                        if (_delayBeforeAlarm > 0)
                        {
                            if (!_timerAlarmDelay.Enabled && !_isSoundAlertPlaying)
                            {
                                vca.resetSoundLabelColor = true;
                                _timerAlarmDelayArgs     = new TimerDelayArgs(DateTime.Now);
                                _timerAlarmDelay.Start();
                            }
                            //  else return;
                        }
                        else
                        {
                            PlayAlarm(!_isControllerMode);
                        }
                    }
                    else
                    {
                        if (_isSoundAlertPlaying)
                        {
                            _soundOut.Stop();
                            _isSoundAlertPlaying       = false;
                            vca.resetSoundLabelColor   = true;
                            vca.resetSoundLabelContent = true;
                        }
                    }
                    if (_isOverlayAlertEnabled)
                    {
                        if (!_timerOverlayShow.Enabled && !_timerOverlayUpdate.Enabled && !_isOverlayAlertPlaying)
                        {
                            vca.resetOverlayLabelColor = true;
                            _timerOverlayDelayArgs     = new TimerDelayArgs(DateTime.Now);
                            _timerOverlayShow.Start();
                        }
                    }
                    else
                    {
                        if (_timerOverlayShow.Enabled)
                        {
                            _timerOverlayShow.Stop();
                            vca.resetOverlayLabelColor = true;
                            _isOverlayAlertPlaying     = false;
                        }
                        if (_overlayWorking)
                        {
                            vca.resetOverlayLabelContent = true;
                            _overlayWorking        = false;
                            _isOverlayAlertPlaying = false;
                        }
                    }
                }
                else
                {
                    //if (_soundOut.PlaybackState == PlaybackState.Playing)
                    //{
                    vca.meterColor               = VOLUME_OK;
                    vca.resetSoundLabelContent   = true;
                    vca.resetOverlayLabelContent = true;
                    if (_soundOut.PlaybackState == PlaybackState.Playing)
                    {
                        _soundOut.Pause();
                    }
                    _isSoundAlertPlaying = false;

                    _isOverlayAlertPlaying = false;

                    if (_timerAlarmDelay != null)
                    {
                        if (_timerAlarmDelay.Enabled)
                        {
                            _timerAlarmDelay.Stop();
                        }
                    }
                    if (_timerOverlayShow.Enabled)
                    {
                        _timerOverlayShow.Stop();
                    }
                    if (_overlayWorking)
                    {
                        _overlayWorking = false;
                    }
                    //}
                }

                OnVolumeCheck(this, vca);
            }
            catch (Exception e)
            {
                Trace.TraceError("Volume check error: " + e);
            }
        }