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