Re: Volume confusion

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

 



On Mon, 2013-04-15 at 21:20 +0200, Clemens Ladisch wrote:
> jon wrote:
> > Is the min value supposed to be negative or is the most common code
> > everyone is using broken ?
> 
> >     snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
> >     newvol= ( (long)volume * (long)max ) / (long)100 ;
> >     if ( snd_mixer_selem_set_playback_volume_all(elem,newvol) <0)
> 
> Ignoring the min value is, of course, wrong.
> 
> "Math is hard, let's go shopping!"
> File a bug and suggest to go shopping here:
> <http://git.alsa-project.org/?p=alsa-utils.git;a=blob;f=alsamixer/volume_mapping.c;hb=HEAD>
> 
> 
Thanks :-)

I had already hacked round the issue, not good at math though - it
shows.

Looks like the code you pointed at would offer a more progressive volume
than this:

// **************************************************** Raspberry Pi
#ifdef RPI
#ifdef ALSA
#include <alsa/asoundlib.h>
void set_volume(long volume)
{
        long min, max, newvol;
        snd_mixer_t *handle = NULL;
        snd_mixer_selem_id_t *sid = NULL;
        const char *card = "default";
        //const char *selem_name = "Master";
        const char *selem_name = "PCM";
        float vpercent, fnewvol;

        if (snd_mixer_open(&handle, 0) < 0)
        {
                printf("%s: set_volume() RPI - snd_mixer_open failed\n",PROGNAME);
                fflush(stdout);
                perror("snd_mixer_open");
        }
        else
        {
                snd_mixer_attach(handle, card);
                snd_mixer_selem_register(handle, NULL, NULL);
                snd_mixer_load(handle);

                //snd_mixer_selem_id_alloca(&sid);
                snd_mixer_selem_id_malloc(&sid);
                snd_mixer_selem_id_set_index(sid, 0);
                snd_mixer_selem_id_set_name(sid, selem_name);
                snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid);
                if (elem == NULL)
                {
                        printf("%s: set_volume() RPI - snd_mixer_find_selem failed()\n",PROGNAME);
                        fflush(stdout);
                }
                else
                {
                        snd_mixer_selem_get_playback_volume_range(elem, &min, &max);

                        vpercent = (float)volume/100;
                        fnewvol = (float)min + vpercent * ( (float)max - (float)min);
                        newvol = (long)fnewvol;

                        //printf("volume=%ld  min=%ld   max=%ld   newvol=%ld\n",volume,min,max,newvol); fflush(stdout);
                        if ( snd_mixer_selem_set_playback_volume_all(elem,newvol) <0)
                        {
                                printf("%s: set_volume() RPI - snd_mixer_selem_set_playback_volume_all() failed\n",PROGNAME);
                                fflush(stdout);
                        }
                }
                snd_mixer_close(handle);
        }
}
#endif                  // ALSA



------------------------------------------------------------------------------
Precog is a next-generation analytics platform capable of advanced
analytics on semi-structured data. The platform includes APIs for building
apps and a phenomenal toolset for data science. Developers can use
our toolset for easy data analysis & visualization. Get a free account!
http://www2.precog.com/precogplatform/slashdotnewsletter
_______________________________________________
Alsa-user mailing list
Alsa-user@xxxxxxxxxxxxxxxxxxxxx
https://lists.sourceforge.net/lists/listinfo/alsa-user




[Index of Archives]     [ALSA Devel]     [Linux Audio Users]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]

  Powered by Linux