Skip to content

Commit

Permalink
fix - Fixed a nasty bug with CLI player
Browse files Browse the repository at this point in the history
We've fixed a bug when playing any music in the CLI player.

---

We've fixed a very nasty bug regarding corrupted data that MPG123 reports by moving the total duration out of the playback loop.

Details
=======

When MPG123 plays a music by the PlaybackTools.Play() function from Basolia, subsequent calls to AudioInfoTools.GetDuration(true) causes the buffer that Play() uses to become corrupt, essentially causing messages talking about invalid MPEG data to appear in the console, hence messing the TUI up.

According to the libmpg123.c native code version 1.31.3, mpg123_scan() calls an internal function, seek_frame(), to try to seek to the beginning of the music stream. After that, it runs a loop in which it tests for this condition: read_frame(mh) == 1. This loop increments both the track frame count by 1 and the track samples by the "samples per frame" variable found in the mh struct, mh->spf.

---

Type: fix
Breaking: False
Doc Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Aug 29, 2023
1 parent 4147ee7 commit 5d26a1b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
5 changes: 5 additions & 0 deletions BassBoom.Basolia/Format/AudioInfoTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using BassBoom.Native.Interop.Analysis;
using BassBoom.Native.Interop.Init;
using BassBoom.Basolia.File;
using BassBoom.Basolia.Playback;

namespace BassBoom.Basolia.Format
{
Expand All @@ -40,6 +41,10 @@ public static int GetDuration(bool scan)
if (!FileTools.IsOpened)
throw new BasoliaException("Can't play a file that's not open", mpg123_errors.MPG123_BAD_FILE);

// Check to see if we're playing
if (PlaybackTools.Playing && !InitBasolia._fugitive)
throw new BasoliaException("Trying to get the duration during playback causes playback corruption! Don't call this function during playback. If you're willing to take a risk, turn on Fugitive Mode.", mpg123_errors.MPG123_ERR_READER);

// We're now entering the dangerous zone
unsafe
{
Expand Down
5 changes: 4 additions & 1 deletion BassBoom.Basolia/InitBasolia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ namespace BassBoom.Basolia
/// </summary>
public static class InitBasolia
{
internal static bool _fugitive = false;
private static bool _basoliaInited = false;

/// <summary>
/// Initializes the MPG123 library for Basolia to function
/// </summary>
public static void Init()
/// <param name="Fugitive">Allows illegal operations. NEVER SET THIS TO TRUE UNLESS YOU KNOW WHAT YOU'RE DOING!</param>
public static void Init(bool Fugitive = false)
{
Mpg123Instance.InitializeLibrary();
_fugitive = Fugitive;
_basoliaInited = true;
}

Expand Down
24 changes: 20 additions & 4 deletions BassBoom.Cli/CliBase/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,26 @@ internal static class Player
public static void PlayerLoop(string musicPath)
{
bool exiting = false;
bool rerender = true;

// Try to open the file after loading the library
InitBasolia.Init();
FileTools.OpenFile(musicPath);
int total = AudioInfoTools.GetDuration(true);

// First, clear the screen to draw our TUI
ConsoleTools.ActionCursorVisible(false);
ColorTools.LoadBack();
while (!exiting)
{
try
{
// If we need to render again, do it
if (rerender)
{
rerender = false;
ConsoleTools.ActionCursorVisible(false);
ColorTools.LoadBack();
}

// First, print the keystrokes
string keystrokes = "[SPACE] Play/Pause - [ESC] Stop - [Q] Exit";
CenteredTextColor.WriteCentered(ConsoleTools.ActionWindowHeight() - 2, keystrokes);
Expand All @@ -66,8 +74,7 @@ public static void PlayerLoop(string musicPath)
{
// Print the progress bar
int position = PlaybackPositioningTools.GetCurrentDuration();
int total = AudioInfoTools.GetDuration(true);
ProgressBarColor.WriteProgress(position / (double)total, 2, ConsoleTools.ActionWindowHeight() - 8, 6);
ProgressBarColor.WriteProgress(100 * (position / (double)total), 2, ConsoleTools.ActionWindowHeight() - 8, 6);

// Wait for any keystroke asynchronously
if (ConsoleTools.ActionKeyAvailable())
Expand Down Expand Up @@ -106,15 +113,24 @@ public static void PlayerLoop(string musicPath)
}
catch (BasoliaException bex)
{
if (PlaybackTools.Playing)
PlaybackTools.Stop();
InfoBoxColor.WriteInfoBox("There's an error with Basolia when trying to process the music file.\n\n" + bex.Message);
rerender = true;
}
catch (BasoliaOutException bex)
{
if (PlaybackTools.Playing)
PlaybackTools.Stop();
InfoBoxColor.WriteInfoBox("There's an error with Basolia output when trying to process the music file.\n\n" + bex.Message);
rerender = true;
}
catch (Exception ex)
{
if (PlaybackTools.Playing)
PlaybackTools.Stop();
InfoBoxColor.WriteInfoBox("There's an unknown error when trying to process the music file.\n\n" + ex.Message);
rerender = true;
}
}

Expand Down

0 comments on commit 5d26a1b

Please sign in to comment.