Re: Added volume stepping to mixer

2003-01-07 Thread Dan Lukes
[EMAIL PROTECTED] wrote, On 01/06/03 18:53:

Last week I modified the mixer to support volume stepping. Having a



Sample usage:

Increasing:
[daren@wee mixer]$mixer -i 5:5
Increasing the mixer vol from 40:40 to 45:45.
[daren@wee mixer]$

Decreasing:
[daren@wee mixer]$mixer -d vol 10
Decreasing the mixer vol from 45:45 to 35:35.
[daren@wee mixer]$


	Mixer can change volume for more than one device within one invocation.
So, what about more generic way to implement stepping ?

You may want to increase volume on one input simultaneously decreasing
the volume on another input. Your can't do it in one step.

Let's implement stepping according to followint example:
[dan@~]$mixer vol 40:40 line1 +15:+15 line2 -10

Set volume for 'vol' to absolute value 40:40, increase line1's volume by
15 and decreasing line2's volume by 10 (both channel).

	It seems to be more generic way to me ...

	The change is backward compatible with current command line format.

	The patch for manual page should be reviewed by someone with better
knowledge of english.


	The patch can be applied to stable also.


	Dan


--
Dan Lukes tel: +420 2 21914205, fax: +420 2 21914206
root of  FIONet, KolejNET,  webmaster  of www.freebsd.cz
AKA: [EMAIL PROTECTED], [EMAIL PROTECTED],[EMAIL PROTECTED]


