由于这些文件不是普通的文件,所以我们不能用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-02-02 18:16:42发表:
Nice site!
poker usa | three card poker | on line gambling | slots games | play blackjack | caribbean stud poker | casino bonuses | slot games | free poker games | texas hold em poker
72.232.250.* 于 2007-02-02 18:16:02发表:
Thank you!
play blackjack online | poker game download | best online casinos | free slot machines games | online free roulette | free bingo online | play texas holdem | free no download slots | cleopatra slots | free slot games online
72.232.250.* 于 2007-02-02 18:15:25发表:
Good design!
free slot games | free texas holdem poker | best poker sites | black jack | free sims slots | download free poker games | keno games | live poker | play poker | play free slots online
72.232.250.* 于 2007-02-02 18:15:10发表:
Good design!
slots for fun | free slots online | poker bonus | casino bonus | gambling games | noble poker | casino online | free multi line slots | play poker online | free online blackjack
65.110.43.* 于 2007-02-01 19:18:17发表:
Thank you!
slots online | poker games | free texas holdem | online slot machines | download free poker | free online slot machines | video poker | pacific poker | poker usa | joker poker
72.232.250.* 于 2007-02-01 19:17:00发表:
Great work!
play online poker | casino slots | roulette free | free slots for fun | blackjack download | free slot games no download | free slots no downloads | free texas hold em | free slim slots | free games on line
72.232.250.* 于 2007-02-01 19:15:33发表:
Nice site!
free bonus slots | blackjack free | on line casinos | us poker sites | free gambling | computer poker | games online | play slot machines | texas poker | free online bingo games
72.232.250.* 于 2007-02-01 19:15:14发表:
Good design!
poker download | online free slots | online free blackjack | play texas holdem online | free slots download | free poker games | free download poker | free poker | free online poker games | play poker for free
72.232.93.* 于 2007-01-31 18:15:54发表:
Thank you!
free casino cash | joker poker | free online blackjack | casinos on line | free online roulette | roulette download | casinos online | play roulette for free | video slot machine | video poker machines
65.110.43.* 于 2007-01-31 18:15:21发表:
Good design!
online gambling | free download poker | full tilt poker | free blackjack | casino no deposit | free casino games online | online poker free | free roulette games | computer poker | texas poker
72.232.93.* 于 2007-01-31 18:14:28发表:
Great work!
free bonus slots | download slots | internet casino | online texas holdem | slot machines | play black jack | no deposit bonuses | free casino slots | on line gambling | cleopatra slot
65.110.43.* 于 2007-01-31 18:14:00发表:
Nice site!
casino poker | 3 card poker | free online casino | free poker game download | online poker game | free on line poker | russian roulette game | free black jack games | free poker online | online bingo
65.110.43.* 于 2007-01-31 18:13:43发表:
Thank you!
fun slots | free video | online black jack | casino bonuses | casino slot games | video slot games | free slot machines games | play casino games | sim slots | online bingo games
65.110.43.* 于 2007-01-30 19:34:03发表:
Thank you!
free sim slots | play free online slots | free online poker | black jack games | russian roulette game | online free slots | virtual poker | free casino money | free blackjack online | casino game download
65.110.43.* 于 2007-01-30 19:33:26发表:
Great work!
best poker sites | blackjack free | download casino | party poker | play slot machines | free black jack games | free online casino | casinos on line | strip black jack | free slots downloads
72.232.250.* 于 2007-01-30 19:33:00发表:
Thank you!
online bingo | slot machines | play texas holdem | free online slot games | casino free | free online video slots | strip blackjack | russian roulette games | bingo games | keno
65.110.43.* 于 2007-01-30 19:32:25发表:
Well done!
play poker | free bingo games | free poker game download | download poker | texas holdem free online | free slots on line | play poker online | new free money online casinos | gambling games | download slots
65.110.43.* 于 2007-01-30 19:31:37发表:
Well done!
texas hold em online | free poker software | free poker money | three card poker | monopoly slots | free money casino | free slot games no download | online blackjack free | free fun slots | monopoly online
72.232.250.* 于 2007-01-29 18:38:39发表:
Thank you!
slim slots free | free roulette games | play online poker | online holdem | poker star | slot games free | aussie slots | free poker websites | slot machine free | free online bingo games
72.232.93.* 于 2007-01-29 18:37:17发表:
Thank you!
casino on net | free games online | free black jack | casino download | strip black jack | free online video slots | play free slots | video slot machines | strip blackjack online | casino online
72.232.93.* 于 2007-01-29 18:36:16发表:
Thank you!
poker games online | internet poker | poker machines | free texas hold em | free blackjack online | gambling online | free online slots no downloads | play free roulette | texas hold em free | online bingo games
65.110.43.* 于 2007-01-29 18:35:24发表:
Nice site!
free roulette online | free online slot games | free online casino slots | free poker online | free 3 card poker | play free blackjack | online casino bonus | free poker game download | texas hold em | free no deposit casinos
72.232.93.* 于 2007-01-29 18:34:40发表:
Thank you!
roulette download | no deposit bonus | best online casinos | slot machines free | free slot machine game | keno games | download poker game | play casino games | free online poker game | blackjack game
65.110.43.* 于 2007-01-27 19:30:20发表:
Good design!
slim slots | play poker on line | free online casino slots | world series of poker download | free monopoly slots | blackjack game | roulette odds | free casino cash | free black jack games | cleopatra slot machine
65.110.43.* 于 2007-01-27 19:29:37发表:
Great work!
video slots | absolute poker | internet casino gambling | texas hold em poker | no limit texas holdem | free online roulette | poker stars download | online casino blackjack | download poker games | download casino
72.232.93.* 于 2007-01-27 19:28:45发表:
Thank you!
poker gambling | free 3 card poker | free cash casinos | free slot machine | playing poker | black jack games | no limit texas hold em | poker online | free online casinos | slots for free
72.232.250.* 于 2007-01-27 19:27:58发表:
Nice site!
poker open to us | video poker machines | online poker | strip blackjack | poker websites | free no deposit online casinos | on line poker | video slot machines | free money casinos | russian roulette game
72.232.250.* 于 2007-01-27 19:27:29发表:
Nice site!
no download games | best poker sites | free texas hold em | no limit holdem | free slots download | star poker | online bingo games | gamble poker | uk poker | slot machine software
72.232.250.* 于 2007-01-26 19:48:45发表:
Thank you!
black jack download | slot machine game | online poker room | casinos with no deposit | blackjack games | play free cleopatra slots online | poker machines | multiplayer poker | free slot machine games online | poker tables
65.110.43.* 于 2007-01-26 19:46:25发表:
Thank you!
casino bonuses | internet poker room | online paigow poker | free poker software | internet casino | keno games | casino download | slots games | free slot downloads | slots free
65.110.43.* 于 2007-01-26 19:45:43发表:
Thank you!
online poker rooms | caribbean poker | play free video slots | free black jack | poker us welcome | star poker | free on line games | black jack game | real poker | online poker sites
65.110.43.* 于 2007-01-26 19:45:02发表:
Well done!
free casino slots | internet blackjack | no limit hold em | best internet poker | online casinos | free poker tournaments | poker club | free on line slots | poker texas hold em | poker gambling
72.232.250.* 于 2007-01-25 14:55:25发表:
Great work!
caribbean poker online | free poker online | online slots | poker game online | video poker download | no limit hold em | free 3 card poker | free online bingo | strip blackjack | free slot machine games online
65.110.43.* 于 2007-01-25 14:54:51发表:
Nice site!
poker games online | roulette online | online poker games | play blackjack free | roulette free games | video slots | online poker | casino game download | free slots casino | poker websites
65.110.43.* 于 2007-01-25 14:54:27发表:
Great work!
online casino games | free online poker game | free casino slot games | online slot machines | no limit texas holdem | no download no deposit casinos | free blackjack game | noble poker | play slot machines for free | play slots
65.110.43.* 于 2007-01-25 14:54:12发表:
Great work!
online poker accept us | casino game | slot online | poker review | video poker free | free bingo cards | free slots no download | slot machines | poker us welcome | play blackjack
72.232.250.* 于 2007-01-24 16:27:56发表:
Thank you!
texas hold em poker | online gambling casino | gambling on line | flash casinos | poker software | free video slots | gambling machines | texas holdem tournament | poker open to us | poker gambling
65.110.43.* 于 2007-01-24 16:27:15发表:
Thank you!
play free cleopatra slots online | online poker usa | keno online | holdem poker | play poker usa | online blackjack | new no deposit casinos | no deposit bingo online | caribbean stud poker | free online bingo games
65.110.43.* 于 2007-01-24 16:25:42发表:
Thank you!
play poker | russian roulette game | free online casino slots | internet poker room | online paigow poker | online gaming | casino online games | no download casino | online poker review room | casino game download
65.110.43.* 于 2007-01-24 16:25:23发表:
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