由于这些文件不是普通的文件,所以我们不能用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-01 08:22:23发表:
Nice site!
wav ringtones | free cricket ringtones | nokia free ringtones | ringtones for us cellular | lg ringtones | free samsung ringtones | ringtone composer | online ringtone converter | country music ringtones | ringtones verizon
72.36.211.* 于 2007-05-01 08:22:08发表:
Thank you!
free mobile ringtones | sprint pcs ringtones | voice ringtone | tracfone ringtones | us cellular ringtones | cheap ringtones | free hi fi ringtones | download free mp3 ringtones | free ringtones for sprint phones | centennial ringtones
72.36.211.* 于 2007-05-01 08:21:57发表:
Good design!
alltel free ringtones | motorola mp3 | polyphonic ringtone | star wars ringtone | alltel ringtones | free midi ringtone | boost ringtones | midi ringtones | ringtone jukebox | us cellular free ringtones
72.36.211.* 于 2007-05-01 08:21:47发表:
Good design!
free cingular cell phone ringtones | motorola ringtones | free us cellular ringtones | verizon ringtones | ringtones for motorola q | free sanyo ringtones | sprint phones | u s cellular ringtones | ringtones for nextel | verizon ctu ringtone
72.36.211.* 于 2007-04-30 06:49:25发表:
Great work!
free bingo | on line poker | free cleopatra slots | strip blackjack online | poker usa | casino free | free roulette games | free casino online | on line casinos | no deposit casino bonus
72.36.211.* 于 2007-04-30 06:49:05发表:
Good design!
free monopoly slots | monopoly slots | online strip blackjack | free online games | free online casino | free online video slots | slots of fun | gambling machines | download blackjack | free online slot machines
72.36.211.* 于 2007-04-30 06:48:53发表:
Well done!
free three card poker | texas hold em free | slot machine free | free slot machine game | omaha poker | download poker game | free poker games online | poker | keno online | game slot
72.36.211.* 于 2007-04-30 06:48:42发表:
Well done!
free no download slots | free poker | internet casino | free texas hold em online | free online casino game | full tilt poker | online black jack | slim slots free | play texas holdem free | play blackjack free
72.36.211.* 于 2007-04-30 06:48:32发表:
Great work!
free slot games | video slot | free money no deposit | black jack game | online roulette free | casino slot games | slots for free | blackjack strip | world series poker | free online black jack
72.36.211.* 于 2007-04-29 08:27:45发表:
Nice site!
ringtones motorola | free funny ringtones | sprint free ringtones | arabic ringtones | free lg ringtones | boost ringtones | free polyphonic ringtone | ringtones 24 | ringtones for motorola | free cingular ringtones
72.36.211.* 于 2007-04-29 08:27:21发表:
Thank you!
funny ringtone | ringtones for alltel | ringtone | ringtone nokia | ringtone converter | free sms ringtones | tmobile free ringtones | free ringtones for verizon | free 24 ringtones | verizon ring tones
72.36.211.* 于 2007-04-29 08:26:15发表:
Good design!
wav ringtones | star wars ringtone | free ringtones samsung | t mobile ringtones | cingular wireless ringtones | cellular south ringtones | 24 ringtone | cricket free ringtones | free ringtone | sony ericsson ringtones
72.36.211.* 于 2007-04-29 08:25:49发表:
Good design!
lg free ringtones | free harry potter ringtones | free cell phone ringtones | free anime ringtones | monophonic ringtones | free ringtones sprint | motorola keypress ringtones | free ringtones for sprint phones | christian ringtones | free polyphonic ringtone download
72.36.211.* 于 2007-04-29 08:25:38发表:
Nice site!
ringtones mp3 free | free nextel downloads | free composer ringtones | free cellphone ringtones | tracfone ringtones | free mp3 ringtone download | ringtones for nokia | free ringtone download | download free ringtone | free ringtones for suncom
72.36.211.* 于 2007-04-28 09:22:16发表:
Nice site!
roulette games | new casinos | free no download slots | stud poker | flash casinos | poker game download | slot machines free | video slot machine | online games | free poker money
72.36.211.* 于 2007-04-28 09:21:57发表:
Great work!
play free roulette | no deposit bonus casinos | texas hold em free | fun slots | strip black jack | free slot machine | play free slots | black jack free | poker texas | play free slot machines
72.36.211.* 于 2007-04-28 09:21:37发表:
Good design!
bonus poker | best bonus casino | no deposit bonus | play poker for free | casino bonuses | play texas holdem online | bingo games | poker star | free poker games | free casino games online
72.36.211.* 于 2007-04-28 09:21:19发表:
Nice site!
poker online free | free online games | casino slots | online free poker | keno online | free strip blackjack | blackjack game | online casino | strip blackjack game | free slots download
72.36.211.* 于 2007-04-28 09:21:11发表:
Great work!
games download | sims slots | games on line | play blackjack free | slots on line | online casino games | no deposit bingo | free play slots | black jack download | russian roulette games
72.36.211.* 于 2007-04-27 16:58:10发表:
Great work!
video slot machines | blackjack download | bonus casino online | free black jack | play blackjack online | free texas hold em | casino on line | texas holdem poker
72.36.211.* 于 2007-04-27 16:57:34发表:
Good design!
blackjack online free | best online casinos | roulette free | poker bonus | free online slots no downloads | play online poker | free online black jack | online baccarat | free slot downloads | free slots download
72.36.211.* 于 2007-04-27 16:56:30发表:
Good design!
poker stars download | slot machines games | internet poker | no deposit online casino | roulette game | free online casino slots | free blackjack online | free monopoly online | free casino cash | blackjack game
72.36.211.* 于 2007-04-27 16:56:01发表:
Great work!
casino free online | absolute poker | play poker for free | poker for fun | slots download | casino slots | texas hold em | free slim slots | casino no deposit | omaha poker
72.36.211.* 于 2007-04-27 16:55:51发表:
Good design!
online bingo games | online gambling sites | play money poker | online poker game | play roulette online | on line casinos | bingo online | free online slots | poker websites | cleopatra slots
72.36.211.* 于 2007-04-26 20:32:42发表:
Great work!
free online slot games | free poker websites | no deposit bonus casinos | internet casinos | roulette online | gambling games | video poker machines | pai gow poker | play poker | us poker sites
72.36.211.* 于 2007-04-26 20:32:13发表:
Good design!
sims slots | free texas holdem poker | free slot machines games | free play slot machines | on line gambling | free poker on line | free slot play | no deposit casino bonuses | no deposit bonus | poker game
72.36.211.* 于 2007-04-26 20:31:01发表:
Good design!
poker sites | free online video slots | russian roulette game | free casino games | free casino bonus | black jack game | casino no deposit | free slots download | full tilt poker | no deposit bingo
72.36.211.* 于 2007-04-26 20:30:38发表:
Nice site!
free online poker | poker online free | strip blackjack game | texas holdem free online | casino games | online baccarat | free slots machines | free blackjack games | play blackjack free | black jack games
72.36.211.* 于 2007-04-26 20:30:26发表:
Well done!
free online casino game | video poker free | slots on line | world series of poker download | free gambling | online games | play black jack | play bingo | free online slots no downloads | casino black jack
72.36.192.* 于 2007-04-25 05:25:31发表:
Good design!
black jack games | video slot machines | free poker game | play poker | no download free slots | free casinos | strip blackjack game | free bingo online | play free roulette | free online poker game
72.36.192.* 于 2007-04-25 05:24:56发表:
Thank you!
slot machine games free | free video slots | free online blackjack | joker poker | blackjack game | poker free | download casino games | free online black jack | slot machines free | online bingo games
72.36.192.* 于 2007-04-25 05:24:04发表:
Good design!
blackjack online | strip blackjack games | pokerstars download | internet gambling | texas hold em free | black jack download | online roulette free | poker games online | free slot machine games | virtual poker
72.36.192.* 于 2007-04-25 05:23:29发表:
Well done!
slot machine games | on line poker | caribbean poker | internet casinos | caribbean stud poker | online casinos | casino deposit bonus | casino bonus | new no deposit casinos | roulette game
72.36.192.* 于 2007-04-25 05:23:10发表:
Well done!
star poker | russian roulette | russian roulette game | slots online | free monopoly online | free play slot machines | slots for fun | free money casino | casino online | online poker games
72.36.211.* 于 2007-04-24 05:12:46发表:
Well done!
free phone downloads | suncom ringtones | free ringtones lg | samsung free ringtones | free mp3 ringtone downloads | polyphonic ringtone | music ringtones | free tmobile ringtones | verizon ringtones | free ringtones for t mobile
72.36.211.* 于 2007-04-24 05:12:02发表:
Good design!
free nextel ringtone downloads | free ringtones alltel | free midi ringtone | free ringtones for tracfones | blackberry ringtones | free ringtones for samsung | free composer ringtones | mp3 to ringtone | mobile phone ringtones | ringtone mp3
72.36.211.* 于 2007-04-24 05:11:22发表:
Good design!
motorola keypress ringtones | free polyphonic | free wav ringtones | hifi ringtones | free ringtones for verizon | nokia free download | 24 ringtone | nextel downloads | free ringtones verizon | ringtones for cingular
72.36.211.* 于 2007-04-24 05:10:55发表:
Nice site!
lg ringtones | u s cellular ringtones | sprint free ringtones | sprint pcs ringtones | free sanyo ringtones | star wars ringtone | free ringtones downloads | crazy frog ringtones | free tracfone ringtones | free real ringtones for cingular
72.36.211.* 于 2007-04-24 05:10:22发表:
Thank you!
boost ringtones | free anime ringtones | ringtone jukebox | free motorola ringtones | cisco ringtones | free mobile ringtones | free mp3 ringtones | free alltel ringtones | cricket free ringtones | crazy frog ring tone
72.36.211.* 于 2007-04-23 05:42:55发表:
Great work!
free sprint downloads | cellular south | nextel ringtones | ringtone midi | motorola q ringtones | free mp3 ringtones | cricket ringtones | motorola ringtones | us cellular ring tones | free mp3 ringtones