Skip to content

Commit

Permalink
imp - Rendering is now out of the loop in music player
Browse files Browse the repository at this point in the history
---

We've moved the rendering code outside the loop to avoid problems.

---

Type: imp
Breaking: False
Doc Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Feb 28, 2024
1 parent 256ec89 commit 5ed2dc6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ internal static void Play()
// There could be a chance that the music has fully stopped without any user interaction.
PlaybackPositioningTools.SeekToTheBeginning();
PlayerTui.advance = true;
PlayerTui.rerender = true;
PlayerTui.playerThread.Start();
SpinWait.SpinUntil(() => PlaybackTools.Playing || PlayerTui.failedToPlay);
PlayerTui.failedToPlay = false;
Expand Down Expand Up @@ -177,7 +176,6 @@ internal static void PromptForAddSong()
}
else
InfoBoxColor.WriteInfoBox(Translate.DoTranslation("File \"{0}\" doesn't exist."), path);
PlayerTui.rerender = true;
}

internal static void PromptForAddDirectory()
Expand All @@ -201,7 +199,6 @@ internal static void PromptForAddDirectory()
}
else
InfoBoxColor.WriteInfoBox(Translate.DoTranslation("Music library directory is not found."));
PlayerTui.rerender = true;
}

internal static void Exit()
Expand Down Expand Up @@ -251,7 +248,6 @@ internal static void PopulateMusicFileInfo(string musicPath)
}
else
{
PlayerTui.rerender = true;
InfoBoxColor.WriteInfoBox(Translate.DoTranslation("Loading BassBoom to open {0}..."), false, musicPath);
PlayerTui.total = AudioInfoTools.GetDuration(true);
PlayerTui.totalSpan = AudioInfoTools.GetDurationSpanFromSamples(PlayerTui.total);
Expand Down Expand Up @@ -351,7 +347,6 @@ internal static void RemoveCurrentSong()
PlayerTui.populate = true;
PopulateMusicFileInfo(PlayerTui.musicFiles[PlayerTui.currentSong - 1]);
}
PlayerTui.rerender = true;
}

internal static void RemoveAllSongs()
Expand Down Expand Up @@ -379,7 +374,6 @@ internal static void PromptSeek()
PlayerTui.position = PlayerTui.total;
PlaybackPositioningTools.SeekToFrame(PlayerTui.position);
}
PlayerTui.rerender = true;
}

internal static void ShowHelp()
Expand All @@ -405,7 +399,6 @@ internal static void ShowHelp()
[E] {{Translate.DoTranslation("Open equalizer")}}
"""
);
PlayerTui.rerender = true;
}

internal static void ShowSongInfo()
Expand Down Expand Up @@ -446,7 +439,6 @@ internal static void ShowSongInfo()
{{textsBuilder}}
"""
);
PlayerTui.rerender = true;
}
}
}
137 changes: 63 additions & 74 deletions public/Nitrocid.Addons/Nitrocid.Extras.BassBoom/Player/PlayerTui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ internal static class PlayerTui
internal static TimeSpan totalSpan = new();
internal static int total = 0;
internal static (long rate, int channels, int encoding) formatInfo = new();
internal static bool rerender = true;
internal static int currentSong = 1;
internal static double volume = 1.0;
internal static bool exiting = false;
Expand All @@ -93,7 +92,6 @@ public static void PlayerLoop()

volume = PlaybackTools.GetVolume().baseLinear;
exiting = false;
rerender = true;
paused = false;
populate = true;
advance = false;
Expand All @@ -102,79 +100,72 @@ public static void PlayerLoop()
Screen playerScreen = new();
ScreenTools.SetCurrent(playerScreen);

