由于这些文件不是普通的文件,所以我们不能用ANSI C(标准C)的fopen、fclose等来操作文件,而应该使用系统文件I/O处理函数(open、read、write、lseek和close)来处理这些设备文件。ioctl()或许是Linux下最庞杂的函数,它可以控制各种文件的属性,在Linux声音设备编程中,最重要的就是使用此函数正确设置必要的参数。
下面我们举两个实际的例子来说明如何实现Linux下的声音编程。由于此类编程涉及到系统设备的读写,所以,很多时候需要你有root权限,如果你将下面的例子编译后不能正确执行,那么,首先请你检查是否是因为没有操纵某个设备的权限。
对内部扬声器编程内部扬声器是控制台的一部分,所以它对应的设备文件为/dev/console。变量KIOCSOUND在头文件 /usr /include /linux /kd.h中声明,ioctl函数使用它可以来控制扬声器的发声,使用规则为:
锟斤拷锟斤拷:ioctl ( fd, KIOCSOUND, (int) tone);
fd为文件设备号,tone 是音频值。当tone为0时,终止发声。必须一提的是它所理解的音频和我们平常以为的音频是不同的,由于计算机主板定时器的时钟频率为1.19MHZ,所以要进行正确的发声,必须进行如下的转换:扬声器音频值=1190000/我们期望的音频值。
扬声器发声时间的长短我们通过函数usleep(unsigned long usec)来控制。它是在头文件/usr /include /unistd.h中定义的,让程序睡眠usec微秒。下面即是让扬声器按指定的长度和音频发声的程序的完整清单:
锟斤拷锟斤拷:#include < fcntl.h >
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
#include < unistd.h >
#include < sys/ioctl.h >
#include < sys/types.h >
#include < linux/kd.h >
/* 设定默认值 */
#define DEFAULT_FREQ 440 /* 设定一个合适的频率 */
#define DEFAULT_LENGTH 200 /* 200 微秒,发声的长度是以微秒为单位的*/
#define DEFAULT_REPS 1 /* 默认不重复发声 */
#define DEFAULT_DELAY 100 /* 同样以微秒为单位*/
/* 定义一个结构,存储所需的数据*/
typedef struct {
int freq; /* 我们期望输出的频率,单位为Hz */
int length; /* 发声长度,以微秒为单位*/
int reps; /* 重复的次数*/
int delay; /* 两次发声间隔,以微秒为单位*/
} beep_parms_t;
/* 打印帮助信息并退出*/
void usage_bail ( const char *executable_name ) {
printf ( "Usage: \n \t%s [-f frequency] [-l length] [-r reps] [-d delay] \n ",
executable_name );
exit(1);
}
/ * 分析运行参数,各项意义如下:
* "-f <以HZ为单位的频率值 >"
* "-l <以毫秒为单位的发声时长 >"
* "-r <重复次数 >"
* "-d <以毫秒为单位的间歇时长 >"
*/
void parse_command_line(char **argv, beep_parms_t *result) {
char *arg0 = *(argv++);
while ( *argv ) {
if ( !strcmp( *argv,"-f" )) { /*频率*/
int freq = atoi ( *( ++argv ) );
if ( ( freq <= 0 ) | | ( freq > 10000 ) ) {
fprintf ( stderr, "Bad parameter: frequency must be from 1..10000\n" );
exit (1) ;
} else {
result->freq = freq;
argv++;
}
} else if ( ! strcmp ( *argv, "-l" ) ) { /*时长*/
int length = atoi ( *(++argv ) );
if (length < 0) {
fprintf(stderr, "Bad parameter: length must be >= 0\n");
exit(1);
} else {
result->length = length;
argv++;
}
} else if (!strcmp(*argv, "-r")) { /*重复次数*/
int reps = atoi(*(++argv));
if (reps < 0) {
fprintf(stderr, "Bad parameter: reps must be >= 0\n");
exit(1);
} else {
result->reps = reps;
argv++;
}
} else if (!strcmp(*argv, "-d")) { /* 延时 */
int delay = atoi(*(++argv));
if (delay < 0) {
fprintf(stderr, "Bad parameter: delay must be >= 0\n");
exit(1);
} else {
result->delay = delay;
argv++;
}
} else {
fprintf(stderr, "Bad parameter: %s\n", *argv);
usage_bail(arg0);
}
}
}
int main(int argc, char **argv) {
int console_fd;
int i; /* 循环计数器 */
/* 设发声参数为默认值*/
beep_parms_t parms = {DEFAULT_FREQ, DEFAULT_LENGTH, DEFAULT_REPS,
DEFAULT_DELAY};
/* 分析参数,可能的话更新发声参数*/
parse_command_line(argv, &parms);
/* 打开控制台,失败则结束程序*/
if ( ( console_fd = open ( "/dev/console", O_WRONLY ) ) == -1 ) {
fprintf(stderr, "Failed to open console.\n");
perror("open");
exit(1);
}
/* 真正开始让扬声器发声*/
for (i = 0; i < parms.reps; i++) {
/* 数字1190000从何而来,不得而知*/
int magical_fairy_number = 1190000/parms.freq;
ioctl(console_fd, KIOCSOUND, magical_fairy_number); /* 开始发声 */
usleep(1000*parms.length); /*等待... */
ioctl(console_fd, KIOCSOUND, 0); /* 停止发声*/
usleep(1000*parms.delay); /* 等待... */
} /* 重复播放*/
return EXIT_SUCCESS;
}
将上面的例子稍作扩展,用户即可以让扬声器唱歌。只要找到五线谱或简谱的音阶、音长、节拍和频率、发声时长、间隔的对应关系就可以了。我现在还记得以前在DOS下编写出《世上只有妈妈好》时的兴奋。最后,说一些提外话,这其实是一个很简单的程序,但是我们却用了很长的篇幅,希望读者从以上的代码里能体会到写好的程序的一些方法,或许最重要的是添加注释吧。一个程序的注释永远不会嫌多,即便你写的时候觉得它根本是多余,但相信我,相信曾这样告诉我们的许多优秀的程序员:养成写很多注释的习惯。


