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.
This commit is contained in:
Thomas Weißschuh 2022-12-31 03:46:44 +00:00 committed by Philip Langdale
parent 5510d9f663
commit 870512eb84
10 changed files with 42 additions and 81 deletions

View File

@ -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,

View File

@ -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: {

View File

@ -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);

View File

@ -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:

View File

@ -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;

View File

@ -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;
}

View File

@ -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,

View File

@ -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;

View File

@ -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:

View File

@ -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;
}
}