Hello all,
I'm working on a little app utilizing 'lasound' and I've got bassically
what I want. However, somewhere there is a fault. Editing a random
variable causes a segmentation fault. I've tried gdb but except it
telling me where the application crashed (which I already knew) I did
not get anything useful out of it. To minimize my own potential errors I
have here below the example script from where I started (though I have
removed some unnecessary stuff) and commented out the 'long *outvl' and
'*outvl = -1'. Uncommenting, compile, and run this and I get a
segmentation fault.
The original script is available at the following link:
http://stackoverflow.com/questions/7657624/get-master-sound-volume-in-c-in-…
Can someone tell me where this is going wrong?
Thanks in advance, Maarten
#include <unistd.h>
#include <fcntl.h>
#include <alsa/asoundlib.h>
/*
Drawbacks. Sets volume on both channels but gets volume on one. Can be
easily adapted.
*/
int audio_volume(long* outvol)
{
snd_mixer_t* handle;
snd_mixer_elem_t* elem;
snd_mixer_selem_id_t* sid;
static const char* mix_name = "Fake";
static const char* card = "default";
static int mix_index = 0;
// long *outvl;
// *outvl = -1;
int ret = 0;
long pmin, pmax;
snd_mixer_selem_id_alloca(&sid);
//sets simple-mixer index and name
snd_mixer_selem_id_set_index(sid, mix_index);
snd_mixer_selem_id_set_name(sid, mix_name);
if ((snd_mixer_open(&handle, 0)) < 0)
return -1;
if ((snd_mixer_attach(handle, card)) < 0) {
snd_mixer_close(handle);
return -2;
}
if ((snd_mixer_selem_register(handle, NULL, NULL)) < 0) {
snd_mixer_close(handle);
return -3;
}
ret = snd_mixer_load(handle);
if (ret < 0) {
snd_mixer_close(handle);
return -4;
}
elem = snd_mixer_find_selem(handle, sid);
if (!elem) {
snd_mixer_close(handle);
return -5;
}
long minv, maxv;
snd_mixer_selem_get_playback_volume_range (elem, &minv, &maxv);
fprintf(stderr, "Volume range <%i,%i>\n", minv, maxv);
if(snd_mixer_selem_get_playback_volume(elem, 0, outvol) < 0) {
snd_mixer_close(handle);
return -6;
}
fprintf(stderr, "Get volume %i with status %i\n", *outvol, ret);
/* make the value bound to 100 */
*outvol -= minv;
maxv -= minv;
minv = 0;
*outvol = 100 * (*outvol) / maxv; // make the value bound from 0
to 100
snd_mixer_close(handle);
return 0;
}
int main(void)
{
long vol = -1;
printf("Ret %i\n", audio_volume(&vol));
return 0;
}