using System; using System.Windows; using Correlator.DataService; using Correlator.Util; using NAudio.CoreAudioApi; using Prism.Commands; using Prism.Mvvm; using Prism.Services.Dialogs; namespace Correlator.ViewModels { public class SimplyAuditionDialogViewModel : BindableBase, IDialogAware { public event Action<IDialogResult> RequestClose { add { } remove { } } #region VM public string Title => "管道听音判断"; private int _currentVolume; public int CurrentVolume { get => _currentVolume; set { _currentVolume = value; RaisePropertyChanged(); } } private bool _isRedRecording; public bool IsRedRecording { get => _isRedRecording; set { _isRedRecording = value; RaisePropertyChanged(); } } private bool _isBlueRecording; public bool IsBlueRecording { get => _isBlueRecording; set { _isBlueRecording = value; RaisePropertyChanged(); } } #endregion #region DelegateCommand public DelegateCommand ListenRedSensorCommand { get; } public DelegateCommand ListenBlueSensorCommand { get; } public DelegateCommand SensorMuteCommand { get; } public DelegateCommand<object> ValueChangedCommand { get; } #endregion private readonly ISerialPortService _serialPortService; private readonly IAudioService _audioService; private readonly ICommandService _commandService; private readonly int _sampleRate; private readonly MMDevice _device; public SimplyAuditionDialogViewModel(ISerialPortService serialPortService, IAudioService audioService, IApplicationDataService dataService, ICommandService commandService) { _serialPortService = serialPortService; _audioService = audioService; _commandService = commandService; _sampleRate = dataService.GetSampleRateByWorkMode(RuntimeCache.WorkMode); //获取音频输入设备 var enumerator = new MMDeviceEnumerator(); _device = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia); CurrentVolume = GetCurrentMicVolume(); ListenRedSensorCommand = new DelegateCommand(ListenRedSensor); SensorMuteCommand = new DelegateCommand(SensorMute); ListenBlueSensorCommand = new DelegateCommand(ListenBlueSensor); ValueChangedCommand = new DelegateCommand<object>(SliderValueChanged); } private int GetCurrentMicVolume() { return (int)(_device.AudioEndpointVolume.MasterVolumeLevelScalar * 100); } private void SetMicVolume(int value) { if (value < 0) { _device.AudioEndpointVolume.MasterVolumeLevelScalar = 0 / 100.0f; } else if (value > 100) { _device.AudioEndpointVolume.MasterVolumeLevelScalar = 100 / 100.0f; } else { _device.AudioEndpointVolume.MasterVolumeLevelScalar = value / 100.0f; } } private void ListenRedSensor() { if (_isBlueRecording) { MessageBox.Show("请先停止蓝色传感器听音", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Stop); return; } if (_isRedRecording) { StopRedSensor(_serialPortService, _audioService); } else { _commandService.SendSoundCollectCmd(_serialPortService.Sp, RuntimeCache.Dev1); _audioService.Start(RuntimeCache.Dev1, _sampleRate); IsRedRecording = true; } } /// <summary> /// 静音 /// </summary> private void SensorMute() { CurrentVolume = 0; SetMicVolume(_currentVolume); } private void ListenBlueSensor() { if (_isRedRecording) { MessageBox.Show("请先停止红色传感器听音", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Stop); return; } if (_isBlueRecording) { StopBlueSensor(_serialPortService, _audioService); } else { _commandService.SendSoundCollectCmd(_serialPortService.Sp, RuntimeCache.Dev2); _audioService.Start(RuntimeCache.Dev2, _sampleRate); IsBlueRecording = true; } } private void SliderValueChanged(object value) { if (value == null) { return; } SetMicVolume(Convert.ToInt32(value)); } /// <summary> /// 停止红色传感器听音 /// </summary> /// <param name="serialPortService"></param> /// <param name="audioService"></param> private void StopRedSensor(ISerialPortService serialPortService, IAudioService audioService) { _commandService.SendSoundStopCmd(serialPortService.Sp, RuntimeCache.Dev1); audioService.Stop(); IsRedRecording = false; RuntimeCache.SoundCaches.SaveSoundData(); } /// <summary> /// 停止蓝色传感器听音 /// </summary> /// <param name="serialPortService"></param> /// <param name="audioService"></param> private void StopBlueSensor(ISerialPortService serialPortService, IAudioService audioService) { _commandService.SendSoundStopCmd(serialPortService.Sp, RuntimeCache.Dev2); audioService.Stop(); IsBlueRecording = false; RuntimeCache.SoundCaches.SaveSoundData(); } public bool CanCloseDialog() { return true; } public void OnDialogClosed() { if (_isRedRecording) { StopRedSensor(_serialPortService, _audioService); } if (_isBlueRecording) { StopBlueSensor(_serialPortService, _audioService); } } public void OnDialogOpened(IDialogParameters parameters) { } } }