]> mj.ucw.cz Git - ursary.git/commitdiff
Reformed volume handling
authorMartin Mares <mj@ucw.cz>
Sun, 9 Nov 2014 18:49:19 +0000 (19:49 +0100)
committerMartin Mares <mj@ucw.cz>
Sun, 9 Nov 2014 23:37:27 +0000 (00:37 +0100)
ursaryd.c

index bb057e16a1f105278bbb048d4fe24487d55bfe99..ec38664c71a549efb8442f483b9fa7f220c21d1d 100644 (file)
--- a/ursaryd.c
+++ b/ursaryd.c
@@ -392,6 +392,16 @@ static void pulse_init(void)
 
 static struct main_timer update_timer;
 
+static double volume_from_pa(pa_volume_t vol)
+{
+  return (double) vol / PA_VOLUME_NORM;
+}
+
+static pa_volume_t volume_to_pa(double vol)
+{
+  return vol * PA_VOLUME_NORM + 0.0001;
+}
+
 static void update_ring_from_sink(int ring, const char *sink_name)
 {
   struct pulse_sink *s = pulse_sink_by_name(sink_name);
@@ -409,14 +419,28 @@ static void update_ring_from_sink(int ring, const char *sink_name)
       return;
     }
 
-  double vol = pa_sw_volume_to_linear(s->volume);
-  vol = CLAMP(vol, 0, 1);
-  int val = 0x7f * vol;
-  val = CLAMP(val, 12, 0x7f);
-  noct_set_ring(ring, RING_MODE_LEFT, val);
+  double vol = CLAMP(volume_from_pa(s->volume), 0, 1);
+  noct_set_ring(ring, RING_MODE_LEFT, CLAMP((int)(0x7f * vol), 12, 0x7f));
   noct_set_button(ring, 0);
 }
 
+static void update_sink_from_rotary(int delta, const char *sink_name)
+{
+  struct pulse_sink *s = pulse_sink_by_name(sink_name);
+  if (!s)
+    return;
+
+  double vol = volume_from_pa(s->volume) + delta * 0.02;
+  pa_volume_t pavol = volume_to_pa(CLAMP(vol, 0, 1));
+  if (pavol == s->volume)
+    return;
+  pa_cvolume cvol;
+  pa_cvolume_set(&cvol, s->channels, pavol);
+
+  DBG("## Setting volume of sink %s to %d", s->name, cvol.values[0]);
+  PULSE_ASYNC_RUN(pa_context_set_sink_volume_by_index, pulse_ctx, s->idx, &cvol, pulse_success_cb);
+}
+
 struct client_map {
   int rotary;
   const char *client;
@@ -428,6 +452,7 @@ static struct client_map client_map[] = {
   { 4, "Music Player Daemon",  "albireo",      1 },
   { 5, "MPlayer",              NULL,           1 },
   { 6, NULL,                   "ogion",        1 },
+  { 7, NULL,                   "ursula",       1 },
 };
 
 #define NUM_CLIENTS ARRAY_SIZE(client_map)
@@ -501,8 +526,7 @@ static void update_clients(void)
        }
       else
        {
-         double vol = pa_sw_volume_to_linear(cs->volume);
-         vol = CLAMP(vol, 0, cm->range);
+         double vol = CLAMP(volume_from_pa(cs->volume), 0, cm->range);
          int val = 0x7f * vol / cm->range;
          val = CLAMP(val, 12, 0x7f);
          noct_set_ring(cm->rotary, RING_MODE_LEFT, val);
@@ -511,6 +535,31 @@ static void update_clients(void)
     }
 }
 
+static void update_client_from_rotary(int rotary, int delta)
+{
+  int i = find_client_by_rotary(rotary);
+  if (i < 0)
+    return;
+  struct client_map *cm = &client_map[i];
+  struct client_state *cs = &client_state[i];
+
+  calc_clients();
+  double vol = volume_from_pa(cs->volume) + delta*0.02;
+  pa_volume_t pavol = volume_to_pa(CLAMP(vol, 0, cm->range));
+
+  HASH_FOR_ALL(pulse_sink_input, s)
+    {
+      if (s->noct_client_idx == i && s->volume != pavol)
+       {
+         DBG("@@ Client #%d, sink input #%d: setting volume=%u", s->client_idx, s->idx, pavol);
+         pa_cvolume cvol;
+         pa_cvolume_set(&cvol, s->channels, pavol);
+         PULSE_ASYNC_RUN(pa_context_set_sink_input_volume, pulse_ctx, s->idx, &cvol, pulse_success_cb);
+       }
+    }
+  HASH_END_FOR;
+}
+
 static void do_update(struct main_timer *t)
 {
   timer_del(t);
@@ -557,52 +606,6 @@ void schedule_update(void)
   timer_add_rel(&update_timer, 10);
 }
 
-static void update_sink_from_rotary(int delta, const char *sink_name)
-{
-  struct pulse_sink *s = pulse_sink_by_name(sink_name);
-  if (!s)
-    return;
-
-  double vol = pa_sw_volume_to_linear(s->volume);
-  vol += delta * 0.02;
-  vol = CLAMP(vol, 0, 1);
-  pa_volume_t pavol = pa_sw_volume_from_linear(vol);
-  if (pavol == s->volume)
-    return;
-  pa_cvolume cvol;
-  pa_cvolume_set(&cvol, s->channels, pavol);
-
-  DBG("## Setting volume of sink %s to %d", s->name, cvol.values[0]);
-  PULSE_ASYNC_RUN(pa_context_set_sink_volume_by_index, pulse_ctx, s->idx, &cvol, pulse_success_cb);
-}
-
-static void update_client_from_rotary(int rotary, int delta)
-{
-  int i = find_client_by_rotary(rotary);
-  if (i < 0)
-    return;
-  struct client_map *cm = &client_map[i];
-  struct client_state *cs = &client_state[i];
-
-  calc_clients();
-  double vol = pa_sw_volume_to_linear(cs->volume);
-  vol += delta * 0.02;
-  vol = CLAMP(vol, 0, cm->range);
-  pa_volume_t pavol = pa_sw_volume_from_linear(vol);
-
-  HASH_FOR_ALL(pulse_sink_input, s)
-    {
-      if (s->noct_client_idx == i && s->volume != pavol)
-       {
-         DBG("@@ Client #%d, sink input #%d: setting volume=%u", s->client_idx, s->idx, pavol);
-         pa_cvolume cvol;
-         pa_cvolume_set(&cvol, s->channels, pavol);
-         PULSE_ASYNC_RUN(pa_context_set_sink_input_volume, pulse_ctx, s->idx, &cvol, pulse_success_cb);
-       }
-    }
-  HASH_END_FOR;
-}
-
 void notify_rotary(int rotary, int delta)
 {
   if (pulse_state != PS_ONLINE)