diff --git a/ConsolePlayer/Program.cs b/ConsolePlayer/Program.cs index 50616e1..d8c7ba2 100644 --- a/ConsolePlayer/Program.cs +++ b/ConsolePlayer/Program.cs @@ -3,7 +3,6 @@ using System.IO.Ports; using System.Numerics; using System.Timers; -using SoundFlow.Abstracts; using SoundFlow.Backends.MiniAudio; using SoundFlow.Components; using SoundFlow.Enums; @@ -15,6 +14,8 @@ namespace ConsolePlayer { static SerialPort Port; static System.Timers.Timer FrameTimer; + static System.Timers.Timer ResyncTimer; + static SoundPlayer AudioPlayer; static int FramesPerTick = 6; static int ShowtapeIndex = 0; @@ -26,6 +27,8 @@ namespace ConsolePlayer static bool DetectedController = false; static bool Playing = false; static bool TripFlag = false; + static bool Paused = false; + static bool SyncMsg = false; static string[] ShowtapeFrames; @@ -65,19 +68,51 @@ namespace ConsolePlayer FrameTimer.Elapsed += PlayFrame; FrameTimer.AutoReset = true; - using MiniAudioEngine audioEngine = new MiniAudioEngine(44100, Capability.Playback); + ResyncTimer = new System.Timers.Timer(15000); + ResyncTimer.Elapsed += Resync; + ResyncTimer.AutoReset = true; + + using MiniAudioEngine audioEngine = new MiniAudioEngine(48000, Capability.Playback); using StreamDataProvider dataProvider = new StreamDataProvider(File.OpenRead("pc3playertempaudio.tmp")); - SoundPlayer player = new SoundPlayer(dataProvider); + AudioPlayer = new SoundPlayer(dataProvider); - Mixer.Master.AddComponent(player); + Mixer.Master.AddComponent(AudioPlayer); Console.WriteLine($"Playing Showtape \"{ShowtapeName}\" ({ShowtapeFormattedLength})"); + Console.WriteLine("Controls:\n[SPACE] to pause and unpause\n[TAB] to toggle sync messages"); Playing = true; - player.Play(); + AudioPlayer.Play(); FrameTimer.Start(); - while (Playing) Thread.Sleep(1000); - Mixer.Master.RemoveComponent(player); + ResyncTimer.Start(); + while (Playing) + { + ConsoleKeyInfo input = Console.ReadKey(); + if (Playing) + { + if (input.Key == ConsoleKey.Spacebar) + { + if (Paused) + { + Paused = false; + FrameTimer.Start(); + ResyncTimer.Start(); + AudioPlayer.Seek((float)(((float)ShowtapeIndex) / 60.0)); + AudioPlayer.Play(); + } + else + { + Paused = true; + FrameTimer.Stop(); + ResyncTimer.Stop(); + AudioPlayer.Pause(); + } + } + else if (input.Key == ConsoleKey.Tab) SyncMsg = !SyncMsg; + } + else if (!Playing) break; + } + Mixer.Master.RemoveComponent(AudioPlayer); } static void OpenSerialPortSpecific(string specifiedPortName) @@ -155,7 +190,7 @@ namespace ConsolePlayer } } - } catch (Exception) {} + } catch (Exception) { continue; } } if (!DetectedController) { @@ -256,12 +291,18 @@ namespace ConsolePlayer { FrameTimer.Stop(); Playing = false; - Console.WriteLine("Complete!"); + Console.WriteLine("Complete! Press any key to exit."); if (File.Exists("pc3playertempaudio.tmp")) File.Delete("pc3playertempaudio.tmp"); return; } Port.Write(ShowtapeFrames[ShowtapeIndex]); ShowtapeIndex += FramesPerTick; } + + static void Resync(Object sender, ElapsedEventArgs e) + { + if (SyncMsg) Console.WriteLine($"Resynced by {(int)(AudioPlayer.Time * 60) - ShowtapeIndex} frames"); + ShowtapeIndex = (int)(AudioPlayer.Time * 60); + } } }