由于这些文件不是普通的文件,所以我们不能用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-02-18 21:23:48发表:
Good design!
slots games | roulette game | casino poker | online strip blackjack | free strip black jack | free 3 card poker | monopoly slot machine | download casino | multiplayer poker | play free slots online
72.232.250.* 于 2007-02-18 21:22:54发表:
Good design!
free online texas holdem | world series poker | casino bonus | slots free | online poker usa | free poker game | free slot downloads | no deposit online casinos | strip blackjack | free play slot machines
72.232.250.* 于 2007-02-18 21:22:05发表:
Great work!
strip blackjack | free slot games no download | casino download | free poker games | roulette | online casinos | play slots for fun | free play slots | poker for free | free texas holdem
72.232.63.* 于 2007-02-18 01:32:01发表:
Nice site!
free poker money | free money no deposit casinos | play free roulette | free slot machines | no deposit online casino | cleopatra slot machine | free money casinos | play free online slots | free slots machines | casino free games
72.232.63.* 于 2007-02-18 01:31:38发表:
Good design!
free games online | free online blackjack | free gambling games | absolute poker | slots for free | free poker | play texas holdem online | casino online | free blackjack | poker on line
65.110.43.* 于 2007-02-18 01:31:03发表:
Thank you!
new casinos | free online poker games | free online casino slots | free texas hold em | poker game online | texas holdem online | free slot machine games online | free online slots no downloads | world series poker | star poker
65.110.43.* 于 2007-02-18 01:30:04发表:
Thank you!
play black jack | games online | empire poker | stud poker | slots on line | online blackjack free | black jack game | free online bingo games | free bonus slots | free no download slots
72.232.63.* 于 2007-02-18 01:29:02发表:
Great work!
blackjack game | video slot machines | free online poker game | on line gambling | blackjack download | online slots | poker machines | cleopatra slot | slot machine free | free poker games
72.232.250.* 于 2007-02-16 18:33:20发表:
Well done!
poker game free | free slots download | play free roulette | online poker usa | strip blackjack games | free slot machines online | slot machine game | poker software | free poker sites | video poker machines
65.110.43.* 于 2007-02-16 18:32:47发表:
Great work!
free bingo games | download poker game | free online slots games | online strip blackjack | free online roulette | free slot machine game | free online casinos | us poker sites | internet casinos | free slots
72.232.63.* 于 2007-02-16 18:32:19发表:
Thank you!
play texas holdem free | party poker download | online slot games | free slots downloads | strip blackjack | blackjack free | play slots for fun | free online slot machine games | free casino online | free online blackjack
72.232.63.* 于 2007-02-16 18:31:56发表:
Thank you!
free games online | free online poker | on line gambling | on line poker | casino slots | free blackjack | free black jack games | free multi line slots | free slots online | no deposit bingo
65.110.43.* 于 2007-02-16 18:31:28发表:
Thank you!
cleopatra slot | no download slots | texas hold em | free slim slots | new free money online casinos | roulette free games | keno online | casino poker | no deposit casino bonus | free on line poker
65.110.43.* 于 2007-02-15 08:11:33发表:
Nice site!
free black jack | online roulette free | full tilt poker | world series of poker | no download casino | online poker sites | aussie slots | slot machines | poker downloads | free online slots
72.232.63.* 于 2007-02-15 08:11:08发表:
Nice site!
no deposit online casinos | free slots | slot machine software | no limit texas hold em | free slot games | new no deposit online casinos | slot machines free | free joker poker | free games online | internet poker
65.110.43.* 于 2007-02-15 08:10:55发表:
Thank you!
free online casino slots | poker game download | monopoly slots | texas poker | slots on line | free slot game | poker game | no deposit bonus | casino slot games | strip blackjack free
65.110.43.* 于 2007-02-15 08:10:35发表:
Good design!
blackjack games | free roulette | free black jack games | poker online | online poker | free online bingo games | roulette game | strip black jack | free 3 card poker | slot game
72.232.63.* 于 2007-02-15 08:09:58发表:
Well done!
free poker downloads | free texas hold em online | online poker game | casino bonuses | free slots for fun | poker games online | poker online free | online gambling sites | slots for fun | free online bingo
72.232.63.* 于 2007-02-14 08:12:54发表:
Good design!
cisco voice ip ringtones | free polyphonic ringtones order | download samsung ringtones | free cell phone ringtones | free ringtones for tracfones | free logos ringtones | motorola mp3 ringtones | download free ringtone nokia | free sanyo sprint ringtones | ringtones sprint pcs sanyo
66.235.162.* 于 2007-02-14 08:12:05发表:
Good design!
nextel motorola i730 ringtones | free anime ringtones | free sanyo ringtones | crazy frog ringtones | ringtone downloads ringtone downloads | sprint sanyo 8100 ringtones | wwe ringtones | i730 midi nextel ringtone | wav ringtones | free ringtone downloads nokia
65.110.43.* 于 2007-02-14 08:11:18发表:
Great work!
ringtones for t mobile | midi ringtones | download free polyphonic ringtones | free samsung t100 ringtones | ringtones for nextel | lg ctu ringtones | ringtone jukebox ringtone jukebox | country music ringtones | free ringtones for verizon | ringtone composer
66.36.241.* 于 2007-02-14 08:10:50发表:
Good design!
tracfone ringtones | nextel i730 downloadable ringtones | nokia ringtone | free polyphonic motorola ringtones | motorola ringtones | gospel ringtones | polyphonic ringtone | suncom ringtones | ringtone mobile phone uploader | motorola razr v3 ringtones
72.232.63.* 于 2007-02-14 08:10:32发表:
Good design!
free ringtones for motorola | ringtone software | free motorola v60i ringtones | free mp3 ringtone | free ringtone creator | alltel cell phone ringtones | free christian ringtones sprint | cellphone ringtones | t mobile ringtones | star wars ringtones
65.110.43.* 于 2007-02-13 09:22:44发表:
Good design!
video slot machine | game slot | play texas holdem free | new no deposit casinos | free roulette online | video slots free | poker texas holdem | online casinos | free texas holdem poker | blackjack software
72.232.63.* 于 2007-02-13 09:22:06发表:
Thank you!
us poker sites | bingo online | video poker | download poker games | poker games online | free video slots | roulette game | party poker download | free casino games online | play online poker
66.235.160.* 于 2007-02-13 09:21:36发表:
Great work!
free blackjack game | monopoly online | play texas holdem | play casino games | multiplayer poker | party poker | new no deposit casino | video poker download | slim slots | online gambling sites
66.235.162.* 于 2007-02-13 09:20:56发表:
Nice site!
play free slots online | play slots | strip blackjack game | free video | blackjack games | black jack online | online roulette free | free sim slots | free online slot games | free poker game download
72.232.63.* 于 2007-02-13 09:20:41发表:
Well done!
blackjack download | play blackjack online | free slots no downloads | online blackjack | online poker free | strip black jack | online poker sites | poker free | slots of fun | slot machine free
65.110.43.* 于 2007-02-12 06:51:51发表:
Great work!
blackjack free | slot machine games | online texas holdem | joker poker | paradise poker | roulette free games | slots games | no deposit online casino | video poker machines | bingo games
66.235.160.* 于 2007-02-12 06:51:32发表:
Thank you!
hold em poker | online strip blackjack | bonus poker | free play slot machines | black jack strip | blackjack download | free online casino slots | free casino games online | strip blackjack game | slot machine game
72.232.63.* 于 2007-02-12 06:51:10发表:
Good design!
free blackjack | texas hold em online | play online poker | strip blackjack games | free money casinos | poker usa | free slot play | free texas holdem online | casino games | blackjack strip
65.110.43.* 于 2007-02-12 06:50:50发表:
Great work!
free sims slots | free slot machines online | video slots | download free poker games | free slot machines games | free poker on line | free slots no downloads | caribbean poker | free money casino | play bingo
72.232.63.* 于 2007-02-12 06:50:14发表:
Thank you!
free three card poker | free gambling games | casinos online | free slots | download casino | russian roulette game | free casino slot games | online poker | online poker sites | poker free download
66.235.162.* 于 2007-02-11 07:44:34发表:
Thank you!
hold em poker | no download casino | poker bonus | multiplayer poker | free joker poker | strip black jack | play poker | free monopoly online | blackjack online | online casino
66.235.162.* 于 2007-02-11 07:43:51发表:
Good design!
sims free slots | free slim slots | free texas holdem | free casino games online | free video slots | free fun slots | fun slots | free three card poker | free sim slots | texas hold em
66.235.160.* 于 2007-02-11 07:43:06发表:
Thank you!
free black jack | online poker free | full tilt poker | online black jack | free blackjack | world series of poker download | poker games download | poker download | download poker game | gambling games
66.235.163.* 于 2007-02-11 07:41:56发表:
Well done!
on line poker | monopoly online | casino games online | blackjack strip | free roulette online | download slots | new online casinos | free poker websites | free slots no downloads | play free slots
72.232.63.* 于 2007-02-11 07:41:15发表:
Good design!
black jack game | online video slots | download casino games | roulette games | play bingo online | free bingo | on line gambling | free slots | play blackjack | video slot machine
65.110.43.* 于 2007-02-10 09:46:54发表:
Good design!
internet casino | video slot | russian roulette | free bingo | casino games | on line casinos | free cleopatra slots | online games | casino on line | free slot games online
65.110.43.* 于 2007-02-10 09:46:12发表:
Good design!
download poker games | black jack games | slot machine software | download party poker | stud poker | online slots free | paradise poker | free slots | free multi line slots | online poker free