72.232.63.* 于 2007-03-15 21:30:30发表:
Thank you!
polyphonic ringtones | free mp3 ringtone downloads | free sprint pcs ringtones | ringtone uploader | online ringtone converter | free mp3 download | free sprint ringtones | free ringtones for t-mobile | sprint ringtones | ringtones
65.110.43.* 于 2007-03-15 21:29:42发表:
Well done!
blackberry ringtones | alltel ringtones | ringtone motorola | hindi ringtone | free real music ringtones | free alltel ringtones | lg free ringtones | ringtones for nextel | ringtones for tracfone | us cellular ring tones
72.232.250.* 于 2007-03-15 21:28:52发表:
Good design!
razr v3 ringtones | midi ringtone | ringtone download | free ringtones downloads | mosquito ring tone | download free ringtone | nokia downloads | free ringtones download | 24 ringtones | 24 ctu ringtones
72.232.250.* 于 2007-03-15 21:28:17发表:
Nice site!
free boost ringtones | u s cellular ringtones | cingular wireless ringtones | christian ringtones | free motorola ringtones | funny ringtones | polyphonic ringtone | mp3 ringtones download | free cingular cell phone ringtones | free nextel downloads
72.232.250.* 于 2007-03-14 22:28:31发表:
Thank you!
download poker | download poker games | free poker downloads | poker for free | blackjack strip | gambling sites | free slot game | free online casino games | online slots free | free three card poker
65.110.43.* 于 2007-03-14 22:27:54发表:
Great work!
russian roulette | free slots no download | casino games | full tilt poker | black jack strip | slot machines | texas hold em | free on line poker | us poker sites | free money no deposit
65.110.43.* 于 2007-03-14 22:27:25发表:
Thank you!
download poker | download poker games | free poker downloads | poker for free | blackjack strip | gambling sites | free slot game | free online casino games | online slots free | free three card poker
72.232.63.* 于 2007-03-14 22:26:09发表:
Nice site!
play poker for free | online roulette | free video slots | casino no deposit | free sim slots | free no deposit casino | free online casinos | play money poker | play slots for fun | free online slots no download
72.232.63.* 于 2007-03-14 22:24:44发表:
Well done!
texas poker | online texas holdem | free texas holdem | poker texas holdem | free video slot games | roulette games | online strip blackjack | computer poker | internet poker | casino slots
65.110.43.* 于 2007-03-14 22:24:05发表:
Thank you!
strip blackjack free | free slot machine play | free poker sites | free online games | blackjack online free | slot machine | free poker on line | free bingo games | online slot machines | play free slots
65.110.43.* 于 2007-03-13 21:42:26发表:
Well done!
video slot machines | play casino games | on line gambling | free gambling | flash casino games | free online bingo games | blackjack for free | free slot | download slots | play roulette online
72.232.63.* 于 2007-03-13 21:41:51发表:
Well done!
video slot machine | free sim slots | roulette | sims slots | texas hold em online | new no deposit online casinos | online blackjack free | pacific poker | blackjack software | play bingo
72.232.250.* 于 2007-03-13 21:41:17发表:
Good design!
texas holdem free | slot machine | gambling machines | monopoly slot machine | free slots | poker software | free poker | flash casinos | free poker downloads | black jack game
72.232.63.* 于 2007-03-13 21:40:56发表:
Thank you!
free roulette games | free texas holdem online | play free slot machines | caribbean poker | free bingo online | internet casinos | free casino games | free online games | poker games download | no deposit bingo
65.110.43.* 于 2007-03-13 21:40:52发表:
Nice site!
slots online | no download casino | blackjack online free | black jack online | free poker games | free casino slots | free roulette | poker free | paradise poker | holdem poker
65.110.43.* 于 2007-03-13 04:36:06发表:
Nice site!
aussie slots | free online slots | strip blackjack | play free blackjack online | online texas holdem | slot machine software | free bingo games | free casino slot games | free slot | star poker
72.232.250.* 于 2007-03-13 04:35:40发表:
Thank you!
free poker websites | download free poker game | online slot machines | slim slots | video slot machine | free black jack | free strip black jack | play black jack | play texas hold em | free online slot machine games
72.232.63.* 于 2007-03-13 04:35:01发表:
Well done!
strip blackjack | no deposit casino bonus | online roulette free | no limit texas hold em | new free money online casinos | free fun slots | free poker sites | gambling games | free slots no downloads | free monopoly online
65.110.43.* 于 2007-03-13 04:34:21发表:
Great work!
us poker sites | roulette game | free slots machines | slots online | internet blackjack | free money casinos | poker for fun | free slots game | online bingo games | free on line poker
72.232.250.* 于 2007-03-13 04:34:01发表:
Nice site!
play poker online | on line casinos | flash casino | free sim slots | play money poker | online casino | poker downloads | poker stars | games on line | download casino games
72.232.63.* 于 2007-03-11 20:18:49发表:
Thank you!
free online slots | gambling games | slot machine software | download free poker games | play black jack | free texas hold em online | casino download | strip blackjack games | absolute poker | free monopoly slots
72.232.63.* 于 2007-03-11 20:18:16发表:
Good design!
free strip blackjack | free slot machines | free poker online | bingo online | casinos online | casino game download | free online texas holdem | pokerstars download | free online casino | blackjack games
65.110.43.* 于 2007-03-11 20:17:59发表:
Well done!
video slot games | poker game online | online gambling sites | no deposit bonus | download poker | poker online | slot machines games | flash casino games | play bingo | free blackjack online
72.232.250.* 于 2007-03-11 20:17:29发表:
Great work!
online slots free | free poker sites | free bingo online | free play slot machines | on line gambling | poker for free | cleopatra slots | poker download | free casino games online | slim slots
72.232.63.* 于 2007-03-11 20:17:15发表:
Great work!
online texas holdem | texas holdem free online | monopoly online | poker game download | free monopoly online | new online casinos | free strip black jack | poker web sites | internet casinos | online poker game
65.110.43.* 于 2007-03-10 20:22:12发表:
Thank you!
free video slots | free slot games online | casino slot games | computer poker | blackjack online free | casino slots | online casino games | black jack | free poker games | bingo online
72.232.250.* 于 2007-03-10 20:21:49发表:
Thank you!
texas hold em poker | video slot machines | online strip blackjack | play free online slots | download poker | monopoly online | roulette download | free strip black jack | casino free games | play free roulette
72.232.250.* 于 2007-03-10 20:21:24发表:
Great work!
no deposit casino bonus | free video | slot machine games free | cleopatra slots | free multi line slots | online texas holdem | online gambling | free online blackjack | joker poker | planet poker
65.110.43.* 于 2007-03-10 20:20:33发表:
Thank you!
free blackjack online | casino free | slots online | online free poker | poker games download | new no deposit casino | black jack games | online poker usa | roulette game | free online slot machines
65.110.43.* 于 2007-03-10 20:20:14发表:
Thank you!
free online video slots | blackjack for free | new casinos | empire poker | on line poker | free online texas hold em | world series of poker download | games on line | free slots machines | poker texas
65.110.43.* 于 2007-03-09 18:48:16发表:
Good design!
Play Bingo | Roulette download | Online poker game | Poker usa | Free Online Games | Internet Blackjack | Casino on net | No Download Casino | Video poker download | Free casino downloads
65.110.43.* 于 2007-03-09 18:47:45发表:
Great work!
New No Deposit Casinos | Free Poker Downloads | Poker Web Sites | Online roulette | Free Online Slots Games | Joker Poker | Slot Machines | Black Jack Strip | Download poker games | Free Bingo Games
65.110.43.* 于 2007-03-09 18:47:15发表:
Great work!
Play Roulette Online | Free Online Strip Blackjack | Free poker downloads | Free Slots Online | Poker game download | Download Free Poker | Free Poker Games Online | Casino on Net | Free Slot Machines | Black Jack
65.110.43.* 于 2007-03-09 18:46:47发表:
Great work!
Online Casino Games | Blackjack Game | Texas Holdem Free Online | Slot Machine Games Free | Online Poker Sites | Full Tilt Poker | Blackjack Software | Caribbean poker | Free Online Video Slots | Casino Bonus
65.110.43.* 于 2007-03-09 18:46:33发表:
Great work!
No deposit casino bonus | Slot Machine Games | Slots For Free | No download casino | Free Poker Downloads | Poker Downloads | Star Poker | Free Slot Play | New online casinos | Poker on line
72.232.250.* 于 2007-03-08 15:41:01发表:
Thank you!
online video slots | casino on line | on line gambling | free online casino games | free casinos | games online | black jack online | slots of fun | slot machine | slots free
72.232.63.* 于 2007-03-08 15:40:39发表:
Great work!
free casino online | strip blackjack games | gambling games | play free slots online | poker machines | online roulette free | free money no deposit casinos | free bingo | black jack | free blackjack games
65.110.43.* 于 2007-03-08 15:40:10发表:
Good design!
games slots | poker free | play roulette | texas hold em | online bingo games | slot machine games | roulette | download free poker game | strip blackjack | casino online
65.110.43.* 于 2007-03-08 15:39:43发表:
Nice site!
free online slot machines | download free poker | play free slot machines | full tilt poker | online slots free | sims slots | casino slot games | play slots | free casino | play poker online
72.232.63.* 于 2007-03-08 15:39:10发表:
Great work!
blackjack online | free games online | free roulette | poker online free | world series poker | internet casino | free black jack | black jack download | free money casino | free video poker