101 lines
2.8 KiB
C#
101 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Windows.Input;
|
|
using Avalonia.Threading;
|
|
using NAudio.CoreAudioApi;
|
|
using NAudio.Wave;
|
|
using ReactiveUI;
|
|
using SoundTester.Service;
|
|
using SoundTesting;
|
|
using Splat;
|
|
|
|
namespace SoundTester.ViewModels;
|
|
|
|
public class VoiceTrackerViewModel : ViewModelBase
|
|
{
|
|
private string _recButtonContent = "Начать проверку";
|
|
private string _selectedDevice;
|
|
private int _selectedDeviceIndex;
|
|
private ObservableCollection<string> _devices;
|
|
private float _volume = -96;
|
|
private bool _isMonitoring = false;
|
|
private bool _isEnabled = false;
|
|
private WasapiCapture _wasapiCapture;
|
|
private WasapiOut _wasapiOut;
|
|
private BufferedWaveProvider _bufferedWaveProvider;
|
|
private DevicesEnumerator _devicesEnumerator;
|
|
|
|
|
|
public string SelectedDevice
|
|
{
|
|
get => _selectedDevice;
|
|
set => this.RaiseAndSetIfChanged(ref _selectedDevice, value);
|
|
}
|
|
|
|
public int SelectedDeviceIndex
|
|
{
|
|
get => _selectedDeviceIndex;
|
|
set => this.RaiseAndSetIfChanged(ref _selectedDeviceIndex, value);
|
|
}
|
|
|
|
public ObservableCollection<string>? Devices
|
|
{
|
|
get => _devices;
|
|
set => this.RaiseAndSetIfChanged(ref _devices, value);
|
|
}
|
|
|
|
public float Volume
|
|
{
|
|
get => _volume;
|
|
set => this.RaiseAndSetIfChanged(ref _volume, value);
|
|
}
|
|
|
|
public bool IsEnabled
|
|
{
|
|
get => _isEnabled;
|
|
set => this.RaiseAndSetIfChanged(ref _isEnabled, value);
|
|
}
|
|
|
|
public string RecButtonContent
|
|
{
|
|
get => _recButtonContent;
|
|
set => this.RaiseAndSetIfChanged(ref _recButtonContent, value);
|
|
}
|
|
|
|
public ICommand RecordCommand { get; }
|
|
|
|
public VoiceTrackerViewModel(DevicesEnumerator? devicesEnumerator = null) //Конструктор
|
|
{
|
|
;
|
|
|
|
_devicesEnumerator = devicesEnumerator ?? Locator.Current.GetService<DevicesEnumerator>()!;
|
|
|
|
Devices = _devicesEnumerator!.DevicesUpdater.InputDevices;
|
|
SelectedDevice = Devices.FirstOrDefault();
|
|
VoiceTrackerService voiceTrackerService = new VoiceTrackerService(SelectedDevice);
|
|
voiceTrackerService.DecibelLevelEvent += DecibelTracker;
|
|
RecordCommand = ReactiveCommand.Create(() => voiceTrackerService.Record());
|
|
this.WhenAnyValue(x => x.Devices, x => x.SelectedDevice)
|
|
.WhereNotNull()
|
|
.Subscribe(x =>
|
|
{
|
|
Devices = _devicesEnumerator.DevicesUpdater.InputDevices;
|
|
IsEnabled = SelectedDeviceIndex == -1 || Devices.Count == 0 ? false : true;
|
|
});
|
|
|
|
|
|
}
|
|
|
|
private void DecibelTracker(float level)
|
|
{
|
|
RefreshProgressBar(level);
|
|
}
|
|
|
|
|
|
|
|
private void RefreshProgressBar(float percentage)
|
|
{
|
|
Volume = percentage;
|
|
}
|
|
} |