public Task <bool> StopAsync()
 {
     service.Stop();
     return(Task.FromResult(!service.IsRunning));
 }
        //    private ObservableAsPropertyHelper<ObservableCollection<AudioMeterModel>> _audioMeters;

        public MainWindowViewModel()
        {
            _deviceEnumerator   = new MMDeviceEnumerator();
            _notificationClient = new MMNotificationClient(_deviceEnumerator);

            CaptureService.ItemsUpdated.ObserveOn(RxApp.MainThreadScheduler).Subscribe(x =>
            {
                if (AudioMeters.Count == 0)
                {
                    foreach (var audioMeterModel in x)
                    {
                        AudioMeters.Add(audioMeterModel);
                    }

                    return;
                }

                if (PeakVolume > 0)
                {
                    if (x[0].Value > PeakVolume + PeakOffset)
                    {
                        Console.WriteLine("ALERT");
                    }
                }
                test.Add(x[0].Value);
                for (var i = 0; i < x.Count; i++)
                {
                    AudioMeters[i] = x[i];
                }
            });


            UpdateDevices = ReactiveCommand.Create(() =>
            {
                Devices.Clear();
                foreach (var device in _deviceEnumerator.EnumAudioEndpoints(DataFlow.All, DeviceState.Active))
                {
                    Devices.Add(device);
                }
            });

            StartVolumeCapture =
                ReactiveCommand.Create(() => { CaptureService.Start(); }, outputScheduler: RxApp.MainThreadScheduler);
            StopVolumeCapture =
                ReactiveCommand.Create(() => { CaptureService.Stop(); }, outputScheduler: RxApp.MainThreadScheduler);
            PeakVolumeCapture =
                ReactiveCommand.CreateFromObservable <Unit, Unit>(_ =>
            {
                CaptureService.Start();
                test.Clear();
                return(Observable.Return(Unit.Default)
                       .Delay(TimeSpan.FromSeconds(10))
                       .Do(x => CaptureService.Stop()));
            });
            PeakVolumeCapture.Subscribe(x => PeakVolume = test.Max());
            PeakVolumeCapture.ThrownExceptions.Subscribe(Console.WriteLine);
            Observable.FromEventPattern <DeviceNotificationEventArgs>(
                x => _notificationClient.DeviceAdded += x,
                x => _notificationClient.DeviceAdded -= x).Merge(
                Observable.FromEventPattern <DeviceNotificationEventArgs>(
                    y => _notificationClient.DeviceRemoved += y,
                    y => _notificationClient.DeviceRemoved -= y))
            .ObserveOn(RxApp.MainThreadScheduler)
            .Select(x => Unit.Default)
            .InvokeCommand(UpdateDevices);

            Observable.FromEventPattern <DevicePropertyChangedEventArgs>(
                z => _notificationClient.DevicePropertyChanged += z,
                z => _notificationClient.DevicePropertyChanged -= z)
            .ObserveOn(RxApp.MainThreadScheduler)
            .Select(x => Unit.Default)
            .InvokeCommand(UpdateDevices);
        }
예제 #3
0
 public Task <bool> StopAsync(CancellationToken cancellationToken)
 {
     service.Stop();
     return(Task.FromResult(!service.IsRunning));
 }