*** mixer.c.ORIGTue Aug  7 20:15:10 2001
--- mixer.c Tue Jan  7 10:48:06 2003
***
*** 23,41 
  #include stdlib.h
  #include unistd.h
  #include sys/soundcard.h
  
  const char *names[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
  
  void usage(int devmask, int recmask);
  int res_name(const char *name, int mask);
  void print_recsrc(int recsrc);
  
  void
  usage(int devmask, int recmask)
  {
int i, n;
  
!   printf(usage: mixer [-f device] [-s] [[dev [voll[:volr]] | recsrc | 
{^|+|-|=}rec recdev] ... ]\n);
printf( devices: );
for (i = 0, n = 0; i  SOUND_MIXER_NRDEVICES; i++)
if ((1  i)  devmask)  {
--- 23,43 
  #include stdlib.h
  #include unistd.h
  #include sys/soundcard.h
+ #include ctype.h
  
  const char *names[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
  
  void usage(int devmask, int recmask);
  int res_name(const char *name, int mask);
  void print_recsrc(int recsrc);
+ int get_vol(char *string, int *l, int *r, int *dl, int *dr);
  
  void
  usage(int devmask, int recmask)
  {
int i, n;
  
!   printf(usage: mixer [-f device] [-s] [[dev [[+|-]voll[:[+|-]volr]] | recsrc | 
{^|+|-|=}rec recdev] ... ]\n);
printf( devices: );
for (i = 0, n = 0; i  SOUND_MIXER_NRDEVICES; i++)
if ((1  i)  devmask)  {
***
*** 84,96 
fprintf(stderr, \n);
  }
  
  int
  main(int argc, char *argv[])
  {
int foo, bar, baz, dev;
int devmask = 0, recmask = 0, recsrc = 0, orecsrc;
int dusage = 0, drecsrc = 0, shortflag = 0;
!   int l = 0, r = 0, t = 0;
char ch;
  
char *name;
--- 86,158 
fprintf(stderr, \n);
  }
  
+ #define PLUSSIGN  '+'
+ #define MINUSSIGN '-'
+ #define DELIMITER ':'
+ int
+ get_vol(char *string, int *l, int *r, int *dl, int *dr)
+ {
+   char *sptr = string;
+   char *endptr;
+   long int value;
+   
+   while(isspace(*sptr))
+   sptr++;
+ 
+   /* if dl==NULL revert to old (non [+|-]) syntax */
+   if (dl != NULL) {
+   if (*sptr == PLUSSIGN) {
+   *dl = 1;
+   sptr++;
+   } else if  (*sptr == MINUSSIGN) {
+   *dl = -1;
+   sptr++;
+   } else
+   *dl = 0;
+   }
+   /* catch strings like '+-10' '++3' or '+ 2'*/
+   if (!isdigit(*sptr))
+   return(0);
+   value=strtol(sptr, endptr, 10);
+   if (*endptr != '\0'  *endptr != DELIMITER  !isspace(*endptr))
+   return(0);
+ 
+   *l=(int)value;
+   sptr=endptr;
+ 
+   if (*sptr!=DELIMITER)
+   return(1);
+ 
+   sptr++;
+   if (dr != NULL) {
+   if (*sptr == PLUSSIGN) {
+   *dr = 1;
+   sptr++;
+   } else if  (*sptr == MINUSSIGN) {
+   *dr = -1;
+   sptr++;
+   } else
+   *dr = 0;
+   }
+   /* catch strings like '+-10' '++3' or '+ 2'*/
+   if (!isdigit(*sptr))
+   return(1);
+   value=strtol(sptr, endptr, 10);
+   if (*endptr != '\0'  !isspace(*endptr))
+   return(1);
+ 
+   *r=(int)value;
+ 
+   return(2);
+ }
+ 
  int
  main(int argc, char *argv[])
  {
int foo, bar, baz, dev;
int devmask = 0, recmask = 0, recsrc = 0, orecsrc;
int dusage = 0, drecsrc = 0, shortflag = 0;
!   int l = 0, r = 0, t = 0, dl = 0, dr = 0;
char ch;
  
char *name;
***
*** 181,187 
  

Added volume stepping to mixer

2003-01-06 Thread Daren Desjardins
Last week I modified the mixer to support volume stepping. Having a
keyboard with volume control on it, it has come in very handy to be able
to use the mixer to increase/decrease the volume. I submitted the
changes to send-pr and have an open ticket. For those that are
interested, Im including the diff against mixer.c v1.17.

http://www.freebsd.org/cgi/query-pr.cgi?pr=46679

Sample usage:

Increasing:
[daren@wee mixer]$mixer -i 5:5
Increasing the mixer vol from 40:40 to 45:45.
[daren@wee mixer]$

Decreasing:
[daren@wee mixer]$mixer -d vol 10
Decreasing the mixer vol from 45:45 to 35:35.
[daren@wee mixer]$



*** mixer_new.c	Wed Jan  1 13:26:23 2003
--- mixer.c	Wed Jan  1 13:32:41 2003
***
*** 3,8 
--- 3,9 
   *
   *	updated 1/1/93 to add stereo, level query, broken
   *  	devmask kludge - [EMAIL PROTECTED]
+  *	updated 1/1/03 to add volume stepping - [EMAIL PROTECTED]
   *
   * (C) Craig Metz and Hannu Savolainen 1993.
   *
***
*** 13,19 
  
  #ifndef lint
  static const char rcsid[] =
!   $FreeBSD: src/usr.sbin/mixer/mixer.c,v 1.17 2002/12/30 04:23:08 jmallett Exp $;
  #endif /* not lint */
  
  #include err.h
--- 14,20 
  
  #ifndef lint
  static const char rcsid[] =
!   $FreeBSD: src/usr.sbin/mixer/mixer.c,v 1.11.2.6 2001/07/30 10:22:58 dd Exp $;
  #endif /* not lint */
  
  #include err.h
***
*** 35,41 
  {
  	int i, n;
  
! 	printf(usage: mixer [-f device] [-s] [[dev [voll[:volr]] | recsrc | {^|+|-|=}rec recdev] ... ]\n);
  	printf( devices: );
  	for (i = 0, n = 0; i  SOUND_MIXER_NRDEVICES; i++)
  		if ((1  i)  devmask)  {
--- 36,42 
  {
  	int i, n;
  
! 	printf(usage: mixer [-f device] [-s] [-i|-d] [[dev [voll[:volr]] | recsrc | {^|+|-|=}rec recdev] ... ]\n);
  	printf( devices: );
  	for (i = 0, n = 0; i  SOUND_MIXER_NRDEVICES; i++)
  		if ((1  i)  devmask)  {
***
*** 91,96 
--- 92,99 
  	int devmask = 0, recmask = 0, recsrc = 0, orecsrc;
  	int dusage = 0, drecsrc = 0, shortflag = 0;
  	int l = 0, r = 0, t = 0;
+ 	bool volumeInc = false;
+ 	bool volumeDec = false;
  	char ch;
  
  	char *name;
***
*** 102,108 
  	else if (!strcmp(argv[0], mixer3))
  		name = strdup(/dev/mixer2);
  
! 	while ((ch = getopt(argc, argv, f:s)) != -1)
  		switch (ch) {
  			case 'f':
  name = strdup(optarg);
--- 105,111 
  	else if (!strcmp(argv[0], mixer3))
  		name = strdup(/dev/mixer2);
  
! 	while ((ch = getopt(argc, argv, f:sid)) != -1)
  		switch (ch) {
  			case 'f':
  name = strdup(optarg);
***
*** 110,115 
--- 113,124 
  			case 's':
  shortflag = 1;
  break;
+ 			case 'i':
+ volumeInc = true;
+ break;
+ 			case 'd':
+ volumeDec = true;
+ break;
  			default:
  dusage = 1;
  		}
***
*** 181,195 
  			continue;
  		}
  
  		if ((t = sscanf(*argv, %d:%d, l, r))  0) {
  			dev = 0;
  		}
  		else if((dev = res_name(*argv, devmask)) == -1) {
  			warnx(unknown device: %s, *argv);
  			dusage = 1;
  			break;
  		}
! 
  		switch(argc  1 ? sscanf(argv[1], %d:%d, l, r) : t) {
  		case 0:
  			if (ioctl(baz, MIXER_READ(dev),bar)== -1) {
--- 190,206 
  			continue;
  		}
  
+ 		// Check if device is specified
  		if ((t = sscanf(*argv, %d:%d, l, r))  0) {
  			dev = 0;
  		}
+ 		// read and verify the device
  		else if((dev = res_name(*argv, devmask)) == -1) {
  			warnx(unknown device: %s, *argv);
  			dusage = 1;
  			break;
  		}
! 		// Read in the volume changes
  		switch(argc  1 ? sscanf(argv[1], %d:%d, l, r) : t) {
  		case 0:
  			if (ioctl(baz, MIXER_READ(dev),bar)== -1) {
***
*** 208,213 
--- 219,259 
  		case 1:
  			r = l;
  		case 2:
+ 
+ 			// Read the current volum
+ 			if(ioctl(baz, MIXER_READ(dev), bar) == -1)
+ 			{
+ warn(MIXER_READ);
+ continue;
+ 			}
+ 
+ 			int leftVolume = bar  0x7f;
+ 			int rightVolume = (bar  8)  0x7f;
+ 
+ 			if(volumeInc || volumeDec)
+ 			{
+ // Read the current volume for stepping
+ if(ioctl(baz, MIXER_READ(dev), bar) == -1)
+ {
+ 	warn(MIXER_READ);
+ 	continue;
+ }
+ 
+ int leftVolume = bar  0x7f;
+ int rightVolume = (bar  8)  0x7f;
+ 
+ if(volumeInc)
+ {
+ 	l = leftVolume +l;
+ 	r = rightVolume +r;
+ }
+ else
+ {
+ 	l = leftVolume -l;
+ 	r = rightVolume -r;
+ }
+ 			}
+ 
  			if (l  0)
  l = 0;
  			else if (l  100)
***
*** 217,230 
  			else if (r  100)
  r = 100;
  
! 			if (ioctl(baz, MIXER_READ(dev),bar)== -1) {
! warn(MIXER_READ);
! argc--; argv++;
! continue;
  			}
! 
! 			printf(Setting the mixer %s from %d:%d to %d:%d.\n,
! 			names[dev], bar  0x7f, (bar  8)  0x7f, l, r);
  
  			l |= r  8;
  			if (ioctl(baz, MIXER_WRITE(dev), l) == -1)
--- 263,278 
  			else if (r  100)
  r = 100;
  
! 			if(volumeInc)
! 			{
! printf(Increasing the mixer %s from %d:%d to %d:%d.\n, 

Re: Added volume stepping to mixer

2003-01-06 Thread Juli Mallett
* De: Daren Desjardins [EMAIL PROTECTED] [ Data: 2003-01-06 ]
[ Subjecte: Added volume stepping to mixer ]
 Last week I modified the mixer to support volume stepping. Having a
 keyboard with volume control on it, it has come in very handy to be able
 to use the mixer to increase/decrease the volume. I submitted the
 changes to send-pr and have an open ticket. For those that are
 interested, Im including the diff against mixer.c v1.17.

OK, I have some critiques, which if you do them (they were in my TODO
anyway) I'll be glad to commit:
Instead of two bool's use one 'int', if it is 0, then act as
we do now.  If it is -1, we're decreasing, if it is +1, we're
increasing.  Thus you do something like:

if (direction != 0)
newvol = oldvol + (newvol * amount);
/* Set newval */

Also, don't do what you do with printf.  What should be done, just before
setting newval (I've already done this locally, but you should as part
of what you're doing anyway) is more like this:

if (newval  oldval)
printf(Decreasing level to blah blah);
else if (newval  oldval)
printf(Increasing level to blah blah);
else {
printf(No change in level at blah);
/* Get on with our lives */
}
/* Set newval */

Let me know.

Thanx,
juli.
-- 
Juli Mallett [EMAIL PROTECTED]
AIM: BSDFlata -- IRC: juli on EFnet.
OpenDarwin, Mono, FreeBSD Developer.
ircd-hybrid Developer, EFnet addict.
FreeBSD on MIPS-Anything on FreeBSD.

To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-current in the body of the message



Re: Added volume stepping to mixer

2003-01-06 Thread Daren Desjardins
Changes made, also added a couple lines of docs here and their and
changed the int variables to more meaningful. Lemme know if their is
anything else you need.

Daren Desjardins

On Mon, 2003-01-06 at 13:59, Juli Mallett wrote:
 * De: Daren Desjardins [EMAIL PROTECTED] [ Data: 2003-01-06 ]
   [ Subjecte: Added volume stepping to mixer ]
  Last week I modified the mixer to support volume stepping. Having a
  keyboard with volume control on it, it has come in very handy to be able
  to use the mixer to increase/decrease the volume. I submitted the
  changes to send-pr and have an open ticket. For those that are
  interested, Im including the diff against mixer.c v1.17.
 
 OK, I have some critiques, which if you do them (they were in my TODO
 anyway) I'll be glad to commit:
   Instead of two bool's use one 'int', if it is 0, then act as
   we do now.  If it is -1, we're decreasing, if it is +1, we're
   increasing.  Thus you do something like:
 
   if (direction != 0)
   newvol = oldvol + (newvol * amount);
   /* Set newval */
 
 Also, don't do what you do with printf.  What should be done, just before
 setting newval (I've already done this locally, but you should as part
 of what you're doing anyway) is more like this:
 
   if (newval  oldval)
   printf(Decreasing level to blah blah);
   else if (newval  oldval)
   printf(Increasing level to blah blah);
   else {
   printf(No change in level at blah);
   /* Get on with our lives */
   }
   /* Set newval */
 
 Let me know.
 
 Thanx,
 juli.

*** mixer_new.c	Thu Jan  2 14:58:34 2003
--- mixer.c	Mon Jan  6 14:31:58 2003
***
*** 3,8 
--- 3,9 
   *
   *	updated 1/1/93 to add stereo, level query, broken
   *  	devmask kludge - [EMAIL PROTECTED]
+  *	updated 6/1/03 to add volume stepping - [EMAIL PROTECTED]
   *
   * (C) Craig Metz and Hannu Savolainen 1993.
   *
***
*** 13,19 
  
  #ifndef lint
  static const char rcsid[] =
!   $FreeBSD: src/usr.sbin/mixer/mixer.c,v 1.17 2002/12/30 04:23:08 jmallett Exp $;
  #endif /* not lint */
  
  #include err.h
--- 14,20 
  
  #ifndef lint
  static const char rcsid[] =
!   $FreeBSD: src/usr.sbin/mixer/mixer.c,v 1.11.2.6 2001/07/30 10:22:58 dd Exp $;
  #endif /* not lint */
  
  #include err.h
***
*** 35,41 
  {
  	int i, n;
  
! 	printf(usage: mixer [-f device] [-s] [[dev [voll[:volr]] | recsrc | {^|+|-|=}rec recdev] ... ]\n);
  	printf( devices: );
  	for (i = 0, n = 0; i  SOUND_MIXER_NRDEVICES; i++)
  		if ((1  i)  devmask)  {
--- 36,42 
  {
  	int i, n;
  
! 	printf(usage: mixer [-f device] [-s] [-i|-d] [[dev [voll[:volr]] | recsrc | {^|+|-|=}rec recdev] ... ]\n);
  	printf( devices: );
  	for (i = 0, n = 0; i  SOUND_MIXER_NRDEVICES; i++)
  		if ((1  i)  devmask)  {
***
*** 90,96 
  	int foo, bar, baz, dev;
  	int devmask = 0, recmask = 0, recsrc = 0, orecsrc;
  	int dusage = 0, drecsrc = 0, shortflag = 0;
! 	int l = 0, r = 0, t = 0;
  	char ch;
  
  	char *name;
--- 91,98 
  	int foo, bar, baz, dev;
  	int devmask = 0, recmask = 0, recsrc = 0, orecsrc;
  	int dusage = 0, drecsrc = 0, shortflag = 0;
! 	int left = 0, right = 0, temp = 0;
! 	int direction = 0; // volume stepping indicator
  	char ch;
  
  	char *name;
***
*** 102,115 
  	else if (!strcmp(argv[0], mixer3))
  		name = strdup(/dev/mixer2);
  
! 	while ((ch = getopt(argc, argv, f:s)) != -1)
  		switch (ch) {
  			case 'f':
  name = strdup(optarg);
  break;
  			case 's':
  shortflag = 1;
  break;
  			default:
  dusage = 1;
  		}
--- 104,126 
  	else if (!strcmp(argv[0], mixer3))
  		name = strdup(/dev/mixer2);
  
! 	while ((ch = getopt(argc, argv, f:sid)) != -1)
  		switch (ch) {
  			case 'f':
+ // user specifed device
  name = strdup(optarg);
  break;
  			case 's':
+ // display levels in short form
  shortflag = 1;
  break;
+ 			case 'i':
+ // increase volume flag present
+ direction = 1;
+ break;
+ 			case 'd':
+ direction = -1;
+ break;
  			default:
  dusage = 1;
  		}
***
*** 181,196 
  			continue;
  		}
  
! 		if ((t = sscanf(*argv, %d:%d, l, r))  0) {
  			dev = 0;
  		}
  		else if((dev = res_name(*argv, devmask)) == -1) {
  			warnx(unknown device: %s, *argv);
  			dusage = 1;
  			break;
  		}
! 
! 		switch(argc  1 ? sscanf(argv[1], %d:%d, l, r) : t) {
  		case 0:
  			if (ioctl(baz, MIXER_READ(dev),bar)== -1) {
  warn(MIXER_READ);
--- 192,209 
  			continue;
  		}
  
! 		// Check if device is specified
! 		if ((temp = sscanf(*argv, %d:%d, left, right))  0) {
  			dev = 0;
  		}
+ 		// read and verify the device
  		else if((dev = res_name(*argv, devmask)) == -1) {
  			warnx(unknown device: %s, *argv);
  			dusage = 1;
  			break;
  		}
! 		// Read in the volume changes
! 		switch(argc  1 ? sscanf(argv[1], %d:%d, left, right) : temp) {
  		case 0