[PATCH 1/2] alsa-mixer: Implement constant volume.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This change makes it possible to configure an arbitrary constant volume for a
volume element in the path configuration, which is applied when the path is
selected. Note: this is only useful when the exact hardware and driver are
known beforehand.
---
 src/modules/alsa/alsa-mixer.c                      |   46 +++++++++++++++++++-
 src/modules/alsa/alsa-mixer.h                      |    9 +++-
 .../alsa/mixer/paths/analog-output.conf.common     |   16 ++++---
 3 files changed, 59 insertions(+), 12 deletions(-)

diff --git a/src/modules/alsa/alsa-mixer.c b/src/modules/alsa/alsa-mixer.c
index 3eef5f9..2893b47 100644
--- a/src/modules/alsa/alsa-mixer.c
+++ b/src/modules/alsa/alsa-mixer.c
@@ -1145,6 +1145,31 @@ static int element_zero_volume(pa_alsa_element *e, snd_mixer_t *m) {
     return r;
 }
 
+static int element_apply_constant_volume(pa_alsa_element *e, snd_mixer_t *m) {
+    snd_mixer_elem_t *me;
+    snd_mixer_selem_id_t *sid;
+    int r;
+
+    pa_assert(m);
+    pa_assert(e);
+
+    SELEM_INIT(sid, e->alsa_name);
+    if (!(me = snd_mixer_find_selem(m, sid))) {
+        pa_log_warn("Element %s seems to have disappeared.", e->alsa_name);
+        return -1;
+    }
+
+    if (e->direction == PA_ALSA_DIRECTION_OUTPUT)
+        r = snd_mixer_selem_set_playback_volume_all(me, e->constant_volume);
+    else
+        r = snd_mixer_selem_set_capture_volume_all(me, e->constant_volume);
+
+    if (r < 0)
+        pa_log_warn("Failed to set volume to %li of %s: %s", e->constant_volume, e->alsa_name, pa_alsa_strerror(errno));
+
+    return r;
+}
+
 int pa_alsa_path_select(pa_alsa_path *p, snd_mixer_t *m) {
     pa_alsa_element *e;
     int r = 0;
@@ -1185,6 +1210,10 @@ int pa_alsa_path_select(pa_alsa_path *p, snd_mixer_t *m) {
                 r = element_zero_volume(e, m);
                 break;
 
+            case PA_ALSA_VOLUME_CONSTANT:
+                r = element_apply_constant_volume(e, m);
+                break;
+
             case PA_ALSA_VOLUME_MERGE:
             case PA_ALSA_VOLUME_IGNORE:
                 r = 0;
@@ -1368,6 +1397,12 @@ static int element_probe(pa_alsa_element *e, snd_mixer_t *m) {
                 pa_log_warn("Your kernel driver is broken: it reports a volume range from %li to %li which makes no sense.", e->min_volume, e->max_volume);
                 e->volume_use = PA_ALSA_VOLUME_IGNORE;
 
+            } else if (e->volume_use == PA_ALSA_VOLUME_CONSTANT &&
+                       (e->min_volume > e->constant_volume || e->max_volume < e->constant_volume)) {
+                pa_log_warn("Constant volume %li configured for element %s, but the available range is from %li to %li.",
+                            e->constant_volume, e->alsa_name, e->min_volume, e->max_volume);
+                e->volume_use = PA_ALSA_VOLUME_IGNORE;
+
             } else {
                 pa_bool_t is_mono;
                 pa_channel_position_t p;
@@ -1685,8 +1720,15 @@ static int element_parse_volume(
     else if (pa_streq(rvalue, "zero"))
         e->volume_use = PA_ALSA_VOLUME_ZERO;
     else {
-        pa_log("[%s:%u] Volume invalid of '%s'", filename, line, section);
-        return -1;
+        uint32_t constant;
+
+        if (pa_atou(rvalue, &constant) >= 0) {
+            e->volume_use = PA_ALSA_VOLUME_CONSTANT;
+            e->constant_volume = constant;
+        } else {
+            pa_log("[%s:%u] Volume invalid of '%s'", filename, line, section);
+            return -1;
+        }
     }
 
     return 0;
diff --git a/src/modules/alsa/alsa-mixer.h b/src/modules/alsa/alsa-mixer.h
index c24a896..a29aed1 100644
--- a/src/modules/alsa/alsa-mixer.h
+++ b/src/modules/alsa/alsa-mixer.h
@@ -62,9 +62,10 @@ typedef enum pa_alsa_switch_use {
 
 typedef enum pa_alsa_volume_use {
     PA_ALSA_VOLUME_IGNORE,
-    PA_ALSA_VOLUME_MERGE,  /* merge this volume slider into the global volume slider */
-    PA_ALSA_VOLUME_OFF,    /* set this volume to minimal unconditionally */
-    PA_ALSA_VOLUME_ZERO    /* set this volume to 0dB unconditionally */
+    PA_ALSA_VOLUME_MERGE,   /* merge this volume slider into the global volume slider */
+    PA_ALSA_VOLUME_OFF,     /* set this volume to minimal unconditionally */
+    PA_ALSA_VOLUME_ZERO,    /* set this volume to 0dB unconditionally */
+    PA_ALSA_VOLUME_CONSTANT /* set this volume to a constant value unconditionally */
 } pa_alsa_volume_use_t;
 
 typedef enum pa_alsa_enumeration_use {
@@ -137,6 +138,8 @@ struct pa_alsa_element {
     pa_alsa_required_t required_any;
     pa_alsa_required_t required_absent;
 
+    long constant_volume;
+
     pa_bool_t override_map:1;
     pa_bool_t direction_try_other:1;
 
diff --git a/src/modules/alsa/mixer/paths/analog-output.conf.common b/src/modules/alsa/mixer/paths/analog-output.conf.common
index c7c4435..ccaa494 100644
--- a/src/modules/alsa/mixer/paths/analog-output.conf.common
+++ b/src/modules/alsa/mixer/paths/analog-output.conf.common
@@ -75,13 +75,15 @@
 ; required-absent = ignore | switch | volume                  # If set, require this element to not be of this kind and not
 ;                                                             # available, otherwise don't consider this path valid for the card
 ;
-; switch = ignore | mute | off | on | select                  # What to do with this switch: ignore it, make it follow mute status,
-;                                                             # always set it to off, always to on, or make it selectable as port.
-;                                                             # If set to 'select' you need to define an Option section for on
-;                                                             # and off
-; volume = ignore | merge | off | zero   # What to do with this volume: ignore it, merge it into the device
-;                                        # volume slider, always set it to the lowest value possible, or always
-;                                        # set it to 0 dB (for whatever that means)
+; switch = ignore | mute | off | on | select # What to do with this switch: ignore it, make it follow mute status,
+;                                            # always set it to off, always to on, or make it selectable as port.
+;                                            # If set to 'select' you need to define an Option section for on
+;                                            # and off
+; volume = ignore | merge | off | zero | <volume step> # What to do with this volume: ignore it, merge it into the device
+;                                                      # volume slider, always set it to the lowest value possible, or always
+;                                                      # set it to 0 dB (for whatever that means), or always set it to
+;                                                      # <volume step> (this only makes sense in path configurations where
+;                                                      # the exact hardware and driver are known beforehand).
 ; volume-limit = <volume step>           # Limit the maximum volume by disabling the volume steps above <volume step>.
 ; enumeration = ignore | select          # What to do with this enumeration, ignore it or make it selectable
 ;                                        # via device ports. If set to 'select' you need to define an Option section
-- 
1.7.4.1




[Index of Archives]     [Linux Audio Users]     [AMD Graphics]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux