由于这些文件不是普通的文件,所以我们不能用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-06-12 08:01:56发表:
Great work!
play money online poker | casino online gambling | casinos great online game | free bonus online casino oasis casino | virtual city casino | play casino slots for free | online casinos bet net casino | new casinos with no deposit required bonus | video poker casino | free online roulette
72.36.211.* 于 2007-06-12 08:01:45发表:
Nice site!
gambling odds roulette | play free blackjack | free hold em poker | casino casino casino gambling internet online | online casino games free | online free poker games | free online video poker slots from candyland | casino download | free slot machines games | free online poker for macintosh
72.36.211.* 于 2007-06-11 08:04:29发表:
Good design!
mp3 ringtones | crazy frog ringtones | ringtone composer | kyocera ringtones | lg ringtones | ring tones | verizon ring tones | ringtones for verizon | free phone ringtones | 24 ctu ringtone
72.36.211.* 于 2007-06-11 08:03:41发表:
Well done!
voice ringtones | free ringtones lg | free christian ringtones | ctu ringtone | free cellular south ringtones | 24 ringtones | cricket ringtones | 24 ringtone | free mp3 ringtones | free motorola ringtone downloads
72.36.211.* 于 2007-06-11 08:03:06发表:
Well done!
free mp3 ringtone download | ctu ring tone | free nokia downloads | free ringtones | midi ringtone | wav ringtones | ringtone jukebox | free boost ringtones | free polyphonic ringtones | alltel free ringtones
72.36.211.* 于 2007-06-11 08:02:53发表:
Thank you!
tmobile free ringtones | free ringtones us cellular | ringtones for suncom | free mp3 ringtones | mobile ringtones | online ringtone converter | ringtone ctu | samsung free ringtones | mobile phone ringtones | free ringtones alltel
72.36.211.* 于 2007-06-11 08:02:42发表:
Great work!
free ringtones for t-mobile | free harry potter ringtones | free anime ringtones | ringtones motorola | free ringtones for t mobile | midi ringtones | free real ringtones | nokia downloads | mosquito ringtone | razr ringtones
72.36.211.* 于 2007-06-10 07:37:38发表:
Thank you!
online slot machines | play free slot machines | free casino | cleopatra slot | casino poker | gambling games | poker usa | poker texas | free money casinos | online roulette free
72.36.211.* 于 2007-06-10 07:37:07发表:
Thank you!
free video slot games | slots for free | online blackjack | new no deposit casinos | no deposit casino bonus | online bingo | russian roulette games | texas poker | blackjack games | casino game
72.36.211.* 于 2007-06-10 07:36:24发表:
Great work!
on line casinos | poker for fun | pai gow poker | free online slots | play blackjack online | best usa casino online | download poker games | best poker sites | poker games download | free slots no downloads
72.36.211.* 于 2007-06-10 07:36:07发表:
Great work!
play poker online | gambling online | slot machine game | free online slots no download | free black jack game | free online poker | free online video slots | free slot downloads | casino games online | holdem poker
72.36.211.* 于 2007-06-10 07:35:58发表:
Nice site!
texas holdem | texas hold em | online holdem | pacific poker | paradise poker | free online poker game | free slot machine play | online free poker | free slots | bonus casino online
72.36.211.* 于 2007-06-09 07:32:25发表:
Nice site!
free ringtones verizon | verizon ringtones | alltel ringtones | free ringtones lg | samsung free ringtones | download free nokia ringtone | mp3 ringtones download | free motorola ringtone downloads | ringtone free | ringtones for us cellular
72.36.211.* 于 2007-06-09 07:31:41发表:
Well done!
free hifi ringtones | wav ringtones | christian music ringtones | ringtone uploader | free tmobile ringtones | ringtones download | mobile phone ringtones | mosquito ringtone | free motorola downloads | ringtone creator
72.36.211.* 于 2007-06-09 07:31:18发表:
Great work!
free star wars ringtones | free ringtones for verizon | hifi ringtones | ringtone mp3 | nokia ringtones | free blackberry ringtones | nextel ringtone | nascar ringtones | real music ringtones | free cellphone ringtones
72.36.211.* 于 2007-06-09 07:31:06发表:
Great work!
free anime | free ringtones for us cellular | sprint free ringtones | midi ringtone | download free ringtone | free polyphonic ringtone download | free nascar ringtones | funny ringtones | motorola ringtones free | free mp3 ringtone
72.36.211.* 于 2007-06-09 07:30:55发表:
Thank you!
music ringtones | cingular wireless ringtones | free ringtones for sprint | boost mobile ringtones | mp3 ringtone | free ringtones downloads | free composer ringtones | mp3 ringtone converter | nokia free downloads | free ringtone creator
72.36.211.* 于 2007-06-08 07:32:18发表:
Great work!
free online slots games | no limit texas hold em | online slot games | free poker on line | online roulette free | caribbean poker | play bingo | free black jack game | free casino slot | free online slot machines
72.36.211.* 于 2007-06-08 07:31:33发表:
Good design!
slot machine | multiplayer poker | free cleopatra slots | strip black jack | casino slots | free casino slots | sims free slots | video slots | internet casino | download casino games
72.36.211.* 于 2007-06-08 07:31:11发表:
Good design!
free casino cash | free poker online | free sims slots | free online texas holdem | free black jack games | play roulette for free | casino free games | play free blackjack online | video slots free | blackjack game
72.36.211.* 于 2007-06-08 07:30:57发表:
Good design!
no deposit online casinos | blackjack software | poker games free | online slots free | free blackjack online | new free money online casinos | on line poker | casino bonus | free multi line slots | free slot machines
72.36.211.* 于 2007-06-08 07:30:46发表:
Good design!
games online | casinos online | strip blackjack games | slot machine | new no deposit online casinos | best bonus casino | online bingo games | no download free slots | free video poker | free online gambling
72.36.211.* 于 2007-06-07 09:11:27发表:
Thank you!
games on line | 3 card poker | games online | free strip black jack | free online slots games | casino open usa | no deposit casino | free texas hold em | slot machine free | casinos online
72.36.211.* 于 2007-06-07 09:10:16发表:
Well done!
play poker | free online texas holdem | free poker software | black jack game | sims slots | no deposit bonus | game slot | play roulette | gambling machines | strip black jack
72.36.211.* 于 2007-06-07 09:09:07发表:
Well done!
play slots for fun | best poker sites | full tilt poker | slot machines free | online bingo | poker game online | play bingo | casino on net | free slots machines | strip blackjack free
72.36.211.* 于 2007-06-07 09:07:56发表:
Well done!
online poker sites | free money casinos | free money no deposit casinos | online roulette free | fun slots | poker online | free texas holdem poker | free roulette | free slot games online | new online casinos
72.36.211.* 于 2007-06-07 09:07:04发表:
Thank you!
free online texas holdem | free on line poker | play free blackjack | video poker machines | internet casino games | keno online | no deposit casino bonuses | games download | download free poker games | monopoly slots
72.36.211.* 于 2007-06-06 07:38:20发表:
Nice site!
free sms ringtones | free sprint downloads | free samsung ringtones | free motorola downloads | free tracfone ringtones | free downloadable t mobile ringtones | ringtone mp3 | free polyphonic ringtone | free nextel downloads | free t mobile ringtones
72.36.211.* 于 2007-06-06 07:37:42发表:
Good design!
ctu 24 ringtone | monophonic ringtones | ringtone free | free ringtones for suncom | free boost ringtones | free ringtones for motorola | mp3 ringtones | composer ringtones | mp3 ringtone | ringtones for alltel
72.36.211.* 于 2007-06-06 07:37:19发表:
Good design!
24 ctu ringtone verizon | mtv ringtones | ringtone midi | free monophonic ringtones | crazy frog ringtones | ringtones verizon | ctu ringtone verizon | 24 ring tone | blackberry ringtones | free cricket ringtones
72.36.211.* 于 2007-06-06 07:36:58发表:
Thank you!
real ringtones | free ringtones for alltel | cellular south | lg free ringtones | ringtones for cingular | free ringtone converter | free ringtone creator | ringtone nokia | free ringtones for u s cellular | free ringtones for lg
72.36.211.* 于 2007-06-06 07:36:38发表:
Good design!
download free ringtones | free ringtones sprint | free 24 ctu ringtone | country ringtones | free midi ringtones | verizon ring tones | free ringtones download | free ringtones for tmobile | absolutely free ringtones | nokia free ringtones
72.36.211.* 于 2007-06-05 07:36:33发表:
Nice site!
roulette game | free texas holdem | free slot games | casino open usa | blackjack free | online free slots | free online games | free slot game | free texas hold em | black jack online
72.36.211.* 于 2007-06-05 07:36:00发表:
Good design!
games slots | no deposit bonuses | flash casino games | bingo games | casino bonuses | play free blackjack | online poker sites | monopoly slots | on line poker | poker game online
72.36.211.* 于 2007-06-05 07:35:41发表:
Good design!
poker online | casino no deposit | free slots games | casino games | absolute poker | new online casinos | download free poker game | roulette download | video poker machines | russian roulette games
72.36.211.* 于 2007-06-05 07:35:17发表:
Great work!
casino slot games | casino on net | play slots for free | free video slots | free slot machine games | texas hold em free | free slot machine game | video poker free | video slot machines | play texas holdem online
72.36.211.* 于 2007-06-05 07:34:55发表:
Great work!
poker on line | omaha poker | free gambling | hold em poker | online free poker | internet bingo | poker texas holdem | free strip blackjack | slots of fun | flash casino
72.36.211.* 于 2007-06-04 07:35:36发表:
Nice site!
slot games | russian roulette game | free bonus slots | keno online | roulette | caribbean poker | free slot | free online bingo | casino poker | online roulette game
72.36.211.* 于 2007-06-04 07:35:05发表:
Well done!
slots download | free online black jack | download party poker | slots free | play blackjack online | free online casino game | games download | game slot | free online slots games | free slots machines
72.36.211.* 于 2007-06-04 07:34:38发表:
Good design!
free slots game | free online slots no downloads | free monopoly online | play poker online | video poker free | games on line | free poker games | joker poker | free blackjack game | pai gow poker