我找到了关于如何设置音量的this非常有用的帖子,从中我能够推断出snd_mixer_selem_get_playback_volume()的存在以检索当前设置.然而,在我的系统上,这似乎给了我错误的读数 – 混音器程序显示100%,最高约66%.
如果我打开pavucontrol,我可以看到这个输出设备的音量与我从这里得到的读数相匹配,所以我假设它给了我硬件音量设置,而不是我想要的全局主音量.
解决方法 使用gcc audio_volume.c -o audio_volume -lasound或使用gcc audio_volume.c -o audio_volume_oss -DOSSCONTRol编译以下代码. OSS版本写得很粗糙,并没有给出精确的结果.在我的系统中将音量设置为100可设置97%的音量. ALSA版本适用于我和openSUSE中的pulseAudio.需要一些清理和爱,但我希望它是你需要的,我允许在 WTFPL license上使用它.#include <unistd.h>#include <fcntl.h>#ifdef OSSCONTRol#define mixer_DEV "/dev/dsp"#include <sys/soundcard.h>#include <sys/ioctl.h>#include <stdio.h>#else#include <alsa/asoundlib.h>#endiftypedef enum { AUdio_VolUME_SET,AUdio_VolUME_GET,} audio_volume_action;/* Drawbacks. Sets volume on both channels but gets volume on one. Can be easily adapted. */int audio_volume(audio_volume_action action,long* outvol){#ifdef OSSCONTRol int ret = 0; int fd,devs; if ((fd = open(mixer_DEV,O_WRONLY)) > 0) { if(action == AUdio_VolUME_SET) { if(*outvol < 0 || *outvol > 100) return -2; *outvol = (*outvol << 8) | *outvol; ioctl(fd,SOUND_mixer_WRITE_VolUME,outvol); } else if(action == AUdio_VolUME_GET) { ioctl(fd,SOUND_mixer_READ_VolUME,outvol); *outvol = *outvol & 0xff; } close(fd); return 0; } return -1;;#else snd_mixer_t* handle; snd_mixer_elem_t* elem; snd_mixer_selem_ID_t* sID; static const char* mix_name = "Master"; static const char* card = "default"; static int mix_index = 0; long pmin,pmax; long get_vol,set_vol; float f_multi; 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(action == AUdio_VolUME_GET) { if(snd_mixer_selem_get_playback_volume(elem,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 } else if(action == AUdio_VolUME_SET) { if(*outvol < 0 || *outvol > VolUME_BOUND) // out of bounds return -7; *outvol = (*outvol * (maxv - minv) / (100-1)) + minv; if(snd_mixer_selem_set_playback_volume(elem,*outvol) < 0) { snd_mixer_close(handle); return -8; } if(snd_mixer_selem_set_playback_volume(elem,1,*outvol) < 0) { snd_mixer_close(handle); return -9; } fprintf(stderr,"Set volume %i with status %i\n",ret); } snd_mixer_close(handle); return 0;#endif }int main(voID){ long vol = -1; printf("Ret %i\n",audio_volume(AUdio_VolUME_GET,&vol)); printf("Master volume is %i\n",vol); vol = 100; printf("Ret %i\n",audio_volume(AUdio_VolUME_SET,&vol)); return 0;}总结
以上是内存溢出为你收集整理的在Linux中获取C中的主音量全部内容,希望文章能够帮你解决在Linux中获取C中的主音量所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)