summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Weißschuh <thomas@t-8ch.de>2022-12-31 03:46:44 +0000
committerPhilip Langdale <github.philipl@overt.org>2023-01-25 15:49:21 -0800
commit870512eb84a5b8bc4d4fdd794eb11bb3598e234b (patch)
treec68417de22549377028006b72b51657535ecd3b7
parent5510d9f6632c009c398ce48a4d5a89c7f95efc96 (diff)
downloadmpv-870512eb84a5b8bc4d4fdd794eb11bb3598e234b.tar.bz2
mpv-870512eb84a5b8bc4d4fdd794eb11bb3598e234b.tar.xz
audio: simplify implementation of property ao-volume
ao-volume is represented in the code with a `struct ao_control_vol_t` which contains volumes for two channels, left and right. However the code implementing this property in command.c never treats these values individually. They are always averaged together. On the other hand the code in the AOs handling these values also has to handle the case where *not* exactly two channels are handled. So let's remove the `struct ao_control_vol_t` and replace it with a simple float. This makes the semantics clear to AO authors and allows us to drop some code from the AOs and command.c.
-rw-r--r--audio/out/ao.h8
-rw-r--r--audio/out/ao_alsa.c16
-rw-r--r--audio/out/ao_coreaudio.c8
-rw-r--r--audio/out/ao_openal.c6
-rw-r--r--audio/out/ao_oss.c7
-rw-r--r--audio/out/ao_pipewire.c28
-rw-r--r--audio/out/ao_pulse.c19
-rw-r--r--audio/out/ao_sndio.c6
-rw-r--r--audio/out/ao_wasapi.c14
-rw-r--r--player/command.c11
10 files changed, 42 insertions, 81 deletions
diff --git a/audio/out/ao.h b/audio/out/ao.h
index 1bca124599..0ff4902021 100644
--- a/audio/out/ao.h
+++ b/audio/out/ao.h
@@ -26,8 +26,7 @@
#include "audio/chmap_sel.h"
enum aocontrol {
- // _VOLUME commands take struct ao_control_vol pointer for input/output.
- // If there's only one volume, SET should use average of left/right.
+ // _VOLUME commands take a pointer to float for input/output.
AOCONTROL_GET_VOLUME,
AOCONTROL_SET_VOLUME,
// _MUTE commands take a pointer to bool
@@ -61,11 +60,6 @@ enum {
AO_INIT_EXCLUSIVE = 1 << 3,
};
-typedef struct ao_control_vol {
- float left;
- float right;
-} ao_control_vol_t;
-
enum aocontrol_media_role {
AOCONTROL_MEDIA_ROLE_MUSIC,
AOCONTROL_MEDIA_ROLE_MOVIE,
diff --git a/audio/out/ao_alsa.c b/audio/out/ao_alsa.c
index 617209dd43..90a3fd0a46 100644
--- a/audio/out/ao_alsa.c
+++ b/audio/out/ao_alsa.c
@@ -168,15 +168,13 @@ static int control(struct ao *ao, enum aocontrol cmd, void *arg)
switch (cmd) {
case AOCONTROL_SET_VOLUME: {
- ao_control_vol_t *vol = arg;
- set_vol = vol->left / f_multi + pmin + 0.5;
+ float *vol = arg;
+ set_vol = *vol / f_multi + pmin + 0.5;
err = snd_mixer_selem_set_playback_volume(elem, 0, set_vol);
CHECK_ALSA_ERROR("Error setting left channel");
MP_DBG(ao, "left=%li, ", set_vol);
- set_vol = vol->right / f_multi + pmin + 0.5;
-
err = snd_mixer_selem_set_playback_volume(elem, 1, set_vol);
CHECK_ALSA_ERROR("Error setting right channel");
MP_DBG(ao, "right=%li, pmin=%li, pmax=%li, mult=%f\n",
@@ -184,12 +182,14 @@ static int control(struct ao *ao, enum aocontrol cmd, void *arg)
break;
}
case AOCONTROL_GET_VOLUME: {
- ao_control_vol_t *vol = arg;
+ float *vol = arg;
+ float left, right;
snd_mixer_selem_get_playback_volume(elem, 0, &get_vol);
- vol->left = (get_vol - pmin) * f_multi;
+ left = (get_vol - pmin) * f_multi;
snd_mixer_selem_get_playback_volume(elem, 1, &get_vol);
- vol->right = (get_vol - pmin) * f_multi;
- MP_DBG(ao, "left=%f, right=%f\n", vol->left, vol->right);
+ right = (get_vol - pmin) * f_multi;
+ *vol = (left + right) / 2.0;
+ MP_DBG(ao, "vol=%f\n", *vol);
break;
}
case AOCONTROL_SET_MUTE: {
diff --git a/audio/out/ao_coreaudio.c b/audio/out/ao_coreaudio.c
index 7699162d15..67eb07be4c 100644
--- a/audio/out/ao_coreaudio.c
+++ b/audio/out/ao_coreaudio.c
@@ -83,7 +83,7 @@ static OSStatus render_cb_lpcm(void *ctx, AudioUnitRenderActionFlags *aflags,
return noErr;
}
-static int get_volume(struct ao *ao, struct ao_control_vol *vol) {
+static int get_volume(struct ao *ao, float *vol) {
struct priv *p = ao->priv;
float auvol;
OSStatus err =
@@ -91,15 +91,15 @@ static int get_volume(struct ao *ao, struct ao_control_vol *vol) {
kAudioUnitScope_Global, 0, &auvol);
CHECK_CA_ERROR("could not get HAL output volume");
- vol->left = vol->right = auvol * 100.0;
+ *vol = auvol * 100.0;
return CONTROL_TRUE;
coreaudio_error:
return CONTROL_ERROR;
}
-static int set_volume(struct ao *ao, struct ao_control_vol *vol) {
+static int set_volume(struct ao *ao, float *vol) {
struct priv *p = ao->priv;
- float auvol = (vol->left + vol->right) / 200.0;
+ float auvol = *vol / 100.0;
OSStatus err =
AudioUnitSetParameter(p->audio_unit, kHALOutputParam_Volume,
kAudioUnitScope_Global, 0, auvol, 0);
diff --git a/audio/out/ao_openal.c b/audio/out/ao_openal.c
index 6b2e3bc176..5b353920f1 100644
--- a/audio/out/ao_openal.c
+++ b/audio/out/ao_openal.c
@@ -67,13 +67,13 @@ static int control(struct ao *ao, enum aocontrol cmd, void *arg)
case AOCONTROL_GET_VOLUME:
case AOCONTROL_SET_VOLUME: {
ALfloat volume;
- ao_control_vol_t *vol = (ao_control_vol_t *)arg;
+ float *vol = arg;
if (cmd == AOCONTROL_SET_VOLUME) {
- volume = (vol->left + vol->right) / 200.0;
+ volume = *vol / 100.0;
alListenerf(AL_GAIN, volume);
}
alGetListenerf(AL_GAIN, &volume);
- vol->left = vol->right = volume * 100;
+ *vol = volume * 100;
return CONTROL_TRUE;
}
case AOCONTROL_GET_MUTE:
diff --git a/audio/out/ao_oss.c b/audio/out/ao_oss.c
index 2d1a60db4d..b0077cc853 100644
--- a/audio/out/ao_oss.c
+++ b/audio/out/ao_oss.c
@@ -267,7 +267,7 @@ static void uninit(struct ao *ao)
static int control(struct ao *ao, enum aocontrol cmd, void *arg)
{
struct priv *p = ao->priv;
- ao_control_vol_t *vol = (ao_control_vol_t *)arg;
+ float *vol = arg;
int v;
if (p->dsp_fd < 0)
@@ -279,11 +279,10 @@ static int control(struct ao *ao, enum aocontrol cmd, void *arg)
MP_WARN_IOCTL_ERR(ao);
return CONTROL_ERROR;
}
- vol->right = ((v & 0xff00) >> 8);
- vol->left = (v & 0x00ff);
+ *vol = ((v & 0x00ff) + ((v & 0xff00) >> 8)) / 2.0;
return CONTROL_OK;
case AOCONTROL_SET_VOLUME:
- v = ((int)vol->right << 8) | (int)vol->left;
+ v = ((int)*vol << 8) | (int)*vol;
if (ioctl(p->dsp_fd, SNDCTL_DSP_SETPLAYVOL, &v) == -1) {
MP_WARN_IOCTL_ERR(ao);
return CONTROL_ERROR;
diff --git a/audio/out/ao_pipewire.c b/audio/out/ao_pipewire.c
index 4880e776f0..6b38eeb76c 100644
--- a/audio/out/ao_pipewire.c
+++ b/audio/out/ao_pipewire.c
@@ -63,7 +63,7 @@ struct priv {
struct spa_hook core_listener;
bool muted;
- float volume[2];
+ float volume;
struct {
int buffer_msec;
@@ -262,14 +262,8 @@ static void on_control_info(void *userdata, uint32_t id,
p->muted = control->values[0] >= 0.5;
break;
case SPA_PROP_channelVolumes:
- if (control->n_values == 2) {
- p->volume[0] = control->values[0];
- p->volume[1] = control->values[1];
- } else if (control->n_values > 0) {
- float volume = volume_avg(control->values, control->n_values);
- p->volume[0] = volume;
- p->volume[1] = volume;
- }
+ if (control->n_values > 0)
+ p->volume = volume_avg(control->values, control->n_values);
break;
}
}
@@ -608,9 +602,8 @@ static int control(struct ao *ao, enum aocontrol cmd, void *arg)
switch (cmd) {
case AOCONTROL_GET_VOLUME: {
- struct ao_control_vol *vol = arg;
- vol->left = spa_volume_to_mp_volume(p->volume[0]);
- vol->right = spa_volume_to_mp_volume(p->volume[1]);
+ float *vol = arg;
+ *vol = spa_volume_to_mp_volume(p->volume);
return CONTROL_OK;
}
case AOCONTROL_GET_MUTE: {
@@ -627,16 +620,11 @@ static int control(struct ao *ao, enum aocontrol cmd, void *arg)
pw_thread_loop_lock(p->loop);
switch (cmd) {
case AOCONTROL_SET_VOLUME: {
- struct ao_control_vol *vol = arg;
+ float *vol = arg;
uint8_t n = ao->channels.num;
float values[MP_NUM_CHANNELS] = {0};
- if (n == 2) {
- values[0] = mp_volume_to_spa_volume(vol->left);
- values[1] = mp_volume_to_spa_volume(vol->right);
- } else {
- for (int i = 0; i < n; i++)
- values[i] = mp_volume_to_spa_volume(vol->left);
- }
+ for (int i = 0; i < n; i++)
+ values[i] = mp_volume_to_spa_volume(*vol);
ret = CONTROL_RET(pw_stream_set_control(p->stream, SPA_PROP_channelVolumes, n, values, 0));
break;
}
diff --git a/audio/out/ao_pulse.c b/audio/out/ao_pulse.c
index 619824b954..a90f248962 100644
--- a/audio/out/ao_pulse.c
+++ b/audio/out/ao_pulse.c
@@ -680,14 +680,8 @@ static int control(struct ao *ao, enum aocontrol cmd, void *arg)
// we naively copied the struct, without updating pointers etc.
// Pointers might point to invalid data, accessors might fail.
if (cmd == AOCONTROL_GET_VOLUME) {
- ao_control_vol_t *vol = arg;
- if (priv->pi.volume.channels != 2)
- vol->left = vol->right =
- VOL_PA2MP(pa_cvolume_avg(&priv->pi.volume));
- else {
- vol->left = VOL_PA2MP(priv->pi.volume.values[0]);
- vol->right = VOL_PA2MP(priv->pi.volume.values[1]);
- }
+ float *vol = arg;
+ *vol = VOL_PA2MP(pa_cvolume_avg(&priv->pi.volume));
} else if (cmd == AOCONTROL_GET_MUTE) {
bool *mute = arg;
*mute = priv->pi.mute;
@@ -701,16 +695,11 @@ static int control(struct ao *ao, enum aocontrol cmd, void *arg)
priv->retval = 0;
uint32_t stream_index = pa_stream_get_index(priv->stream);
if (cmd == AOCONTROL_SET_VOLUME) {
- const ao_control_vol_t *vol = arg;
+ const float *vol = arg;
struct pa_cvolume volume;
pa_cvolume_reset(&volume, ao->channels.num);
- if (volume.channels != 2)
- pa_cvolume_set(&volume, volume.channels, VOL_MP2PA(vol->left));
- else {
- volume.values[0] = VOL_MP2PA(vol->left);
- volume.values[1] = VOL_MP2PA(vol->right);
- }
+ pa_cvolume_set(&volume, volume.channels, VOL_MP2PA(*vol));
if (!waitop(priv, pa_context_set_sink_input_volume(priv->context,
stream_index,
&volume,
diff --git a/audio/out/ao_sndio.c b/audio/out/ao_sndio.c
index 2233eadc93..ad23137c5a 100644
--- a/audio/out/ao_sndio.c
+++ b/audio/out/ao_sndio.c
@@ -211,18 +211,18 @@ static void uninit(struct ao *ao)
static int control(struct ao *ao, enum aocontrol cmd, void *arg)
{
struct priv *p = ao->priv;
- ao_control_vol_t *vol = arg;
+ float *vol = arg;
switch (cmd) {
case AOCONTROL_GET_VOLUME:
if (!p->havevol)
return CONTROL_FALSE;
- vol->left = vol->right = p->vol * 100 / SIO_MAXVOL;
+ *vol = p->vol * 100 / SIO_MAXVOL;
break;
case AOCONTROL_SET_VOLUME:
if (!p->havevol)
return CONTROL_FALSE;
- sio_setvol(p->hdl, vol->left * SIO_MAXVOL / 100);
+ sio_setvol(p->hdl, *vol * SIO_MAXVOL / 100);
break;
default:
return CONTROL_UNKNOWN;
diff --git a/audio/out/ao_wasapi.c b/audio/out/ao_wasapi.c
index da4a937c7d..57b9c7d87d 100644
--- a/audio/out/ao_wasapi.c
+++ b/audio/out/ao_wasapi.c
@@ -348,13 +348,10 @@ static int thread_control_exclusive(struct ao *ao, enum aocontrol cmd, void *arg
case AOCONTROL_GET_VOLUME:
IAudioEndpointVolume_GetMasterVolumeLevelScalar(
state->pEndpointVolume, &volume);
- *(ao_control_vol_t *)arg = (ao_control_vol_t){
- .left = 100.0f * volume,
- .right = 100.0f * volume,
- };
+ *(float *)arg = volume;
return CONTROL_OK;
case AOCONTROL_SET_VOLUME:
- volume = ((ao_control_vol_t *)arg)->left / 100.f;
+ volume = (*(float *)arg) / 100.f;
IAudioEndpointVolume_SetMasterVolumeLevelScalar(
state->pEndpointVolume, volume, NULL);
return CONTROL_OK;
@@ -381,13 +378,10 @@ static int thread_control_shared(struct ao *ao, enum aocontrol cmd, void *arg)
switch(cmd) {
case AOCONTROL_GET_VOLUME:
ISimpleAudioVolume_GetMasterVolume(state->pAudioVolume, &volume);
- *(ao_control_vol_t *)arg = (ao_control_vol_t){
- .left = 100.0f * volume,
- .right = 100.0f * volume,
- };
+ *(float *)arg = volume;
return CONTROL_OK;
case AOCONTROL_SET_VOLUME:
- volume = ((ao_control_vol_t *)arg)->left / 100.f;
+ volume = (*(float *)arg) / 100.f;
ISimpleAudioVolume_SetMasterVolume(state->pAudioVolume, volume, NULL);
return CONTROL_OK;
case AOCONTROL_GET_MUTE:
diff --git a/player/command.c b/player/command.c
index 9ade4c08df..c3a6729805 100644
--- a/player/command.c
+++ b/player/command.c
@@ -1616,17 +1616,14 @@ static int mp_property_ao_volume(void *ctx, struct m_property *prop,
switch (action) {
case M_PROPERTY_SET: {
- float value = *(float *)arg;
- ao_control_vol_t vol = {value, value};
+ float vol = *(float *)arg;
if (ao_control(ao, AOCONTROL_SET_VOLUME, &vol) != CONTROL_OK)
return M_PROPERTY_UNAVAILABLE;
return M_PROPERTY_OK;
}
case M_PROPERTY_GET: {
- ao_control_vol_t vol = {0};
- if (ao_control(ao, AOCONTROL_GET_VOLUME, &vol) != CONTROL_OK)
+ if (ao_control(ao, AOCONTROL_GET_VOLUME, arg) != CONTROL_OK)
return M_PROPERTY_UNAVAILABLE;
- *(float *)arg = (vol.left + vol.right) / 2.0f;
return M_PROPERTY_OK;
}
case M_PROPERTY_GET_TYPE:
@@ -1637,10 +1634,10 @@ static int mp_property_ao_volume(void *ctx, struct m_property *prop,
};
return M_PROPERTY_OK;
case M_PROPERTY_PRINT: {
- ao_control_vol_t vol = {0};
+ float vol = 0;
if (ao_control(ao, AOCONTROL_GET_VOLUME, &vol) != CONTROL_OK)
return M_PROPERTY_UNAVAILABLE;
- *(char **)arg = talloc_asprintf(NULL, "%.f", (vol.left + vol.right) / 2.0f);
+ *(char **)arg = talloc_asprintf(NULL, "%.f", vol);
return M_PROPERTY_OK;
}
}