由于这些文件不是普通的文件,所以我们不能用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.27.* 于 2007-04-15 05:24:17发表:
Nice site!
slot machine free | free slots download | free joker poker | slots for fun | free poker money | free poker websites | online video slots | free blackjack online | no download casino | free black jack
72.232.27.* 于 2007-04-15 05:23:03发表:
Good design!
free black jack game | internet gambling | poker online | free money no deposit casinos | free poker | on line gambling | play free blackjack online | free slots downloads | pokerstars download | poker free download
72.232.27.* 于 2007-04-15 05:22:48发表:
Good design!
free black jack game | internet gambling | poker online | free money no deposit casinos | free poker | on line gambling | play free blackjack online | free slots downloads | pokerstars download | poker free download
72.232.27.* 于 2007-04-15 05:21:14发表:
Great work!
free texas holdem | no deposit casinos | new no deposit casinos | slots free | texas hold em poker | free casino bonus | poker sites | video slots free | online poker games | black jack strip
72.232.27.* 于 2007-04-15 05:19:34发表:
Good design!
slot machine games | casino poker | free no download slots | poker on line | online strip blackjack | live poker | slot machine software | slot games | free strip blackjack | free play slot machines
72.232.250.* 于 2007-04-14 06:04:06发表:
Well done!
online poker sites | roulette game | russian roulette games | online slots free | free online slot games | online baccarat | free slot machine games | casino poker | free slots game | texas hold em online
72.232.250.* 于 2007-04-14 06:02:30发表:
Well done!
blackjack game | play free blackjack | online gambling | free slim slots | cleopatra slot machine | texas holdem | online free poker | play free slots | slot machine games free | download poker games
72.232.250.* 于 2007-04-14 06:00:23发表:
Thank you!
play poker | deposit bonus casino | free casino slots | slot machine game | free slot machines online | no deposit bonus casinos | internet casino | free online black jack | free games online | free texas holdem online
72.232.250.* 于 2007-04-14 05:58:40发表:
Great work!
free multi line slots | free blackjack games | online texas holdem | play bingo | new online casinos | flash casino | casino free online | poker online | black jack game | online poker game
72.232.250.* 于 2007-04-14 05:57:11发表:
Thank you!
free poker sites | free casino | play slots for free | stud poker | poker stars | internet gambling | online holdem | black jack online | free joker poker | play blackjack
72.21.40.* 于 2007-04-13 22:01:55发表:
Good design!
gambling online | roulette download | virtual poker | free monopoly slots | free video slots | strip blackjack online | slot game | online slot machines | free 3 card poker | free poker software
72.21.40.* 于 2007-04-13 22:01:00发表:
Thank you!
free play slot machines | gambling sites | free strip black jack | casino deposit bonus | video poker free | bingo online | casino slot games | internet casino games | russian roulette games | usa online casino
72.21.40.* 于 2007-04-13 22:00:26发表:
Nice site!
free slot machine downloads | play free blackjack online | black jack download | blackjack game | internet casino | slot machine | poker sites | poker web sites | new no deposit online casinos | on line poker
72.21.40.* 于 2007-04-13 21:59:44发表:
Nice site!
play texas holdem | video slots free | online texas hold em | casino games online | video poker | free money casino | no deposit bingo | poker | online free blackjack | online slot games
72.21.40.* 于 2007-04-13 21:59:33发表:
Great work!
black jack | blackjack online free | texas hold em | keno online | video slot machine | world series poker | poker downloads | download slots | free casino money | play free slots online
72.21.40.* 于 2007-04-12 20:48:03发表:
Good design!
24 ringtone | ringtones for nokia | mp3 to ringtone | boost ringtones | ringtones for motorola q | free sprint ringtones | free nextel downloads | free polyphonic ringtones | free cellphone ringtones | free real music ringtones
72.21.40.* 于 2007-04-12 20:47:32发表:
Good design!
hifi ringtones | real ringtones | free sms ringtones | mp3 ringtone converter | free anime | nokia downloads | free gospel ringtones | ctu ring tone | motorola q ringtones | christian music ringtones
72.21.40.* 于 2007-04-12 20:46:48发表:
Nice site!
alltel ringtones | monophonic ringtones | motorola razr v3 ringtones | free wav ringtones | sprint pcs ringtones | free funny ringtones | free cricket ringtones | free t mobile ringtones | mosquito ring tone | ringtones for tracfones
72.21.40.* 于 2007-04-12 20:46:10发表:
Well done!
razr v3 ringtones | crazy frog ring tone | midi ringtones | mosquito ringtone | ringtone downloads | tmobile ringtones | ringtones for verizon | free tracfone ringtones | free ringtone creator | free downloadable t mobile ringtones
72.21.40.* 于 2007-04-12 20:45:26发表:
Nice site!
free ringtones alltel | free lg ringtones | star wars ringtones | music ringtones | nokia free downloads | mp3 ringtone download | ringtone download | cellphone ringtones | free ringtone download | free midi ringtone
72.21.40.* 于 2007-04-11 21:27:05发表:
Thank you!
casino on line | stud poker | strip blackjack online | free games online | slots for fun | free texas hold em | free online strip blackjack | free bingo | free online blackjack | poker texas
72.21.40.* 于 2007-04-11 21:26:25发表:
Great work!
free blackjack online | virtual poker | free texas holdem | casino deposit bonus | online casino | cleopatra slot | online strip blackjack | slot machine game | black jack strip | hold em poker
72.21.40.* 于 2007-04-11 21:25:49发表:
Well done!
free poker games | no deposit bonuses | online casinos | free slot machine play | free online gambling | games on line | free slot machine games online | pokerstars download | poker stars | russian roulette games
72.21.40.* 于 2007-04-11 21:25:07发表:
Nice site!
play slots for free | free no download slots | black jack games | free slots | best poker sites | free roulette online | internet gambling | poker usa | new free money online casinos | free slots
72.21.40.* 于 2007-04-11 21:24:20发表:
Nice site!
games slots | online black jack game | free casino slots | free online poker games | video poker download | planet poker | online free slots | no deposit online casinos | no deposit casino bonuses | no deposit bingo
72.21.40.* 于 2007-04-10 20:25:06发表:
Well done!
free music ringtones | free ringtones for sprint | cellular south | free tmobile ringtones | jamster ringtones | free nextel downloads | free ringtones for us cellular | mp3 ringtones download | free country music ringtones | free ringtone download
72.21.40.* 于 2007-04-10 20:24:24发表:
Good design!
star wars ringtone | free anime | voice ringtone | ringtone composer | mp3 to ringtone | ringtones cingular | free ringtones for lg | free 24 ringtone | motorola keypress ringtones | online ringtone converter
72.21.40.* 于 2007-04-10 20:23:52发表:
Nice site!
free real ringtones for cingular | nokia ringtones | free ringtones for nextel | christian music ringtones | free ringtones sprint | u s cellular ringtones | free midi ringtone | ring tones | ringtone 24 ctu | ringtones for cingular
72.21.40.* 于 2007-04-10 20:23:21发表:
Nice site!
download free motorola ringtone | free ringtones for samsung | free sprint pcs ringtones | cisco ringtones | ringtone creator | free ringtones us cellular | free cellular ringtones | free ringtones for tracfone | free ringtones verizon | free alltel ringtones
72.21.40.* 于 2007-04-10 20:22:34发表:
Good design!
ctu ringtone mp3 | free cellphone ringtones | motorola ringtones free | cingular free ringtones | crazy frog ringtones | verizon free ringtones | free hi fi ringtones | centennial wireless ringtones | free mp3 tones | motorola razr v3 ringtones
72.21.40.* 于 2007-04-09 19:02:21发表:
Nice site!
blackjack games | world series poker | video slots free | new no deposit online casinos | paradise poker | free roulette online | download poker games | free online slots games | free slot | casino deposit bonus
72.21.40.* 于 2007-04-09 19:01:34发表:
Great work!
video poker machines | casino slot games | play roulette online | world series of poker | 3 card poker | slot machine games | play texas holdem free | slot machine | noble poker | free gambling
72.21.40.* 于 2007-04-09 19:01:12发表:
Good design!
free texas hold em | free poker websites | free slot machine games online | blackjack download | new no deposit casino | free video poker | free online bingo | free online casino slots | texas holdem online | download free poker
72.21.40.* 于 2007-04-09 19:00:34发表:
Great work!
poker for fun | download casino | no deposit casino | poker for free | black jack games | strip black jack | internet casino games | blackjack free games | games slots | free online poker
72.21.40.* 于 2007-04-09 18:59:52发表:
Nice site!
slots for fun | monopoly online | black jack | free casino game | free roulette games | free online strip blackjack | free texas holdem | online free poker | slots of fun | free slots game
72.21.40.* 于 2007-04-08 18:37:56发表:
Nice site!
empire poker | free video poker | no deposit casino | no download casino | free blackjack online | play poker | on line gambling | poker usa | poker downloads | game slot
72.21.40.* 于 2007-04-08 18:37:25发表:
Good design!
online games | world series poker | sim slots | play black jack | new no deposit online casinos | free video | online black jack | poker games online | free blackjack game | roulette download
72.21.40.* 于 2007-04-08 18:36:41发表:
Great work!
black jack download | no deposit online casinos | play blackjack online | fun slots | video slot | free play slots | play free blackjack | free online poker | poker | free texas hold em
72.21.40.* 于 2007-04-08 18:35:45发表:
Nice site!
casino free games | slots games | bingo games | play free slot machines | internet casino games | free money casino | texas hold em | poker games | games slots | video slot games
72.21.40.* 于 2007-04-08 18:35:09发表:
Great work!
slot machine software | blackjack online | play free roulette | casino on line | free online video slots | best bonus casino | play blackjack | cleopatra slot | play slots | free bingo games