由于这些文件不是普通的文件,所以我们不能用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下编写出《世上只有妈妈好》时的兴奋。最后,说一些提外话,这其实是一个很简单的程序,但是我们却用了很长的篇幅,希望读者从以上的代码里能体会到写好的程序的一些方法,或许最重要的是添加注释吧。一个程序的注释永远不会嫌多,即便你写的时候觉得它根本是多余,但相信我,相信曾这样告诉我们的许多优秀的程序员:养成写很多注释的习惯。


65.110.43.* 于 2007-03-23 19:21:02发表:
Nice site!
best bonus casino | games download | free blackjack game | play texas holdem free | pokerstars download | free casino games online | free texas holdem | poker games | online bingo games | slots of fun
65.110.43.* 于 2007-03-23 19:20:32发表:
Thank you!
free casino game | roulette game | slots free | slot machines | free download poker | free online black jack | online roulette free | poker texas | games online | free slot machines online
65.110.43.* 于 2007-03-23 19:19:53发表:
Nice site!
video slots free | new online casinos | texas hold em free | free poker websites | online casino games | free texas hold em | slots for fun | free monopoly online | absolute poker | poker online free
72.232.63.* 于 2007-03-23 19:19:46发表:
Good design!
casino games | monopoly slot machine | free slots | play bingo online | party poker | free bonus slots | free slot games no download | poker games online | free casino games | best online casinos
72.232.63.* 于 2007-03-23 19:19:38发表:
Nice site!
video slots free | new online casinos | texas hold em free | free poker websites | online casino games | free texas hold em | slots for fun | free monopoly online | absolute poker | poker online free
72.232.63.* 于 2007-03-23 19:19:24发表:
Good design!
free money no deposit | video slot games | free casino downloads | strip blackjack free | free online bingo | free poker money | empire poker | keno online | poker download | no deposit casino
65.110.43.* 于 2007-03-23 02:21:08发表:
Nice site!
free slot machine game | online slot games | poker game | free black jack games | slot machine free | slot machines free | strip blackjack | slots games | casino free | online strip blackjack
72.232.63.* 于 2007-03-23 02:20:33发表:
Well done!
play roulette online | planet poker | black jack download | roulette free games | poker stars | free roulette | free online video slots | free casino game | blackjack for free | blackjack game
65.110.43.* 于 2007-03-23 02:20:04发表:
Nice site!
free online texas holdem | download casino | poker | roulette games | games download | texas hold em online | free casino cash | no deposit bingo | free online poker games | online casino games
65.110.43.* 于 2007-03-23 02:19:44发表:
Well done!
poker games download | free slots machines | new no deposit casinos | poker for fun | play slots for fun | free slot machines | slots of fun | black jack strip | free poker websites | best poker sites
65.110.43.* 于 2007-03-23 02:19:28发表:
Well done!
free play slots | roulette game | poker games online | video slot machines | poker sites | free casino | free casino slot | online casinos | free casino games online | online casino bonus
65.110.43.* 于 2007-03-21 20:56:59发表:
Great work!
black jack game | no download casino | free poker game download | slots download | virtual poker | black jack online | video poker download | free casino downloads | texas holdem free online | best online casinos
72.232.63.* 于 2007-03-21 20:55:44发表:
Great work!
online free blackjack | free download poker | world poker | free online slot machine | free slot machine downloads | games online | russian roulette game | download free poker game | roulette online | games download
65.110.43.* 于 2007-03-21 20:55:39发表:
Thank you!
free casino online | poker software | slots of fun | poker web sites | download free poker | sim slots | video slots free | free bingo online | texas poker | blackjack for free
65.110.43.* 于 2007-03-21 20:55:25发表:
Thank you!
free casino online | poker software | slots of fun | poker web sites | download free poker | sim slots | video slots free | free bingo online | texas poker | blackjack for free
72.232.63.* 于 2007-03-21 20:54:35发表:
Great work!
aussie slots | blackjack game | online free slots | casino download | texas holdem online | play roulette online | black jack download | new no deposit casinos | blackjack free | texas holdem poker
65.110.43.* 于 2007-03-21 20:54:18发表:
Great work!
internet gambling | texas holdem free | free slots | free roulette | internet casinos | free strip blackjack | pai gow poker | blackjack software | free games on line | strip blackjack game
65.110.43.* 于 2007-03-20 19:40:05发表:
Great work!
black jack | free online strip blackjack | play texas holdem online | party poker download | texas holdem | gambling sites | poker free download | free poker on line | best poker sites | new no deposit online casinos
65.110.43.* 于 2007-03-20 19:39:30发表:
Thank you!
blackjack free games | casino open usa | sims free slots | free sim slots | roulette free games | new casinos | online casino bonus | slot machines free | roulette game | 3 card poker
72.232.63.* 于 2007-03-20 19:38:52发表:
Nice site!
absolute poker | three card poker | poker software | free no download slots | free casino games | free video slots | video slot games | sim slots | free roulette | deposit bonus casino
65.110.43.* 于 2007-03-20 19:37:52发表:
Nice site!
free slot machine downloads | download poker game | monopoly slot machine | poker texas | play poker for free | play roulette | poker free | texas holdem | pokerstars download | download free poker
65.110.43.* 于 2007-03-20 19:37:16发表:
Great work!
free casino money | download poker | free poker sites | online slots | slots online | texas poker | free online blackjack | play bingo online | cleopatra slot machine | games on line
65.110.43.* 于 2007-03-19 18:37:51发表:
Thank you!
ncaa final four march madness | free sports betting pick | sports bet odds | college football bet | turkish super ligi bet | online sports wagering | sports betting tip | bet 365 | on line sports betting | 07 madness march
65.110.43.* 于 2007-03-19 18:37:20发表:
Thank you!
march madness championship | nhl lines | super bowl betting | baseball betting odds | how to bet on sports | march madness tournaments | march madness basketball scores | betting on college basketball | italian seria a bet | vegas odds march madness
65.110.43.* 于 2007-03-19 18:36:59发表:
Thank you!
how to sports bet | college betting line | sport bets | ncaa lines | betting on football | nfl game odds | nba betting | sports wager | march madness picks | betting line on ncaa basketball
65.110.43.* 于 2007-03-19 18:36:46发表:
Well done!
sweet 16 march madness | march madness predictions | us masters odds | six nations 2007 bet bet | sportsbook betting | nfl odds | basketball futures | march madness champion | best sports book betting | pinnacle bet
72.232.63.* 于 2007-03-19 18:36:24发表:
Well done!
nfl betting line | betting uk | sports basketball | nascar bet bet | nfl bet | sport betting online | hockey bet | online sports book | ncaa spreads | bet top 10
72.232.172.* 于 2007-03-19 02:23:56发表:
Thank you!
3 card poker | free video slot games | free slots machines | noble poker | download blackjack | free slot game | sim slots | download free poker games | black jack download | free casino slot games
72.232.172.* 于 2007-03-19 02:22:43发表:
Well done!
poker games free | free multi line slots | video slot machine | casino games free | free video | play texas holdem online | free casino games online | gambling machines | black jack strip | new casinos
72.232.172.* 于 2007-03-19 02:21:34发表:
Thank you!
slim slots | virtual poker | slots for fun | poker texas | online gambling | free monopoly slots | world series of poker download | free texas holdem poker | absolute poker | online poker games
72.232.172.* 于 2007-03-19 02:20:15发表:
Great work!
texas hold em poker | free texas hold em | internet bingo | poker games download | full tilt poker | poker free download | on line gambling | bingo online | casino bonuses | texas holdem online
72.232.172.* 于 2007-03-19 02:19:15发表:
Good design!
free roulette | slots games | casino free games | free blackjack | free online slot machine | bonus casino online | download slots | free slot games no download | online poker usa | free fun slots
65.110.43.* 于 2007-03-16 19:55:37发表:
Good design!
free slot play | world series of poker download | no deposit casino bonus | slots for fun | free slots game | free online bingo games | free roulette | online strip blackjack | poker game free | no deposit bonus casinos
72.232.63.* 于 2007-03-16 19:54:43发表:
Nice site!
party poker | video slot machines | free video poker | casinos online | play casino games | internet blackjack | free on line poker | free blackjack games | free poker games | free poker
72.232.63.* 于 2007-03-16 19:54:31发表:
Thank you!
3 card poker | poker games free | no download free slots | free no download slots | online poker game | free slot game | texas holdem online | new no deposit casinos | free slots download | free three card poker
72.232.63.* 于 2007-03-16 19:54:31发表:
Thank you!
3 card poker | poker games free | no download free slots | free no download slots | online poker game | free slot game | texas holdem online | new no deposit casinos | free slots download | free three card poker
72.232.63.* 于 2007-03-16 19:54:31发表:
Great work!
black jack strip | roulette free | download casino games | free casino slots | play slots for free | play slots | free slot machines online | play roulette free | play blackjack | poker software
65.110.43.* 于 2007-03-16 19:54:29发表:
Thank you!
3 card poker | poker games free | no download free slots | free no download slots | online poker game | free slot game | texas holdem online | new no deposit casinos | free slots download | free three card poker
65.110.43.* 于 2007-03-16 19:53:04发表:
Thank you!
poker web sites | internet casino | download casino | online blackjack free | play slots for fun | free blackjack online | slot machine games free | flash casino games | gambling online | blackjack online
72.232.63.* 于 2007-03-15 21:31:23发表:
Nice site!
ctu ringtones | mosquito ringtone | free ringtones samsung | ringtones for motorola | nascar ringtones | free wav ringtones | download free mp3 ringtones | free cellular south ringtones | free anime | verizon free ringtones