gpt4 book ai didi

c - 在 Linux 中用 C 语言获取主音量

转载 作者:太空宇宙 更新时间:2023-11-04 11:45:30 26 4
gpt4 key购买 nike

我试图在 Linux 中检索(并且可能稍后设置)主音量。我正在使用 PulseAudio,但理想情况下它也应该适用于 ALSA。

我找到了 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>
#endif


typedef 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, 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
}
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, 0, *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", *outvol, 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;
}

关于c - 在 Linux 中用 C 语言获取主音量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57897914/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com