当我尝试使用 ALSA 库时,我的 C 代码出现了一个奇怪的行为。我使用此代码生成设备的 sid
snd_mixer_selem_id_t*
getSid(){
snd_mixer_selem_id_t *sid;
snd_mixer_selem_id_alloca(&sid);
snd_mixer_selem_id_set_index(sid,0);
snd_mixer_selem_id_set_name(sid, selem_name);
return sid;
}
然后我尝试通过
snd_mixer_t *handle = getHandle();
snd_mixer_selem_id_t *sid = getSid();
snd_mixer_elem_t *elem = snd_mixer_find_selem(handle,sid);
getHandle()
是获取卡处理程序的等效函数,但这个函数正在工作。奇怪的是,如果我在使用函数之前直接使用函数的代码,snd_mixer_find_selem
它就可以工作。我不得不说我是 C 新手,这是我的第一个项目之一,所以这可能是初学者对指针的错误。
错误信息是simple.c:282: snd_mixer_selem_get_playback_volume_range: Assertion 'elem' failed.
那么有人知道问题是什么吗?
这是我使用的代码的最小示例:
#include <alsa/asoundlib.h>
static const char *selem_name = "Master";
static const char *card = "default";
snd_mixer_t*
getHandle(){
snd_mixer_t *handle;
snd_mixer_open(&handle, 0);
snd_mixer_attach(handle, card);
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(handle);
return handle;
}
snd_mixer_selem_id_t*
getSid(){
snd_mixer_selem_id_t *sid;
snd_mixer_selem_id_alloca(&sid);
snd_mixer_selem_id_set_index(sid,0);
snd_mixer_selem_id_set_name(sid, selem_name);
return sid;
}
void
incMasterVol(long step){ // Step is a value in [-100,100]
long min,max,curr;
snd_mixer_t *handle = getHandle();
/*snd_mixer_open(&handle, 0);
snd_mixer_attach(handle, card);
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(handle);*/
snd_mixer_selem_id_t *sid;
snd_mixer_selem_id_alloca(&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);
snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_SIDE_LEFT, &curr);
curr = curr * 100/max;
if(curr + step > 100)
curr = 100;
else if(curr + step < 0)
curr = 0;
else
curr += step;
snd_mixer_selem_set_playback_volume_all(elem, curr * max / 100);
snd_mixer_close(handle);
}
void
toggleMasterVol(){
int swiVal;
snd_mixer_t *handle = getHandle();
snd_mixer_elem_t *elem = snd_mixer_find_selem(handle,getSid());
snd_mixer_selem_get_playback_switch(elem, SND_MIXER_SCHN_SIDE_LEFT, &swiVal);
snd_mixer_selem_set_playback_switch_all(elem, !swiVal);
snd_mixer_close(handle);
}
int
main(int argc, char *argv[]){
incMasterVol(5);
}