From 1cd9723964b5a82540838c0f2b7590314a3968de Mon Sep 17 00:00:00 2001 From: John Colvin Date: Thu, 16 Aug 2018 01:13:18 -0400 Subject: [PATCH 1/5] prevent duplicate sounds from repeating for brief period after playing --- src/client/spark/Notifications/INotifier.cs | 27 ++++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/client/spark/Notifications/INotifier.cs b/src/client/spark/Notifications/INotifier.cs index ad804556f..ff8b6a36b 100644 --- a/src/client/spark/Notifications/INotifier.cs +++ b/src/client/spark/Notifications/INotifier.cs @@ -152,6 +152,9 @@ namespace mbts.ui { } class SoundNotificationPlayer { + // number of milliseconds to delay playing sound, + // during which time all subsequent requests to play the same sound will be ignored. + const int _delay_ms = 75; readonly IReadOnlyDictionary _players; public static SoundNotificationPlayer create() => new SoundNotificationPlayer(_all_players()); @@ -184,7 +187,7 @@ namespace mbts.ui { class NAudioPlayer { WaveOutEvent _wave_out = new WaveOutEvent(); WaveFileReader _wave_reader; - bool _restarting; + bool _is_playing, _is_play_requested; public NAudioPlayer(UnmanagedMemoryStream uwave_stream) { var ms = new MemoryStream(); @@ -196,23 +199,29 @@ namespace mbts.ui { } public void play() { - if (_restarting) { + if (_is_playing) { return; } else if (_wave_out.PlaybackState == PlaybackState.Playing) { - _restarting = true; + _is_play_requested = true; _wave_out.Stop(); - } else { - _wave_out.Play(); - } + } else + _play_core(); } void _on_playback_stopped(object sender, StoppedEventArgs e) { _wave_reader.Position = 0; - if (_restarting) { - _wave_out.Play(); - _restarting = false; + if (_is_play_requested) { + _is_play_requested = false; + _play_core(); } } + + async void _play_core() { + _is_playing = true; + _wave_out.Play(); + await Task.Delay(_delay_ms); + _is_playing = false; + } } } From 0afeca4f3889a45181b1c057669af4bfe02b6da6 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Thu, 16 Aug 2018 11:31:32 -0400 Subject: [PATCH 2/5] un-nest NAudioPlayer (so all Spark proj may access it) and move to own file --- src/client/spark/Notifications/INotifier.cs | 43 ------------ .../spark/Notifications/NAudioPlayer.cs | 67 +++++++++++++++++++ 2 files changed, 67 insertions(+), 43 deletions(-) create mode 100644 src/client/spark/Notifications/NAudioPlayer.cs diff --git a/src/client/spark/Notifications/INotifier.cs b/src/client/spark/Notifications/INotifier.cs index ff8b6a36b..0c0ebc67f 100644 --- a/src/client/spark/Notifications/INotifier.cs +++ b/src/client/spark/Notifications/INotifier.cs @@ -152,9 +152,6 @@ namespace mbts.ui { } class SoundNotificationPlayer { - // number of milliseconds to delay playing sound, - // during which time all subsequent requests to play the same sound will be ignored. - const int _delay_ms = 75; readonly IReadOnlyDictionary _players; public static SoundNotificationPlayer create() => new SoundNotificationPlayer(_all_players()); @@ -183,46 +180,6 @@ namespace mbts.ui { { SoundNotification.SystemSound.CashRegister, new NAudioPlayer(bts.Properties.Resources.cash_register_purchase) }, { SoundNotification.SystemSound.Reload, new NAudioPlayer(bts.Properties.Resources.reload_1911) } }; - - class NAudioPlayer { - WaveOutEvent _wave_out = new WaveOutEvent(); - WaveFileReader _wave_reader; - bool _is_playing, _is_play_requested; - - public NAudioPlayer(UnmanagedMemoryStream uwave_stream) { - var ms = new MemoryStream(); - uwave_stream.CopyTo(ms); - ms.Position = 0; - _wave_reader = new WaveFileReader(ms); - _wave_out.Init(_wave_reader); - _wave_out.PlaybackStopped += _on_playback_stopped; - } - - public void play() { - if (_is_playing) { - return; - } else if (_wave_out.PlaybackState == PlaybackState.Playing) { - _is_play_requested = true; - _wave_out.Stop(); - } else - _play_core(); - } - - void _on_playback_stopped(object sender, StoppedEventArgs e) { - _wave_reader.Position = 0; - if (_is_play_requested) { - _is_play_requested = false; - _play_core(); - } - } - - async void _play_core() { - _is_playing = true; - _wave_out.Play(); - await Task.Delay(_delay_ms); - _is_playing = false; - } - } } // This class adopts to role of view-controller in the mvc paradigm, rather than view-model in mvvm. diff --git a/src/client/spark/Notifications/NAudioPlayer.cs b/src/client/spark/Notifications/NAudioPlayer.cs new file mode 100644 index 000000000..12bbb2dc1 --- /dev/null +++ b/src/client/spark/Notifications/NAudioPlayer.cs @@ -0,0 +1,67 @@ +using bts.shell; +using NAudio.Wave; +using System; +using System.IO; +using System.Threading.Tasks; + +namespace mbts.ui { + class NAudioPlayer { + // number of milliseconds to delay playing sound, + // during which time all subsequent requests to play the same sound will be ignored. + const int _delay_ms = 75; + WaveOutEvent _wave_out = new WaveOutEvent(); + WaveFileReader _wave_reader; + bool _is_playing, _is_play_requested; + + public NAudioPlayer(Stream uwave_stream) { + var ms = new MemoryStream(); + uwave_stream.CopyTo(ms); + ms.Position = 0; + _wave_reader = new WaveFileReader(ms); + _wave_out.Init(_wave_reader); + _wave_out.PlaybackStopped += _on_playback_stopped; + } + + public static NAudioPlayer create(SoundNotification.SystemSound system_sound) => + new NAudioPlayer(_get_stream(system_sound)); + + public void play() { + if (_is_playing) { + return; + } else if (_wave_out.PlaybackState == PlaybackState.Playing) { + _is_play_requested = true; + _wave_out.Stop(); + } else + _play_core(); + } + + void _on_playback_stopped(object sender, StoppedEventArgs e) { + _wave_reader.Position = 0; + if (_is_play_requested) { + _is_play_requested = false; + _play_core(); + } + } + + async void _play_core() { + _is_playing = true; + _wave_out.Play(); + await Task.Delay(_delay_ms); + _is_playing = false; + } + + static Stream _get_stream(SoundNotification.SystemSound system_sound) { + switch (system_sound) { + case SoundNotification.SystemSound.GameCorrect: return bts.Properties.Resources.game_sound_correct; + case SoundNotification.SystemSound.GameIncorrect: return bts.Properties.Resources.game_sound_wrong; + case SoundNotification.SystemSound.GameOverArcade: return bts.Properties.Resources.game_over_arcade; + case SoundNotification.SystemSound.Whistle: return bts.Properties.Resources.whistle; + case SoundNotification.SystemSound.BellCounter: return bts.Properties.Resources.bell_counter; + case SoundNotification.SystemSound.Powerup: return bts.Properties.Resources.powerup; + case SoundNotification.SystemSound.CashRegister: return bts.Properties.Resources.cash_register_purchase; + case SoundNotification.SystemSound.Reload: return bts.Properties.Resources.reload_1911; + default: throw new NotImplementedException($"Unknown {nameof(SoundNotification.SystemSound)}: {system_sound}."); + } + } + } +} \ No newline at end of file From dedccf70986d1ddb4fe128ffdda31445514fde9e Mon Sep 17 00:00:00 2001 From: John Colvin Date: Thu, 16 Aug 2018 11:56:33 -0400 Subject: [PATCH 3/5] preview sound by clicking help button next to dropdown --- .../DIYGrid/Settings/Animation/SoundAnimationView.xaml | 2 ++ .../DIYGrid/Settings/Animation/SoundAnimationView.xaml.cs | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/Animation/SoundAnimationView.xaml b/src/client/spark/view/Controls/DIYGrid/Settings/Animation/SoundAnimationView.xaml index faeb4e6c2..8add7e439 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/Animation/SoundAnimationView.xaml +++ b/src/client/spark/view/Controls/DIYGrid/Settings/Animation/SoundAnimationView.xaml @@ -33,6 +33,8 @@