由于这些文件不是普通的文件,所以我们不能用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.232.63.* 于 2007-03-31 23:09:57发表:
Nice site!
free monopoly slots | best online casinos | gambling sites | texas hold em free | free texas holdem poker | online roulette game | free poker money | free slot machine game | poker game online | free games online
72.232.63.* 于 2007-03-31 23:08:14发表:
Well done!
free slim slots | no deposit casino | slot machines games | free online casino | virtual poker | internet casino | free casino games | play texas hold em | play blackjack online | poker games online
72.232.63.* 于 2007-03-31 23:07:41发表:
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-30 22:21:50发表:
Great work!
free poker on line | play blackjack | free casino | internet casino games | roulette download | texas hold em poker | free online texas hold em | free online poker | play texas holdem free | texas hold em free
72.232.250.* 于 2007-03-30 22:20:34发表:
Thank you!
no download free slots | free gambling games | free online slots no downloads | roulette free games | free online slot games | free cleopatra slots | games on line | play roulette for free | download blackjack | free online texas holdem
72.232.250.* 于 2007-03-30 22:20:25发表:
Good design!
online roulette free | download free poker | free online casino game | caribbean stud poker | poker for fun | blackjack strip | online games | play slot machines | free online casino | free no deposit casinos
72.232.250.* 于 2007-03-30 22:20:26发表:
Great work!
free online slots games | blackjack for free | bingo online | free slim slots | free casino money | hold em poker | poker sites | texas holdem | on line casinos | best online casino games
72.232.63.* 于 2007-03-30 22:20:21发表:
Great work!
free online slots games | blackjack for free | bingo online | free slim slots | free casino money | hold em poker | poker sites | texas holdem | on line casinos | best online casino games
65.110.43.* 于 2007-03-30 22:20:19发表:
Great work!
free casino online | casinos on line | online gambling sites | free online casino games | black jack strip | play online poker | free internet poker | poker downloads | casino games | poker game
72.232.63.* 于 2007-03-30 22:20:17发表:
Good design!
online roulette free | download free poker | free online casino game | caribbean stud poker | poker for fun | blackjack strip | online games | play slot machines | free online casino | free no deposit casinos
72.232.63.* 于 2007-03-30 22:20:17发表:
Great work!
free casino online | casinos on line | online gambling sites | free online casino games | black jack strip | play online poker | free internet poker | poker downloads | casino games | poker game
65.110.43.* 于 2007-03-30 22:20:15发表:
Good design!
online roulette free | download free poker | free online casino game | caribbean stud poker | poker for fun | blackjack strip | online games | play slot machines | free online casino | free no deposit casinos
65.110.43.* 于 2007-03-29 17:41:04发表:
Well done!
free casino online | monopoly online | roulette online | free slots downloads | casino games free | free slots | slot machines | free casino | free online texas holdem | caribbean poker
72.232.63.* 于 2007-03-29 17:40:37发表:
Great work!
video slot games | paradise poker | monopoly slot machine | free online black jack | online baccarat | free black jack games | online poker sites | cleopatra slots | free online slot machine | free poker sites
72.232.250.* 于 2007-03-29 17:40:00发表:
Great work!
world series of poker | casino no deposit | new online casinos | free gambling | slot machine free | free games on line | bingo online | texas hold em free | no deposit bonus casinos | strip blackjack free
72.232.250.* 于 2007-03-29 17:39:37发表:
Well done!
online poker game | poker game online | online roulette free | blackjack free games | blackjack software | video poker machines | free internet poker | stud poker | slots online | free money no deposit casinos
65.110.43.* 于 2007-03-29 17:39:20发表:
Good design!
bonus poker | blackjack game | free strip blackjack | casino free online | free online casino | free sim slots | play slots for fun | black jack game | texas hold em | free online slots games
65.110.43.* 于 2007-03-28 18:03:38发表:
Nice site!
internet sports betting | betting on college basketball | nascar bet bet | nhl lines | odds on the ncaa basketball | betting on line | sports book betting | ncaa tournament march madness | super bowl betting | sports bets
72.232.250.* 于 2007-03-28 18:03:08发表:
Thank you!
march madness live | online bookies | gambling odds | gm march madness | college football betting | anna nicole smith baby bet | ncaa brackets march madness | six nations 2007 bet bet | internet bet | swiss super league bet
72.232.63.* 于 2007-03-28 18:02:31发表:
Great work!
odds sports | nfl futures | odds on ncaa basketball tournament | baseball gambling | nfl betting odds | bet gambling | march madness nit | march madness finals | ncaa football lines | msc march madness
72.232.63.* 于 2007-03-28 18:02:08发表:
Thank you!
march madness 07 | sports wagering | march madness results | march madness basketball | las vegas bet | ncaa march madness schedule | hockey betting | uefa champions league bet | virtual betting | online sports gambling
65.110.43.* 于 2007-03-28 18:01:49发表:
Good design!
ncaa betting lines | 2007 nextel cup championship odds | nba picks | football betting lines | nfl gambling | how to bet on football | baseball betting lines | bet odds | sports book odds | sport odds
65.110.43.* 于 2007-03-27 20:15:02发表:
Good design!
free phone downloads | download free ringtone | cellular south | ringtones for tracfone | free cricket ringtones | free ring tones | motorola ringtone | free tmobile ringtones | free mobile phone ringtones | composer ringtones
65.110.43.* 于 2007-03-27 20:13:30发表:
Thank you!
free polyphonic ringtones | free lg ringtones | free 24 ringtones | centennial wireless ringtones | free wav ringtones | ringtones 24 | free ringtones for cingular | samsung ringtones | sprint ringtones | free real ringtones for cingular
72.232.63.* 于 2007-03-27 20:12:48发表:
Thank you!
star wars ringtones | blackberry ringtones | midi ringtone | simpsons ringtone | boost mobile ringtones | ringtone jukebox | free us cellular ring tones | free nextel ringtones | motorola mp3 ringtones | mobile ringtones
65.110.43.* 于 2007-03-27 02:27:20发表:
Great work!
play slots | monopoly slots | pokerstars download | free casino downloads | video slots | sims free slots | poker game download | free blackjack online | slot games | no download free slots
65.110.43.* 于 2007-03-27 02:26:38发表:
Nice site!
slot machines | play slots for fun | games download | holdem poker | free online poker | gambling sites | poker texas holdem | best usa casino online | free online blackjack | texas hold em online
65.110.43.* 于 2007-03-27 02:25:41发表:
Thank you!
online free blackjack | paradise poker | aussie slots | free online texas hold em | free slot machines games | free slot downloads | internet casino | online poker usa | free no download slots | new no deposit online casinos
65.110.43.* 于 2007-03-27 02:25:22发表:
Good design!
video poker free | keno online | live poker | no deposit bingo | free slot game | roulette free | online roulette | best poker sites | games online | free 3 card poker
65.110.43.* 于 2007-03-27 02:25:04发表:
Thank you!
casino slot games | multiplayer poker | omaha poker | casino online download | play roulette | texas holdem | online poker sites | play slot machines | online bingo games | poker on line
65.110.43.* 于 2007-03-26 05:36:50发表:
Good design!
games on line | online roulette game | gambling games | free online casino slots | party poker download | free slot machines | free online texas holdem | free money no deposit casinos | free slot downloads | blackjack strip
65.110.43.* 于 2007-03-26 05:36:10发表:
Nice site!
poker online free | free internet poker | slot machines | free roulette | free slots | black jack game | keno online | poker stars download | free casino downloads | play poker online
65.110.43.* 于 2007-03-26 05:34:48发表:
Good design!
download party poker | empire poker | bonus casino free online | play free slots | noble poker | best online casinos | strip blackjack game | new no deposit online casinos | slot game | slot machine
72.232.63.* 于 2007-03-26 05:33:24发表:
Nice site!
monopoly online | poker on line | free slots no download | internet casinos | free poker game | online free roulette | black jack free | texas holdem | free poker online | black jack download
65.110.43.* 于 2007-03-26 05:32:37发表:
Good design!
free online casinos | free casino | no deposit casino | pai gow poker | poker bonus | online blackjack | play poker | strip black jack | free online texas hold em | online poker sites
65.110.43.* 于 2007-03-24 20:29:42发表:
Good design!
midi ringtones | cell phone ringtones | cisco ringtones | free polyphonic ringtone | ringtone midi | free mp3 ringtone | crazy frog ringtones | free ringtones for tmobile | ringtones verizon | ringtone jukebox
65.110.43.* 于 2007-03-24 20:29:09发表:
Great work!
ctu 24 ringtone | ringtones for motorola q | free ringtones for sprint | free wwe ringtones | free alltel ringtones | mobile phone ringtones | free wav ringtones | crazy frog ring tone | free country music ringtones | 24 ringtone free
72.232.63.* 于 2007-03-24 20:28:43发表:
Well done!
funny ringtone | free nokia ringtone | free nokia downloads | free nascar ringtones | motorola ring tones | verizon free ringtones | free harry potter ringtones | free cellular south ringtones | free ringtones for samsung | free ringtones
65.110.43.* 于 2007-03-24 20:28:22发表:
Well done!
free ringtones lg | 24 ring tone | motorola ringtones | free christian ringtones | free ringtones for cricket | 24 ctu ringtones | u s cellular ringtones | star wars ringtones | free ringtones for tracfone | sprint phones
65.110.43.* 于 2007-03-24 20:28:08发表:
Good design!
download ringtones | nextel downloads | nextel ringtones | centennial wireless | simpsons ringtone | ringtones 24 | free mobile downloads | keypress ringtones | sanyo ringtones | mp3 to ringtone converter