Skip to content

Commit

Permalink
imp - brk|doc - Further optimize radio support
Browse files Browse the repository at this point in the history
---

We've made improvements to the radio station support. We've also removed CachedSongInfo from Basolia as it was really meant to be in the Cli application.

---

Type: imp
Breaking: True
Doc Required: True
Part: 1/1
  • Loading branch information
AptiviCEO committed May 28, 2024
1 parent dc93b26 commit be2e765
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 150 deletions.
5 changes: 4 additions & 1 deletion BassBoom.Basolia/File/FileTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ public static async Task OpenUrlAsync(string path)

// Check to see if there are any ICY headers
if (!reply.Headers.Any((kvp) => kvp.Key.StartsWith("icy-")))
throw new BasoliaException($"This doesn't look like a radio station. Are you sure?", mpg123_errors.MPG123_BAD_FILE);
throw new BasoliaException("This doesn't look like a radio station. Are you sure?", mpg123_errors.MPG123_BAD_FILE);
var contentType = reply.Content.Headers.ContentType;
if (contentType.MediaType != "audio/mpeg")
throw new BasoliaException($"This doesn't look like an MP3 radio station. You have a(n) {contentType.MediaType} type. Are you sure?", mpg123_errors.MPG123_BAD_FILE);

// We're now entering the dangerous zone
unsafe
Expand Down
27 changes: 25 additions & 2 deletions BassBoom.Basolia/Playback/PlaybackPositioningTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static void SeekToTheBeginning()
PlaybackTools.holding = true;
while (PlaybackTools.bufferPlaying)
Thread.Sleep(1);
NativeOutputLib.out123_drop(outHandle);
Drop();
int status = NativePositioning.mpg123_seek(handle, 0, 0);
PlaybackTools.holding = false;
if (status == (int)mpg123_errors.MPG123_ERR)
Expand Down Expand Up @@ -134,13 +134,36 @@ public static void SeekToFrame(int frame)
PlaybackTools.holding = true;
while (PlaybackTools.bufferPlaying)
Thread.Sleep(1);
NativeOutputLib.out123_drop(outHandle);
Drop();
int status = NativePositioning.mpg123_seek(handle, frame, 0);
PlaybackTools.holding = false;
if (status == (int)mpg123_errors.MPG123_ERR)
throw new BasoliaException($"Can't seek to frame #{frame} of the file", (mpg123_errors)status);
}
}
}

