Skip to content

Commit

Permalink
add - doc - Added the music file labeler
Browse files Browse the repository at this point in the history
---

For album makers who want a quick way in appending numbers in front of the music name, we've added a mechanism from NameNumerizer (deprecating it in the process) to make it easier to access directly from Basolia.

---

Type: add
Breaking: False
Doc Required: True
Part: 1/1
  • Loading branch information
AptiviCEO committed Dec 10, 2023
1 parent 6c11110 commit 2ad17dc
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 11 deletions.
84 changes: 84 additions & 0 deletions BassBoom.Basolia/Albums/MusicFileLabeler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//
// BassBoom Copyright (C) 2023 Aptivi
//
// This file is part of Nitrocid KS
//
// BassBoom is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// BassBoom is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using BassBoom.Basolia.File;
using System.Collections.Generic;
using System.IO;

namespace BassBoom.Basolia.Albums
{
/// <summary>
/// Music file labeling tools for albums
/// </summary>
public static class MusicFileLabeler
{
/// <summary>
/// Prepends the filenames with the file number to prepare it for album folder
/// </summary>
/// <param name="libraryPath">The target path</param>
/// <param name="extensions">Extensions to include (Make sure to include the dot before the extension. For example: .mp3 instead of mp3)</param>
public static void LabelFiles(string libraryPath)
{
string[] files = Directory.GetFiles(libraryPath);
List<string> enumeratedFiles = [];

// Filter the files that we don't need to label
var extensions = FileTools.SupportedExtensions;
foreach (string file in files)
{
if (extensions.Length > 0)
{
// We have extensions to be included. Search for them.
string fileExtension = Path.GetExtension(file);
foreach (string extension in extensions)
{
// If the file has an extension that we're looking for, add it to the final list.
if (fileExtension == extension)
enumeratedFiles.Add(file);
}
}
else
enumeratedFiles.AddRange(files);
}

// Actually label the files
for (int fileIndex = 0; fileIndex <= enumeratedFiles.Count - 1; fileIndex++)
{
string file = enumeratedFiles[fileIndex];
string fileName = Path.GetFileName(file);

// Append the current count into the file name, but we need to check how many leading zeroes we have to put so it looks like
// the track number you usually find on music disks, like:
//
// "001. Artist - Song" if the last track number has three digits, or
// "01. Artist - Song" if the track number has two digits.
int digits = enumeratedFiles.Count.ToString().Length;
int fileNumber = fileIndex + 1;
string labeledName = fileNumber.ToString().PadLeft(digits, '0') + ". " + fileName;

// Finally, form the full path and rename it
string labeledFilePath = Path.Combine(libraryPath, labeledName);
if (!System.IO.File.Exists(labeledFilePath))
{
System.IO.File.Move(file, labeledFilePath);
}
}
}
}
}
4 changes: 2 additions & 2 deletions BassBoom.Basolia/Devices/DeviceTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public struct DeviceDescList
public static ReadOnlyDictionary<string, string> GetDrivers()
{
InitBasolia.CheckInited();
Dictionary<string, string> drivers = new();
Dictionary<string, string> drivers = [];

// We're now entering the dangerous zone
nint names = nint.Zero, descr = nint.Zero;
Expand Down Expand Up @@ -95,7 +95,7 @@ public static ReadOnlyDictionary<string, string> GetDrivers()
public static ReadOnlyDictionary<string, string> GetDevices(string driver, ref string activeDevice)
{
InitBasolia.CheckInited();
Dictionary<string, string> devices = new();
Dictionary<string, string> devices = [];

// We're now entering the dangerous zone
nint names = nint.Zero, descr = nint.Zero, active = nint.Zero;
Expand Down
20 changes: 19 additions & 1 deletion BassBoom.Basolia/File/FileTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,32 @@

namespace BassBoom.Basolia.File
{
/// <summary>
/// File tools that are essential for the music player
/// </summary>
public static class FileTools
{
private static bool isOpened = false;
private static readonly string[] supportedExts =
[
".mp3",
".mp2",
".mpa",
".mpg",
".mpga",
];

/// <summary>
/// List of supported extensions
/// </summary>
public static string[] SupportedExtensions =>
supportedExts;

/// <summary>
/// Is the file open?
/// </summary>
public static bool IsOpened => isOpened;
public static bool IsOpened =>
isOpened;

/// <summary>
/// Opens a media file
Expand Down
4 changes: 2 additions & 2 deletions BassBoom.Basolia/Lyrics/Lyric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public List<LyricLineWord> GetLastLineWordsCurrent()
var processedLines = GetLinesCurrent();
if (processedLines.Length > 0)
return processedLines[^1].LineWords;
return new();
return [];
}

/// <summary>
Expand Down Expand Up @@ -99,7 +99,7 @@ public List<LyricLineWord> GetLastLineWordsAtSpan(TimeSpan span)
var processedLines = GetLinesToSpan(span);
if (processedLines.Length > 0)
return processedLines[^1].LineWords;
return new();
return [];
}

protected internal Lyric(List<LyricLine> lines)
Expand Down
4 changes: 2 additions & 2 deletions BassBoom.Cli/CliBase/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ internal static class Player
internal static bool populate = true;
internal static bool paused = false;
internal static string cachedLyric = "";
internal static readonly List<string> musicFiles = new();
internal static readonly List<CachedSongInfo> cachedInfos = new();
internal static readonly List<string> musicFiles = [];
internal static readonly List<CachedSongInfo> cachedInfos = [];

public static void PlayerLoop()
{
Expand Down
7 changes: 3 additions & 4 deletions BassBoom/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ public class MainViewModel : ViewModelBase
internal static FrameInfo frameInfo = null;
internal static MainView view;
internal static int selectedPath = 0;
internal static readonly List<CachedSongInfo> cachedInfos = new();
internal static readonly List<CachedSongInfo> cachedInfos = [];
private Thread sliderUpdate = new(UpdateSlider);
private static Lyric lyricInstance = null;
private readonly ObservableCollection<string> musicFileSelect = new();
private readonly string[] supportedExtensions = new[] { ".mp3", ".mp2", ".mpa", ".mpg", ".mpga" };
private readonly ObservableCollection<string> musicFileSelect = [];

private FilePickerFileType MusicFiles => new("Music files")
{
Expand Down Expand Up @@ -106,7 +105,7 @@ public async Task AddSongs()
var folder = result.Path.LocalPath;
var files = Directory
.GetFiles(folder, "*.*", SearchOption.AllDirectories)
.Where((file) => supportedExtensions.Contains(Path.GetExtension(file).ToLower()))
.Where((file) => FileTools.SupportedExtensions.Contains(Path.GetExtension(file).ToLower()))
.ToArray();
MusicFileSelect.AddRange(files);
}
Expand Down

0 comments on commit 2ad17dc

Please sign in to comment.