From 0791d62222d4b42d754ea3ac0a4c57672643c367 Mon Sep 17 00:00:00 2001 From: Aptivi CEO Date: Wed, 18 Sep 2024 21:21:21 +0300 Subject: [PATCH] add - doc - Added several format functions --- We've added several format functions, such as checking for format support and encoding size. --- Type: add Breaking: False Doc Required: True Backport Required: False Part: 1/1 --- BassBoom.Basolia/Format/ChannelCount.cs | 44 ++++++++ BassBoom.Basolia/Format/FormatTools.cs | 121 +++++++++++++++++++++ BassBoom.Basolia/Playback/PlaybackTools.cs | 12 +- BassBoom.Cli/CliBase/Common.cs | 11 +- 4 files changed, 176 insertions(+), 12 deletions(-) create mode 100644 BassBoom.Basolia/Format/ChannelCount.cs diff --git a/BassBoom.Basolia/Format/ChannelCount.cs b/BassBoom.Basolia/Format/ChannelCount.cs new file mode 100644 index 0000000..d05c64b --- /dev/null +++ b/BassBoom.Basolia/Format/ChannelCount.cs @@ -0,0 +1,44 @@ +// +// BassBoom Copyright (C) 2023 Aptivi +// +// This file is part of BassBoom +// +// 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 . +// + +namespace BassBoom.Basolia.Format +{ + /// + /// Channel count + /// + public enum ChannelCount + { + /// + /// Unknown channel count + /// + Unknown = -1, + /// + /// Stereo + /// + Stereo = 1, + /// + /// Mono + /// + Mono, + /// + /// Both + /// + Both, + } +} diff --git a/BassBoom.Basolia/Format/FormatTools.cs b/BassBoom.Basolia/Format/FormatTools.cs index 17a2be1..7f289b9 100644 --- a/BassBoom.Basolia/Format/FormatTools.cs +++ b/BassBoom.Basolia/Format/FormatTools.cs @@ -220,5 +220,126 @@ public static FormatInfo[] GetFormats(BasoliaMedia? basolia) // We're now entering the safe zone return [.. formats]; } + + /// + /// Is this format supported? + /// + /// Basolia instance that contains a valid handle + /// Rate + /// Encoding + /// Mono, stereo, or both? + public static bool IsFormatSupported(BasoliaMedia? basolia, long rate, int encoding, out ChannelCount channelCount) + { + InitBasolia.CheckInited(); + if (basolia is null) + throw new BasoliaException("Basolia instance is not provided", mpg123_errors.MPG123_BAD_HANDLE); + + // We're now entering the dangerous zone + unsafe + { + var handle = basolia._mpg123Handle; + + // Check for support + var @delegate = MpgNative.GetDelegate(MpgNative.libManagerMpg, nameof(NativeOutput.mpg123_format_support)); + int channelCountInt = @delegate.Invoke(handle, rate, encoding); + channelCount = channelCountInt == 0 ? ChannelCount.Unknown : (ChannelCount)channelCountInt; + } + + // We're now entering the safe zone + return channelCount != ChannelCount.Unknown; + } + + /// + /// Is this format supported? + /// + /// Encoding + public static int GetEncodingSize(int encoding) + { + InitBasolia.CheckInited(); + int size = -1; + + // We're now entering the dangerous zone + unsafe + { + // Check for support + var @delegate = MpgNative.GetDelegate(MpgNative.libManagerMpg, nameof(NativeOutput.mpg123_encsize)); + size = @delegate.Invoke(encoding); + } + + // We're now entering the safe zone + return size; + } + + /// + /// Makes the underlying media handler accept no format + /// + /// Basolia instance that contains a valid handle + public static void NoFormat(BasoliaMedia? basolia) + { + InitBasolia.CheckInited(); + if (basolia is null) + throw new BasoliaException("Basolia instance is not provided", mpg123_errors.MPG123_BAD_HANDLE); + + // We're now entering the dangerous zone + unsafe + { + var handle = basolia._mpg123Handle; + + // Check for support + var @delegate = MpgNative.GetDelegate(MpgNative.libManagerMpg, nameof(NativeOutput.mpg123_format_none)); + int resetStatus = @delegate.Invoke(handle); + if (resetStatus != (int)mpg123_errors.MPG123_OK) + throw new BasoliaException($"Can't reset output encoding", (mpg123_errors)resetStatus); + } + } + + /// + /// Makes the underlying media handler accept all formats + /// + /// Basolia instance that contains a valid handle + public static void AllFormats(BasoliaMedia? basolia) + { + InitBasolia.CheckInited(); + if (basolia is null) + throw new BasoliaException("Basolia instance is not provided", mpg123_errors.MPG123_BAD_HANDLE); + + // We're now entering the dangerous zone + unsafe + { + var handle = basolia._mpg123Handle; + + // Check for support + var @delegate = MpgNative.GetDelegate(MpgNative.libManagerMpg, nameof(NativeOutput.mpg123_format_all)); + int resetStatus = @delegate.Invoke(handle); + if (resetStatus != (int)mpg123_errors.MPG123_OK) + throw new BasoliaException($"Can't set output format", (mpg123_errors)resetStatus); + } + } + + /// + /// Makes the underlying media handler use this specific format + /// + /// Basolia instance that contains a valid handle + /// Rate + /// Encoding + /// Mono, stereo, or both? + public static void UseFormat(BasoliaMedia? basolia, long rate, ChannelCount channels, int encoding) + { + InitBasolia.CheckInited(); + if (basolia is null) + throw new BasoliaException("Basolia instance is not provided", mpg123_errors.MPG123_BAD_HANDLE); + + // We're now entering the dangerous zone + unsafe + { + var handle = basolia._mpg123Handle; + + // Check for support + var delegate2 = MpgNative.GetDelegate(MpgNative.libManagerMpg, nameof(NativeOutput.mpg123_format)); + int formatStatus = delegate2.Invoke(handle, rate, (int)channels, encoding); + if (formatStatus != (int)mpg123_errors.MPG123_OK) + throw new BasoliaException($"Can't set output encoding to {rate}, {channels}, {encoding}", (mpg123_errors)formatStatus); + } + } } } diff --git a/BassBoom.Basolia/Playback/PlaybackTools.cs b/BassBoom.Basolia/Playback/PlaybackTools.cs index ffb27b0..5db1c8b 100644 --- a/BassBoom.Basolia/Playback/PlaybackTools.cs +++ b/BassBoom.Basolia/Playback/PlaybackTools.cs @@ -113,18 +113,12 @@ public static void Play(BasoliaMedia? basolia) var handle = basolia._mpg123Handle; var outHandle = basolia._out123Handle; - // First, get formats and reset them + // Reset the format. Orders here matter. var (rate, channels, encoding) = FormatTools.GetFormatInfo(basolia); - var @delegate = MpgNative.GetDelegate(MpgNative.libManagerMpg, nameof(NativeOutput.mpg123_format_none)); - int resetStatus = @delegate.Invoke(handle); - if (resetStatus != (int)mpg123_errors.MPG123_OK) - throw new BasoliaException($"Can't reset output encoding", (mpg123_errors)resetStatus); + FormatTools.NoFormat(basolia); // Set the format - var delegate2 = MpgNative.GetDelegate(MpgNative.libManagerMpg, nameof(NativeOutput.mpg123_format)); - int formatStatus = delegate2.Invoke(handle, rate, channels, encoding); - if (formatStatus != (int)mpg123_errors.MPG123_OK) - throw new BasoliaException($"Can't set output encoding", (mpg123_errors)formatStatus); + FormatTools.UseFormat(basolia, rate, (ChannelCount)channels, encoding); Debug.WriteLine($"Format {rate}, {channels}, {encoding}"); // Try to open output to device diff --git a/BassBoom.Cli/CliBase/Common.cs b/BassBoom.Cli/CliBase/Common.cs index 60a4cfd..fc00d08 100644 --- a/BassBoom.Cli/CliBase/Common.cs +++ b/BassBoom.Cli/CliBase/Common.cs @@ -149,9 +149,15 @@ internal static void ShowSpecs() // Get the name and the description string name = FormatTools.GetEncodingName(encoding); string desc = FormatTools.GetEncodingDescription(encoding); + int size = FormatTools.GetEncodingSize(encoding); - encodingsBuilder.AppendLine($" - {name} [{encoding}]: {desc}"); + encodingsBuilder.AppendLine($" - {name} [{encoding}, {size} bytes]: {desc}"); } + + var ratesBuilder = new StringBuilder(); + int[] rates = FormatTools.GetRates(); + foreach (int rate in rates) + ratesBuilder.AppendLine($" - {rate} hertz"); InfoBoxColor.WriteInfoBox( $$""" @@ -177,8 +183,7 @@ Encodings and Rates Encodings: {{encodingsBuilder}} Rates: - - {{string.Join("\n - ", FormatTools.GetRates())}} - + {{ratesBuilder}} System specifications =====================