mixer: restore volume with audio output drivers without persistent volume

Some audio outputs don't provide access to a system-wide mixer control, and
do per-application audio mixing. Further, some of these forget the volume
as soon as the audio device is closed. This can be annoying, because
mplayer will "forget" the volume when playing a new file or when crossing
ordered chapter boundaries. Support restoring the volume on audio
reinitialization if an audio output driver knowingly behaves this way.
(This doesn't change that mplayer never writes any settings into the config
file, including volume settings.)

This commit doesn't yet change any actual output driver to use this code.

Hopefully make some logic in the volume restore code a bit more robust.
This commit is contained in:
wm4 2012-01-06 16:23:43 +01:00
parent 95c52f865d
commit 29effe9593
3 changed files with 35 additions and 20 deletions

View File

@ -78,6 +78,7 @@ struct ao {
int buffer_playable_size;
bool initialized;
bool untimed;
bool no_persistent_volume;
const struct ao_driver *driver;
void *priv;
};

35
mixer.c
View File

@ -38,12 +38,14 @@ float soft_vol_max = 110.0;
// Called after the audio filter chain is built or rebuilt.
void mixer_reinit(mixer_t *mixer)
{
if (mixer->restore_softvol) {
if (!mixer->ao)
return;
if (mixer->restore_volume) {
int muted = mixer->muted;
mixer_setvolume(mixer, mixer->softvol_l, mixer->softvol_r);
mixer_setvolume(mixer, mixer->restore_vol_l, mixer->restore_vol_r);
mixer->muted = muted;
}
if (mixer->balance != 0) {
if (mixer->restore_balance) {
mixer_setbalance(mixer, mixer->balance);
}
}
@ -81,17 +83,21 @@ void mixer_setvolume(mixer_t *mixer, float l, float r)
vol.right = r;
vol.left = l;
if (mixer->ao) {
if (soft_vol ||
CONTROL_OK != ao_control(mixer->ao, AOCONTROL_SET_VOLUME, &vol))
bool use_softvol = soft_vol;
if (!use_softvol) {
if (CONTROL_OK != ao_control(mixer->ao, AOCONTROL_SET_VOLUME, &vol))
{
use_softvol = true;
} else {
mixer->restore_volume = mixer->ao->no_persistent_volume;
}
}
if (use_softvol) {
if (!mixer->afilter)
return;
// af_volume uses values in dB
float db_vals[AF_NCH];
int i;
mixer->softvol_l = l;
mixer->softvol_r = r;
mixer->restore_softvol = 1;
db_vals[0] = (l / 100.0) * (soft_vol_max / 100.0);
db_vals[1] = (r / 100.0) * (soft_vol_max / 100.0);
for (i = 2; i < AF_NCH; i++)
@ -102,16 +108,20 @@ void mixer_setvolume(mixer_t *mixer, float l, float r)
{
mp_tmsg(MSGT_GLOBAL, MSGL_INFO,
"[Mixer] No hardware mixing, inserting volume filter.\n");
if (af_add(mixer->afilter, "volume")) {
if (!af_control_any_rev(mixer->afilter,
AF_CONTROL_VOLUME_LEVEL | AF_CONTROL_SET, db_vals))
if (!(af_add(mixer->afilter, "volume")
&& af_control_any_rev(mixer->afilter,
AF_CONTROL_VOLUME_LEVEL | AF_CONTROL_SET, db_vals)))
{
mp_tmsg(MSGT_GLOBAL, MSGL_ERR,
"[Mixer] No volume control available.\n");
return;
}
}
mixer->restore_volume = true;
}
if (mixer->restore_volume) {
mixer->restore_vol_l = l;
mixer->restore_vol_r = r;
}
}
mixer->muted = 0;
@ -181,6 +191,7 @@ void mixer_setbalance(mixer_t *mixer, float val)
return;
mixer->balance = val;
mixer->restore_balance = true;
if (af_control_any_rev(mixer->afilter,
AF_CONTROL_PAN_BALANCE | AF_CONTROL_SET, &val))
@ -189,7 +200,7 @@ void mixer_setbalance(mixer_t *mixer, float val)
if (!(af_pan_balance = af_add(mixer->afilter, "pan"))) {
mp_tmsg(MSGT_GLOBAL, MSGL_ERR,
"[Mixer] No balance control available.\n");
mixer->balance = 0;
mixer->restore_balance = false;
return;
}

View File

@ -19,6 +19,8 @@
#ifndef MPLAYER_MIXER_H
#define MPLAYER_MIXER_H
#include <stdbool.h>
#include "libaf/af.h"
#include "libao2/audio_out.h"
@ -33,9 +35,10 @@ typedef struct mixer_s {
int volstep;
int muted;
float last_l, last_r;
float softvol_l, softvol_r;
int restore_softvol;
float restore_vol_l, restore_vol_r;
bool restore_volume;
float balance;
bool restore_balance;
} mixer_t;
void mixer_reinit(mixer_t *mixer);