用 C 语言在 Linux 上发出哔哔声

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10072909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:41:40  来源:igfitidea点击:

Beep on Linux in C

clinuxioctlbeepexternal-dependencies

提问by omnidan

I want to generate a beep sound with a specific frequency and length (for different sound signals) using the system beeper (and only the speakers if beeper is not available / accessible). I know it is possible to do this by using ioctl, but that requires root access, which I don't want.

我想使用系统蜂鸣器(如果蜂鸣器不可用/可访问,则只有扬声器)生成具有特定频率和长度(针对不同声音信号)的蜂鸣声。我知道可以通过使用 ioctl 来做到这一点,但这需要 root 访问权限,这是我不想要的。

I know I could just use the "beep" command, but that would be a dependency, which, if possible, shouldn't be used (no external dependencies at all, just the basic linux libraries and C).

我知道我可以只使用“beep”命令,但这将是一个依赖项,如果可能,不应该使用它(根本没有外部依赖项,只有基本的 linux 库和 C)。

What I currently have is the following code (but this requires superuser privileges to run):

我目前拥有的是以下代码(但这需要超级用户权限才能运行):

#include <stdlib.h>
#include <fcntl.h>
#include <linux/kd.h>

int main(int argc, char *argv[])
{
 int fd = open("/dev/console", O_RDONLY);
 if (fd == -1 || argc != 3) return -1;
 return ioctl(fd, KDMKTONE, (atoi(argv[2])<<16)+(1193180/atoi(argv[1])));
}

If there is no other way to do this, then I will use beep, but I would really like to avoid dependencies and integrate the beep directly into my script, but I'm sure somebody here will know a solution / workaround.

如果没有其他方法可以做到这一点,那么我将使用 beep,但我真的很想避免依赖性并将 beep 直接集成到我的脚本中,但我相信这里有人会知道解决方案/解决方法。

I don't really want external libraries as the program should be as lightweight as possible.

我真的不想要外部库,因为程序应该尽可能轻量级。

采纳答案by omnidan

I think the only way to do this is to either use suid to give my own program root access, or to use beep, which already has suid. I suppose I will just add one more dependency, then, as beepis not too big anyway.

我认为这样做的唯一方法是使用 suid 为我自己的程序提供 root 访问权限,或者使用beep已经有 suid 的 。我想我会再添加一个依赖项,因为beep无论如何都不会太大。

Thank you for all the answers, I'm sure other libraries are great for more complex signals, but I need a very simple one!

感谢您提供所有答案,我相信其他库非常适合更复杂的信号,但我需要一个非常简单的!

I think this question can be marked as solved / closed, then.

我认为这个问题可以标记为已解决/已关闭。

If anybody finds a way to create a beep using the console without superuser-privileges, I'm still interested in this solution :)

如果有人找到了一种在没有超级用户权限的情况下使用控制台创建蜂鸣声的方法,我仍然对这个解决方案感兴趣 :)

Thank you all again.

再次感谢大家。

回答by Pablo Santa Cruz

Try using an audio library such as OpenAL.

尝试使用诸如OpenAL 之类的音频库。

回答by wildplasser

The most basic beep is still '\a' , if your terminal supports it:

如果您的终端支持,最基本的提示音仍然是 '\a' :

fprintf(stdout, "\aBeep!\n" );

回答by dappiu

Please look at the standard linux beepsource code. http://www.johnath.com/beep/beep.c

请查看标准的 linuxbeep源代码。 http://www.johnath.com/beep/beep.c

It uses KIOCSOUND ioctl to "beep", but you don't need superuser privileges to make it play. I have configured it to be readable and executable by users on the "beep" group.

它使用 KIOCSOUND ioctl 来“发出哔哔声”,但您不需要超级用户权限就可以播放。我已将其配置为“beep”组中的用户可读和可执行。

So my standard user with UID 1000 is in the group with GID 501 (i called it "beep"). Next to this I had to chmod 4750 /usr/bin/beepand now I'm able to play beeps (in the range 20-20000Hz) without asking for superuser privileges.

所以我的 UID 为 1000 的标准用户在 GID 501 的组中(我称之为“哔”)。接下来我不得不这样做chmod 4750 /usr/bin/beep,现在我可以在不要求超级用户权限的情况下播放哔哔声(在 20-20000Hz 范围内)。

回答by karel

open("/dev/tty", O_RDONLY); instead /dev/console will work with sound OK for terminal consoles without requiring superuser privileges. But virtual consoles in X session will fail with sound then, even for superuser.

open("/dev/tty", O_RDONLY); 相反, /dev/console 将在终端控制台上正常工作,而无需超级用户权限。但是,即使对于超级用户,X 会话中的虚拟控制台也会出现声音失败。