// First, clear the screen to draw our TUI
while (!exiting)
{
ScreenPart screenPart = new();
Thread.Sleep(1);
try
{
// Redraw if necessary
if (ConsoleResizeHandler.WasResized(true))
rerender = true;
bool wasRerendered = rerender;
if (rerender)
{
rerender = false;
screenPart.AddDynamicText(HandleDraw);
}
// First, make a screen part to draw our TUI
ScreenPart screenPart = new();

// Current duration
position = FileTools.IsOpened ? PlaybackPositioningTools.GetCurrentDuration() : 0;
var posSpan = FileTools.IsOpened ? PlaybackPositioningTools.GetCurrentDurationSpan() : new();
string indicator =
Translate.DoTranslation("Seek") + $": {PlayerControls.seekRate:0.00} | " +
Translate.DoTranslation("Volume") + $": {volume:0.00}";
screenPart.AddDynamicText(() =>
{
var buffer = new StringBuilder();
buffer.Append(
ProgressBarColor.RenderProgress(100 * (position / (double)total), 2, ConsoleWrapper.WindowHeight - 8, 3, 3, KernelColorTools.GetColor(KernelColorType.Progress), ColorTools.GetGray(), KernelColorTools.GetColor(KernelColorType.Background)) +
TextWriterWhereColor.RenderWhere($"{posSpan} / {totalSpan}", 3, ConsoleWrapper.WindowHeight - 9, KernelColorTools.GetColor(KernelColorType.NeutralText), KernelColorTools.GetColor(KernelColorType.Background)) +
TextWriterWhereColor.RenderWhere(indicator, ConsoleWrapper.WindowWidth - indicator.Length - 3, ConsoleWrapper.WindowHeight - 9, KernelColorTools.GetColor(KernelColorType.NeutralText), KernelColorTools.GetColor(KernelColorType.Background))
);
return buffer.ToString();
});
// Redraw if necessary
bool wasRerendered = true;
screenPart.AddDynamicText(HandleDraw);

// Get the lyrics
if (PlaybackTools.Playing)
// Current duration
screenPart.AddDynamicText(() =>
{
var buffer = new StringBuilder();
position = FileTools.IsOpened ? PlaybackPositioningTools.GetCurrentDuration() : 0;
var posSpan = FileTools.IsOpened ? PlaybackPositioningTools.GetCurrentDurationSpan() : new();
string indicator =
Translate.DoTranslation("Seek") + $": {PlayerControls.seekRate:0.00} | " +
Translate.DoTranslation("Volume") + $": {volume:0.00}";
buffer.Append(
ProgressBarColor.RenderProgress(100 * (position / (double)total), 2, ConsoleWrapper.WindowHeight - 8, 3, 3, KernelColorTools.GetColor(KernelColorType.Progress), ColorTools.GetGray(), KernelColorTools.GetColor(KernelColorType.Background)) +
TextWriterWhereColor.RenderWhere($"{posSpan} / {totalSpan}", 3, ConsoleWrapper.WindowHeight - 9, KernelColorTools.GetColor(KernelColorType.NeutralText), KernelColorTools.GetColor(KernelColorType.Background)) +
TextWriterWhereColor.RenderWhere(indicator, ConsoleWrapper.WindowWidth - indicator.Length - 3, ConsoleWrapper.WindowHeight - 9, KernelColorTools.GetColor(KernelColorType.NeutralText), KernelColorTools.GetColor(KernelColorType.Background))
);
return buffer.ToString();
});

// Get the lyrics
screenPart.AddDynamicText(() =>
{
var buffer = new StringBuilder();
if (PlaybackTools.Playing)
{
// Print the lyrics, if any
if (lyricInstance is not null)
{
// Print the lyrics, if any
if (lyricInstance is not null)
string current = lyricInstance.GetLastLineCurrent();
if (current != cachedLyric || wasRerendered)
{
string current = lyricInstance.GetLastLineCurrent();
if (current != cachedLyric || wasRerendered)
{
cachedLyric = current;
screenPart.AddDynamicText(() =>
{
var buffer = new StringBuilder();
buffer.Append(
TextWriterWhereColor.RenderWhere(ConsoleClearing.GetClearLineToRightSequence(), 0, ConsoleWrapper.WindowHeight - 10, KernelColorTools.GetColor(KernelColorType.NeutralText), KernelColorTools.GetColor(KernelColorType.Background)) +
CenteredTextColor.RenderCentered(ConsoleWrapper.WindowHeight - 10, lyricInstance.GetLastLineCurrent(), KernelColorTools.GetColor(KernelColorType.NeutralText), KernelColorTools.GetColor(KernelColorType.Background))
);
return buffer.ToString();
});
}
cachedLyric = current;
buffer.Append(
TextWriterWhereColor.RenderWhere(ConsoleClearing.GetClearLineToRightSequence(), 0, ConsoleWrapper.WindowHeight - 10, KernelColorTools.GetColor(KernelColorType.NeutralText), KernelColorTools.GetColor(KernelColorType.Background)) +
CenteredTextColor.RenderCentered(ConsoleWrapper.WindowHeight - 10, lyricInstance.GetLastLineCurrent(), KernelColorTools.GetColor(KernelColorType.NeutralText), KernelColorTools.GetColor(KernelColorType.Background))
);
}
else
cachedLyric = "";
}
else
{
cachedLyric = "";
screenPart.AddDynamicText(() =>
{
var buffer = new StringBuilder();
buffer.Append(
TextWriterWhereColor.RenderWhere(ConsoleClearing.GetClearLineToRightSequence(), 0, ConsoleWrapper.WindowHeight - 10, KernelColorTools.GetColor(KernelColorType.NeutralText), KernelColorTools.GetColor(KernelColorType.Background))
);
return buffer.ToString();
});
}
}
else
{
cachedLyric = "";
buffer.Append(
TextWriterWhereColor.RenderWhere(ConsoleClearing.GetClearLineToRightSequence(), 0, ConsoleWrapper.WindowHeight - 10, KernelColorTools.GetColor(KernelColorType.NeutralText), KernelColorTools.GetColor(KernelColorType.Background))
);
}
return buffer.ToString();
});

// Render the buffer
playerScreen.AddBufferedPart("BassBoom Player", screenPart);

// Render the buffer
playerScreen.AddBufferedPart("BassBoom Player", screenPart);
// Then, the main loop
while (!exiting)
{
Thread.Sleep(1);
try
{
wasRerendered = ConsoleResizeHandler.WasResized(false);
ScreenTools.Render();

// Handle the keystroke
Expand All @@ -192,23 +183,22 @@ public static void PlayerLoop()
if (PlaybackTools.Playing)
PlaybackTools.Stop();
InfoBoxColor.WriteInfoBox(Translate.DoTranslation("There's an error with Basolia when trying to process the music file.") + "\n\n" + bex.Message);
rerender = true;
playerScreen.RequireRefresh();
}
catch (BasoliaOutException bex)
{
if (PlaybackTools.Playing)
PlaybackTools.Stop();
InfoBoxColor.WriteInfoBox(Translate.DoTranslation("There's an error with Basolia output when trying to process the music file.") + "\n\n" + bex.Message);
rerender = true;
playerScreen.RequireRefresh();
}
catch (Exception ex)
{
if (PlaybackTools.Playing)
PlaybackTools.Stop();
InfoBoxColor.WriteInfoBox(Translate.DoTranslation("There's an unknown error when trying to process the music file.") + "\n\n" + ex.Message);
rerender = true;
playerScreen.RequireRefresh();
}
playerScreen.RemoveBufferedParts();
}

// Close the file if open
Expand All @@ -219,6 +209,7 @@ public static void PlayerLoop()
ConsoleWrapper.CursorVisible = true;
ColorTools.LoadBack();
ScreensaverManager.AllowLock();
playerScreen.RemoveBufferedParts();
ScreenTools.UnsetCurrent(playerScreen);
}

Expand Down Expand Up @@ -270,7 +261,7 @@ private static void HandleKeypressIdleMode(ConsoleKeyInfo keystroke, Screen play
break;
case ConsoleKey.E:
Equalizer.OpenEqualizer(playerScreen);
rerender = true;
playerScreen.RequireRefresh();
break;
case ConsoleKey.Q:
PlayerControls.Exit();
Expand Down Expand Up @@ -339,7 +330,7 @@ private static void HandleKeypressPlayMode(ConsoleKeyInfo keystroke, Screen play
break;
case ConsoleKey.E:
Equalizer.OpenEqualizer(playerScreen);
rerender = true;
playerScreen.RequireRefresh();
break;
case ConsoleKey.Q:
PlayerControls.Exit();
Expand Down Expand Up @@ -376,7 +367,6 @@ private static void HandlePlay()
finally
{
lyricInstance = null;
rerender = true;
}
}

Expand All @@ -385,7 +375,6 @@ private static string HandleDraw()
// Prepare things
var drawn = new StringBuilder();
ConsoleWrapper.CursorVisible = false;
ColorTools.LoadBack();

// First, print the keystrokes
string keystrokes =
Expand Down

0 comments on commit 5ed2dc6

Please sign in to comment.