由于这些文件不是普通的文件,所以我们不能用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下编写出《世上只有妈妈好》时的兴奋。最后,说一些提外话,这其实是一个很简单的程序,但是我们却用了很长的篇幅,希望读者从以上的代码里能体会到写好的程序的一些方法,或许最重要的是添加注释吧。一个程序的注释永远不会嫌多,即便你写的时候觉得它根本是多余,但相信我,相信曾这样告诉我们的许多优秀的程序员:养成写很多注释的习惯。


65.110.43.* 于 2006-12-31 18:49:38发表:
Thank you!
poker sites accept us | play poker online | online bingo | free casino slot games | holdem poker online | free casino bonus | gambling games | play free roulette | poker web sites | play casino games
72.232.250.* 于 2006-12-31 18:48:45发表:
Well done!
no download games | three card poker | play casino game | poker money | online casino games | play free blackjack | bonus poker | play online poker | no limit texas hold em | no deposit bonus casinos
65.110.43.* 于 2006-12-31 18:48:10发表:
Nice site!
card poker | free three card poker | poker site | black jack games | no limit texas holdem | paradise poker | russian roulette game | playing roulette | free online casino games | poker games online
65.110.43.* 于 2006-12-31 18:47:57发表:
Thank you!
free slot machine downloads | poker accept us | online poker games | flash casino | cleopatra slot machine | free online slots no download | party poker | free online poker game | free slots no download | free blackjack games
72.232.95.* 于 2006-12-30 18:26:00发表:
Good design!
free online texas holdem | free casino | online gaming casinos | sims slots | play roulette online | free slot machine games online | no download casino | black jack game | free online slot machines | easy online real bingo
72.232.95.* 于 2006-12-30 18:25:37发表:
Well done!
free video slots | free on line games | online casino slots | internet casino gambling | free black jack games | aussie slots | free slot | free money no deposit casinos | free no download slots | free strip blackjack
72.232.95.* 于 2006-12-30 18:25:01发表:
Thank you!
gambling online | free blackjack game | casinos on line | texas holdem | free fun slots | slim slots | internet blackjack | strip black jack | free multi line slots | keno
72.232.95.* 于 2006-12-30 18:24:58发表:
Thank you!
gambling online | free blackjack game | casinos on line | texas holdem | free fun slots | slim slots | internet blackjack | strip black jack | free multi line slots | keno
72.232.95.* 于 2006-12-30 18:24:38发表:
Good design!
free online video slots | play bingo online | video slot machines | online roulette | free blackjack games | casino download | no limit hold em | free play slot machines | online holdem | casino on net
72.232.95.* 于 2006-12-30 18:24:07发表:
Good design!
slot machines | new no deposit online casinos | blackjack download | free blackjack | play free roulette | slots games | no deposit online casinos | free casino online | free slot games | gambling on line
72.232.95.* 于 2006-12-30 18:24:02发表:
Good design!
slot machines | new no deposit online casinos | blackjack download | free blackjack | play free roulette | slots games | no deposit online casinos | free casino online | free slot games | gambling on line
72.232.95.* 于 2006-12-30 00:38:32发表:
Great work!
texas holdem online | free blackjack game | no limit holdem | roulette online | casino on line | real poker | no deposit bonus casinos | blackjack game | texas holdem poker online | free casino games
72.232.95.* 于 2006-12-30 00:37:59发表:
Great work!
texas holdem online | free blackjack game | no limit holdem | roulette online | casino on line | real poker | no deposit bonus casinos | blackjack game | texas holdem poker online | free casino games
72.232.95.* 于 2006-12-30 00:36:50发表:
Great work!
poker room rating | bonus poker | roulette download | poker sets | live poker | casino slot machine | free video slots | slot machine | video poker free | party poker download
72.232.95.* 于 2006-12-29 06:00:43发表:
Well done!
free slot play | 10 best online casinos | poker room | free on line games | free blackjack game | free slots for fun | free no deposit online casinos | slots for fun | free blackjack games | poker accepting americans
72.232.95.* 于 2006-12-29 06:00:35发表:
Thank you!
best casinos | free texas holdem | best casino online | online casino blackjack | bingo games | casino stud poker | casino portal | bingo cards | online slots free | poker hands
72.232.95.* 于 2006-12-29 05:59:19发表:
Well done!
casino bonuses | free poker | poker machine | strip blackjack | free online strip blackjack | no download games | multi player poker | slots on line | free slot machine games online | free poker sites
72.232.95.* 于 2006-12-29 05:58:53发表:
Good design!
draw poker | russian roulette game | internet poker | paradise poker review | monopoly online | multiplayer poker | casino royale | full tilt poker | poker us | download free poker games
72.232.95.* 于 2006-12-29 05:58:03发表:
Thank you!
free bingo games | playing poker | gambling sites | free slot machines games | free slot games online | on line poker | free online slots no download | poker online free | money poker | free slot games
72.232.95.* 于 2006-12-28 09:16:51发表:
Great work!
blackjack odds | no deposit bingo online | free slot machine games online | online paigow poker | virtual poker | new casinos | free slot machines games | texas poker | free monopoly slots | stud poker
72.232.95.* 于 2006-12-28 09:16:30发表:
Good design!
world series poker | black jack online | free slots casino | poker accepting americans | poker for us players | casino games | free on line poker | free texas holdem poker | playing roulette | free blackjack game
72.232.95.* 于 2006-12-28 09:16:14发表:
Great work!
blackjack odds | no deposit bingo online | free slot machine games online | online paigow poker | virtual poker | new casinos | free slot machines games | texas poker | free monopoly slots | stud poker
72.232.95.* 于 2006-12-28 09:13:29发表:
Thank you!
free poker on line | slots for free | free slots download | poker cards | free money casino | slot online | online poker us | free bingo games | internet casino | gambling on line
65.110.43.* 于 2006-12-26 22:57:08发表:
Nice site!
caribbean poker | play free blackjack online | free poker downloads | sims free slots | internet gambling sites | free online strip blackjack | omaha poker | casino games free | online poker tournament | planet poker
65.110.43.* 于 2006-12-26 22:55:46发表:
Great work!
poker tour | multi hand poker | roulette free | 3 card poker | new online casinos | free monopoly slots | keno games | texas hold em | play money poker | best internet poker
65.110.43.* 于 2006-12-26 22:54:39发表:
Good design!
casino poker | poker accepting americans | play casino game | poker rooms accepting usa | card poker | casino online | casino on net | casino stud poker | on line poker | free casino online
65.110.43.* 于 2006-12-26 22:53:58发表:
Well done!
free no download slots | online slots | online casinos | poker texas hold em | free gambling games | free poker game | best poker sites | poker us welcome | poker texas holdem | free online slot games
72.232.92.* 于 2006-12-26 22:53:14发表:
Nice site!
casinos on line | free blackjack game | free slots on line | poker game download | video poker download | no deposit bonus | casino games online | free strip blackjack | online holdem | play bingo
65.110.43.* 于 2006-12-26 02:43:23发表:
Well done!
free cash casinos | blackjack free games | poker hands | no deposit casino | online slots free | texas hold em poker | poker rooms | online casino | party poker | free casino cash
65.110.43.* 于 2006-12-26 02:41:40发表:
Good design!
download free poker games | free casino money | keno online | poker tables | slots games | slot machine | free on line games | gambling poker | free online slots games | free online bingo
72.232.93.* 于 2006-12-26 02:40:52发表:
Good design!
internet casinos | internet casino | casino game download | texas hold em poker online | online black jack | paradise poker | online paigow poker | gambling online | roulette download | free money no deposit casinos
72.232.93.* 于 2006-12-26 02:40:13发表:
Great work!
poker machine | free slots | casino on net | online poker us | poker stars download | casino stud poker | texas holdem poker online | online gaming casinos | real poker | live poker
65.110.43.* 于 2006-12-26 02:39:38发表:
Well done!
slot machine game | new online casinos | monopoly slots | blackjack games | live poker rooms | games slots | free 3 card poker | poker game online | free online blackjack | slot machines
65.110.43.* 于 2006-12-25 04:45:25发表:
Nice site!
free poker sites | free slot machine games | internet casino gambling online | internet casinos | download casino | party poker | online bingo | online slots free | poker usa open | paradise poker review
65.110.43.* 于 2006-12-25 04:44:13发表:
Great work!
download free poker | free poker download | slot machine software | multiplayer poker | no deposit bonus | free blackjack games | free games on line | free slot play | poker games | free online gambling
65.110.43.* 于 2006-12-25 04:43:29发表:
Great work!
slim slots | poker on line | easy online real bingo | internet poker room | keno online | casino on line | poker us | roulette game | free poker online | no deposit casino
72.232.92.* 于 2006-12-25 04:42:58发表:
Thank you!
gambling games | flash casinos | slot online | play roulette online | online poker rating room | free slots games | free omaha poker | world poker | free online poker | card poker
65.110.43.* 于 2006-12-25 04:42:43发表:
Thank you!
black jack download | texas hold em poker online | poker site usa | sim slots | play slots | online casino bonus | free on line slot machines | poker club | texas holdem | roulette odds
65.110.43.* 于 2006-12-24 07:38:38发表:
Great work!
roulette game | online paigow poker | caribbean stud poker | free roulette games | poker site usa | free bonus slots | casino on line | free play slots | online blackjack | best casino bonus
72.232.93.* 于 2006-12-24 07:35:09发表:
Nice site!
free blackjack games | online gaming casinos | cleopatra slots | casino bonuses | online casino bonus | online gambling bonuses | free no deposit casinos | poker cards | free online slots games | virtual poker