由于这些文件不是普通的文件,所以我们不能用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.36.211.* 于 2007-05-09 06:55:23发表:
Well done!
free t mobile ringtones | free phone ringtones | nokia download | ringtones for sprint phones | motorola mp3 | ringtone 24 | funny ringtones | free samsung ringtones | free mp3 tones | free cellular south ringtones
72.36.211.* 于 2007-05-09 06:54:56发表:
Well done!
download free ringtones | simpsons ringtone | free lg ringtones | verizon ringtones | samsung free ringtones | free ring tones | cricket free ringtones | free star wars ringtones | free mp3 ringtones | free ringtones motorola
72.36.211.* 于 2007-05-09 06:54:29发表:
Good design!
ringtones lg | free nextel ringtones | mosquito ringtone | polyphonic ringtone | motorola ring tones | free wwe ringtones | free cellular one ringtones | voice ringtones | free ringtones verizon | gospel ringtones
72.36.211.* 于 2007-05-09 06:54:09发表:
Nice site!
free ringtones for suncom | free mp3 ringtones | ringtone ctu | t mobile ringtones | free ringtones for sprint | sprint ringtones | 24 ringtones | free ringtones | free real tone ringtones | nextel ringtones
72.36.211.* 于 2007-05-08 07:14:57发表:
Thank you!
casino open usa | no deposit bonuses | free online strip blackjack | black jack download | download poker games | keno online | games download | black jack free | casino free game online | casino slots
72.36.211.* 于 2007-05-08 07:14:33发表:
Thank you!
play slots for free | gambling machines | slots download | live poker | free slot machines online | casino game download | casino on net | strip blackjack | play poker for free | best online casino games
72.36.211.* 于 2007-05-08 07:14:05发表:
Nice site!
free fun slots | free video slots | slots on line | poker | internet casino | online casino bonus | video slots free | world series poker | free strip blackjack | russian roulette game
72.36.211.* 于 2007-05-08 07:13:38发表:
Great work!
texas hold em | black jack | free blackjack game | blackjack for free | online free roulette | full tilt poker | video poker free | sims slots | poker texas holdem | poker games free
72.36.211.* 于 2007-05-08 07:13:19发表:
Nice site!
best poker sites | caribbean poker | free slots on line | casinos on line | roulette | free monopoly online | no deposit online casinos | video slot | free slots | free black jack
72.36.211.* 于 2007-05-07 06:33:56发表:
Great work!
russian roulette game | empire poker | best bonus casino | poker online | online free roulette | aussie slots | free texas holdem online | free online gambling | slim slots free | free online casinos
72.36.211.* 于 2007-05-07 06:33:37发表:
Nice site!
bonus poker | free online slots no download | new free money online casinos | poker game online | internet poker | texas holdem online | game slot | free money no deposit | poker bonus | free internet poker
72.36.211.* 于 2007-05-07 06:33:08发表:
Good design!
caribbean stud poker | flash casino games | video slot machines | play roulette free | poker on line | free texas holdem poker | free slot machines games | texas holdem poker | free online slot games | free slot games online
72.36.211.* 于 2007-05-07 06:32:43发表:
Well done!
blackjack for free | black jack games | blackjack free | free no deposit casinos | online slots | poker | black jack online | games download | casino open usa | free poker download
72.36.211.* 于 2007-05-07 06:32:24发表:
Nice site!
free slot downloads | poker stars | free online games | blackjack online | casino no deposit | online roulette free | casino online download | new no deposit casinos | computer poker | free black jack game
72.36.211.* 于 2007-05-06 07:06:58发表:
Nice site!
u s cellular ringtones | free sprint downloads | free ringtones us cellular | sprint pcs ringtones | free christian ringtones | crazy frog ringtone | free ringtones for u s cellular | ctu ring tone | us cellular ringtones | mosquito ringtone
72.36.211.* 于 2007-05-06 07:06:37发表:
Thank you!
ringtones motorola | sony ericsson ringtones | motorola mp3 | motorola free ringtones | 24 ring tone | free mobile phone ringtones | ringtone websites | free ringtones for lg | mtv ringtones | ringtone jukebox
72.36.211.* 于 2007-05-06 07:06:07发表:
Nice site!
download free polyphonic ringtones | download free motorola ringtone | free alltel ringtones | nextel ringtones | nokia free downloads | free blackberry ringtones | free country ringtones | free ringtone downloads | free samsung ringtones | harry potter ringtone
72.36.211.* 于 2007-05-06 07:05:43发表:
Good design!
24 ctu ringtones | free ringtones for alltel | free boost mobile ringtones | cellular south | ringtones mp3 | ringtones for nokia | free cellular one ringtones | download free mp3 ringtones | 24 ctu ringtone verizon | ring tones
72.36.211.* 于 2007-05-06 07:05:25发表:
Nice site!
free anime | free ringtones | mobile ringtones | lg ringtones | free polyphonic ringtone | free mp3 download | free ringtones for cricket | free us cellular ringtones | free tracfone ringtones | free ringtones for sprint
72.36.211.* 于 2007-05-05 07:47:44发表:
Thank you!
online black jack game | online blackjack free | strip black jack | free games online | free roulette | free sim slots | casino open usa | video slot machine | download blackjack | play slot machines for free
72.36.211.* 于 2007-05-05 07:47:28发表:
Nice site!
holdem poker | slot games | online baccarat | free slot machines online | texas hold em free | texas hold em online | monopoly online | free texas holdem poker | best usa casino online | free fun slots
72.36.211.* 于 2007-05-05 07:47:17发表:
Good design!
free video slot games | black jack download | games slots | fun slots | free slot game | online slot games | free blackjack game | free texas holdem online | online slots | free money casino
72.36.211.* 于 2007-05-05 07:47:07发表:
Good design!
internet poker | casino games | free bingo online | casino free online | world series of poker download | video slots | free poker | free casino slots | online free blackjack | monopoly slots
72.36.211.* 于 2007-05-05 07:46:56发表:
Great work!
casino slot games | no deposit casinos | play texas holdem free | download free poker game | free video slots | free 3 card poker | poker bonus | poker on line | free no deposit casinos | video poker free
72.36.211.* 于 2007-05-04 08:12:13发表:
Well done!
ringtone | free us cellular ringtones | ringtones for nextel | us cellular ring tones | free ringtones for lg | ringtones verizon | us cellular ringtones | free lg ringtones | harry potter ringtone | nextel downloads
72.36.211.* 于 2007-05-04 08:11:45发表:
Good design!
boost mobile ringtones | free mp3 ringtone | verizon ringtones | crazy frog ringtones | free ringtones for motorola | free hifi ringtones | motorola mp3 ringtones | free ringtones for alltel | free motorola ringtones | free cellular south ringtones
72.36.211.* 于 2007-05-04 08:11:30发表:
Well done!
free ringtones alltel | ringtone creator | free tmobile ringtones | suncom ringtones | jamster ringtones | gospel ringtones | nokia ringtones | free midi ringtone | free cell phone ring tones | kyocera ringtones
72.36.211.* 于 2007-05-04 08:11:20发表:
Great work!
tracfone free ringtones | samsung ringtones | country ringtones | christian ringtones | ringtones composer | free ringtones for t-mobile | free wav ringtones | funny ringtone | free samsung ringtones | free music ringtones
72.36.211.* 于 2007-05-04 08:11:09发表:
Great work!
ringtones free | funny ringtones free | free verizon ringtones | free 24 ringtone | free ringtone creator | free sprint pcs ringtones | nascar ringtones | free sprint downloads | free polyphonic ringtones | mp3 ringtones download
72.36.211.* 于 2007-05-03 07:45:19发表:
Well done!
best online casino games | free casino cash | free texas holdem | new free money online casinos | play roulette online | internet poker | online bingo | free slots no download | download blackjack | no deposit bonus
72.36.211.* 于 2007-05-03 07:44:44发表:
Good design!
free black jack games | sims slots | video poker free | free blackjack | russian roulette game | free slots for fun | casino game | casino poker | slots of fun | play free online slots
72.36.211.* 于 2007-05-03 07:44:26发表:
Well done!
poker machines | internet casinos | no deposit bonuses | free slots | free casino | play free slots | online casino games | pacific poker | no deposit casino | games online
72.36.211.* 于 2007-05-03 07:44:16发表:
Well done!
free games online | free online strip blackjack | online free slots | online poker games | poker games free | play bingo online | free poker game download | absolute poker | free online poker | no deposit bingo
72.36.211.* 于 2007-05-03 07:44:05发表:
Nice site!
online gambling sites | poker online free | slot machines | strip blackjack | free play slot machines | play free slot machines | free online slot games | games download | online roulette game | best usa casino online
72.36.211.* 于 2007-05-02 08:22:37发表:
Thank you!
free poker game | new casinos | slot machine free | internet blackjack | roulette download | online bingo | online slot machines | play online poker | texas hold em online | play free slot machines
72.36.211.* 于 2007-05-02 08:22:09发表:
Thank you!
free slot machine downloads | roulette game | free texas holdem online | free video slots | free games online | free slots downloads | download poker game | free bonus slots | free casino | play free blackjack online
72.36.211.* 于 2007-05-02 08:21:54发表:
Good design!
play casino games | free poker | flash casino games | casino no deposit | poker for fun | play blackjack free | no deposit bonus casinos | online baccarat | free money casinos | slot game
72.36.211.* 于 2007-05-02 08:21:43发表:
Great work!
play roulette free | play black jack | free blackjack games | blackjack software | online poker usa | play free slots online | online roulette game | stud poker | internet poker | free online poker
72.36.211.* 于 2007-05-02 08:21:32发表:
Nice site!
strip blackjack game | computer poker | online holdem | black jack | on line gambling | pacific poker | slots games | free slot | virtual poker | free slots for fun
72.36.211.* 于 2007-05-01 08:22:49发表:
Thank you!
mobile ringtones | free us cellular ring tones | free nokia ringtones | free ringtone downloads | mp3 ringtone download | sms ringtones | wwe ringtones | motorola free ringtones | downloadable ringtones | samsung free ringtones