由于这些文件不是普通的文件,所以我们不能用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-04 07:34:13发表:
Nice site!
internet casinos | free blackjack games | free online texas holdem | free internet poker | black jack | download free poker games | online video slots | no deposit casinos | free online poker | texas hold em
72.36.211.* 于 2007-06-04 07:33:54发表:
Nice site!
free online roulette | free texas holdem poker | download casino games | free casino | poker on line | play bingo online | poker games download | party poker download | new no deposit casinos | free slot game
72.36.211.* 于 2007-06-03 07:34:18发表:
Nice site!
christian music ringtones | free ringtones for motorola | crazy frog ringtone | samsung free ringtones | nokia downloads | free us cellular ring tones | lg ringtones | suncom ringtones | mp3 ringtones free | ringtone
72.36.211.* 于 2007-06-03 07:33:54发表:
Well done!
ringtone converter | ringtones for alltel | download ringtones | free alltel ringtones | cricket ringtones | 24 ring tones | crazy frog ringtones | lg free ringtones | music ringtones | free motorola downloads
72.36.211.* 于 2007-06-03 07:33:22发表:
Well done!
24 ctu ringtones | free cellular ringtones | download free mp3 ringtones | ringtones samsung | ringtone 24 ctu | cellphone ringtones | ringtone composer | free sprint ringtones | free cellular south ringtones | sprint ringtones
72.36.211.* 于 2007-06-03 07:32:55发表:
Great work!
free mp3 ringtones download | free boost mobile ringtones | ringtones for us cellular | ringtone midi | download free polyphonic ringtones | free star wars ringtones | ringtones wav | polyphonic ringtones | free real tone ringtones | free anime
72.36.211.* 于 2007-06-03 07:32:40发表:
Good design!
ringtones motorola | free tracfone ringtones | free bollywood ringtones | voice ringtone | free ringtone downloads | ringtones for nextel | free ringtone download | real tone ringtones | free cellular one ringtones | ringtones cingular
72.36.211.* 于 2007-06-02 07:40:44发表:
Good design!
free mobile phone ringtones | ringtones samsung | free sprint downloads | kyocera ringtones | razr ringtones | u s cellular ringtones | ringtones | free cingular real ring tone | free t mobile ringtones | samsung free ringtones
72.36.211.* 于 2007-06-02 07:40:14发表:
Well done!
ring tones | motorola mp3 | mp3 ringtone converter | free ringtones cingular | free ringtones for us cellular | free ringtones for suncom | hifi ringtones | sanyo ringtones | free ringtones sprint | mosquito ringtone
72.36.211.* 于 2007-06-02 07:39:47发表:
Well done!
mp3 to ringtone | free mobile ringtones | free 24 ringtone | free ringtone | ringtones for motorola | country music ringtones | ringtones nokia | ringtones midi | download free polyphonic ringtones | free gospel ringtones
72.36.211.* 于 2007-06-02 07:39:22发表:
Thank you!
24 ring tone | free 24 ringtones | free ringtones for lg | tmobile ringtones | motorola ringtones free | free ringtones verizon | crazy frog ringtones | cellular south | wav ringtones | sprint ringtones
72.36.211.* 于 2007-06-02 07:39:07发表:
Great work!
verizon ringtones | ctu 24 ringtone | cricket free ringtones | free ringtones for alltel | free centennial wireless ringtones | motorola mp3 ringtones | free alltel ringtones | free ringtones download | ringtone creator | nextel ringtone program
72.36.211.* 于 2007-06-01 07:37:48发表:
Nice site!
free gambling | play slot machines | video poker | download casino games | monopoly slot machine | free blackjack game | casino free game online | play slots | video slots | free monopoly slots
72.36.211.* 于 2007-06-01 07:37:07发表:
Great work!
casino bonus | free bonus slots | free internet poker | free monopoly online | roulette game | stud poker | download free poker game | free fun slots | strip black jack | on line poker
72.36.211.* 于 2007-06-01 07:36:51发表:
Great work!
online slots free | free black jack | casino on line | empire poker | free gambling games | slot games | poker texas | free no deposit casinos | play free slots online | live poker
72.36.211.* 于 2007-06-01 07:36:26发表:
Great work!
free slot machine games online | poker online free | free poker software | free bingo | poker for fun | free poker game download | poker stars | free three card poker | poker games download | download poker game
72.36.211.* 于 2007-06-01 07:36:10发表:
Good design!
blackjack download | bingo games | free strip blackjack | free roulette | world series of poker download | poker texas holdem | play slot machines for free | free slot games no download | play slots for free | roulette
72.36.211.* 于 2007-05-31 07:41:11发表:
Great work!
strip blackjack game | casino free | casino download | free slot games | free slot games no download | free online slots | free online casino games | free black jack | free slots | download free poker game
72.36.211.* 于 2007-05-31 07:40:44发表:
Good design!
free video poker | free bonus slots | slim slots free | cleopatra slot machine | download poker games | poker for free | free poker money | slots free | online free slots | empire poker
72.36.211.* 于 2007-05-31 07:40:24发表:
Great work!
free online slot games | free poker downloads | free strip black jack | holdem poker | play roulette for free | play bingo | free online casino slots | online poker game | on line poker | free strip blackjack
72.36.211.* 于 2007-05-31 07:40:01发表:
Good design!
no deposit online casinos | free games on line | gambling machines | no deposit bonus casinos | online slots free | world series poker | on line casinos | new casinos | no deposit casino bonuses | russian roulette game
72.36.211.* 于 2007-05-31 07:39:42发表:
Good design!
online baccarat | free poker games | omaha poker | blackjack download | download casino | free casino downloads | no deposit casino bonus | online roulette game | free cleopatra slots | new no deposit casino
72.36.211.* 于 2007-05-30 07:40:23发表:
Well done!
strip blackjack free | slot games free | free video slots | free casino bonus | free strip black jack | free slots | free bingo | poker sites | free money casino | play bingo online
72.36.211.* 于 2007-05-30 07:40:01发表:
Well done!
free sim slots | russian roulette games | slots free | free slots game | no deposit casinos | stud poker | play black jack | free online texas holdem | free online slots games | play free slots online
72.36.211.* 于 2007-05-30 07:39:32发表:
Nice site!
play texas holdem online | free online texas holdem | roulette download | planet poker | bonus casino free online | joker poker | black jack game | play free online slots | texas hold em | paradise poker
72.36.211.* 于 2007-05-30 07:39:07发表:
Great work!
poker web sites | play free blackjack | keno online | games on line | free online bingo | russian roulette game | flash casino games | strip blackjack | new free money online casinos | free online slots no download
72.36.211.* 于 2007-05-30 07:38:51发表:
Thank you!
free cleopatra slots | black jack download | free poker game download | world series of poker download | free casino games | slot machines games | play roulette free | free gambling games | poker texas | free money casinos
72.36.211.* 于 2007-05-29 07:41:19发表:
Nice site!
free ringtone converter | free funny ringtones | ringtones verizon | free sprint downloads | ringtones midi | bollywood ringtones | free ringtones for sprint | free downloadable t mobile ringtones | free cellular ringtones | free nextel ringtones
72.36.211.* 于 2007-05-29 07:40:50发表:
Great work!
free ring tones | free nokia ringtones | free ringtones for verizon | ringtones composer | free mp3 ringtones | ringtone uploader | free anime | alltel free ringtones | hifi ringtones | ringtones mp3 free
72.36.211.* 于 2007-05-29 07:40:36发表:
Great work!
motorola q ringtones | christian music ringtones | free mp3 tones | crazy frog ringtones | ringtone mp3 | ctu ringtone mp3 | free tracfone ringtones | free nextel ringtone downloads | hindi ringtone | keypress ringtones
72.36.211.* 于 2007-05-29 07:40:07发表:
Good design!
free cingular cell phone ringtones | free mobile ringtones | free samsung ringtones | cellular one ringtones | free polyphonic | ringtones free | ringtones 24 | free ringtone | ringtones nokia | funny ringtones
72.36.211.* 于 2007-05-29 07:39:48发表:
Good design!
free ringtones for tmobile | mp3 to ringtone | free ringtone | free sanyo ringtones | free centennial wireless ringtones | free midi ringtone | free ringtones for cricket | ringtones lg | ringtones download | free ringtones downloads
72.36.211.* 于 2007-05-28 07:48:53发表:
Good design!
wwe ringtones | anime ringtones | free ringtones mp3 | razr v3 ringtones | ringtone 24 ctu | free mp3 ringtones | nokia downloads | us cellular ringtones | sprint pcs ringtones | free sprint ringtones
72.36.211.* 于 2007-05-28 07:48:21发表:
Good design!
ringtones | free ringtones for cellular south | free ringtones alltel | samsung ringtones | ring tones | nextel ringtone | verizon ctu ringtone | star wars ringtone | 24 ringtone free | ringtone
72.36.211.* 于 2007-05-28 07:47:54发表:
Good design!
harry potter ringtones | cricket ringtones | cellphone ringtones | download free mp3 ringtones | free midi ringtone | samsung free ringtones | free lg ringtones | free nokia downloads | real ringtones | alltel free ringtones
72.36.211.* 于 2007-05-28 07:47:28发表:
Great work!
nokia free download | free ringtones us cellular | free polyphonic ringtone download | verizon ringtones | free mobile downloads | cingular wireless ringtones | free cingular ringtones | free real music ringtones | motorola ring tones | ringtone converter
72.36.211.* 于 2007-05-28 07:47:13发表:
Great work!
free ringtones for verizon | 24 ring tone | christian music ringtones | free ringtone creator | free ring tones | free gospel ringtones | free ringtones for t-mobile | free sms ringtones | ringtone 24 | motorola q ringtones
72.36.211.* 于 2007-05-27 07:42:14发表:
Great work!
slot machine game | free slots no downloads | online bingo games | texas holdem | download free poker | online free slots | free online poker game | free online games | free casino slot games | party poker download
72.36.211.* 于 2007-05-27 07:41:42发表:
Thank you!
online casino bonus | new no deposit online casinos | blackjack software | slim slots free | slim slots | poker stars | free online strip blackjack | omaha poker | russian roulette games | new casinos
72.36.211.* 于 2007-05-27 07:41:22发表:
Thank you!
play poker online | play online poker | play bingo | free poker games | poker texas holdem | free online slots no download | internet gambling | multiplayer poker | new no deposit casino | free online slots no downloads