我想发出哔哔声,但我就是做不到。我已经尝试过:
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
cout << '\a' << flush;
return 0;
}
我也试过使用这个:http://www.johnath.com/beep/ 但根本不会发出哔哔声。
(如果我在终端上运行$ speaker-test -t sine -f 500 -l 2 2>&1
,它会发出哔哔声,但我想用c++发出哔哔声来学习底层声音编程)
而且我希望能够控制频率和持续时间。
最佳答案
除非您从控制台登录,否则 cout
不会引用系统控制台。您需要打开 /dev/console
并将 \a
发送到那里。
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int s = open ("/dev/console", O_WRONLY);
if (s < 0)
perror ("unable to open console");
else
{
if (write (s, "\a", 1) != 1)
perror ("unable to beep");
}
}
关于c++ - 哔哔声不起作用(linux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40443410/