由于这些文件不是普通的文件,所以我们不能用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-19 07:40:27发表:
Great work!
free ringtones us cellular | free polyphonic | us cellular ringtones | hifi ringtones | ringtone 24 | star wars ringtone | free wav ringtones | cricket free ringtones | centennial wireless | 24 ctu ringtone verizon
72.36.211.* 于 2007-05-19 07:40:06发表:
Nice site!
free ringtones for nextel | verizon ringtones free | mosquito ring tone | country music ringtones | 24 ctu ringtones | cell phone ringtones | ringtones cingular | ringtone mp3 | motorola ring tones | free ringtones sprint
72.36.211.* 于 2007-05-18 07:41:12发表:
Great work!
free online casino game | online video slots | slot games | black jack free | poker usa | video poker machines | casino free games | casinos on line | casino online | poker on line
72.36.211.* 于 2007-05-18 07:40:37发表:
Thank you!
poker for fun | free texas hold em | free texas holdem | free money no deposit | texas poker | free casino | play casino games | free slots machines | free slim slots | game slot
72.36.211.* 于 2007-05-18 07:40:12发表:
Good design!
black jack game | online gambling sites | slot machine software | free online slots games | texas holdem free | play free blackjack online | fun slots | strip blackjack game | play slots for fun | gambling sites
72.36.211.* 于 2007-05-18 07:39:49发表:
Nice site!
poker stars download | black jack download | free video slot games | free black jack game | russian roulette games | video slot machine | casino no deposit | party poker download | free online casinos | free strip blackjack
72.36.211.* 于 2007-05-18 07:39:28发表:
Nice site!
free on line slots | play free online slots | play bingo online | black jack online | no deposit bonus | poker game | bonus casino online | free casino slot | texas hold em | free games on line
72.36.211.* 于 2007-05-17 07:42:59发表:
Well done!
download free ringtones | ringtone converter | ringtones motorola | crazy frog ring tone | bollywood ringtones | free nokia downloads | mp3 ringtone download | ringtones for alltel | nextel ringtones | ringtones nokia
72.36.211.* 于 2007-05-17 07:42:23发表:
Good design!
alltel ringtones | cingular ringtones | free ringtones | free ringtones sprint | free cingular real ring tone | motorola ringtone | free samsung ringtones | ringtones mp3 free | suncom ringtones | free verizon ringtones
72.36.211.* 于 2007-05-17 07:41:55发表:
Good design!
free boost ringtones | country ringtones | ringtone downloads | free ringtones for sprint phones | free hi fi ringtones | midi ringtones | absolutely free ringtones | sprint pcs ringtones | centennial wireless ringtones | tmobile free ringtones
72.36.211.* 于 2007-05-17 07:41:29发表:
Nice site!
ringtone jukebox | polyphonic ringtone | verizon ringtones | free tracfone ringtones | crazy frog ringtone | download free ringtone | downloadable ringtones | funny ringtones free | nextel ringtone program | ringtones samsung
72.36.211.* 于 2007-05-17 07:41:15发表:
Well done!
free motorola ringtone downloads | ringtone motorola | wav ringtones | free ringtones for cingular | kyocera ringtones | midi ringtone | free 24 ringtones | cellular south | ringtones lg | free phone ringtones
72.36.211.* 于 2007-05-16 07:35:55发表:
Nice site!
free phone downloads | free ringtone creator | mp3 ringtone converter | verizon ringtones free | ctu ringtone mp3 | free funny ringtones | free blackberry ringtones | free monophonic ringtones | nokia free download | free verizon ringtones
72.36.211.* 于 2007-05-16 07:35:32发表:
Thank you!
free ringtones for lg | download free polyphonic ringtones | tmobile free ringtones | free polyphonic ringtone download | free motorola downloads | cellphone ringtones | free country ringtones | nascar ringtones | free midi ringtone | ringtones for nokia
72.36.211.* 于 2007-05-16 07:35:00发表:
Great work!
free ringtones verizon | free cingular ringtones | harry potter ringtone | free cingular cell phone ringtones | free ringtones for tracfones | tracfone free ringtones | free cingular real ring tone | free cellular ringtones | motorola free ringtones | free kyocera ringtones
72.36.211.* 于 2007-05-16 07:34:34发表:
Well done!
nextel ringtones | cell phone ringtones | nextel downloads | free ringtones motorola | cricket ringtones | free ringtones for alltel | midi ringtones | funny ringtone | free nascar ringtones | free ringtones alltel
72.36.211.* 于 2007-05-16 07:34:14发表:
Good design!
free ringtones for sprint | ringtone downloads | free mp3 ringtones | free boost ringtones | ringtone jukebox | ringtone download | free composer ringtones | ringtones | free cell phone ringtones | ringtones for sprint phones
72.36.211.* 于 2007-05-15 07:36:22发表:
Good design!
free ringtones | cricket ringtones | ringtone motorola | free ringtones for us cellular | ringtone nokia | ringtone websites | sprint phones | midi ringtone | free ringtones for u s cellular | free downloadable t mobile ringtones
72.36.211.* 于 2007-05-15 07:36:02发表:
Thank you!
alltel ringtones | polyphonic ringtones | free phone ringtones | mp3 ringtones download | free cellular ringtones | keypress ringtones | ringtone midi | ring tones | free mobile phone ringtones | verizon free ringtones
72.36.211.* 于 2007-05-15 07:35:31发表:
Thank you!
free ringtone | free country music ringtones | nextel ringtone | free ctu ringtone | hindi ringtone | crazy frog ringtone | free ringtones for sprint | mp3 ringtone converter | verizon ring tones | ringtone composer
72.36.211.* 于 2007-05-15 07:35:08发表:
Well done!
mp3 ringtones | 24 ctu ringtones | t mobile ringtones | ringtone 24 ctu | ringtones download | 24 ringtone free | free ringtones for nokia | free cellular one ringtones | ringtones wav | samsung ringtones
72.36.211.* 于 2007-05-15 07:34:49发表:
Good design!
ringtones verizon | free mp3 ringtone | free ringtones for t-mobile | free anime ringtones | centennial wireless ringtones | star wars ringtones | free ringtones for verizon | motorola ringtone | free boost mobile ringtones | nextel downloads
72.36.211.* 于 2007-05-14 01:41:46发表:
Good design!
free cellular one ringtones | christian ringtones | cellular south | free ringtones for lg | free nascar ringtones | 24 ring tones | free ringtones for alltel | ringtone creator | midi ringtones | free ring tones
72.36.211.* 于 2007-05-14 01:41:17发表:
Great work!
free country ringtones | free bollywood ringtones | tmobile ringtones | free mobile ringtones | free us cellular ring tones | free ringtones for us cellular | free mp3 ringtone download | free mp3 ringtones download | free mp3 ringtone | ringtones samsung
72.36.211.* 于 2007-05-14 01:40:49发表:
Thank you!
music ringtones | nextel ringtone | us cellular free ringtones | free mp3 download | centennial ringtones | free phone ringtones | motorola keypress ringtones | download free motorola ringtone | absolutely free ringtones | simpsons ringtone
72.36.211.* 于 2007-05-14 01:40:26发表:
Thank you!
ringtones cingular | ringtones for tracfones | free ringtones for sprint | centennial wireless ringtones | mosquito ringtone | free sms ringtones | free sprint pcs ringtones | ctu ringtone verizon | free motorola downloads | free ringtones samsung
72.36.211.* 于 2007-05-14 01:40:06发表:
Well done!
anime ringtones | mp3 ringtones free | 24 ctu ringtone | free monophonic ringtones | free nokia ringtone | free cellphone ringtones | free ringtones download | 24 ringtone | ringtones composer | verizon ringtones free
72.36.211.* 于 2007-05-12 19:14:43发表:
Nice site!
casino game download | us poker sites | slot machine game | free online slots | monopoly online | poker bonus | russian roulette | pokerstars download | free fun slots | blackjack online
72.36.211.* 于 2007-05-12 19:14:22发表:
Good design!
blackjack free | poker stars download | play free slots online | noble poker | on line gambling | free video poker | free online slot machine | free monopoly slots | best online casinos | free poker online
72.36.211.* 于 2007-05-11 05:32:07发表:
Good design!
monopoly online | party poker | download free poker games | free slot machines online | bingo games | free slim slots | casino open usa | bonus casino online | gambling sites | casino download
72.36.211.* 于 2007-05-11 05:31:36发表:
Well done!
games slots | free black jack game | live poker | casino free online | free online blackjack | bingo online | online black jack game | free slot game | blackjack free | slot machines free
72.36.211.* 于 2007-05-11 05:31:11发表:
Good design!
play free blackjack online | play blackjack online | russian roulette games | on line gambling | online black jack | free slot machine downloads | best online casinos | play bingo online | free poker money | internet casinos
72.36.211.* 于 2007-05-11 05:30:45发表:
Good design!
online poker | online poker usa | poker online | free online casino slots | casino slots | free poker games | keno online | play slot machines for free | online slots | download poker game
72.36.211.* 于 2007-05-11 05:30:29发表:
Well done!
slot machine games | play free slots | free online strip blackjack | download free poker | online video slots | caribbean poker | free no deposit casino | free online gambling | casino on net | free online poker
72.36.211.* 于 2007-05-10 08:00:32发表:
Well done!
arabic ringtones | free real tone ringtones | ringtones lg | ctu ringtone verizon | free star wars ringtones | jamster ringtones | free ringtone converter | ringtone jukebox | free ringtones for cricket | motorola keypress ringtones
72.36.211.* 于 2007-05-10 08:00:09发表:
Great work!
free ringtones for t-mobile | ringtones for nokia | ringtones samsung | boost ringtones | free phone ringtones | ringtones composer | lg free ringtones | crazy frog ringtones | nokia ringtone | ringtone 24
72.36.211.* 于 2007-05-10 07:59:44发表:
Well done!
motorola free ringtones | free hifi ringtones | free ringtone creator | free ringtones for sprint | free christian ringtones | absolutely free ringtones | free midi ringtone | ringtones motorola | free sprint downloads | free mp3 ringtone
72.36.211.* 于 2007-05-10 07:59:19发表:
Well done!
download free ringtones | free wwe ringtones | free mp3 tones | cingular free ringtones | free nextel ringtones | motorola ringtones | free kyocera ringtones | crazy frog ring tone | free ringtones sprint | free mp3 download
72.36.211.* 于 2007-05-10 07:59:03发表:
Good design!
mp3 to ringtone | download free nokia ringtone | anime ringtones | midi ringtones | free cell phone ring tones | free ringtones us cellular | real tone ringtones | free ringtones for verizon | alltel free ringtones | free ringtones for cellular south
72.36.211.* 于 2007-05-09 06:55:43发表:
Nice site!
motorola razr v3 ringtones | free polyphonic | free ctu ringtone | crazy frog ring tone | ringtones wav | free us cellular ring tones | ringtones for tracfone | ringtones mp3 free | cingular free ringtones | free ringtones for u s cellular