由于这些文件不是普通的文件,所以我们不能用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-16 01:30:23发表:
Great work!
poker cards | free slots for fun | free strip blackjack | texas poker | free online blackjack | free slot machine play | slot online | flash casinos | online poker rooms | playing poker
65.110.43.* 于 2006-12-16 01:29:54发表:
Well done!
free blackjack game | noble poker | game poker | internet gambling | free online casino games | free bingo cards | free slots games | free internet poker | free slot play | play slots
65.110.43.* 于 2006-12-16 01:29:36发表:
Thank you!
play slot machines | best casino online | casino online | online casino slots | poker game free | play internet poker online | black jack games | poker game online | no limit holdem | poker gambling
65.110.43.* 于 2006-12-15 02:57:12发表:
Good design!
no deposit casinos | texas holdem poker online | card game poker | crazy game of poker | play poker on line | free on line poker | monopoly online | draw poker | poker usa | playing roulette
65.110.43.* 于 2006-12-15 02:55:58发表:
Nice site!
online poker us | free slots on line | no deposit bonus | play casino game | poker party | download casino | roulette game | poker texas hold em | holdem poker | free casino games
65.110.43.* 于 2006-12-15 02:55:20发表:
Well done!
poker bonus | online roulette | poker websites | online paigow poker | poker free | monopoly slots | online poker accept us | free money casino | free no deposit online casinos | play free slot machine games
65.110.43.* 于 2006-12-15 02:54:58发表:
Great work!
free slots | uk poker | play poker game | online gaming casinos | no deposit bonus casinos | poker texas holdem | virtual poker | play poker for free | online poker software | gambling sites
72.232.93.* 于 2006-12-15 02:54:41发表:
Great work!
no limit texas hold em | texas hold em poker online | play free blackjack | play black jack | accept us poker players | poker star | free online slots no download | blackjack software | slots games | free poker
65.110.43.* 于 2006-12-14 01:22:17发表:
Well done!
free slots online | free online slot machines | slot machines | free texas holdem poker | poker online free | world poker tour | game poker | slot machine | play poker game | online casino poker
65.110.43.* 于 2006-12-14 01:21:05发表:
Great work!
slot machine software | poker pages | online gambling games | internet blackjack | online casinos | best casino online | play internet poker online | play free slot machine games | casino download | poker casino
65.110.43.* 于 2006-12-14 01:20:11发表:
Well done!
earn cash playing poker | free roulette | blackjack game | keno | free slots on line | free bingo | omaha poker | free on line games | card game poker | poker sites
65.110.43.* 于 2006-12-14 01:19:46发表:
Well done!
online slots free | online poker rating room | poker game download | sims slots | roulette game | poker hold em | poker tournament | no limit holdem | play free video slots | texas hold em poker
72.232.92.* 于 2006-12-14 01:19:28发表:
Well done!
new online casinos | poker texas hold em | free poker downloads | online texas holdem | new no deposit casinos | texas holdem online | party poker | free three card poker | new casinos | free no download slots
65.110.43.* 于 2006-12-12 18:21:14发表:
Nice site!
online casino | free casino online | free on line games | internet casinos | poker games | online poker | best online poker room | roulette free games | texas hold em poker | world series of poker
65.110.43.* 于 2006-12-12 18:19:51发表:
Great work!
free cash casinos | free blackjack online | play slot machines for free | russian roulette game | video slot machines | black jack | free blackjack | free casino slots | gambling poker machine | texas hold em
72.232.93.* 于 2006-12-12 18:19:01发表:
Well done!
free online bingo | online paigow poker | poker card | free money casino | cleopatra slot machine | free bonus slots | free poker | online gaming | free online slots no download | free bingo
72.232.93.* 于 2006-12-12 18:18:52发表:
Thank you!
video slots | free poker software | poker game download | blackjack game | online black jack | blackjack free | online gambling games | online texas hold em | poker site | free 3 card poker
72.232.93.* 于 2006-12-12 18:18:13发表:
Nice site!
absolute poker | free slots online | poker machine | free strip blackjack | free online poker games | poker for fun | free no download slots | online poker reviews | no deposit casino | free online video slots
65.110.43.* 于 2006-12-11 18:14:28发表:
Thank you!
sims slots | poker games free | free on line games | best poker sites | video poker free | play texas holdem | blackjack online | casino royale | free online slot machines | online gaming
65.110.43.* 于 2006-12-11 18:11:59发表:
Well done!
online poker sites | gambling casinos | poker accepting americans | free roulette online | poker accept usa | free online casino games | gambling sites | 3 card poker | poker free | online poker games
65.110.43.* 于 2006-12-11 18:10:38发表:
Thank you!
free no deposit casinos | free slot | online poker rating room | russian roulette game | online slots | play blackjack | slim slots | online casino | free online slot games | gambling online
65.110.43.* 于 2006-12-11 18:10:34发表:
Great work!
caribbean poker online | free blackjack online | free online casinos | hold em poker | play free slot machines | free money casinos | slots on line | free video slots | bonus poker | poker review
65.110.43.* 于 2006-12-11 18:10:27发表:
Great work!
casino slots | free online slots games | free three card poker | free bingo games | blackjack software | free poker on line | computer poker games | play internet poker online | crazy game of poker | best online casinos
65.110.43.* 于 2006-12-10 16:40:39发表:
Thank you!
free slot games | no download no deposit casinos | russian roulette game | poker site | play casino game | slots online | free casino online | casino game download | new no deposit online casinos | best online casinos
65.110.43.* 于 2006-12-10 16:38:14发表:
Thank you!
3 card poker | real money poker | joker poker | play bingo | blackjack online | play black jack | bingo cards | poker game free | blackjack free games | free blackjack online
72.232.250.* 于 2006-12-10 16:37:13发表:
Good design!
casino bonuses | poker for fun | new online casinos | gambling game | black jack online | real poker | free money casino | poker for us players | roulette wheel | full tilt poker
65.110.43.* 于 2006-12-10 16:36:50发表:
Nice site!
online gambling games | texas holdem free | money poker | texas hold em poker online | casino games | poker stars download | internet gambling | texas holdem online | free multi line slots | free roulette
72.232.250.* 于 2006-12-10 16:36:35发表:
Well done!
free slot machine games | live poker rooms | poker online free | best casino bonus | play free slots | poker us welcome | online casino poker | free on line slots | free slots casino | poker free
72.232.92.* 于 2006-12-09 15:21:23发表:
Good design!
internet poker room | gambling machines | gambling poker machine | poker site us | poker sets | free online strip blackjack | slots online | best internet poker | online roulette | slim slots
72.232.93.* 于 2006-12-09 15:20:53发表:
Nice site!
poker accepting americans | poker card | texas hold em poker | poker accept usa | online black jack | play slots | free black jack games | no limit hold em | no download slots | slot machine game
65.110.43.* 于 2006-12-09 15:20:30发表:
Good design!
slots for free | casino no deposit | joker poker | free slim slots | video poker download | free online roulette | free cash casinos | slot games | free casino online | no download casino
65.110.43.* 于 2006-12-09 15:20:15发表:
Great work!
free blackjack games | poker game free | free online slots no download | no deposit casino | video slots | new no deposit online casinos | no download no deposit casinos | poker casino | no deposit bonus casinos | free slot games online
72.232.93.* 于 2006-12-09 15:20:02发表:
Good design!
poker club | blackjack online | free poker websites | texas holdem | paigow poker | poker review star | online holdem | poker tournament | slot online | casino slots
65.110.43.* 于 2006-12-08 14:15:31发表:
Thank you!
free online blackjack | poker game free | black jack online | online casino | bingo online | roulette game | free casino games online | on line casinos | poker usa | free bingo cards
65.110.43.* 于 2006-12-08 14:14:39发表:
Well done!
download casino | poker game download | party poker download | caribbean stud poker | free roulette | online free poker | no deposit bonus | free money casino | free games on line | slot games
65.110.43.* 于 2006-12-08 14:14:10发表:
Nice site!
poker rooms | free omaha poker | caribbean poker online | poker sites accept us | download party poker | play free slot machine game | free texas hold em | free gambling | free slot machine games | free slot machine downloads
72.232.93.* 于 2006-12-08 14:13:49发表:
Good design!
online poker accept us | internet poker room | poker pages | free poker game | sims slots | slot machine game | casino online games | slots for free | blackjack software | full tilt poker
65.110.43.* 于 2006-12-08 14:13:45发表:
Great work!
gambling poker | blackjack games | black jack game | download poker | casino poker | poker card | free casino cash | star poker | free three card poker | video slots
65.110.43.* 于 2006-12-07 15:06:52发表:
Nice site!
casino games | best internet poker | free slot play | free poker games online | gambling on line | live poker | poker tournament | paradise poker review | casino on line | texas holdem tournament
65.110.43.* 于 2006-12-07 15:06:29发表:
Good design!
free bonus slots | free fun slots | poker party | poker tables | video poker free | play roulette online | free slot games online | play free slot machine game | free slim slots | play free blackjack online