[pulseaudio-discuss] [PATCH 1/2] pactl: Accept more volume specification formats

2011-03-24 Thread Maarten Bosmans
With this you can specify the volume with 6554, 10%, 0.001 or -60dB,
all resulting in the same volume change.
---
 src/utils/pactl.c |   98 +---
 1 files changed, 62 insertions(+), 36 deletions(-)

diff --git a/src/utils/pactl.c b/src/utils/pactl.c
index ad5c0b8..672bfbb 100644
--- a/src/utils/pactl.c
+++ b/src/utils/pactl.c
@@ -65,6 +65,12 @@ static uint32_t module_index;
 static pa_bool_t suspend;
 static pa_bool_t mute;
 static pa_volume_t volume;
+enum volume_type {
+VOL_SW,
+VOL_PERCENT,
+VOL_LINEAR,
+VOL_DECIBEL
+};
 
 static pa_proplist *proplist = NULL;
 
@@ -956,6 +962,54 @@ static void exit_signal_callback(pa_mainloop_api *m, 
pa_signal_event *e, int sig
 quit(0);
 }
 
+static int parse_volume(const char *vol_spec, pa_volume_t *vol) {
+int volume_type = VOL_SW;
+double v;
+char *vs;
+
+pa_assert(vol_spec);
+pa_assert(vol);
+
+vs = pa_xstrdup(vol_spec);
+
+for (char *c = vs; *c; c++) {
+  if (*c == '.')
+volume_type = VOL_LINEAR;
+}
+if (pa_endswith(vs, %)) {
+volume_type = VOL_PERCENT;
+vs[strlen(vs)-1] = 0;
+}
+if (pa_endswith(vs, db) || pa_endswith(vs, dB)) {
+volume_type = VOL_DECIBEL;
+vs[strlen(vs)-2] = 0;
+}
+
+if (pa_atod(vs, v)  0) {
+pa_log(_(Invalid volume specification));
+pa_xfree(vs);
+return -1;
+}
+
+pa_xfree(vs);
+
+if (volume_type == VOL_PERCENT)
+v = v * (double) PA_VOLUME_NORM / 100;
+if (volume_type == VOL_LINEAR)
+v = pa_sw_volume_from_linear(v);
+if (volume_type == VOL_DECIBEL)
+v = pa_sw_volume_from_dB(v);
+
+if (!PA_VOLUME_IS_VALID((pa_volume_t) v)) {
+pa_log(_(Volume outside permissible range.\n));
+return -1;
+}
+
+*vol = (pa_volume_t) v;
+
+return 0;
+}
+
 static void help(const char *argv0) {
 
 printf(_(%s [options] stat\n
@@ -1235,7 +1289,6 @@ int main(int argc, char *argv[]) {
 port_name = pa_xstrdup(argv[optind+2]);
 
 } else if (pa_streq(argv[optind], set-sink-volume)) {
-uint32_t v;
 action = SET_SINK_VOLUME;
 
 if (argc != optind+3) {
@@ -1243,21 +1296,12 @@ int main(int argc, char *argv[]) {
 goto quit;
 }
 
-if (pa_atou(argv[optind+2], v)  0) {
-pa_log(_(Invalid volume specification));
-goto quit;
-}
+sink_name = pa_xstrdup(argv[optind+1]);
 
-if (!PA_VOLUME_IS_VALID(v)) {
-pa_log(_(Volume outside permissible range.\n));
+if (parse_volume(argv[optind+2], volume)  0)
 goto quit;
-}
-
-sink_name = pa_xstrdup(argv[optind+1]);
-volume = (pa_volume_t) v;
 
 } else if (pa_streq(argv[optind], set-source-volume)) {
-uint32_t v;
 action = SET_SOURCE_VOLUME;
 
 if (argc != optind+3) {
@@ -1265,21 +1309,12 @@ int main(int argc, char *argv[]) {
 goto quit;
 }
 
-if (pa_atou(argv[optind+2], v)  0) {
-pa_log(_(Invalid volume specification));
-goto quit;
-}
+source_name = pa_xstrdup(argv[optind+1]);
 
-if (!PA_VOLUME_IS_VALID(v)) {
-pa_log(_(Volume outside permissible range.\n));
+if (parse_volume(argv[optind+2], volume)  0)
 goto quit;
-}
-
-source_name = pa_xstrdup(argv[optind+1]);
-volume = (pa_volume_t) v;
 
 } else if (pa_streq(argv[optind], set-sink-input-volume)) {
-uint32_t v;
 action = SET_SINK_INPUT_VOLUME;
 
 if (argc != optind+3) {
@@ -1292,17 +1327,8 @@ int main(int argc, char *argv[]) {
 goto quit;
 }
 
-if (pa_atou(argv[optind+2], v)  0) {
-pa_log(_(Invalid volume specification));
-goto quit;
-}
-
-if (!PA_VOLUME_IS_VALID(v)) {
-pa_log(_(Volume outside permissible range.\n));
+if (parse_volume(argv[optind+2], volume)  0)
 goto quit;
-}
-
-volume = (pa_volume_t) v;
 
 } else if (pa_streq(argv[optind], set-sink-mute)) {
 int b;
@@ -1314,7 +1340,7 @@ int main(int argc, char *argv[]) {
 }
 
 if ((b = pa_parse_boolean(argv[optind+2]))  0) {
-pa_log(_(Invalid volume specification));
+pa_log(_(Invalid mute specification));
 goto quit;
 }
 
@@ -1331,7 +1357,7 @@ int main(int argc, char *argv[]) {
 }
 
 if ((b = pa_parse_boolean(argv[optind+2]))  0) {
-pa_log(_(Invalid volume specification));
+pa_log(_(Invalid mute specification));
 goto quit;
 }
 

Re: [pulseaudio-discuss] [PATCH 1/2] pactl: Accept more volume specification formats

2011-03-24 Thread Maarten Bosmans
2011/3/24 Maarten Bosmans mkbosm...@gmail.com:
 With this you can specify the volume with 6554, 10%, 0.001 or -60dB,
 all resulting in the same volume change.

I was also going to add relative volumes, such as +3dB and -5%, by
detecting a + or - sign in the volume. But that clashes with the
absolute dB scale (insofar a dB can ever be absolute) that can also be
negative.

Any suggestions for graceful handling of this?

Maarten
___
pulseaudio-discuss mailing list
pulseaudio-discuss@mail.0pointer.de
https://tango.0pointer.de/mailman/listinfo/pulseaudio-discuss


Re: [pulseaudio-discuss] [PATCH 1/2] pactl: Accept more volume specification formats

2011-03-24 Thread Colin Guthrie
'Twas brillig, and Maarten Bosmans at 24/03/11 12:44 did gyre and gimble:
 2011/3/24 Maarten Bosmans mkbosm...@gmail.com:
 With this you can specify the volume with 6554, 10%, 0.001 or -60dB,
 all resulting in the same volume change.
 
 I was also going to add relative volumes, such as +3dB and -5%, by
 detecting a + or - sign in the volume. But that clashes with the
 absolute dB scale (insofar a dB can ever be absolute) that can also be
 negative.
 
 Any suggestions for graceful handling of this?

How about if the first letter of the volume change is an i or a d
then this indicated increment or decrement relative volume?

It's not as clean as the +/- labelling sadly but such is life.

Alternatively your absolute dB volumes could be specified as 60-dB or
7+dB (where 7dB implies 7+dB)... That way the prefix +/- notation
could be used for relative adjustments. The only downside there is that
setting absolute dB volumes is more confusing (you'd never need to use
anything other than XdB for relative adjustments anyway).

Personally I'd go for the later as I think relative adjustments are
probably more common, so it's syntax should be neatest, but I could be
very wrong :D

Col

-- 

Colin Guthrie
gmane(at)colin.guthr.ie
http://colin.guthr.ie/

Day Job:
  Tribalogic Limited [http://www.tribalogic.net/]
Open Source:
  Mageia Contributor [http://www.mageia.org/]
  PulseAudio Hacker [http://www.pulseaudio.org/]
  Trac Hacker [http://trac.edgewall.org/]

___
pulseaudio-discuss mailing list
pulseaudio-discuss@mail.0pointer.de
https://tango.0pointer.de/mailman/listinfo/pulseaudio-discuss


Re: [pulseaudio-discuss] [PATCH 1/2] pactl: Accept more volume specification formats

2011-03-24 Thread Arun Raghavan
On Thu, 2011-03-24 at 13:07 +, Colin Guthrie wrote:
 'Twas brillig, and Maarten Bosmans at 24/03/11 12:44 did gyre and gimble:
  2011/3/24 Maarten Bosmans mkbosm...@gmail.com:
  With this you can specify the volume with 6554, 10%, 0.001 or -60dB,
  all resulting in the same volume change.
  
  I was also going to add relative volumes, such as +3dB and -5%, by
  detecting a + or - sign in the volume. But that clashes with the
  absolute dB scale (insofar a dB can ever be absolute) that can also be
  negative.
  
  Any suggestions for graceful handling of this?
 
 How about if the first letter of the volume change is an i or a d
 then this indicated increment or decrement relative volume?
 
 It's not as clean as the +/- labelling sadly but such is life.
 
 Alternatively your absolute dB volumes could be specified as 60-dB or
 7+dB (where 7dB implies 7+dB)... That way the prefix +/- notation
 could be used for relative adjustments. The only downside there is that
 setting absolute dB volumes is more confusing (you'd never need to use
 anything other than XdB for relative adjustments anyway).
 
 Personally I'd go for the later as I think relative adjustments are
 probably more common, so it's syntax should be neatest, but I could be
 very wrong :D

Or maybe just do this as a separate set-volume-step command (or
-increment or something better named)?

-- Arun

___
pulseaudio-discuss mailing list
pulseaudio-discuss@mail.0pointer.de
https://tango.0pointer.de/mailman/listinfo/pulseaudio-discuss


Re: [pulseaudio-discuss] [PATCH 1/2] pactl: Accept more volume specification formats

2011-03-24 Thread Tanu Kaskinen
On Thu, 2011-03-24 at 22:22 +0530, Arun Raghavan wrote:
 On Thu, 2011-03-24 at 13:07 +, Colin Guthrie wrote:
  'Twas brillig, and Maarten Bosmans at 24/03/11 12:44 did gyre and gimble:
   2011/3/24 Maarten Bosmans mkbosm...@gmail.com:
   With this you can specify the volume with 6554, 10%, 0.001 or -60dB,
   all resulting in the same volume change.
   
   I was also going to add relative volumes, such as +3dB and -5%, by
   detecting a + or - sign in the volume. But that clashes with the
   absolute dB scale (insofar a dB can ever be absolute) that can also be
   negative.
   
   Any suggestions for graceful handling of this?
  
  How about if the first letter of the volume change is an i or a d
  then this indicated increment or decrement relative volume?
  
  It's not as clean as the +/- labelling sadly but such is life.
  
  Alternatively your absolute dB volumes could be specified as 60-dB or
  7+dB (where 7dB implies 7+dB)... That way the prefix +/- notation
  could be used for relative adjustments. The only downside there is that
  setting absolute dB volumes is more confusing (you'd never need to use
  anything other than XdB for relative adjustments anyway).
  
  Personally I'd go for the later as I think relative adjustments are
  probably more common, so it's syntax should be neatest, but I could be
  very wrong :D
 
 Or maybe just do this as a separate set-volume-step command (or
 -increment or something better named)?

I agree - I think a separate command is a good idea. For command naming,
I suggest increase-sink-volume and decrease-sink-volume.

-- 
Tanu

___
pulseaudio-discuss mailing list
pulseaudio-discuss@mail.0pointer.de
https://tango.0pointer.de/mailman/listinfo/pulseaudio-discuss


Re: [pulseaudio-discuss] [PATCH 1/2] pactl: Accept more volume specification formats

2011-03-24 Thread Colin Guthrie
'Twas brillig, and Tanu Kaskinen at 24/03/11 18:31 did gyre and gimble:
 On Thu, 2011-03-24 at 22:22 +0530, Arun Raghavan wrote:
 On Thu, 2011-03-24 at 13:07 +, Colin Guthrie wrote:
 'Twas brillig, and Maarten Bosmans at 24/03/11 12:44 did gyre and gimble:
 2011/3/24 Maarten Bosmans mkbosm...@gmail.com:
 With this you can specify the volume with 6554, 10%, 0.001 or -60dB,
 all resulting in the same volume change.

 I was also going to add relative volumes, such as +3dB and -5%, by
 detecting a + or - sign in the volume. But that clashes with the
 absolute dB scale (insofar a dB can ever be absolute) that can also be
 negative.

 Any suggestions for graceful handling of this?

 How about if the first letter of the volume change is an i or a d
 then this indicated increment or decrement relative volume?

 It's not as clean as the +/- labelling sadly but such is life.

 Alternatively your absolute dB volumes could be specified as 60-dB or
 7+dB (where 7dB implies 7+dB)... That way the prefix +/- notation
 could be used for relative adjustments. The only downside there is that
 setting absolute dB volumes is more confusing (you'd never need to use
 anything other than XdB for relative adjustments anyway).

 Personally I'd go for the later as I think relative adjustments are
 probably more common, so it's syntax should be neatest, but I could be
 very wrong :D

 Or maybe just do this as a separate set-volume-step command (or
 -increment or something better named)?
 
 I agree - I think a separate command is a good idea. For command naming,
 I suggest increase-sink-volume and decrease-sink-volume.

While I think this is a valid solution, I'd prefer to see a single
command for the volume adjustment (change-sink-volume?) but use the +/-
notation. When we're talking relative adjustments the problem of how the
+'s and -'s are interpreted is gone anyway, so this is possible.

But I'm not overly fussed so if two commands for inc+dec is easier then
so be it.

Col

-- 

Colin Guthrie
gmane(at)colin.guthr.ie
http://colin.guthr.ie/

Day Job:
  Tribalogic Limited [http://www.tribalogic.net/]
Open Source:
  Mageia Contributor [http://www.mageia.org/]
  PulseAudio Hacker [http://www.pulseaudio.org/]
  Trac Hacker [http://trac.edgewall.org/]

___
pulseaudio-discuss mailing list
pulseaudio-discuss@mail.0pointer.de
https://tango.0pointer.de/mailman/listinfo/pulseaudio-discuss