Skip to content

Commit

Permalink
Fix crash when Microsoft GS Wavetable Synth does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
sabihoshi committed May 23, 2021
1 parent c247105 commit b7bd01c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion GenshinLyreMidiPlayer.WPF/GenshinLyreMidiPlayer.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<UseWPF>true</UseWPF>
<StartupObject>GenshinLyreMidiPlayer.WPF.App</StartupObject>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Version>2.0.1</Version>
<Version>2.0.2</Version>
<ApplicationIcon>item_windsong_lyre.ico</ApplicationIcon>
<Nullable>enable</Nullable>
<RepositoryUrl>https://github.com/sabihoshi/GenshinLyreMidiPlayer</RepositoryUrl>
Expand Down
22 changes: 18 additions & 4 deletions GenshinLyreMidiPlayer.WPF/ViewModels/LyrePlayerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class LyrePlayerViewModel : Screen,
private readonly IEventAggregator _events;
private readonly MediaPlayer? _player;
private readonly SettingsPageViewModel _settings;
private readonly OutputDevice _speakers;
private readonly OutputDevice? _speakers;
private readonly PlaybackCurrentTimeWatcher _timeWatcher;
private bool _ignoreSliderChange;
private InputDevice? _inputDevice;
Expand All @@ -40,7 +40,6 @@ public class LyrePlayerViewModel : Screen,
public LyrePlayerViewModel(IContainer ioc,
SettingsPageViewModel settings, PlaylistViewModel playlist)
{
_speakers = OutputDevice.GetByName("Microsoft GS Wavetable Synth");
_timeWatcher = PlaybackCurrentTimeWatcher.Instance;

_events = ioc.Get<IEventAggregator>();
Expand All @@ -66,6 +65,18 @@ public LyrePlayerViewModel(IContainer ioc,
_player.CommandManager.PlayReceived += async (_, _) => await PlayPause();
_player.CommandManager.PauseReceived += async (_, _) => await PlayPause();
}

try
{
_speakers = OutputDevice.GetByName("Microsoft GS Wavetable Synth");
}
catch (ArgumentException e)
{
new ErrorContentDialog(e, closeText: "Ignore").ShowAsync();

_settings.CanUseSpeakers = false;
Settings.UseSpeakers = false;
}
}

public BindableCollection<MidiInput> MidiInputs { get; } = new()
Expand Down Expand Up @@ -225,8 +236,11 @@ public void UpdateButtons()

private void InitializeTracks()
{
if (Playlist.OpenedFile?.Midi is null)
return;

MidiTracks.Clear();
MidiTracks.AddRange(Playlist.OpenedFile?
MidiTracks.AddRange(Playlist.OpenedFile
.Midi.GetTrackChunks()
.Select(t => new MidiTrack(_events, t)));
}
Expand Down Expand Up @@ -425,7 +439,7 @@ private void PlayNote(NoteEvent noteEvent)
{
if (Settings.UseSpeakers)
{
_speakers.SendEvent(noteEvent);
_speakers?.SendEvent(noteEvent);
return;
}

Expand Down
2 changes: 2 additions & 0 deletions GenshinLyreMidiPlayer.WPF/ViewModels/SettingsPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public SettingsPageViewModel(IContainer ioc)

public bool CanStartStopTimer => DateTime - DateTime.Now > TimeSpan.Zero;

public bool CanUseSpeakers { get; set; } = true;

public bool IncludeBetaUpdates { get; set; } = Settings.IncludeBetaUpdates;

public bool IsCheckingUpdate { get; set; }
Expand Down
1 change: 1 addition & 0 deletions GenshinLyreMidiPlayer.WPF/Views/SettingsPageView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<ui:SimpleStackPanel Orientation="Horizontal">
<ui:ToggleSwitch
Header="Test Mode (Speakers)"
IsEnabled="{Binding CanUseSpeakers}"
IsOn="{Binding Default.UseSpeakers, Source={StaticResource Settings}}" />

<ui:ToggleSwitch
Expand Down

0 comments on commit b7bd01c

Please sign in to comment.