Volume confusion

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

 



I see lots of code like this online :

http://hdlab.googlecode.com/svn/trunk/xbmc/tools/XBMCLive/setAlsaVolumes.c

Unfortunately on the raspberry Pi board the min value from
snd_mixer_selem_get_playback_volume_range() is -10239.

If I do a calculation volume*max/100 then the lowest value I can get is
zero.  As a result the code only moves the volume by the top 20% ish of
its range.

Is the min value supposed to be negative or is the most common code
everyone is using broken ?

Thanks,
Jon


Example output
min=-10239  max=400   writing 0



**** Code i've crudely hacked together from various examples.
// volume is percentage
#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";

        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);
                        newvol= ( (long)volume * (long)max ) / (long)100 ;
                        printf("min=%ld  max=%ld   writing %ld\n",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);
        }
}



------------------------------------------------------------------------------
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