On Saturday 05 July 2014 20:32:18 bifferos wrote:
> Here's a mixer applet (OSS).  It gets the list of mixer devices, gets and 
> sets individual device values.
> 
> 
> 
> / # mixer --help
> BusyBox v1.22.1 (2014-06-23 15:19:41 BST) multi-call binary.
> 
> Usage: mixer [-d DEVICE] [MIXER [LEVEL]]
> 
> Sets the mixer volume at a specific level (0-100)
> 
>         -d DEVICE    use given mixer device (default /dev/mixer)
> 
> / # mixer
> Mixer: speaker mic phout
> 
> / # mixer speaker
> Level (L/R): 99/99
> 
> / # mixer speaker 20
> Level (L/R): 20/20
> 
> It adds about about 2.6k to Busybox and I don't really understand why.  Even 
> with the list of mixers it should only be ~500 bytes.
> If anyone has any suggestions I can try to fix that.
> 
> 
> regards,
> Biff.

Hi,
just for fun some size reduction. 
Tested it a little bit and it seems to work.

Ciao,
Tito

scripts/bloat-o-meter busybox_old busybox_unstripped
function                                             old     new   delta
pr_level_exit                                         27       -     -27
mixer_main                                           385     335     -50
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 0/1 up/down: 0/-77)             Total: -77 bytes

int mixer_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int mixer_main(int argc, char **argv)
{
        int fd_mixer;
        char* device = (char*)"/dev/mixer";
        uint32_t level;
        int devmask, n_dev;
        const char *m_names[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES ;

        /* option handling */
        opt_complementary = "?2";  /* must have no more than 2 */
        getopt32(argv, "d:", &device);
        argc -= optind;
        argv += optind;

        fd_mixer = xopen(device, O_RDWR);
        ioctl_or_perror_and_die(fd_mixer, SOUND_MIXER_READ_DEVMASK, &devmask, 
"DEVMASK");

        switch (argc) {
                case 2:
                        level = xatoi_range(argv[1], 1, 100);
                        break;
                case 0:
                        printf("Mixer:");
        }

        /* mixer device enumeration */
        for (n_dev = 0; n_dev < SOUND_MIXER_NRDEVICES; ++n_dev) {
                if ((1 << n_dev) & devmask) {
                        if (*argv) {
                                if (strcmp(m_names[n_dev], *argv) == 0)
                                        break;
                        } else {
                                printf(" %s", m_names[n_dev]);
                        }
                }
        }

        if (!*argv) {
                putchar('\n');
        } else {
                if (argc == 2) {
                        /* mixer device setting */
                        level = (level << 8) + level;
                        ioctl_or_perror_and_die(fd_mixer, MIXER_WRITE(n_dev), 
&level, "MIXER_WRITE");
                } else {
                        /* mixer device reading */
                        ioctl_or_perror_and_die(fd_mixer, MIXER_READ(n_dev), 
&level, "MIXER_READ");
                }
                printf("Level (l/r): %d/%d\n", level & 0xff, ((level & 0xff00) 
>> 8));
        }
        return 0;
}
/* vi: set sw=4 ts=4: */
/*
 * Audio mixer for busybox (OSS emulation only)
 *
 * Copyright (c) Bifferos (sa...@bifferos.com)
 *
 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
 *
 * Setting L/R stereo channels independently is unsupported in order to keep this applet small.
 *
 *
 */


#include <linux/soundcard.h>
#include "libbb.h"


//usage:#define mixer_trivial_usage
//usage:       "[-d DEVICE] [MIXER [LEVEL]]"
//usage:#define mixer_full_usage "\n\n"
//usage:       "Sets the mixer volume at a specific level (0-100)\n"
//usage:       "\n        -d DEVICE    use given mixer device (default /dev/mixer)"
//usage:#define mixer_example_usage
//usage:       "# mixer vol 50\n"

int mixer_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int mixer_main(int argc, char **argv)
{
	int fd_mixer;
	char* device = (char*)"/dev/mixer";
	uint32_t level;
	int devmask, n_dev;
	const char *m_names[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES ;

	/* option handling */
	opt_complementary = "?2";  /* must have no more than 2 */
	getopt32(argv, "d:", &device);
	argc -= optind;
	argv += optind;

	fd_mixer = xopen(device, O_RDWR);
	ioctl_or_perror_and_die(fd_mixer, SOUND_MIXER_READ_DEVMASK, &devmask, "DEVMASK");

	switch (argc) {
		case 2:
			level = xatoi_range(argv[1], 1, 100);
			break;
		case 0:
			printf("Mixer:");
	}

	/* mixer device enumeration */
	for (n_dev = 0; n_dev < SOUND_MIXER_NRDEVICES; ++n_dev) {
		if ((1 << n_dev) & devmask) {
			if (*argv) {
				if (strcmp(m_names[n_dev], *argv) == 0)
					break;
			} else {
				printf(" %s", m_names[n_dev]);
			}
		}
	}

	if (!*argv) {
		putchar('\n');
	} else {
		if (argc == 2) {
			/* mixer device setting */
			level = (level << 8) + level;
			ioctl_or_perror_and_die(fd_mixer, MIXER_WRITE(n_dev), &level, "MIXER_WRITE");
		} else {
			/* mixer device reading */
			ioctl_or_perror_and_die(fd_mixer, MIXER_READ(n_dev), &level, "MIXER_READ");
		}
		printf("Level (l/r): %d/%d\n", level & 0xff, ((level & 0xff00) >> 8));
	}
	return 0;
}
_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to