由于这些文件不是普通的文件,所以我们不能用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.92.* 于 2006-12-24 07:34:25发表:
Well done!
free poker game download | joker poker | world series of poker download | free online casinos | full poker review tilt | party poker bonus | poker site us | games slots | online slots | real poker
65.110.43.* 于 2006-12-24 07:33:36发表:
Good design!
bingo games | play poker on line | no limit holdem | gamble poker | draw poker | world poker tour | slots free | online texas holdem | online slot machines | online casino blackjack
65.110.43.* 于 2006-12-24 07:32:58发表:
Well done!
internet poker | black jack game | play texas holdem | free casino downloads | keno online | planet poker | free casino games | poker room | gambling poker machine | play slot machines for free
72.232.93.* 于 2006-12-23 09:39:01发表:
Nice site!
no limit texas holdem | paigow poker | russian roulette game | three card poker | crazy game of poker | online hold em | free poker tournaments | free online bingo games | gamble poker | online casino
72.232.93.* 于 2006-12-23 09:38:00发表:
Great work!
blackjack software | no limit hold em | poker tour | play free slot machine games | poker game | no download slots | cleopatra slots | slots for free | free slots | no deposit online casinos
65.110.43.* 于 2006-12-23 09:36:47发表:
Well done!
full poker review tilt | online poker game | card game poker | casino game online | download casino games | poker texas hold em | online poker room | download poker | internet poker game | download free poker games
65.110.43.* 于 2006-12-23 09:35:58发表:
Thank you!
free blackjack game | online slots free | free online casino slots | free poker game | casino stud poker | free money no deposit casinos | bingo online | texas poker | cleopatra slot machine | casino portal
65.110.43.* 于 2006-12-23 09:35:42发表:
Good design!
online poker games | internet casino poker | play texas holdem | free omaha poker | playing roulette | free slot machine | best casinos | play free slot machine game | video slot machines | 10 best online casinos
65.110.43.* 于 2006-12-22 13:03:57发表:
Good design!
casinos with no deposit | flash casino | free no download slots | play poker | free online strip blackjack | aussie slots | download party poker | internet casino gambling | online poker us | poker sets
72.232.92.* 于 2006-12-22 13:02:57发表:
Well done!
free no deposit casinos | free slot | slot game | free games on line | on line gambling | real money poker | live poker | live poker rooms | flash casinos | no limit holdem
65.110.43.* 于 2006-12-22 13:02:14发表:
Good design!
free poker on line | free slot machine downloads | play free slots | casino games online | casino games free | gambling casinos | poker stars | free online poker game | play blackjack | black jack games
65.110.43.* 于 2006-12-22 13:01:53发表:
Well done!
gambling poker | online gambling holdem | poker game online | poker bonus | playing poker | free slots on line | blackjack odds | texas hold em poker online | poker site usa | omaha poker
65.110.43.* 于 2006-12-22 13:01:38发表:
Nice site!
online poker review room | free casinos | free poker downloads | free online slots | casino bonus | online texas hold em | play free slot machines | free on line slots | no download slots | strip black jack
72.232.93.* 于 2006-12-21 13:23:14发表:
Great work!
online slot machines | play free video slots | poker web sites | download casino games | black jack game | free poker game | free bingo | casino no deposit | poker games | poker usa
65.110.43.* 于 2006-12-21 13:22:05发表:
Nice site!
online poker usa | strip black jack | blackjack download | free three card poker | free online slot machines | free slot machine games | poker site us | online poker game | free on line slots | poker bonus
65.110.43.* 于 2006-12-21 13:21:38发表:
Thank you!
free casino online | bingo online | planet poker | poker for free | online poker rating room | free gambling | free money casino | casinos with no deposit | poker casino | free roulette
72.232.93.* 于 2006-12-21 13:21:19发表:
Great work!
casino on net | 10 best online casinos | flash casino games | world poker tour | poker hold em | playing roulette | keno | internet casino poker | online gaming | free strip blackjack
72.232.92.* 于 2006-12-21 13:21:02发表:
Thank you!
free poker tournaments | online poker games | slot machine | poker hands | poker rooms | casino games online | poker on line | poker usa open | no limit texas holdem | blackjack free games
65.110.43.* 于 2006-12-20 03:22:47发表:
Nice site!
free slot machines games | free play slot machines | free blackjack game | black jack | planet poker | free online slots no download | poker cards | roulette free games | poker money | poker site
65.110.43.* 于 2006-12-20 03:22:06发表:
Good design!
free online poker games | free online slots games | no deposit online casinos | computer poker games | internet poker game | play free slot machine game | play roulette | blackjack online free | online slots | no limit hold em
65.110.43.* 于 2006-12-20 03:21:41发表:
Well done!
free online casino slots | play internet poker online | free video poker | play free cleopatra slots online | free casino | blackjack online | online casino slots | poker for fun | free on line slot machines | texas hold em
65.110.43.* 于 2006-12-20 03:21:10发表:
Thank you!
best internet casinos | free poker game download | keno games | machine games | black jack download | poker for free | free roulette | free bingo | free no deposit online casinos | poker machine
65.110.43.* 于 2006-12-20 03:20:53发表:
Nice site!
free poker game | poker game | download casino | caribbean poker online | black jack free | texas holdem online | poker sites accepting us | online gambling bonuses | bingo online | free slots games
65.110.43.* 于 2006-12-19 03:36:12发表:
Well done!
internet poker game | online poker review room | internet bingo | texas hold em poker online | poker hold em | poker site us | online poker software | texas hold em poker | slot machine | online blackjack
65.110.43.* 于 2006-12-19 03:35:24发表:
Great work!
internet casino gambling online | poker accept us | poker directory | no deposit bonus | new online casinos | free slots download | free online bingo | playing poker | texas holdem online | free online texas holdem
65.110.43.* 于 2006-12-19 03:34:53发表:
Great work!
free casino money | gambling game | poker site usa | slot games free | texas hold em | free online strip blackjack | free omaha poker | blackjack software | play black jack | free slots games
72.232.93.* 于 2006-12-19 03:34:42发表:
Nice site!
multi player poker | poker free | play free blackjack online | strip blackjack | poker for fun | free poker websites | online black jack | best casinos | free money no deposit casinos | poker us welcome
65.110.43.* 于 2006-12-19 03:34:13发表:
Nice site!
slots for fun | joker poker | no download slots | poker star | play free cleopatra slots online | poker sites accept us | poker game online | online free poker | free online poker game | free slots on line
65.110.43.* 于 2006-12-18 03:40:42发表:
Great work!
play poker online | video slots | no limit texas holdem | poker tables | poker game free | black jack | gambling sites | holdem poker texas | online poker us | poker site us
65.110.43.* 于 2006-12-18 03:39:14发表:
Well done!
poker sites accept us | poker star | free black jack games | new online casinos | download free poker | on line poker | poker hold em | keno online | roulette download | free video slots
65.110.43.* 于 2006-12-18 03:38:21发表:
Good design!
casinos on line | poker texas holdem | poker for us players | play blackjack free | slots for free | no deposit bingo online | poker rooms accept us | free online strip blackjack | play roulette online | play casino games
72.232.93.* 于 2006-12-18 03:37:54发表:
Good design!
texas hold em poker | joker poker | bingo cards | free download poker | free slots no download | free no deposit online casinos | free online slot machines | blackjack game | roulette free games | casino games online
72.232.93.* 于 2006-12-18 03:37:34发表:
Well done!
poker for free | play bingo | casino on net | stud poker | gambling games | slot online | casino free games | casino game download | free slot machine | casino slot machine
65.110.43.* 于 2006-12-17 01:06:30发表:
Good design!
gambling machines | free casino | free casino online | texas hold em poker | slots online | free poker tournaments | online roulette | download party poker | texas holdem free | no deposit bingo online
65.110.43.* 于 2006-12-17 01:04:50发表:
Well done!
play poker online | free casino slot games | slot online | free slot play | free roulette | online gambling casino | gambling online | free black jack | texas holdem poker | online slots
65.110.43.* 于 2006-12-17 01:04:04发表:
Nice site!
poker online | free poker websites | play casino games | internet bingo | online poker software | keno online | flash casinos | online holdem | play poker on line | free online slots
65.110.43.* 于 2006-12-17 01:03:41发表:
Nice site!
free online blackjack | online poker rooms | best online poker room | download free poker games | free three card poker | play internet poker online | free fun slots | free online poker game | poker site us | online casino poker
65.110.43.* 于 2006-12-17 01:03:23发表:
Great work!
play free slot machine games | absolute poker | casino online games | online poker usa | free bingo | sims free slots | casino games free | play texas holdem | play poker game | full poker review tilt
65.110.43.* 于 2006-12-16 01:32:21发表:
Nice site!
play free slot machine game | free download poker | free no deposit online casinos | roulette free | money poker | casino games online | virtual poker | casinos online | free video slots | casino download
72.232.93.* 于 2006-12-16 01:31:13发表:
Good design!
poker for fun | black jack free | free online bingo | free roulette | play free slot machine games | online poker usa | no limit texas holdem | poker downloads | free on line slot machines | slots games