转载地址:http://blog.chinaunix.net/uid-22950545-id-2600584.html
WM8960的音量控制测试程序
一,前言:刚完成的WM8960声卡驱动,其中的控制多是以snd_kcontrol_new的结构形式表现,然后通过snd_ctl_add函数添加到core里面去。为了验证一下控制的正确性,经过对alsa库函数研究了一天,终于是简单的完成了这个音量的控制测试程序。
二程序如下:
/*vol_control.c*/
/*used for volume control test.author:sparkle*/
#include
#include
#include
unsigned char left_volume,right_volume;
int process_cmdline(int argc, char **argv)
{
int i;
if(argc==1){
printf("volume control usage:\n" \
" -all :left and right vol is the same, range:0~255\n" \
" -l :left vol , range:0~255\n" \
" -r :right vol , range:0~255\n\n");
return -1;
}
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-all") == 0 && argc==3) {
left_volume = right_volume=atoi(argv[++i]);
}
else if (strcmp(argv[i], "-l") == 0 && argc==5) {
left_volume = atoi(argv[++i]);
}
else if (strcmp(argv[i], "-r") == 0 && argc==5) {
right_volume = atoi(argv[++i]);
}
else{
printf("volume control usage:\n" \
" -all :left and right vol is the same, range:0~255\n" \
" -l :left vol , range:0~255\n" \
" -r :right vol , range:0~255\n\n");
return -1;
}
}
printf("left_volume = %d\n", left_volume);
printf("right_volume = %d\n", right_volume);
return 0;
}
int main(int argc, char **argv)
{
int idx, err,i;
snd_ctl_t *handle;
char str[128];
snd_ctl_elem_value_t * elem_value;
long * pdata;
if (process_cmdline(argc, argv) < 0) {
return -1;
}
snd_ctl_elem_value_alloca(&elem_value);
/*ccording to my wm8960 driver,the the numid of
snd_kcontrol_new "Playback Volume" is 4*/
snd_ctl_elem_value_set_numid(elem_value,4);
idx = -1;
if ((err = snd_card_next(&idx)) < 0) {
printf("Card next error: %s\n", snd_strerror(err));
return -1;
}
sprintf(str, "hw:CARD=%i", idx);
if ((err = snd_ctl_open(&handle, str, 0)) < 0) {
printf("Open control error: %s\n", snd_strerror(err));
return -1;
}
if ((err = snd_ctl_elem_read(handle,elem_value)) < 0) {
printf("snd_ctl_elem_read error: %s\n", snd_strerror(err));
return -1;
}
pdata=(long *)snd_ctl_elem_value_get_bytes(elem_value);
printf("before changing ,left volume is:%d,right volume is %d\n",*pdata,*(pdata+1));
//to set the new value
snd_ctl_elem_value_set_integer(elem_value,0,left_volume);
snd_ctl_elem_value_set_integer(elem_value,1,right_volume);
if ((err = snd_ctl_elem_write(handle,elem_value)) < 0) {
printf("snd_ctl_elem_write error: %s\n", snd_strerror(err));
return -1;
}
//read it out again to check if we did set the registers.
if ((err = snd_ctl_elem_read(handle,elem_value)) < 0) {
printf("snd_ctl_elem_read error: %s\n", snd_strerror(err));
return -1;
}
pdata=(long *)snd_ctl_elem_value_get_bytes(elem_value);
printf("after changing, left volume is:%d,right volume is %d\n",*pdata,*(pdata+1));
snd_ctl_close(handle);
return 0;
}
三,测试结果:
/nand2 # ./vol_control -l 127 -r 200
left_volume = 127
right_volume = 200
before changing ,left volume is:255,right volume is 255
after changing, left volume is:127,right volume is 200
/nand2 # cd mplayer/
/nand2/mplayer # ./mplayer-y justonelastdance.mp3
结果左右声道的音量是不一样的,测试成功。