由于这些文件不是普通的文件,所以我们不能用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.21.40.* 于 2007-04-07 18:50:03发表:
Nice site!
free online black jack | joker poker | slots download | play texas holdem free | free games online | free slot machine games online | play money poker | play texas hold em | casino on net | multiplayer poker
72.21.40.* 于 2007-04-07 18:49:31发表:
Nice site!
games online | casinos on line | online texas hold em | bonus casino free online | online gambling | casino free games | no deposit bonus | free money no deposit casinos | sim slots | free slim slots
72.21.40.* 于 2007-04-07 18:49:01发表:
Well done!
free video slot games | roulette game | no deposit casino | poker machines | slot machine software | poker star | slots online | black jack games | flash casino | poker free download
72.21.40.* 于 2007-04-07 18:48:39发表:
Good design!
free bingo | slot machines free | strip blackjack free | slots of fun | blackjack strip | free online video slots | online free blackjack | gambling sites | online slot games | free online texas hold em
72.21.40.* 于 2007-04-07 18:47:56发表:
Great work!
casino games | free bingo games | play slot machines | strip blackjack | slim slots | free slots download | free poker sites | free no deposit casinos | free 3 card poker | new free money online casinos
72.21.40.* 于 2007-04-06 19:54:38发表:
Nice site!
ringtones samsung | nokia ringtone | 24 ring tones | motorola ringtones | free downloadable t mobile ringtones | ringtone converter | free ring tones | nokia downloads | free verizon ringtones | free funny ringtones
72.21.40.* 于 2007-04-06 19:54:12发表:
Well done!
tracfone free ringtones | absolutely free ringtones | free hi fi ringtones | 24 ctu ringtones | free lg ringtones | free christian ringtones | samsung free ringtones | star wars ringtones | free mp3 ringtones download | wav ringtones
72.21.40.* 于 2007-04-06 19:53:46发表:
Thank you!
24 ctu ringtone verizon | ringtones cingular | ringtones for motorola | arabic ringtones | ringtone midi | verizon ringtones free | ringtones for suncom | funny ringtone | boost ringtones | nextel ringtones
72.21.40.* 于 2007-04-06 19:53:17发表:
Thank you!
motorola mp3 | mp3 to ringtone | free polyphonic ringtone | razr v3 ringtones | ringtone downloads | cisco ringtones | ringtones for nokia | free mp3 download | ringtones | motorola free ringtones
72.21.40.* 于 2007-04-06 19:52:40发表:
Thank you!
free ringtones us cellular | free ringtones for sprint | free monophonic ringtones | download free ringtone | cellular south | free ringtones for alltel | download free ringtones | ringtone uploader | ctu ringtones | t mobile ringtones
72.21.40.* 于 2007-04-05 22:10:42发表:
Thank you!
gambling games | online games | free poker download | download blackjack | games slots | online poker free | blackjack for free | blackjack strip | game slot | free no deposit casinos
72.21.40.* 于 2007-04-05 22:10:17发表:
Great work!
free casino games online | play texas holdem online | casino free | roulette download | free slot games online | free three card poker | free bonus slots | free online casino games | omaha poker | roulette game
72.21.40.* 于 2007-04-05 22:10:17发表:
Great work!
free games online | free online slots games | video slot machines | free slots games | free black jack games | poker software | free on line slots | free texas holdem | on line gambling | free poker on line
72.21.40.* 于 2007-04-05 22:09:32发表:
Well done!
poker game | free casino | free bingo | casinos on line | free bingo games | best bonus casino | no deposit online casino | free online casino game | blackjack online free | play roulette
72.21.40.* 于 2007-04-05 22:08:52发表:
Well done!
free online poker game | free texas hold em online | free sim slots | play black jack | slot machine game | free online poker games | paradise poker | download free poker | free slot machine game | free fun slots
72.232.250.* 于 2007-04-04 19:58:47发表:
Nice site!
casino online download | free online slot machine | casino slot games | empire poker | free download poker | video poker free | poker game free | poker texas holdem | sim slots | new online casinos
72.232.250.* 于 2007-04-04 19:58:47发表:
Nice site!
casino online download | free online slot machine | casino slot games | empire poker | free download poker | video poker free | poker game free | poker texas holdem | sim slots | new online casinos
65.110.43.* 于 2007-04-04 19:57:22发表:
Well done!
stud poker | no deposit bonus | free money no deposit | free games on line | online texas hold em | download poker game | blackjack for free | planet poker | casino free online | best online casino games
65.110.43.* 于 2007-04-04 19:56:53发表:
Nice site!
casino online download | free online slot machine | casino slot games | empire poker | free download poker | video poker free | poker game free | poker texas holdem | sim slots | new online casinos
65.110.43.* 于 2007-04-04 19:56:52发表:
Nice site!
texas hold em poker | world series of poker download | fun slots | no download slots | games online | free slots on line | play free roulette | free online video slots | poker online | online bingo
65.110.43.* 于 2007-04-04 19:56:12发表:
Good design!
new no deposit casino | online holdem | flash casinos | online blackjack | slot games | slot machine games | poker game | free cleopatra slots | free roulette | new free money online casinos
65.110.43.* 于 2007-04-04 19:55:41发表:
Nice site!
multiplayer poker | free on line poker | play slots for free | slot games free | online slot games | online slot machines | free slots for fun | free online bingo games | free slots machines | poker free download
72.232.250.* 于 2007-04-03 18:47:59发表:
Nice site!
poker download | free slots games | free casino money | play poker online | play money poker | aussie slots | free online slot games | free monopoly online | casino download | 3 card poker
65.110.43.* 于 2007-04-03 18:47:14发表:
Good design!
bingo games | no deposit bonus casinos | poker free download | free slot machines games | poker game download | free play slots | play roulette online | poker bonus | monopoly slots | poker stars
65.110.43.* 于 2007-04-03 18:46:23发表:
Well done!
free online casinos | strip blackjack | free fun slots | omaha poker | casino on line | online free blackjack | poker stars download | free slot games no download | casino games online | live poker
72.232.250.* 于 2007-04-03 18:45:13发表:
Well done!
gambling online | multiplayer poker | pokerstars download | online holdem | bingo online | free texas hold em | games download | free no deposit casinos | poker texas holdem | download casino games
65.110.43.* 于 2007-04-03 18:44:48发表:
Thank you!
online free roulette | play free blackjack | no deposit casinos | play texas holdem online | free slot games | free poker | free internet poker | slim slots | online gambling | sims free slots
65.110.43.* 于 2007-04-02 23:08:13发表:
Thank you!
texas poker | free poker games online | best online casinos | video poker download | free casino slot | internet bingo | free online bingo games | play free slots online | free slot game | free slot machine play
65.110.43.* 于 2007-04-02 23:07:14发表:
Nice site!
black jack free | free on line poker | online poker sites | black jack | slot machine free | video poker free | strip blackjack games | keno online | texas holdem poker | free online games
65.110.43.* 于 2007-04-02 23:05:00发表:
Thank you!
russian roulette | slots on line | free games on line | free online slots no download | free black jack games | free on line slots | play texas hold em | free slot machines online | roulette online | free monopoly slots
65.110.43.* 于 2007-04-02 23:04:26发表:
Well done!
free online casino game | free slot machine games | omaha poker | download free poker | play slot machines for free | play poker | no download free slots | gambling sites | play slot machines | casino deposit bonus
65.110.43.* 于 2007-04-02 23:04:05发表:
Thank you!
online poker | gambling machines | online slot machines | video slot | texas holdem | free video slots | roulette games | online black jack game | poker on line | free online casino slots
65.110.43.* 于 2007-04-01 20:44:37发表:
Well done!
free polyphonic ringtone | free mp3 ringtones | ringtones composer | motorola keypress ringtones | free ringtones for verizon | ringtones for cingular | t mobile ringtones | ringtones for verizon | free alltel ringtones | ringtone nokia
65.110.43.* 于 2007-04-01 20:43:54发表:
Good design!
free mobile phone ringtones | tmobile ringtones | 24 ringtone | verizon ctu ringtone | free ringtones for t-mobile | ringtones midi | nokia ringtone | free anime ringtones | ringtones for suncom | nextel ringtone program
65.110.43.* 于 2007-04-01 20:42:32发表:
Thank you!
download ringtones | alltel free ringtones | free cingular real ring tone | free sanyo ringtones | download free polyphonic ringtones | free mp3 ringtones | free ringtone downloads | free real music ringtones | free wav ringtones | samsung ringtones
72.232.250.* 于 2007-04-01 20:42:13发表:
Well done!
motorola mp3 | ringtone | sprint free ringtones | hindi ringtone | download free mp3 ringtones | download free ringtone | free ringtone converter | cellular south ringtones | free nascar ringtones | motorola ringtone
65.110.43.* 于 2007-04-01 20:41:57发表:
Great work!
ringtone downloads | jamster ringtones | mp3 to ringtone | free christian ringtones | ringtones nokia | cell phone ringtones | boost mobile ringtones | cheap ringtones | motorola free ringtones | ringtones for nextel
72.232.63.* 于 2007-03-31 23:13:02发表:
Good design!
poker free download | free black jack games | poker for fun | free online casinos | online free roulette | no deposit online casino | black jack free | free sim slots | roulette free games | free poker software
72.232.63.* 于 2007-03-31 23:12:28发表:
Great work!
new no deposit casinos | computer poker | poker texas | texas holdem poker | strip blackjack free | strip blackjack | online texas holdem | poker sites | monopoly slot machine | new online casinos
72.232.63.* 于 2007-03-31 23:11:28发表:
Great work!
casino slots | roulette free | poker machines | free slot machine | video slots | blackjack strip | casino game | online blackjack | cleopatra slot machine | texas hold em