博文

目前显示的是标签为“happybirthday”的博文

Shell脚本弹奏中文版“生日快乐歌”

在《Shell命令控制蜂鸣器发声》一文中,我们了解到了如何在Ubuntu下安装beep命令来控制PC主板上蜂鸣器发声,这次我们想让蜂鸣器弹奏出中文版的“生日快乐歌”,首先还是要用sudo modprobe pcspkr命令加载驱动,然后在Shell脚本所在目录下执行bash -e beep_birthday_song.sh命令,这样就可以执行我们写的脚本弹奏了,我们的脚本名字为beep_birthday_song.sh,内容如下: #!/bin/sh freq=(392 392 440 392 523 494 392 392 440 392 587 523 392 392 784 659 523 494 440 698 698 659 523 587 523) delay=(375 125 500 500 500 1000 375 125 500 500 500 1000 375 125 500 500 500 500 1000 375 125 500 500 500 1000) i=0 len=${#freq[*]} while [ $i -lt $len ] do beep -f ${freq[$i]} -l ${delay[$i]} let ++i done 参考网址: http://jpuyy.com/2012/11/linux-beep.html http://stackoverflow.com/questions/12919378/solvedplaying-beep-in-c-linux http://kirrus.co.uk/2010/09/linux-beep-music/ http://snipplr.com/view/62662/jingle-bells-song/ http://wiki.mikrotik.com/wiki/Super_Mario_Theme http://frank-buss.de/beep/index.html http://stackoverflow.com/questions/7247279/bash-set-e-and-i-0let-i-do-not-agree http://zhenshi4597.blog.51cto.com/1891465/871166 ht...

C编程演奏中文版“生日快乐歌”

有了《C编程控制PC蜂鸣器》一文的基础后,我们在其基础上修改函数入口参数,由原来的1个参数(频率)改为现在的2个(频率、延时<即该频率响多长时间>),然后就按照节奏实现中文版的“生日快乐歌”,其源码如下: #include <unistd.h> #include <sys/io.h> /* The clock frequency of the i8253/i8254 PIT */ #define PIT_TICK_RATE 1193182ul void beep(unsigned int freq, unsigned int delay) { unsigned int count = PIT_TICK_RATE / freq; iopl(3); outb_p(0xB6, 0x43); outb_p(count & 0xFF, 0x42); outb((count >> 8) & 0xFF, 0x42); outb_p(inb_p(0x61) | 3, 0x61); usleep(1000*delay); outb_p(inb_p(0x61) & 0xfc, 0x61); iopl(0); } void play(unsigned int* freq, unsigned int* time) { int i; for(i=0;freq[i]!=0;i++) { beep(freq[i], time[i]); } } int main(int argc,char * argv[]) { /*Happy Birthday chinese version*/ unsigned frequency[]= { 392,392,440,392,523,494, 392,392,440,392,587,523, 392...