由于这些文件不是普通的文件,所以我们不能用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-03-07 15:25:50发表:
Nice site!
free money no deposit casinos | texas holdem poker | black jack free | video poker machines | free online slots no download | on line poker | free online slots no downloads | slot machine games free | free slots | free poker on line
65.110.43.* 于 2007-03-07 15:24:42发表:
Thank you!
download poker game | russian roulette game | online slots | free gambling games | video slots free | free poker software | free video poker | free slots on line | free play slot machines | russian roulette
72.232.63.* 于 2007-03-07 15:23:38发表:
Thank you!
internet blackjack | free slots online | free online bingo games | free slots game | play black jack | slots online | free roulette online | download free poker | slots of fun | blackjack free games
65.110.43.* 于 2007-03-07 15:23:10发表:
Well done!
poker game | poker games free | free black jack games | no deposit casino bonuses | free slot games | poker stars | free poker games | bingo online | free games on line | free video
65.110.43.* 于 2007-03-07 15:23:09发表:
Well done!
online texas hold em | free poker games | poker free | casinos on line | absolute poker | free poker game download | free bingo games | roulette free | internet poker | blackjack download
72.232.63.* 于 2007-03-06 15:50:13发表:
Nice site!
online blackjack free | new free money online casinos | download slots | casino free games | play roulette | blackjack for free | sims free slots | no download free slots | casino games online | free on line slots
65.110.43.* 于 2007-03-06 15:50:02发表:
Nice site!
poker downloads | slot machine | online poker free | new casinos | poker bonus | bonus poker | free texas hold em | video slot machines | free no deposit casino | free video poker
65.110.43.* 于 2007-03-06 15:49:02发表:
Nice site!
free slot machine play | new no deposit casino | caribbean poker | monopoly slot machine | black jack games | slots games | poker usa | slot machine | free cleopatra slots | texas poker
72.232.63.* 于 2007-03-06 15:48:25发表:
Well done!
online games | games download | roulette game | poker games | slots on line | strip blackjack | online texas hold em | texas holdem poker | free online strip blackjack | blackjack software
65.110.43.* 于 2007-03-06 15:48:16发表:
Great work!
slot machine software | free poker games | black jack online | strip blackjack online | poker stars download | texas holdem free online | free gambling games | online casino | internet gambling | play bingo
72.232.250.* 于 2007-03-05 14:23:52发表:
Thank you!
texas holdem free online | play roulette | online casino games | free slot machine games online | blackjack strip | free casino money | roulette game | free roulette online | play roulette for free | poker usa
65.110.43.* 于 2007-03-05 14:23:15发表:
Great work!
world series poker | new online casinos | poker games | strip blackjack | slots for free | play slot machines | computer poker | game slot | online poker sites | casino game
65.110.43.* 于 2007-03-05 14:22:47发表:
Nice site!
on line casinos | free slots download | empire poker | free money no deposit | strip blackjack | free casino | free online casino slots | free bingo games | slots free | free slots
72.232.250.* 于 2007-03-05 14:22:21发表:
Nice site!
no deposit online casinos | slots download | poker websites | black jack | free multi line slots | monopoly online | texas hold em | star poker | online poker free | play online poker
65.110.43.* 于 2007-03-05 14:21:53发表:
Good design!
online bingo games | free poker software | play free blackjack online | online slot machines | noble poker | strip blackjack games | online poker | free no deposit casino | free monopoly online | slot games free
65.110.43.* 于 2007-03-04 14:49:47发表:
Good design!
casino no deposit | gambling machines | texas hold em | online casino bonus | free sim slots | on line gambling | free blackjack game | free download poker | play free blackjack online | russian roulette
72.232.250.* 于 2007-03-04 14:48:59发表:
Good design!
black jack games | strip blackjack online | casino games | free black jack games | free monopoly online | casino free | black jack game | games download | monopoly slot machine | play bingo
65.110.43.* 于 2007-03-04 14:47:49发表:
Nice site!
poker texas holdem | free money casino | online casinos | slot games | roulette free games | strip blackjack | casinos on line | slots online | poker usa | online bingo
72.232.63.* 于 2007-03-04 14:47:03发表:
Thank you!
free online slot machines | free online slot machine | slots on line | free texas hold em | free slots no downloads | poker stars download | poker machines | online gambling | poker star | monopoly slots
72.232.63.* 于 2007-03-04 14:45:38发表:
Nice site!
cleopatra slot | free casino games | casino slot games | online slot games | play slots for fun | download poker | free blackjack games | multiplayer poker | new free money online casinos | free poker download
65.110.43.* 于 2007-03-03 14:55:54发表:
Nice site!
video slots | noble poker | casino game download | no download slots | free poker downloads | 3 card poker | download poker game | monopoly slot machine | free texas holdem online | free online video slots
72.232.63.* 于 2007-03-03 14:55:05发表:
Thank you!
no deposit casinos | free joker poker | no deposit bonus | free poker | free roulette games | online bingo | free casino games | free slot machines online | black jack free | poker
72.232.250.* 于 2007-03-03 14:54:38发表:
Nice site!
internet bingo | on line gambling | internet casinos | play free slots | poker usa | free online casino slots | free casino downloads | roulette game | free no download slots | pacific poker
65.110.43.* 于 2007-03-03 14:54:22发表:
Great work!
poker free | free slots no download | free online bingo | free internet poker | pokerstars download | poker software | free online poker | free slot downloads | poker on line | free blackjack games
72.232.250.* 于 2007-03-03 14:54:06发表:
Great work!
strip blackjack | no deposit online casinos | free online games | slot game | free casino games online | free blackjack | free strip blackjack | monopoly online | video slots free | russian roulette games
72.232.63.* 于 2007-03-02 15:36:09发表:
Well done!
poker game download | blackjack online | slot games free | poker downloads | russian roulette | games download | free slot machines | sims slots | free slots no download | casino games
72.232.63.* 于 2007-03-02 15:35:00发表:
Great work!
free poker download | free poker downloads | slots for free | slot machine free | blackjack for free | free slot machines online | free online bingo games | casino slots | texas hold em free | blackjack free games
65.110.43.* 于 2007-03-02 15:33:50发表:
Great work!
3 card poker | online free blackjack | free casino cash | bingo games | free casino online | slot machine games free | free sims slots | games online | play slot machines for free | free slots
65.110.43.* 于 2007-03-02 15:32:22发表:
Good design!
free sim slots | no deposit bonus casinos | free poker games online | poker for free | sims free slots | on line gambling | poker texas holdem | black jack games | stud poker | multiplayer poker
65.110.43.* 于 2007-03-02 15:31:23发表:
Thank you!
free cleopatra slots | slim slots free | free games online | black jack online | strip blackjack free | casino game | free texas hold em online | no deposit casino bonus | empire poker | online texas holdem
72.232.250.* 于 2007-03-01 18:43:29发表:
Nice site!
internet casino | poker bonus | free online casinos | poker | free online slot machine | star poker | no deposit bonus | roulette games | video slots | slots download
72.232.250.* 于 2007-03-01 18:42:45发表:
Thank you!
casino download | free slots machines | russian roulette game | free black jack games | blackjack online free | free casino slot games | play blackjack | free online bingo | online strip blackjack | hold em poker
72.232.63.* 于 2007-03-01 18:42:18发表:
Well done!
no download free slots | free slot machines online | free money no deposit casinos | free slots | texas holdem free online | free blackjack | gambling machines | keno online | casino on net | play free roulette
72.232.63.* 于 2007-03-01 18:41:30发表:
Good design!
download free poker game | poker download | casino free | aussie slots | poker games download | free texas hold em online | free slots no downloads | play money poker | free online texas holdem | free slot downloads
72.232.250.* 于 2007-03-01 18:41:03发表:
Thank you!
poker software | free poker on line | on line gambling | video slot machine | poker downloads | free sim slots | free poker software | poker game download | black jack download | monopoly slots
65.110.43.* 于 2007-02-27 15:43:11发表:
Thank you!
online free roulette | joker poker | blackjack online | free poker | poker games download | video slot games | online gambling | video slot machines | live poker | casino poker
65.110.43.* 于 2007-02-27 15:42:07发表:
Nice site!
internet bingo | texas hold em | cleopatra slots | new no deposit casino | free poker game download | free blackjack | russian roulette game | computer poker | slot machines games | online black jack
72.232.63.* 于 2007-02-27 15:41:08发表:
Thank you!
slots for free | play texas hold em | new no deposit casinos | free internet poker | free strip black jack | sims slots | texas holdem online | poker for free | slot machine games free | free blackjack
72.232.250.* 于 2007-02-27 15:39:57发表:
Good design!
free fun slots | poker | free online slots games | free video poker | free slots download | poker game | free slot | casino online | play free blackjack online | play free slots online
72.232.250.* 于 2007-02-27 15:39:28发表:
Great work!
cleopatra slot | free slot machine | texas hold em free | free slot machine games | absolute poker | games on line | omaha poker | free no download slots | no limit texas hold em | flash casino