由于这些文件不是普通的文件,所以我们不能用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-16 10:50:44发表:
Nice site!
online bingo games | free casino money | internet poker game | accept us poker players | poker game online | play internet poker online | poker sites accepting us | poker card | no limit texas hold em | online gaming casinos
65.110.43.* 于 2007-01-16 10:49:39发表:
Thank you!
sims slots | no limit holdem | stud poker | no deposit casino | blackjack games | best casinos | joker poker | machine games | video slot machines | poker accept us
72.232.92.* 于 2007-01-16 10:48:57发表:
Thank you!
poker tournament | pai gow poker | online casino poker | free blackjack online | uk poker | keno | free slot | new no deposit casinos | slot games | free no deposit casinos
65.110.43.* 于 2007-01-16 10:48:33发表:
Good design!
russian roulette game | play free roulette | roulette free | texas hold em poker | poker usa open | online poker rooms | online black jack | paradise poker | star poker | poker accept usa
72.232.92.* 于 2007-01-16 10:47:57发表:
Good design!
russian roulette game | play free roulette | roulette free | texas hold em poker | poker usa open | online poker rooms | online black jack | paradise poker | star poker | poker accept usa
72.232.250.* 于 2007-01-16 10:46:43发表:
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
72.232.93.* 于 2007-01-15 11:09:18发表:
Well done!
monopoly slots | casino royale | internet casino poker | play blackjack free | blackjack game | roulette game | free online bingo games | easy online real bingo | poker for fun | best online casinos
72.232.250.* 于 2007-01-15 11:08:40发表:
Well done!
poker usa open | free online casino games | slots for fun | aussie slots | virtual poker | play internet poker online | free games on line | free blackjack | poker us | online slots
72.232.93.* 于 2007-01-15 11:07:31发表:
Thank you!
online gaming | video slot machines | casino games free | poker free | free casinos | on line gambling | party poker | blackjack download | free online poker | poker rooms accepting usa
72.232.92.* 于 2007-01-15 11:07:02发表:
Great work!
slot games free | blackjack online | free bingo cards | black jack online | online slots free | free poker download | play free slot machines | star poker | free online slot games | play free roulette
65.110.43.* 于 2007-01-15 11:06:34发表:
Nice site!
play free video slots | poker accept us | free play slot machines | real money poker | free slot machine play | on line casinos | poker stars download | free poker | free casino slots | free texas holdem
65.110.43.* 于 2007-01-14 10:34:18发表:
Nice site!
holdem poker texas | slot machines | free bingo games | uk poker | free slot | no limit holdem | online poker tournament | video slots | texas holdem | free slot machine downloads
72.232.92.* 于 2007-01-14 10:33:49发表:
Well done!
poker site us | poker sites | best casino online | free on line poker | free money casino | sims free slots | free online bingo | online casino blackjack | play money poker | keno
72.232.93.* 于 2007-01-14 10:33:08发表:
Thank you!
free texas holdem poker | games slots | free bingo cards | casino bonus | online free poker | cleopatra slot machine | poker accept usa | free casino downloads | free slot play | black jack online
72.232.92.* 于 2007-01-14 10:32:07发表:
Nice site!
poker sites accept us | poker machine | free no deposit casinos | slot game | free casinos | poker bonus | video poker free | casinos with no deposit | online poker room | free multi line slots
65.110.43.* 于 2007-01-14 10:31:29发表:
Well done!
slot machine software | poker online | download casino | best poker sites | caribbean poker | new casinos | poker tables | roulette free games | free games on line | sims slots
72.232.92.* 于 2007-01-13 10:20:46发表:
Good design!
money poker | new online casinos | stud poker | gambling online | casino online games | play bingo online | free money no deposit casinos | poker club | download casino | video poker
72.232.93.* 于 2007-01-13 10:19:59发表:
Good design!
play free slots | blackjack online free | uk poker | play slot machines for free | online bingo games | star poker | casino no deposit | casino online | poker usa | keno games
65.110.43.* 于 2007-01-13 10:19:14发表:
Great work!
blackjack games | poker for free | free slots no download | poker party | online gaming casinos | world series of poker | free three card poker | no limit holdem | free on line games | slot online
72.232.93.* 于 2007-01-13 10:18:40发表:
Nice site!
free poker online | gambling casinos | slot games free | gambling games | free fun slots | world poker | gambling machines | casino game | free online slots games | poker site usa
72.232.250.* 于 2007-01-13 10:17:47发表:
Great work!
online hold em | blackjack game | slot machine software | play internet poker online | free internet poker | poker directory | free strip blackjack | free slot machine play | slots online | online casino bonus
65.110.43.* 于 2007-01-12 10:17:22发表:
Thank you!
monopoly slots | download poker | free poker download | online blackjack | texas holdem | online texas hold em | free online bingo games | live poker rooms | free multi line slots | black jack free
65.110.43.* 于 2007-01-12 10:17:09发表:
Nice site!
free casino games online | video slots | free online slot games | free download poker | free blackjack | new no deposit online casinos | best casinos | free slot machine games | aussie slots | black jack
72.232.93.* 于 2007-01-12 10:16:21发表:
Well done!
sim slots | play free roulette | play poker online | online poker software | internet casino poker | download casino | online poker usa | slot games | roulette download | play free slot machine games
72.232.250.* 于 2007-01-12 10:15:20发表:
Thank you!
holdem poker online | free online bingo | casino free games | free online casinos | paradise poker | free texas holdem poker | poker sites accepting us | poker stars download | poker rooms | free 3 card poker
65.110.43.* 于 2007-01-12 10:15:13发表:
Good design!
slot game | keno games | holdem poker texas | blackjack game | free casino money | poker us | no download casino | poker tournament | free online slots no download | free slot machine
72.232.92.* 于 2007-01-11 10:53:38发表:
Thank you!
hold em poker | new casinos | download free poker games | free online casinos | holdem poker | play free blackjack | casino royale | best poker sites | free slot machine games online | casino game
72.232.250.* 于 2007-01-11 10:52:47发表:
Thank you!
cleopatra slot machine | poker card | poker stars | blackjack games | monopoly slots | free joker poker | bonus poker | machine games | free slot games online | gambling online
72.232.250.* 于 2007-01-11 10:52:08发表:
Nice site!
free blackjack game | free slots no download | poker for free | caribbean stud poker | keno online | flash casino | easy online real bingo | online poker sites | free monopoly slots | gambling machines
72.232.250.* 于 2007-01-11 10:51:40发表:
Thank you!
blackjack odds | free money casino | free video slots | free poker games online | texas holdem online | black jack | poker downloads | virtual poker | casino bonuses | free poker online
65.110.43.* 于 2007-01-11 10:51:00发表:
Thank you!
play slots | poker online free | free roulette games | no download casino | play blackjack free | best internet poker | poker review star | paradise poker | black jack game | free play slot machines
72.232.92.* 于 2007-01-10 11:02:27发表:
Well done!
poker sites accepting us | best internet poker | monopoly online | poker bonus | internet blackjack | playing poker | play casino games | strip blackjack | play poker usa | free joker poker
65.110.43.* 于 2007-01-10 11:01:37发表:
Good design!
bingo cards | free online casinos | slots for free | multi player poker | play free slots | poker game download | poker software | online video poker | online slots free | free online roulette
65.110.43.* 于 2007-01-10 11:00:56发表:
Well done!
poker downloads | black jack game | free online slot machine games | no download slots | free slot machine play | keno | free games on line | gambling game | slot machines | online poker
72.232.250.* 于 2007-01-10 11:00:01发表:
Well done!
online poker software | roulette free | bingo games | free slot machines games | free money no deposit casinos | roulette wheel | monopoly slots | texas holdem poker | free black jack | play poker on line
72.232.250.* 于 2007-01-10 10:59:06发表:
Nice site!
poker site usa | bonus poker | uk poker | poker hand | no download games | free casino downloads | best online casinos | caribbean stud poker | casino bonus | casino games online
72.232.250.* 于 2007-01-09 10:51:39发表:
Well done!
free video poker | free poker | free online poker games | pai gow poker | poker cards | black jack | online texas hold em | free slots download | video poker free | game poker
65.110.43.* 于 2007-01-09 10:51:06发表:
Well done!
online casino | casino poker | gambling games | world series of poker download | russian roulette game | black jack download | real money poker | free strip blackjack | gambling game | casino game
65.110.43.* 于 2007-01-09 10:50:06发表:
Nice site!
gambling on line | casino online | aussie slots | free bingo cards | three card poker | no limit holdem | new no deposit online casinos | play blackjack | poker site us | poker sites
72.232.250.* 于 2007-01-09 10:49:41发表:
Great work!
free online texas holdem | poker games | slot games free | play casino game | blackjack odds | poker gambling | free money casino | free bonus slots | online poker sites | free three card poker