I've run in to a problem while trying to get a C volume knob monitor running.
Once I detect a knob event, I call adj_vol(c), with c=+1 for up,
c=-1 for down. Here is the code in its present state.
/*This was swiped from
https://stackoverflow.com/questions/6787318/
set-alsa-master-volume-from-c-code
*/
static void adj_vol(int c)
{
snd_mixer_t *mix_h;
snd_mixer_selem_id_t *sid;
const char *card = "default";
const char *selem_name = "Master";
snd_mixer_elem_t* elem;
/*open an empty mixer handle*/
if (snd_mixer_open(&mix_h, 0)<0) goto erxit;
/*attach the card to it*/
if (snd_mixer_attach(mix_h, card)<0) goto erxit;
/*register it*/
if (snd_mixer_selem_register(mix_h, NULL, NULL)<0) goto erxit;
/*load its elements*/
if (snd_mixer_load(mix_h)<0) goto erxit;
/*allocate an element ID (on the stack)*/
snd_mixer_selem_id_alloca(&sid);
/*set its index part*/
snd_mixer_selem_id_set_index(sid, 0);
/*set its name part*/
snd_mixer_selem_id_set_name(sid, selem_name);
/*find the element*/
if ((elem=snd_mixer_find_selem(mix_h, sid))<0) goto erxit;
{long lv,rv;
if (snd_mixer_selem_get_playback_volume(elem,SND_MIXER_SCHN_FRONT_LEFT,&lv)
<0) goto erxit;
/*DEBUG*/ printf("L=%li\n",lv);
if (snd_mixer_selem_get_playback_volume(elem,SND_MIXER_SCHN_FRONT_RIGHT,&rv)
<0) goto erxit;
/*DEBUG*/ printf("R=%li\n",rv);
}
snd_mixer_close(mix_h);
return;
erxit:
...
}
When I run this, it doesn't report any error, but prints incorrect
values for the volume levels. In one run, it printed
L=30129
R=30129
"amixer get Master" prints
Simple mixer control 'Master',0
Capabilities: pvolume pswitch pswitch-joined
Playback channels: Front Left - Front Right
Limits: Playback 0 - 65536
Mono:
Front Left: Playback 18535 [28%] [on]
Front Right: Playback 18535 [28%] [on]
I've traced through the axmixer code, even run it under gdb, and it
seems to be doing what my program does. I ran amixer under gdb
with the invocation "amixer set Master 3%+", and set a breakpoint at
line 368 of amixer.c (of the slackware 14.2 version of alsa-utils).
Stepping through the call, the value of "orig" is 18535.
I'm wondering whether this is a pulseaudio issue. After a fresh system
startup, pulseaudio is not running. Turning the knob with my program
running starts it.
pactl list sinks shows
Sink #0
State: SUSPENDED
Name: alsa_output.usb-Dell_Dell_AC511_USB_SoundBar-00.analog-stereo
Description: Dell AC511 USB SoundBar Analog Stereo
Driver: module-alsa-card.c
Sample Specification: s16le 2ch 44100Hz
Channel Map: front-left,front-right
Owner Module: 6
Mute: no
Volume: front-left: 18535 / 28% / -32.91 dB, front-right: 18535 / 28% / -32.91 dB
balance 0.00
Base Volume: 107931 / 165% / 13.00 dB
Monitor Source: alsa_output.usb-Dell_Dell_AC511_USB_SoundBar-00.analog-stereo.monitor
Latency: 0 usec, configured 0 usec
Flags: HARDWARE HW_MUTE_CTRL HW_VOLUME_CTRL DECIBEL_VOLUME LATENCY
...