Skip to content

Commit

Permalink
volume control only for kew
Browse files Browse the repository at this point in the history
  • Loading branch information
ravachol committed Dec 4, 2023
1 parent a498a8f commit a6465b4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 36 deletions.
9 changes: 7 additions & 2 deletions src/soundgapless.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ void createDevice(UserData *userData, ma_device *device, ma_context *context, ma
result = ma_device_init(context, &deviceConfig, device);
if (result != MA_SUCCESS)
return;

setVolume(getCurrentVolume());

result = ma_device_start(device);
if (result != MA_SUCCESS)
return;
Expand Down Expand Up @@ -134,7 +137,8 @@ void vorbis_createAudioDevice(UserData *userData, ma_device *device, ma_context
return;
}

// Start the device again
setVolume(getCurrentVolume());

result = ma_device_start(device);
if (result != MA_SUCCESS)
{
Expand Down Expand Up @@ -165,7 +169,8 @@ void opus_createAudioDevice(UserData *userData, ma_device *device, ma_context *c
return;
}

// Start the device again
setVolume(getCurrentVolume());

result = ma_device_start(device);
if (result != MA_SUCCESS)
{
Expand Down
1 change: 1 addition & 0 deletions src/soundgapless.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "soundopus.h"
#include "soundvorbis.h"
#include "soundpcm.h"
#include "volume.h"

#ifndef USERDATA_STRUCT
#define USERDATA_STRUCT
Expand Down
4 changes: 2 additions & 2 deletions src/visuals.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
int bufferSize = 4800;
int prevBufferSize = 0;
float magnitudeCeil = 120;
float alpha = 0.2;
float lastMax = 60;
float alpha = 0.1;
float lastMax = 120;
bool unicodeSupport = false;
fftwf_complex *fftInput = NULL;
fftwf_complex *fftOutput = NULL;
Expand Down
51 changes: 19 additions & 32 deletions src/volume.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@
volume.c
Functions related to volume control.
*/

int soundVolume = 100;

int getCurrentVolume()
{
return soundVolume;
}

int getSystemVolume()
{
FILE *fp;
char command_str[1000];
Expand All @@ -33,10 +41,6 @@ int getCurrentVolume()

void setVolume(int volume)
{
char command_str[1000];
FILE *fp;

// Limit new volume to a maximum of 100%
if (volume > 100)
{
volume = 100;
Expand All @@ -46,42 +50,25 @@ void setVolume(int volume)
volume = 0;
}

snprintf(command_str, 1000, "pactl set-sink-volume @DEFAULT_SINK@ %d%%", volume);
soundVolume = volume;

fp = popen(command_str, "r");
if (fp == NULL)
{
return;
}
pclose(fp);
ma_device_set_master_volume(getDevice(), (float)volume / 100);
}

int adjustVolumePercent(int volumeChange)
{
char command_str[1000];
FILE *fp;
int currentVolume = getCurrentVolume();
int sysVol = getSystemVolume();

int newVolume = currentVolume + volumeChange;
if (sysVol == 0)
return 0;

// Limit new volume to a maximum of 100%
if (newVolume > 100)
{
newVolume = 100;
}
else if (newVolume < 0)
{
newVolume = 0;
}
int step = 100 / sysVol * 5;

snprintf(command_str, 1000, "pactl set-sink-volume @DEFAULT_SINK@ %d%%", newVolume);
int relativeVolChange = volumeChange / 5 * step;

fp = popen(command_str, "r");
if (fp == NULL)
{
return -1;
}
pclose(fp);
soundVolume += relativeVolChange;

setVolume(soundVolume);

return 0;
}
1 change: 1 addition & 0 deletions src/volume.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include <stdio.h>
#include "soundcommon.h"

int getCurrentVolume(void);

Expand Down

0 comments on commit a6465b4

Please sign in to comment.