|
@@ -1,5 +1,6 @@
|
|
|
using System.CodeDom;
|
|
|
using System.ComponentModel;
|
|
|
+using System.Diagnostics;
|
|
|
using System.IO;
|
|
|
using System.Text;
|
|
|
using System.Threading.Channels;
|
|
@@ -20,7 +21,7 @@ using AsioOut = Bmp.Core.Lite.Playback.Outputs.NAudioASIO.AsioOut;
|
|
|
|
|
|
namespace Bmp.WinForms
|
|
|
{
|
|
|
- public partial class MainForm : Form
|
|
|
+ internal partial class MainForm : Form
|
|
|
{
|
|
|
private const string EMOJI_X = "❌";
|
|
|
private const string EMOJI_WARN = "⚠";
|
|
@@ -31,10 +32,9 @@ namespace Bmp.WinForms
|
|
|
|
|
|
private readonly Channel<string> _pendingAddToList = Channel.CreateUnbounded<string>();
|
|
|
|
|
|
- private IServiceProvider? _serviceProvider;
|
|
|
- private ILogger<MainForm> _logger = new Logger<MainForm>(NullLoggerFactory.Instance);
|
|
|
- private IEventBus? _eventBus;
|
|
|
- private SaveLoadService? _sl;
|
|
|
+ private readonly ILogger<MainForm> _logger;
|
|
|
+ private readonly IEventBus? _eventBus;
|
|
|
+ private readonly SaveLoadService? _saveLoadService;
|
|
|
|
|
|
private bool _isRunning;
|
|
|
|
|
@@ -50,24 +50,19 @@ namespace Bmp.WinForms
|
|
|
private UIPlaybackState _playbackState;
|
|
|
private bool _trackBarHolding;
|
|
|
|
|
|
- public MainForm()
|
|
|
+ public MainForm(IEventBus eventBus, SaveLoadService saveLoadService, ILogger<MainForm> logger)
|
|
|
{
|
|
|
- Initialize();
|
|
|
InitializeComponent();
|
|
|
- }
|
|
|
|
|
|
- private void Initialize()
|
|
|
- {
|
|
|
+ _eventBus = eventBus;
|
|
|
+ _saveLoadService = saveLoadService;
|
|
|
+ _logger = logger;
|
|
|
+
|
|
|
Text = Const.AppTitle;
|
|
|
var eIcon = IconExtractor.GetMainIcon();
|
|
|
- if (null != eIcon)
|
|
|
- {
|
|
|
- Icon = eIcon;
|
|
|
- }
|
|
|
+ if (null != eIcon) Icon = eIcon;
|
|
|
}
|
|
|
|
|
|
- public void SetServiceProvider(IServiceProvider serviceProvider) => _serviceProvider = serviceProvider;
|
|
|
-
|
|
|
// ----------------- playback control -----------------
|
|
|
|
|
|
private async Task LoadItemAsync(ListViewItem item)
|
|
@@ -189,7 +184,7 @@ namespace Bmp.WinForms
|
|
|
|
|
|
private async Task ReloadSource()
|
|
|
{
|
|
|
- _inputSource = await Task.Run(() => InputSourceProvider.CreateWaveStream(_currentListViewItem!.Name, _sl?.State?.DecodeDsdToPcm == true));
|
|
|
+ _inputSource = await Task.Run(() => InputSourceProvider.CreateWaveStream(_currentListViewItem!.Name, _saveLoadService?.State?.DecodeDsdToPcm == true));
|
|
|
}
|
|
|
|
|
|
private async Task DeInitOutputDeviceAsync()
|
|
@@ -228,7 +223,7 @@ namespace Bmp.WinForms
|
|
|
if (_inputSource is DsdAudioStream)
|
|
|
{
|
|
|
_nativeDsd = true;
|
|
|
- if (_sl?.State?.SelectedDsdAsioOutputDeviceId == null)
|
|
|
+ if (_saveLoadService?.State?.SelectedDsdAsioOutputDeviceId == null)
|
|
|
{
|
|
|
finalState = EMOJI_X;
|
|
|
balloonShow = true;
|
|
@@ -242,7 +237,7 @@ namespace Bmp.WinForms
|
|
|
var allDevices = OutputDeviceProvider.GetAllSupportedDevices();
|
|
|
IOutputDeviceInfo? selectedDevice = null;
|
|
|
|
|
|
- selectedDevice = allDevices.FirstOrDefault(p => p.Id == _sl?.State?.SelectedDsdAsioOutputDeviceId);
|
|
|
+ selectedDevice = allDevices.FirstOrDefault(p => p.Id == _saveLoadService?.State?.SelectedDsdAsioOutputDeviceId);
|
|
|
|
|
|
if (selectedDevice == null)
|
|
|
{
|
|
@@ -261,7 +256,7 @@ namespace Bmp.WinForms
|
|
|
else
|
|
|
{
|
|
|
_nativeDsd = false;
|
|
|
- if (_sl?.State?.SelectedPcmOutputDeviceId == null)
|
|
|
+ if (_saveLoadService?.State?.SelectedPcmOutputDeviceId == null)
|
|
|
{
|
|
|
//TODO: 消息机制 错误 未指定输出设备
|
|
|
|
|
@@ -277,7 +272,7 @@ namespace Bmp.WinForms
|
|
|
var allDevices = OutputDeviceProvider.GetAllSupportedDevices();
|
|
|
IOutputDeviceInfo? selectedDevice = null;
|
|
|
|
|
|
- selectedDevice = allDevices.FirstOrDefault(p => p.Id == _sl.State.SelectedPcmOutputDeviceId);
|
|
|
+ selectedDevice = allDevices.FirstOrDefault(p => p.Id == _saveLoadService.State.SelectedPcmOutputDeviceId);
|
|
|
|
|
|
if (selectedDevice == null)
|
|
|
{
|
|
@@ -335,12 +330,6 @@ namespace Bmp.WinForms
|
|
|
_outputDevice = od;
|
|
|
_outputDevice.PlaybackStopped += _outputDevice_PlaybackStopped;
|
|
|
}
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- //TODO: 异常处理 ReInitOutputDevice
|
|
|
- Console.WriteLine(e);
|
|
|
- throw;
|
|
|
- }
|
|
|
finally
|
|
|
{
|
|
|
_currentListViewItem!.SubItems[StateColumnHeader.Index].Text = finalState;
|
|
@@ -451,13 +440,13 @@ namespace Bmp.WinForms
|
|
|
|
|
|
private void SaveState()
|
|
|
{
|
|
|
- if (_sl == null) return;
|
|
|
- if (_sl.State != null)
|
|
|
+ if (_saveLoadService == null) return;
|
|
|
+ if (_saveLoadService.State != null)
|
|
|
{
|
|
|
- _sl.State.FormPosition = Location;
|
|
|
- _sl.State.FormSize = Size;
|
|
|
+ _saveLoadService.State.FormPosition = Location;
|
|
|
+ _saveLoadService.State.FormSize = Size;
|
|
|
|
|
|
- _sl.State.Playlist = MainListView.Items.Cast<ListViewItem>().Select(p => new SaveLoadPlaylistItem
|
|
|
+ _saveLoadService.State.Playlist = MainListView.Items.Cast<ListViewItem>().Select(p => new SaveLoadPlaylistItem
|
|
|
{
|
|
|
Path = p.Name,
|
|
|
Title = p.SubItems[TitleColumnHeader.Index].Text,
|
|
@@ -466,7 +455,7 @@ namespace Bmp.WinForms
|
|
|
}).ToArray();
|
|
|
}
|
|
|
|
|
|
- _sl.Save();
|
|
|
+ _saveLoadService.Save();
|
|
|
}
|
|
|
|
|
|
private async void StartProcessPendingAddQueue()
|
|
@@ -521,8 +510,8 @@ namespace Bmp.WinForms
|
|
|
ctx.Items.Add("-");
|
|
|
|
|
|
var allSupportedDevices = OutputDeviceProvider.GetAllSupportedDevices();
|
|
|
- var selectedPcmDevice = allSupportedDevices.FirstOrDefault(p => p.Id == _sl?.State?.SelectedPcmOutputDeviceId);
|
|
|
- var selectedDsdDevice = allSupportedDevices.FirstOrDefault(p => p.Id == _sl?.State?.SelectedDsdAsioOutputDeviceId);
|
|
|
+ var selectedPcmDevice = allSupportedDevices.FirstOrDefault(p => p.Id == _saveLoadService?.State?.SelectedPcmOutputDeviceId);
|
|
|
+ var selectedDsdDevice = allSupportedDevices.FirstOrDefault(p => p.Id == _saveLoadService?.State?.SelectedDsdAsioOutputDeviceId);
|
|
|
|
|
|
var pcmOutputSelect = new ToolStripMenuItem($"PCM 输出{(selectedPcmDevice == null ? "(未选择)" : "")}");
|
|
|
ctx.Items.Add(pcmOutputSelect);
|
|
@@ -531,13 +520,13 @@ namespace Bmp.WinForms
|
|
|
ctx.Items.Add(dsdOutputSelect);
|
|
|
|
|
|
var dsdToPcm = new ToolStripMenuItem("软解码成PCM");
|
|
|
- if (_sl?.State?.DecodeDsdToPcm == true) dsdToPcm.CheckState = CheckState.Indeterminate;
|
|
|
+ if (_saveLoadService?.State?.DecodeDsdToPcm == true) dsdToPcm.CheckState = CheckState.Indeterminate;
|
|
|
dsdOutputSelect.DropDownItems.Add(dsdToPcm);
|
|
|
dsdToPcm.Click += delegate
|
|
|
{
|
|
|
- if (_sl?.State == null) return;
|
|
|
- _sl.State.DecodeDsdToPcm = true;
|
|
|
- _sl.State.SelectedDsdAsioOutputDeviceId = null;
|
|
|
+ if (_saveLoadService?.State == null) return;
|
|
|
+ _saveLoadService.State.DecodeDsdToPcm = true;
|
|
|
+ _saveLoadService.State.SelectedDsdAsioOutputDeviceId = null;
|
|
|
ShowSettingContextMenu();
|
|
|
};
|
|
|
|
|
@@ -549,9 +538,9 @@ namespace Bmp.WinForms
|
|
|
if (selectedPcmDevice == deviceInfo) pcmDeviceItem.CheckState = CheckState.Indeterminate;
|
|
|
pcmDeviceItem.Click += delegate
|
|
|
{
|
|
|
- if (_sl?.State == null) return;
|
|
|
- _sl.State.SelectedPcmOutputDeviceId = deviceInfo.Id;
|
|
|
- _sl.State.OutputDeviceLatency = null;
|
|
|
+ if (_saveLoadService?.State == null) return;
|
|
|
+ _saveLoadService.State.SelectedPcmOutputDeviceId = deviceInfo.Id;
|
|
|
+ _saveLoadService.State.OutputDeviceLatency = null;
|
|
|
ShowSettingContextMenu();
|
|
|
};
|
|
|
|
|
@@ -564,9 +553,9 @@ namespace Bmp.WinForms
|
|
|
|
|
|
dsdDeviceItem.Click += delegate
|
|
|
{
|
|
|
- if (_sl?.State == null) return;
|
|
|
- _sl.State.SelectedDsdAsioOutputDeviceId = deviceInfo.Id;
|
|
|
- _sl.State.DecodeDsdToPcm = false;
|
|
|
+ if (_saveLoadService?.State == null) return;
|
|
|
+ _saveLoadService.State.SelectedDsdAsioOutputDeviceId = deviceInfo.Id;
|
|
|
+ _saveLoadService.State.DecodeDsdToPcm = false;
|
|
|
ShowSettingContextMenu();
|
|
|
};
|
|
|
}
|
|
@@ -585,15 +574,15 @@ namespace Bmp.WinForms
|
|
|
const int mulCount = 4;
|
|
|
|
|
|
var defaultLatency = selectedPcmDevice.Latency;
|
|
|
- var selectedLatency = _sl?.State?.OutputDeviceLatency ?? defaultLatency;
|
|
|
+ var selectedLatency = _saveLoadService?.State?.OutputDeviceLatency ?? defaultLatency;
|
|
|
|
|
|
- var latencyMenu = new ToolStripMenuItem("PCM 输出缓冲大小:" + (_sl.State?.OutputDeviceLatency ?? defaultLatency));
|
|
|
+ var latencyMenu = new ToolStripMenuItem("PCM 输出缓冲大小:" + (_saveLoadService.State?.OutputDeviceLatency ?? defaultLatency));
|
|
|
ctx.Items.Add(latencyMenu);
|
|
|
|
|
|
void SetLatency(int latency)
|
|
|
{
|
|
|
- if (_sl.State == null) return;
|
|
|
- _sl.State.OutputDeviceLatency = latency;
|
|
|
+ if (_saveLoadService.State == null) return;
|
|
|
+ _saveLoadService.State.OutputDeviceLatency = latency;
|
|
|
ShowSettingContextMenu();
|
|
|
//ShowSettingContextMenu(ctx.Items.IndexOf(latencyMenu));
|
|
|
}
|
|
@@ -633,7 +622,7 @@ namespace Bmp.WinForms
|
|
|
|
|
|
if (selectedDsdDevice == null)
|
|
|
{
|
|
|
- if (_sl?.State?.DecodeDsdToPcm == false)
|
|
|
+ if (_saveLoadService?.State?.DecodeDsdToPcm == false)
|
|
|
{
|
|
|
ctx.Items.Add("(未选择 DSD 输出设备)").Enabled = false;
|
|
|
}
|
|
@@ -674,8 +663,8 @@ namespace Bmp.WinForms
|
|
|
var dbgItem = new ToolStripMenuItem("调试");
|
|
|
ctx.Items.Add(dbgItem);
|
|
|
|
|
|
- dbgItem.DropDownItems.Add("清除选中 PCM 设备").Click += delegate { if (_sl is { State: { } }) _sl.State.SelectedPcmOutputDeviceId = null; };
|
|
|
- dbgItem.DropDownItems.Add("清除选中 DSD 设备").Click += delegate { if (_sl is { State: { } }) _sl.State.SelectedDsdAsioOutputDeviceId = null; };
|
|
|
+ dbgItem.DropDownItems.Add("清除选中 PCM 设备").Click += delegate { if (_saveLoadService is { State: { } }) _saveLoadService.State.SelectedPcmOutputDeviceId = null; };
|
|
|
+ dbgItem.DropDownItems.Add("清除选中 DSD 设备").Click += delegate { if (_saveLoadService is { State: { } }) _saveLoadService.State.SelectedDsdAsioOutputDeviceId = null; };
|
|
|
|
|
|
#endif
|
|
|
}
|
|
@@ -684,12 +673,6 @@ namespace Bmp.WinForms
|
|
|
|
|
|
private void MainForm_Shown(object sender, EventArgs e)
|
|
|
{
|
|
|
- if (_serviceProvider == null) throw new InvalidOperationException($"Must set {nameof(ServiceProvider)} before show!");
|
|
|
-
|
|
|
- _eventBus = _serviceProvider.GetRequiredService<IEventBus>();
|
|
|
- _sl = _serviceProvider.GetRequiredService<SaveLoadService>();
|
|
|
- _logger = _serviceProvider.GetRequiredService<ILogger<MainForm>>();
|
|
|
-
|
|
|
//var lvi = new ListViewItem();
|
|
|
|
|
|
//AlbumImageList.Images.Add(this.Icon);
|
|
@@ -699,17 +682,17 @@ namespace Bmp.WinForms
|
|
|
|
|
|
MainListView.Items.Clear();
|
|
|
|
|
|
- if (_sl != null)
|
|
|
+ if (_saveLoadService != null)
|
|
|
{
|
|
|
- if (_sl.State != null)
|
|
|
+ if (_saveLoadService.State != null)
|
|
|
{
|
|
|
- if (_sl.State.FormPosition.HasValue) Location = _sl.State.FormPosition.Value;
|
|
|
+ if (_saveLoadService.State.FormPosition.HasValue) Location = _saveLoadService.State.FormPosition.Value;
|
|
|
Application.DoEvents();
|
|
|
- if (_sl.State.FormSize.HasValue) Size = _sl.State.FormSize.Value;
|
|
|
+ if (_saveLoadService.State.FormSize.HasValue) Size = _saveLoadService.State.FormSize.Value;
|
|
|
|
|
|
- if (_sl.State.Playlist != null)
|
|
|
+ if (_saveLoadService.State.Playlist != null)
|
|
|
{
|
|
|
- foreach (var playlistItem in _sl.State.Playlist)
|
|
|
+ foreach (var playlistItem in _saveLoadService.State.Playlist)
|
|
|
{
|
|
|
var item = new ListViewItem();
|
|
|
for (var i = 0; i < MainListView.Columns.Count - 1; i++) item.SubItems.Add("");
|