如何用c ++制作硬件蜂鸣声?
由于
答案 0 :(得分:61)
答案 1 :(得分:47)
如果您使用的是Windows操作系统,则会有一个名为Beep()
#include <iostream>
#include <windows.h> // WinApi header
using namespace std;
int main()
{
Beep(523,500); // 523 hertz (C5) for 500 milliseconds
cin.get(); // wait
return 0;
}
来源:http://www.daniweb.com/forums/thread15252.html
对于基于Linux的操作系统,有:
echo -e "\007" >/dev/tty10
如果你不想在Windows中使用Beep()
,你可以这样做:
echo "^G"
答案 2 :(得分:8)
有一些特定于操作系统的例程可用于发出哔哔声。
在类Unix操作系统上,尝试the (n)curses beep() function。这可能比其他人建议的'\a'
更容易移植,尽管对于大多数可能有用的终端模拟器而言。
在某些* BSD中有一个PC speaker device。读取驱动程序源,SPKRTONE
ioctl似乎对应于原始硬件接口,但似乎还有一个围绕write()
构建驱动程序字符串的高级语言,如联机帮助页中所述
看起来Linux有一个类似的驱动程序(例如,请参阅this article;如果向下滚动一下,this page上还会有一些示例代码。)。
< / LI>在Windows中有一个名为Beep()的函数。
答案 3 :(得分:5)
或者在包含stdio.h后的c或c ++中
char d=(char)(7);
printf("%c\n",d);
(char)7被称为钟形字符。
答案 4 :(得分:4)
std::cout << '\7';
答案 5 :(得分:3)
答案 6 :(得分:2)
最简单的方法可能就是打印an ascii bell
答案 7 :(得分:2)
#include<iostream>
#include<conio.h>
#include<windows.h>
using namespace std;
int main()
{
Beep(1568, 200);
Beep(1568, 200);
Beep(1568, 200);
Beep(1245, 1000);
Beep(1397, 200);
Beep(1397, 200);
Beep(1397, 200);
Beep(1175, 1000);
cout<<endl;
_getch()
return 0
}
答案 8 :(得分:2)
您可以使用条件编译:
#ifdef WINDOWS
#include <Windows.h>
void beep() {
Beep(440, 1000);
}
#elif LINUX
#include <stdio.h>
void beep() {
system("echo -e "\007" >/dev/tty10");
}
#else
#include <stdio.h>
void beep() {
cout << "\a" << flush;
}
#endif
答案 9 :(得分:0)
ASCII铃声字符可能正是您要找的。 this表中的第7号。
答案 10 :(得分:0)
cout << "\a";
在Xcode中,编译完成后,您必须手动运行可执行文件才能听到哔哔声。
答案 11 :(得分:0)
我在这里尝试了大多数事情,但都没有在我的Ubuntu VM上工作。
这是一个快速的技巧(功劳归here):
#include <iostream>
int main() {
system("(speaker-test -t sine -f 1000)& pid=$!; sleep 1.0s; kill -9 $pid");
}
它将基本上使用系统的speaker-test
发出声音。但是,该操作不会很快终止,因此该命令在后台(&
部分)运行它,然后捕获其进程ID(pid=$1
部分),休眠一定的时间,您可以更改它( sleep 1.0s
部分),然后杀死该进程(kill -9 $pid
部分)。
sine
是产生的声音。您可以将其更改为pink
或wav
文件。