Skip to content

Commit

Permalink
add - Implemented volume manipulation
Browse files Browse the repository at this point in the history
We've added volume manipulation

---

Added volume manipulation to the Basolia library and to BassBoom GUI and CLI

---

Type: add
Breaking: False
Doc Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Sep 5, 2023
1 parent b0df334 commit 0362535
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
42 changes: 42 additions & 0 deletions BassBoom.Basolia/Playback/PlaybackTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using System.Diagnostics;
using System.Threading.Tasks;
using BassBoom.Basolia.Devices;
using System.Runtime.InteropServices;

namespace BassBoom.Basolia.Playback
{
Expand Down Expand Up @@ -140,5 +141,46 @@ public static void Stop()
state = PlaybackState.Stopped;
PlaybackPositioningTools.SeekToTheBeginning();
}

public static void SetVolume(double volume)
{
InitBasolia.CheckInited();

// Check the volume
if (volume < 0)
volume = 0;
if (volume > 1)
volume = 1;

// Try to set the volume
unsafe
{
var handle = Mpg123Instance._mpg123Handle;
int status = NativeVolume.mpg123_volume(handle, volume);
if (status != (int)out123_error.OUT123_OK)
throw new BasoliaOutException($"Can't set volume to {volume}", (out123_error)status);
}
}

public static (double baseLinear, double actualLinear, double decibelsRva) GetVolume()
{
InitBasolia.CheckInited();

double baseLinearAddr = 0;
double actualLinearAddr = 0;
double decibelsRvaAddr = 0;

// Try to get the volume
unsafe
{
var handle = Mpg123Instance._mpg123Handle;
int status = NativeVolume.mpg123_getvolume(handle, ref baseLinearAddr, ref actualLinearAddr, ref decibelsRvaAddr);
if (status != (int)out123_error.OUT123_OK)
throw new BasoliaOutException($"Can't get volume (base, really, and decibels)", (out123_error)status);
}

// Get the volume information
return (baseLinearAddr, actualLinearAddr, decibelsRvaAddr);
}
}
}
27 changes: 26 additions & 1 deletion BassBoom.Cli/CliBase/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static void PlayerLoop(string musicPath)
InitBasolia.Init();
FileTools.OpenFile(musicPath);
int total = AudioInfoTools.GetDuration(true);
double volume = PlaybackTools.GetVolume().baseLinear;

// First, clear the screen to draw our TUI
while (!exiting)
Expand All @@ -58,7 +59,7 @@ public static void PlayerLoop(string musicPath)
}

// First, print the keystrokes
string keystrokes = "[SPACE] Play/Pause - [ESC] Stop - [Q] Exit";
string keystrokes = "[SPACE] Play/Pause - [ESC] Stop - [Q] Exit - [UP/DOWN] Vol";
CenteredTextColor.WriteCentered(ConsoleTools.ActionWindowHeight() - 2, keystrokes);

// Print the separator
Expand All @@ -82,6 +83,18 @@ public static void PlayerLoop(string musicPath)
var keystroke = Input.DetectKeypress().Key;
switch (keystroke)
{
case ConsoleKey.UpArrow:
volume += 0.05;
if (volume > 1)
volume = 1;
PlaybackTools.SetVolume(volume);
break;
case ConsoleKey.DownArrow:
volume -= 0.05;
if (volume < 0)
volume = 0;
PlaybackTools.SetVolume(volume);
break;
case ConsoleKey.Spacebar:
PlaybackTools.Pause();
break;
Expand All @@ -101,6 +114,18 @@ public static void PlayerLoop(string musicPath)
var keystroke = Input.DetectKeypress().Key;
switch (keystroke)
{
case ConsoleKey.UpArrow:
volume += 0.05;
if (volume > 1)
volume = 1;
PlaybackTools.SetVolume(volume);
break;
case ConsoleKey.DownArrow:
volume -= 0.05;
if (volume < 0)
volume = 0;
PlaybackTools.SetVolume(volume);
break;
case ConsoleKey.Spacebar:
if (PlaybackTools.State == PlaybackState.Stopped)
// There could be a chance that the music has fully stopped without any user interaction.
Expand Down
3 changes: 2 additions & 1 deletion BassBoom.Native/Interop/Play/NativeVolume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// 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 System;
using System.Runtime.InteropServices;
using BassBoom.Native.Interop.Init;

Expand Down Expand Up @@ -102,6 +103,6 @@ public static unsafe class NativeVolume
/// MPG123_EXPORT int mpg123_getvolume(mpg123_handle *mh, double *base, double *really, double *rva_db);
/// </summary>
[DllImport(LibraryTools.LibraryName, CharSet = CharSet.Ansi)]
internal static extern int mpg123_getvolume(mpg123_handle* mh, double* @base, double* really, double* rva_db);
internal static extern int mpg123_getvolume(mpg123_handle* mh, ref double @base, ref double really, ref double rva_db);
}
}
1 change: 1 addition & 0 deletions BassBoom/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
ColumnDefinitions="Auto, Auto, Auto, Auto, Auto, Auto"
Margin="15">
<CheckBox Grid.Row="1" Name="DetermineDevice" IsChecked="true">Determine device and driver</CheckBox>
<Slider Name="volumeSlider" Minimum="0" Maximum="1" Value="0.5"></Slider>
</Grid>
</StackPanel>
<StackPanel
Expand Down
8 changes: 8 additions & 0 deletions BassBoom/Views/MainView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public MainView()
PathToMp3.TextChanged += CheckPath;
DetermineDevice.IsCheckedChanged += MakeDeviceDeterministic;
durationRemain.ValueChanged += HandleDurationValueChange;
volumeSlider.ValueChanged += HandleVolumeValueChange;
volumeSlider.Value = PlaybackTools.GetVolume().baseLinear;
}

internal void EnablePlay()
Expand Down Expand Up @@ -81,6 +83,12 @@ private void HandleDurationValueChange(object sender, RangeBaseValueChangedEvent
Debug.WriteLine($"Changed. {e.OldValue} -> {e.NewValue}");
PlaybackPositioningTools.SeekToFrame((int)e.NewValue);
}

private void HandleVolumeValueChange(object sender, RangeBaseValueChangedEventArgs e)
{
Debug.WriteLine($"Vol. changed. {e.OldValue} -> {e.NewValue}");
PlaybackTools.SetVolume(e.NewValue);
}
}

public class BassBoomData
Expand Down

0 comments on commit 0362535

Please sign in to comment.