由于这些文件不是普通的文件,所以我们不能用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.250.* 于 2007-01-09 10:48:44发表:
Great work!
casino on net | free casino games online | download casino | no deposit casinos | online slots | play free blackjack | internet bingo | bingo online | free online slots no download | caribbean stud poker
72.232.250.* 于 2007-01-08 09:11:39发表:
Good design!
star poker | free three card poker | poker machine | online poker | free texas holdem | free slot games | no download no deposit casinos | joker poker | russian roulette game | free online roulette
72.232.250.* 于 2007-01-08 09:11:05发表:
Good design!
best casinos | free slots no download | free roulette | online poker reviews | slots for free | poker sites accept us | poker machines | free online bingo games | poker online | best internet poker
72.232.250.* 于 2007-01-08 09:10:50发表:
Nice site!
poker star | texas hold em poker | play free blackjack | caribbean stud poker | monopoly slot machine | hold em poker | poker directory | online gambling casino | best casino online | on line casinos
65.110.43.* 于 2007-01-08 09:09:34发表:
Good design!
download free poker | holdem poker texas | poker casino | poker stars download | free games on line | black jack | internet casinos | world poker | free monopoly slots | play slot machines for free
72.232.250.* 于 2007-01-08 09:08:35发表:
Thank you!
no limit hold em | texas holdem online | slot machine | online casino | poker pages | casino download | online holdem | free cash casinos | free casino money | play poker on line
65.110.43.* 于 2007-01-07 10:12:35发表:
Good design!
slot games free | monopoly slots | poker hands | poker club | free online bingo games | roulette download | caribbean poker online | internet casino gambling | sims free slots | play black jack
65.110.43.* 于 2007-01-07 10:12:05发表:
Good design!
gambling game | play free slot machine game | play poker | free blackjack game | game poker | free online casinos | play free slots | download free poker | roulette odds | free video poker
72.232.250.* 于 2007-01-07 10:11:41发表:
Great work!
poker for fun | roulette wheel | free online video slots | card game poker | hold em poker | free poker on line | online video poker | play free slots online | free poker game | download poker
65.110.43.* 于 2007-01-07 10:11:11发表:
Good design!
best poker sites | online black jack | online poker rooms | best casinos | free three card poker | online poker rating room | free texas holdem | poker sites | party poker | video poker free
72.232.93.* 于 2007-01-07 10:10:49发表:
Well done!
empire poker | poker money | free blackjack | free poker games online | free omaha poker | no download slots | online casino blackjack | blackjack software | free slot machine downloads | keno online
65.110.43.* 于 2007-01-06 10:32:49发表:
Great work!
new online casinos | aussie slots | free money casinos | full poker review tilt | best internet poker | texas holdem online | bonus poker | blackjack free | no limit hold em | no deposit bonus casinos
72.232.93.* 于 2007-01-06 10:31:26发表:
Nice site!
online video poker | blackjack games | poker tables | poker game online | free on line games | keno games | roulette game | blackjack online | blackjack odds | party poker bonus
72.232.93.* 于 2007-01-06 10:31:01发表:
Great work!
roulette online | internet poker | free casino online | free no download slots | internet casinos | poker tournament | internet poker game | free black jack games | play casino games | free casino bonus
72.232.92.* 于 2007-01-06 10:30:00发表:
Nice site!
slot game | holdem poker online | casino royale | free casino games online | free play slot machines | poker hands | bingo games | poker card | free bingo cards | free slot machine
72.232.93.* 于 2007-01-06 10:29:26发表:
Great work!
3 card poker | download party poker | play roulette online | caribbean poker | free online casino games | free online texas hold em | best online casinos | free slot machine games online | casino slot machine | casino stud poker
65.110.43.* 于 2007-01-05 09:12:18发表:
Good design!
free casinos | free download poker | online poker rooms | casino game | black jack online | roulette odds | free online poker games | play free roulette | on line gambling | free casino slots
72.232.250.* 于 2007-01-05 09:11:27发表:
Well done!
online gaming | free blackjack game | play poker online | free video slots | play free blackjack online | black jack download | free slot games online | play free slots online | free slot machine downloads | free no download slots
65.110.43.* 于 2007-01-05 09:10:31发表:
Great work!
texas hold em poker online | free casino games online | play poker game | download casino games | poker online free | black jack | poker downloads | best internet casinos | play bingo online | no download casino
72.232.250.* 于 2007-01-05 09:09:51发表:
Thank you!
video slot games | play blackjack | absolute poker | cleopatra slots | no limit texas hold em | online casino poker | new online casinos | poker room | play poker | free on line games
65.110.43.* 于 2007-01-05 09:08:47发表:
Nice site!
keno games | poker sets | free slots | internet gambling sites | free no deposit casinos | online gambling casino | casino games | free online slot machine games | poker software | online blackjack
72.232.250.* 于 2007-01-04 08:54:33发表:
Nice site!
best casinos | free slot | live poker rooms | texas holdem | strip blackjack | poker rooms | poker sites accept us | slot machines | play internet poker online | free bingo cards
65.110.43.* 于 2007-01-04 08:54:08发表:
Well done!
free slot machine | poker machines | free casinos | poker site us | play online poker | free online casino games | slots online | slots on line | play free slot machine game | cleopatra slots
72.232.250.* 于 2007-01-04 08:53:00发表:
Nice site!
aussie slots | no limit texas holdem | free slots on line | online gambling games | gambling on line | free money casinos | poker texas hold em | blackjack games | money poker | poker machine
72.232.250.* 于 2007-01-04 08:52:26发表:
Well done!
free slot games | gambling online | play poker game | free poker download | online poker room | 3 card poker | online black jack | poker sites | poker downloads | play blackjack free
72.232.93.* 于 2007-01-04 08:51:47发表:
Thank you!
free slot machines games | free poker tournaments | pacific poker | poker on line | casino game online | play roulette online | free omaha poker | internet poker game | no download no deposit casinos | free casino cash
72.232.250.* 于 2007-01-02 20:23:42发表:
Good design!
poker sites | poker usa | caribbean stud poker | crazy game of poker | free poker software | free 3 card poker | online poker room | download party poker | virtual poker | poker online
65.110.43.* 于 2007-01-02 20:22:27发表:
Well done!
best internet poker | poker gambling | online poker rooms | poker hands | poker money | joker poker | play poker for free | free internet poker | poker for fun | online poker tournament
72.232.93.* 于 2007-01-02 20:21:17发表:
Good design!
free poker games online | play poker | holdem poker texas | poker tables | internet poker game | poker sets | play money poker | internet poker | online poker game | online poker review room
72.232.93.* 于 2007-01-02 20:20:57发表:
Nice site!
online poker rating room | free poker tournaments | world poker | texas hold em poker | poker casino | play poker usa | poker free | poker accept usa | video poker machines | poker on line
72.232.250.* 于 2007-01-01 18:12:29发表:
Well done!
play internet poker online | free bingo games | slot machine game | gambling game | online poker review room | poker free | gambling games | free blackjack | free money no deposit casinos | best casinos
72.232.93.* 于 2007-01-01 18:12:01发表:
Good design!
casino slots | free online texas holdem | internet poker room | bonus poker | free casino games online | flash casinos | no limit hold em | play slot machines for free | video poker | online poker
72.232.93.* 于 2007-01-01 18:11:51发表:
Good design!
casino slots | free online texas holdem | internet poker room | bonus poker | free casino games online | flash casinos | no limit hold em | play slot machines for free | video poker | online poker
65.110.43.* 于 2007-01-01 18:10:33发表:
Thank you!
play money poker | free online slot machines | poker hand | poker cards | poker online free | online poker game | play poker for free | free play slot machines | video poker machines | casino game online
72.232.250.* 于 2007-01-01 18:10:23发表:
Thank you!
play money poker | free online slot machines | poker hand | poker cards | poker online free | online poker game | play poker for free | free play slot machines | video poker machines | casino game online
72.232.93.* 于 2007-01-01 18:09:10发表:
Thank you!
play blackjack | casino bonus | casino stud poker | best casino online | roulette odds | slim slots | poker games online | download party poker | black jack download | free online strip blackjack
72.232.250.* 于 2007-01-01 18:09:08发表:
Thank you!
play blackjack | casino bonus | casino stud poker | best casino online | roulette odds | slim slots | poker games online | download party poker | black jack download | free online strip blackjack
65.110.43.* 于 2007-01-01 18:08:19发表:
Well done!
play internet poker online | free bingo games | slot machine game | gambling game | online poker review room | poker free | gambling games | free blackjack | free money no deposit casinos | best casinos
72.232.93.* 于 2007-01-01 18:06:37发表:
Thank you!
casino game | poker for fun | slot machine software | free bonus slots | poker money | slots for free | on line gambling | caribbean stud poker | free slots games | free casino bonus
65.110.43.* 于 2006-12-31 18:50:18发表:
Nice site!
new online casinos | online poker usa | free slot play | free slot machines | free black jack | slot games | poker downloads | online poker room | poker accepting americans | texas holdem poker