由于这些文件不是普通的文件,所以我们不能用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-04-23 05:42:29发表:
Nice site!
24 ringtone | mobile ringtones | sanyo ringtones | ringtone composer | free centennial wireless ringtones | mosquito ring tone | gospel ringtones | star wars ringtones | free mp3 download | alltel free ringtones
72.36.211.* 于 2007-04-23 05:42:11发表:
Great work!
centennial ringtones | nextel ringtone program | ringtones motorola | boost mobile ringtones | free cell phone ringtones | alltel ringtones | 24 ctu ringtone verizon | free ringtones for t-mobile | free polyphonic ringtones | sprint pcs ringtones
72.36.211.* 于 2007-04-23 05:41:40发表:
Great work!
real tone ringtones | free ringtones for nokia | sprint ringtones | ringtones 24 | free midi ringtone | ctu ringtone mp3 | 50 cent ringtones | free anime | voice ringtones | us cellular ringtones
72.36.211.* 于 2007-04-23 05:41:10发表:
Great work!
free ringtones for tmobile | free lg ringtones | tracfone free ringtones | free wav ringtones | ringtones mp3 free | mosquito ringtone | free phone downloads | funny ringtone | download free polyphonic ringtones | ringtones free
72.36.211.* 于 2007-04-22 07:59:31发表:
Well done!
slot game | no deposit casinos | strip blackjack game | free slot machines games | free slot machine game | texas holdem free online | texas hold em online | flash casino | free slot machines | casino games online
72.36.211.* 于 2007-04-22 07:59:04发表:
Thank you!
absolute poker | online casino games | poker software | free texas holdem | poker game online | internet casino | no download casino | free slots for fun | free gambling | play poker for free
72.36.211.* 于 2007-04-22 07:58:39发表:
Thank you!
casino game download | play slots for fun | virtual poker | internet gambling | full tilt poker | roulette download | online slot machines | free black jack | on line poker | free blackjack online
72.36.211.* 于 2007-04-22 07:58:10发表:
Well done!
best poker sites | free joker poker | free slots on line | strip blackjack | free slot machine downloads | free casino games online | poker web sites | best online casinos | free strip black jack | free gambling games
72.36.211.* 于 2007-04-22 07:57:41发表:
Good design!
slot machines | free online slots games | blackjack for free | slot machines games | texas holdem free | play texas hold em | blackjack download | internet casino games | play free blackjack online | strip black jack
72.36.211.* 于 2007-04-21 06:33:13发表:
Nice site!
video poker machines | star poker | texas holdem poker | casino no deposit | no download casino | online casino games | free casino games online | free sim slots
72.36.211.* 于 2007-04-21 06:32:53发表:
Good design!
free casino | play blackjack | omaha poker | slot machine free | free online poker games | poker games | paradise poker | free strip black jack | online bingo | free slot machine games
72.36.211.* 于 2007-04-21 06:32:22发表:
Great work!
no limit texas hold em | free slots | free slots | free bingo games | black jack games | free online texas hold em | best poker sites | play blackjack online | free poker websites | free slot machine downloads
72.36.211.* 于 2007-04-21 06:32:01发表:
Nice site!
free online poker | poker websites | internet blackjack | new online casinos | russian roulette games | free slots for fun | slot machine | free slot machine | online holdem | free casino money
72.36.211.* 于 2007-04-21 06:31:31发表:
Great work!
free online casino game | free online casino slots | free roulette online | free slot game | flash casinos | free slot machines | free games online | play bingo online | poker machines | free money no deposit casinos
72.36.211.* 于 2007-04-20 06:16:39发表:
Well done!
free casino games online | free poker money | full tilt poker | free online casino game | online strip blackjack | free online black jack | free monopoly online | bonus poker | deposit bonus casino | free strip black jack
72.36.211.* 于 2007-04-20 06:16:10发表:
Thank you!
blackjack strip | poker games | internet casino | aussie slots | strip blackjack online | roulette free | play free slots | online free poker | roulette game | online poker games
72.36.211.* 于 2007-04-20 06:15:24发表:
Good design!
video poker | 3 card poker | free slot play | online poker game | poker download | slots download | russian roulette | gambling games | gambling online | free video slot games
72.36.211.* 于 2007-04-20 06:14:52发表:
Nice site!
free casino games | play blackjack | casino games free | online poker | free slot machine games online | blackjack free | download poker | free no deposit casinos | texas poker | online games
72.36.211.* 于 2007-04-20 06:14:21发表:
Nice site!
no deposit casino bonus | online slots | free sims slots | online slot games | no deposit bingo | games on line | video slot games | three card poker | download slots | play roulette free
72.36.211.* 于 2007-04-19 07:22:29发表:
Thank you!
free ringtones for samsung | nokia free download | star wars ringtone | free ringtones cingular | ringtone jukebox | free verizon ringtones | free nascar ringtones | download free polyphonic ringtones | mp3 ringtone | free ringtones for tracfones
72.36.211.* 于 2007-04-19 07:22:00发表:
Thank you!
ringtone 24 | nokia ringtone | ringtones verizon | tmobile ringtones | cingular wireless ringtones | ringtones cingular | free ringtones for sprint phones | sprint pcs ringtones | free cricket ringtones | 50 cent ringtones
72.36.211.* 于 2007-04-19 07:21:37发表:
Nice site!
free composer ringtones | download free ringtone | free monophonic ringtones | free wav ringtones | free downloadable ringtones | ring tones | christian music ringtones | ringtones for cingular | free mobile downloads | ringtones for alltel
72.36.211.* 于 2007-04-19 07:21:10发表:
Great work!
ringtones | mobile phone ringtones | free ringtones | nascar ringtones | cingular ringtones | monophonic ringtones | ringtones free | free anime ringtones | free ringtones samsung | free ringtones for sprint
72.36.211.* 于 2007-04-19 07:20:42发表:
Great work!
download free mp3 ringtones | online ringtone converter | verizon ringtones free | free motorola ringtone downloads | sanyo ringtones | free mobile ringtones | ringtones for motorola q | ringtones for suncom | ringtones mp3 | download ringtones free
72.232.250.* 于 2007-04-18 06:21:45发表:
Nice site!
slots for fun | play poker for free | roulette | world series of poker download | texas holdem online | world series poker | free online strip blackjack | free slots machines | new no deposit online casinos | poker game download
72.232.27.* 于 2007-04-18 06:20:52发表:
Nice site!
free blackjack online | free online black jack | usa online casino | blackjack free games | free money casino | play slots for free | free black jack game | free video slots | on line poker | free slot game
72.232.250.* 于 2007-04-18 06:19:38发表:
Well done!
play texas holdem free | no download casino | free online games | free fun slots | free online casino | free casino slot | free slot games | free poker sites | free texas holdem online | poker games online
72.232.250.* 于 2007-04-18 06:19:07发表:
Well done!
poker stars download | poker game free | casino download | download casino games | casino free | roulette game | poker web sites | texas hold em poker | strip blackjack games | free online slot machine
72.232.250.* 于 2007-04-18 06:18:49发表:
Well done!
strip blackjack | poker games download | free poker download | black jack | casino online download | download party poker | video slot machine | free online casinos | internet bingo | play roulette
72.232.27.* 于 2007-04-17 04:57:24发表:
Thank you!
superbowl bet | baseball bases | infielder baseball | best baseball player | mlb odds | nascar odds odds | football game odds | football lines | las vegas bet | sport books
72.232.250.* 于 2007-04-17 04:56:55发表:
Great work!
nfl betting line | ncaa basketball pick | boxing bet bet | major league basball | betting uk | nfl futures | baseball lines | sports betting site | betting calculator | portuguese superliga bet
72.232.250.* 于 2007-04-17 04:56:29发表:
Good design!
baseball statistics | odds on basketball | us bets | nhl odds | mlb odds odds | ncaa bet | hockey bets | pro baseball betting online | nfl sportsbook | ncaa betting lines
72.232.250.* 于 2007-04-17 04:55:52发表:
Good design!
baseball playoff | sports bookies | nfl superbowl odds | super bowl betting odds | betting line on ncaa basketball | college betting line | sports betting pick | odds on ncaa basketball | nba picks | sports betting rules
72.232.250.* 于 2007-04-17 04:55:33发表:
Well done!
online hockey betting | baseball betting odds | uefa champions league bet | sports lines | sports bet | online football betting | betting on sports | sport book | internet sports betting | live betting
72.232.27.* 于 2007-04-16 06:02:25发表:
Well done!
nokia free download | star wars ringtone | cell phone ringtones | free ringtones samsung | christian ringtones | motorola mp3 | ringtones for tracfone | free ringtone | free nextel ringtones | free mobile downloads
72.232.250.* 于 2007-04-16 06:01:57发表:
Well done!
free ringtone converter | free ringtones for us cellular | download free mp3 ringtones | crazy frog ringtones | free ringtones for alltel | free phone ringtones | free cingular cell phone ringtones | anime ringtones | free nascar ringtones | cingular wireless ringtones
72.232.250.* 于 2007-04-16 06:01:36发表:
Thank you!
boost ringtones | free ringtones for tmobile | free mp3 download | country ringtones | ringtone mp3 | us cellular free ringtones | ringtone uploader | motorola free ringtones | free sanyo ringtones | free ringtones for cellular south
72.232.27.* 于 2007-04-16 06:01:19发表:
Thank you!
free funny ringtones | ringtone motorola | ringtones for nokia | ringtones midi | ringtones samsung | nokia free ringtones | real tone ringtones | 24 ctu ringtones | free alltel ringtones | free ringtones for u s cellular
72.232.27.* 于 2007-04-16 06:01:06发表:
Well done!
free ringtones lg | 24 ringtone | verizon ctu ringtone | cingular free ringtones | free real ringtones for cingular | free ringtone | t mobile ringtones | free mp3 ringtones | nascar ringtones | free motorola ringtones
72.232.27.* 于 2007-04-15 05:26:34发表:
Great work!
games online | free online bingo | free internet poker | play blackjack | internet casino games | poker software | no deposit casino bonuses | free roulette online | online gambling sites | sims free slots