/// <summary>
/// Drops all MPEG frames to the device
/// </summary>
/// <exception cref="BasoliaException"></exception>
public static void Drop()
{
lock (PositionLock)
{
InitBasolia.CheckInited();

// Check to see if the file is open
if (!FileTools.IsOpened)
throw new BasoliaException("Can't drop.", mpg123_errors.MPG123_BAD_FILE);

// We're now entering the dangerous zone
unsafe
{
var outHandle = Mpg123Instance._out123Handle;
NativeOutputLib.out123_drop(outHandle);
}
}
}
}
}
19 changes: 16 additions & 3 deletions BassBoom.Basolia/Playback/PlaybackTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@
using BassBoom.Basolia.Devices;
using System.Runtime.InteropServices;
using BassBoom.Native.Interop.Analysis;
using BassBoom.Basolia.Radio;
using System.Net.Http;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace BassBoom.Basolia.Playback
{
Expand Down Expand Up @@ -66,6 +64,21 @@ public static class PlaybackTools
public static string RadioIcy =>
radioIcy;

/// <summary>
/// Current radio ICY metadata
/// </summary>
public static string RadioNowPlaying
{
get
{
string icy = RadioIcy;
if (icy.Length == 0 || !FileTools.IsRadioStation)
return "";
icy = Regex.Match(icy, @"StreamTitle='((?:[^']|\\')*)'").Groups[1].Value.Trim().Replace("\\'", "'");
return icy;
}
}

/// <summary>
/// Plays the currently open file (synchronous)
/// </summary>
Expand Down
12 changes: 10 additions & 2 deletions BassBoom.Cli/BassBoomCli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ namespace BassBoom.Cli
internal class BassBoomCli
{
private static readonly Version version = Assembly.GetAssembly(typeof(InitBasolia)).GetName().Version;
internal static Version mpgVer;
internal static Version outVer;

static int Main(string[] args)
{
Expand All @@ -54,12 +56,18 @@ static int Main(string[] args)
return 1;
}
if (!isRadio)
Player.musicFiles.Add(musicPath);
Player.passedMusicPaths.Add(musicPath);
}

// Initialize Basolia
InitBasolia.Init();

// Initialize versions
mpgVer = InitBasolia.MpgLibVersion;
outVer = InitBasolia.OutLibVersion;

// Now, open an interactive TUI
ConsoleResizeListener.StartResizeListener();
InitBasolia.Init();
if (isRadio)
Radio.RadioLoop();
else
Expand Down
8 changes: 5 additions & 3 deletions BassBoom.Cli/CliBase/Equalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,13 @@ private static string HandleDraw()
drawn.Append(CenteredTextColor.RenderCentered(ConsoleWrapper.WindowHeight - 4, separator));

// Write powered by...
drawn.Append(TextWriterWhereColor.RenderWhere($"╣ Powered by BassBoom and MPG123 v{Player.mpgVer} ╠", 2, ConsoleWrapper.WindowHeight - 4));
drawn.Append(TextWriterWhereColor.RenderWhere($"╣ Powered by BassBoom and MPG123 v{BassBoomCli.mpgVer} ╠", 2, ConsoleWrapper.WindowHeight - 4));

// Write current song
if (Player.musicFiles.Count > 0 )
drawn.Append(PlayerControls.RenderSongName(Player.musicFiles[Player.currentSong - 1]));
if (Player.cachedInfos.Count > 0)
drawn.Append(PlayerControls.RenderSongName(Player.cachedInfos[Player.currentSong - 1].MusicPath));
else if (Radio.cachedInfos.Count > 0)
drawn.Append(RadioControls.RenderStationName());

// Now, print the list of bands and their values.
var choices = new List<InputChoiceInfo>();
Expand Down
48 changes: 27 additions & 21 deletions BassBoom.Cli/CliBase/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using BassBoom.Basolia;
using BassBoom.Basolia.File;
using BassBoom.Basolia.Format;
using BassBoom.Basolia.Format.Cache;
using BassBoom.Basolia.Lyrics;
using BassBoom.Basolia.Playback;
using System;
Expand All @@ -39,6 +38,7 @@
using Terminaux.Reader;
using Terminaux.Inputs.Styles.Selection;
using Terminaux.Inputs;
using BassBoom.Cli.Tools;

namespace BassBoom.Cli.CliBase
{
Expand All @@ -61,10 +61,8 @@ internal static class Player
internal static bool paused = false;
internal static bool failedToPlay = false;
internal static string cachedLyric = "";
internal static readonly List<string> musicFiles = [];
internal static readonly List<CachedSongInfo> cachedInfos = [];
internal static Version mpgVer;
internal static Version outVer;
internal static readonly List<string> passedMusicPaths = [];

public static void PlayerLoop()
{
Expand All @@ -74,10 +72,6 @@ public static void PlayerLoop()
populate = true;
advance = false;

// Initialize versions
mpgVer = InitBasolia.MpgLibVersion;
outVer = InitBasolia.OutLibVersion;

// Populate the screen
Screen playerScreen = new();
ScreenTools.SetCurrent(playerScreen);
Expand Down Expand Up @@ -346,15 +340,15 @@ private static void HandlePlay()
{
try
{
foreach (var musicFile in musicFiles.Skip(currentSong - 1))
foreach (var musicFile in cachedInfos.Skip(currentSong - 1))
{
if (!advance || exiting)
return;
else
populate = true;
currentSong = musicFiles.IndexOf(musicFile) + 1;
PlayerControls.PopulateMusicFileInfo(musicFile);
TextWriterRaw.WritePlain(PlayerControls.RenderSongName(musicFile), false);
currentSong = cachedInfos.IndexOf(musicFile) + 1;
PlayerControls.PopulateMusicFileInfo(musicFile.MusicPath);
TextWriterRaw.WritePlain(PlayerControls.RenderSongName(musicFile.MusicPath), false);
if (paused)
{
paused = false;
Expand Down Expand Up @@ -393,28 +387,40 @@ private static string HandleDraw()
drawn.Append(CenteredTextColor.RenderCentered(ConsoleWrapper.WindowHeight - 4, separator));

// Write powered by...
drawn.Append(TextWriterWhereColor.RenderWhere($"╣ Powered by BassBoom and MPG123 v{mpgVer} ╠", 2, ConsoleWrapper.WindowHeight - 4));
drawn.Append(TextWriterWhereColor.RenderWhere($"╣ Powered by BassBoom and MPG123 v{BassBoomCli.mpgVer} ╠", 2, ConsoleWrapper.WindowHeight - 4));

// In case we have no songs in the playlist...
if (musicFiles.Count == 0)
if (cachedInfos.Count == 0)
{
int height = (ConsoleWrapper.WindowHeight - 10) / 2;
drawn.Append(CenteredTextColor.RenderCentered(height, "Press 'A' to insert a single song to the playlist, or 'S' to insert the whole music library."));
return drawn.ToString();
if (passedMusicPaths.Count > 0)
{
foreach (string path in passedMusicPaths)
{
PlayerControls.PopulateMusicFileInfo(path);
populate = true;
}
passedMusicPaths.Clear();
}
else
{
int height = (ConsoleWrapper.WindowHeight - 10) / 2;
drawn.Append(CenteredTextColor.RenderCentered(height, "Press 'A' to insert a single song to the playlist, or 'S' to insert the whole music library."));
return drawn.ToString();
}
}

// Populate music file info, as necessary
if (populate)
PlayerControls.PopulateMusicFileInfo(musicFiles[currentSong - 1]);
drawn.Append(PlayerControls.RenderSongName(musicFiles[currentSong - 1]));
PlayerControls.PopulateMusicFileInfo(cachedInfos[currentSong - 1].MusicPath);
drawn.Append(PlayerControls.RenderSongName(cachedInfos[currentSong - 1].MusicPath));

// Now, print the list of songs.
var choices = new List<InputChoiceInfo>();
int startPos = 3;
int endPos = ConsoleWrapper.WindowHeight - 10;
int songsPerPage = endPos - startPos;
int max = musicFiles.Select((_, idx) => idx).Max((idx) => $" {idx + 1}) ".Length);
for (int i = 0; i < musicFiles.Count; i++)
int max = cachedInfos.Select((_, idx) => idx).Max((idx) => $" {idx + 1}) ".Length);
for (int i = 0; i < cachedInfos.Count; i++)
{
// Populate the first pane
var (musicName, musicArtist, _) = PlayerControls.GetMusicNameArtistGenre(i);
Expand Down
Loading

0 comments on commit be2e765

Please sign in to comment.