由于这些文件不是普通的文件,所以我们不能用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-27 07:40:55发表:
Thank you!
gambling online | play slot machines for free | free online texas holdem | video poker free | cleopatra slot machine | new no deposit casinos | play free blackjack | black jack | monopoly slot machine | us poker sites
72.36.211.* 于 2007-05-27 07:40:37发表:
Nice site!
download slots | free online casino game | casino bonus | blackjack download | monopoly slots | play black jack | internet casino games | free money no deposit casinos | free blackjack online | virtual poker
72.36.211.* 于 2007-05-26 07:41:34发表:
Well done!
free poker downloads | free slots on line | stud poker | poker game download | blackjack strip | texas hold em online | free video | free texas holdem poker | virtual poker | poker online free
72.36.211.* 于 2007-05-26 07:41:00发表:
Nice site!
no download free slots | free slot games online | free poker games | online slots free | gambling machines | poker free download | download free poker games | black jack game | free strip black jack | texas holdem free
72.36.211.* 于 2007-05-26 07:40:30发表:
Great work!
free no download slots | slots of fun | free black jack games | free online poker games | free slot machines games | free online slots games | free multi line slots | slot machine | no deposit casino bonuses | download free poker game
72.36.211.* 于 2007-05-26 07:40:03发表:
Thank you!
free casino downloads | poker game | strip blackjack free | free black jack game | online roulette | sims slots | casino slot games | no deposit bingo | computer poker | play blackjack
72.36.211.* 于 2007-05-26 07:39:45发表:
Thank you!
strip blackjack game | bingo games | no deposit bonus casinos | free casino online | texas holdem poker | free slots | free online slots no download | flash casino games | new casinos | download poker
72.36.211.* 于 2007-05-25 07:44:31发表:
Nice site!
cricket ringtones | nokia download | 24 ring tones | free ringtones for sprint phones | cingular wireless ringtones | free downloadable ringtones | free sms ringtones | ringtones mp3 | free cell phone ring tones | cellular south ringtones
72.36.211.* 于 2007-05-25 07:44:04发表:
Thank you!
ringtones composer | nextel ringtone | free ringtones mp3 | ringtone converter | 24 ringtone free | free ringtones for tracfones | ringtone midi | free real ringtones for cingular | ringtone free | midi ringtones
72.36.211.* 于 2007-05-25 07:43:38发表:
Well done!
ringtones for sprint phones | nextel ringtone program | free polyphonic ringtone download | free mobile downloads | sprint free ringtones | mosquito ring tone | free phone ringtones | free tracfone ringtones | free suncom ringtones | blackberry ringtones
72.36.211.* 于 2007-05-25 07:43:07发表:
Good design!
free mobile ringtones | us cellular free ringtones | mp3 ringtone download | free kyocera ringtones | free country music ringtones | star wars ringtones | alltel ringtones | free funny ringtones | christian music ringtones | free ringtones for verizon
72.36.211.* 于 2007-05-25 07:42:58发表:
Great work!
motorola ring tones | ringtone jukebox | nokia ringtone | free anime ringtones | arabic ringtones | wav ringtones | mp3 ringtone | free ringtones for boost mobile | free mp3 ringtone downloads | motorola ringtones free
72.36.211.* 于 2007-05-24 07:42:04发表:
Thank you!
texas holdem online | pokerstars download | free bingo games | free poker on line | free online slots games | free online poker game | free no deposit casinos | casino slot games | free slot machine game | online blackjack free
72.36.211.* 于 2007-05-24 07:41:36发表:
Good design!
slot machines free | best usa casino online | casino bonus | download free poker game | slots games | slot game | cleopatra slots | no deposit online casinos | world series of poker | black jack strip
72.36.211.* 于 2007-05-24 07:41:13发表:
Thank you!
online games | free slots games | blackjack online | play texas holdem free | online casinos | gambling sites | monopoly slots | free online bingo | party poker | new no deposit online casinos
72.36.211.* 于 2007-05-24 07:40:46发表:
Well done!
best online casinos | slim slots free | free casino games online | free black jack games | free blackjack online | roulette online | blackjack games | poker web sites | free poker downloads | free sims slots
72.36.211.* 于 2007-05-24 07:40:31发表:
Thank you!
cleopatra slot | free video | internet casino games | three card poker | pacific poker | slim slots | texas holdem | free casino games | free poker game | free slot game
72.36.211.* 于 2007-05-23 07:44:28发表:
Great work!
24 ring tones | midi ringtone | ringtone composer | voice ringtone | ringtones wav | verizon ringtones | real ringtones | free sanyo ringtones | ringtones for tracfone | free nextel downloads
72.36.211.* 于 2007-05-23 07:43:53发表:
Great work!
free samsung ringtones | free midi ringtones | free ringtone download | free bollywood ringtones | free mobile phone ringtones | ringtone websites | free ringtones for verizon | nascar ringtones | harry potter ringtones | download free ringtone
72.36.211.* 于 2007-05-23 07:43:31发表:
Well done!
samsung ringtones | download free mp3 ringtones | 24 ring tone | free cellular south ringtones | nokia downloads | free ringtones for t mobile | free ctu ringtone | free ringtones verizon | boost ringtones | lg ringtones
72.36.211.* 于 2007-05-23 07:43:08发表:
Good design!
ringtones download | alltel free ringtones | mp3 ringtones | star wars ringtones | free christian ringtones | free downloadable ringtones | mosquito ringtone | funny ringtones free | sprint ringtones | tmobile ringtones
72.36.211.* 于 2007-05-23 07:42:50发表:
Well done!
hindi ringtone | free sprint pcs ringtones | ringtones motorola | ringtone converter | ringtone uploader | free ringtones for motorola | cricket free ringtones | real tone ringtones | 24 ctu ringtone | ringtones mp3
72.36.211.* 于 2007-05-22 07:43:54发表:
Thank you!
free cellphone ringtones | free country ringtones | hindi ringtone | nokia ringtones | cellular one ringtones | free mp3 ringtone downloads | mp3 ringtone converter | sprint ringtones | free kyocera ringtones | star wars ringtones
72.36.211.* 于 2007-05-22 07:43:20发表:
Well done!
suncom ringtones | free boost mobile ringtones | free ringtones sprint | 24 ctu ringtone verizon | ringtones samsung | ringtone 24 | mosquito ring tone | free cellular one ringtones | free centennial wireless ringtones | free cell phone ringtones
72.36.211.* 于 2007-05-22 07:42:55发表:
Good design!
free real ringtones | free alltel ringtones | ringtones for alltel | free ringtones for lg | free ringtone downloads | nokia downloads | ringtone mp3 | ringtones midi | ringtones for tracfone | download ringtones
72.36.211.* 于 2007-05-22 07:42:28发表:
Well done!
free nextel downloads | download free ringtones | christian ringtones | ringtones download | composer ringtones | ringtones wav | ringtones lg | polyphonic ringtones | free cellular ringtones | ringtones mp3 free
72.36.211.* 于 2007-05-22 07:42:12发表:
Well done!
free ringtones download | blackberry ringtones | free polyphonic | verizon ringtones free | nokia download | motorola ringtones | ringtones for cingular | free ringtones samsung | free ringtones motorola | ringtone websites
72.36.211.* 于 2007-05-21 07:43:28发表:
Thank you!
cingular wireless ringtones | free mobile downloads | verizon free ringtones | free t mobile ringtones | real music ringtones | motorola keypress ringtones | mosquito ring tone | lg ringtones | mosquito ringtone | midi ringtone
72.36.211.* 于 2007-05-21 07:42:52发表:
Thank you!
free nokia downloads | centennial ringtones | free christian ringtones | free real music ringtones | free mp3 download | crazy frog ringtone | free ringtones cingular | free ringtones download | tracfone free ringtones | free ringtones sprint
72.36.211.* 于 2007-05-21 07:42:31发表:
Thank you!
tmobile free ringtones | mp3 ringtones | nextel ringtone | nextel downloads | ringtone composer | crazy frog ring tone | ringtone downloads | ringtone websites | free downloadable t mobile ringtones | real ringtones
72.36.211.* 于 2007-05-21 07:42:09发表:
Good design!
free ringtones lg | sony ericsson ringtones | free ringtones for samsung | ringtone uploader | free ringtones for sprint | free ringtones downloads | free cricket ringtones | free cingular cell phone ringtones | motorola q ringtones | free 24 ringtones
72.36.211.* 于 2007-05-21 07:41:50发表:
Well done!
ringtones mp3 free | country ringtones | ringtones wav | ringtones midi | mp3 to ringtone converter | cingular ringtones | free boost ringtones | free ringtones for verizon | free samsung ringtones | ringtones cingular
72.36.211.* 于 2007-05-20 07:45:39发表:
Well done!
strip blackjack online | free online black jack | free slots | slots for fun | pai gow poker | play texas hold em | free online poker game | games online | strip blackjack free | black jack games
72.36.211.* 于 2007-05-20 07:45:04发表:
Thank you!
free money no deposit casinos | free online bingo | online poker | free slot machines online | free slot game | free slots on line | full tilt poker | free texas hold em | free video | casino free games
72.36.211.* 于 2007-05-20 07:44:42发表:
Thank you!
poker star | free online casino slots | slim slots free | free online roulette | free three card poker | casino games online | free online bingo games | free 3 card poker | monopoly slots | games download
72.36.211.* 于 2007-05-20 07:44:12发表:
Great work!
free blackjack games | no deposit bonus casinos | free online slots games | online roulette game | strip blackjack games | free online slots no download | poker texas | play blackjack online | poker | free slot machine downloads
72.36.211.* 于 2007-05-20 07:43:58发表:
Well done!
new no deposit casinos | free poker money | free roulette games | keno online | casino games | free slot games online | free online slot machines | omaha poker | poker online | poker games online
72.36.211.* 于 2007-05-19 07:41:49发表:
Good design!
keypress ringtones | free ringtones for tmobile | nokia free downloads | sprint ringtones | free polyphonic ringtone | wwe ringtones | ringtone midi | ringtones composer | sms ringtones | free mp3 download
72.36.211.* 于 2007-05-19 07:41:21发表:
Thank you!
ringtones for motorola | ringtones samsung | free lg ringtones | free midi ringtones | nokia ringtones | free cingular ringtones | free ringtones for tracfone | free anime | ringtones for cingular | mp3 ringtones
72.36.211.* 于 2007-05-19 07:40:50发表:
Thank you!
christian ringtones | free boost mobile ringtones | ringtones for verizon | free hifi ringtones | kyocera ringtones | online ringtone converter | ctu 24 ringtone | free real tone ringtones | funny ringtone | free phone downloads