diff options
author | wm4 <wm4@mplayer2.org> | 2012-01-06 16:23:43 +0100 |
---|---|---|
committer | wm4 <wm4@mplayer2.org> | 2012-01-18 04:21:45 +0100 |
commit | 29effe9593465f9096227edb79e09e51ca8a054d (patch) | |
tree | 5d451166dcaef5cd3c7d707195ca818e5f47f54b | |
parent | 95c52f865da2459f47ed708c41324f0cc5386da3 (diff) | |
download | mpv-29effe9593465f9096227edb79e09e51ca8a054d.tar.bz2 mpv-29effe9593465f9096227edb79e09e51ca8a054d.tar.xz |
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.
-rw-r--r-- | libao2/audio_out.h | 1 | ||||
-rw-r--r-- | mixer.c | 47 | ||||
-rw-r--r-- | mixer.h | 7 |
3 files changed, 35 insertions, 20 deletions
diff --git a/libao2/audio_out.h b/libao2/audio_out.h index e96e123700..a8efc92069 100644 --- a/libao2/audio_out.h +++ b/libao2/audio_out.h @@ -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; }; @@ -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)) - { - mp_tmsg(MSGT_GLOBAL, MSGL_ERR, - "[Mixer] No volume control available.\n"); - return; - } + 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; } @@ -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); |