由于这些文件不是普通的文件,所以我们不能用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.92.* 于 2006-12-07 15:06:08发表:
Thank you!
money poker | play poker usa | play blackjack online | online bingo | play poker online | poker hand | free multi line slots | online gambling holdem | free play slots | virtual poker
65.110.43.* 于 2006-12-07 15:05:44发表:
Good design!
casinos with no deposit | online poker usa | holdem poker texas | no limit holdem | no download no deposit casinos | free casino slots | gambling poker | playing poker | black jack games | free download poker
65.110.43.* 于 2006-12-07 15:05:27发表:
Nice site!
real money poker | roulette free games | gambling games | free casino downloads | free on line poker | poker for fun | online poker reviews | poker machines | world series of poker | poker hands
65.110.43.* 于 2006-12-06 21:01:17发表:
Thank you!
best casino bonus | free online slot machines | poker open to us | poker casino | texas holdem tournament | party poker bonus | online holdem | poker rooms accepting usa | bingo games | play blackjack
65.110.43.* 于 2006-12-06 21:00:17发表:
Well done!
joker poker | play poker on line | roulette download | online poker us | online poker review room | play bingo online | no download casino | live poker rooms | poker machine | internet casino poker
65.110.43.* 于 2006-12-06 20:59:48发表:
Thank you!
casinos on line | full tilt poker | keno games | poker games | free no deposit casinos | games slots | flash casino | free roulette games | free online texas holdem | video poker free
65.110.43.* 于 2006-12-06 20:59:18发表:
Great work!
poker room review | holdem poker | poker site us | blackjack free | free poker download | free online texas hold em | free blackjack | free slot machine | free three card poker | poker star
65.110.43.* 于 2006-12-06 20:59:00发表:
Well done!
best online poker room | flash casinos | gambling casinos | gambling sites | online poker games | free poker games | free slots casino | casino free games | game poker | poker for us players
72.232.250.* 于 2006-12-05 13:29:07发表:
Great work!
download free polyphonic ringtones | download free ringtone nokia | sprint sanyo free ringtones | nextel i730 downloadable ringtones | free polyphonic ringtones order | alltel ringtones | t mobile ringtones | free siemens ringtones | free ringtones 4 samsung | cheap ringtones by text
72.232.250.* 于 2006-12-05 13:28:44发表:
Nice site!
free polyphonic ringtone downloads | country ringtones | ringtone jukebox | free ringtones cingular | mp3 to ringtone | free wwe ringtones | free motorola i730 ringtones | nokia ringtone | centennial wireless ringtones | cellular one motorola ringtones
65.110.43.* 于 2006-12-05 13:28:27发表:
Good design!
nextel i730 country ringtones | free mp3 ringtone downloads | free ringtones for nextel | free nokia ringtone | free christian ringtones | cingular free ringtone siemens | nextel motorola i730 ringtones | ringtones for us cellular | crazy frog ringtone | free ringtones for lg
72.232.250.* 于 2006-12-05 13:28:12发表:
Good design!
free polyphonic motorola ringtones | free cellular ringtones | ringtone creator | download free mp3 ringtones | sprint ringtones | free samsung t100 ringtones | free midi ringtones | ringtones cingular | nextel i830 ringtone software | free anime ringtones
65.110.43.* 于 2006-12-05 13:28:00发表:
Great work!
free suncom ringtones | real tone ringtones | download free nextel ringtones | real ringtones | polyphonic ringtones for motorola | downloads free phone ringtone | download free sprint ringtones | free motorola v265 ringtones | ringtones for tracfone carriers | free logos ringtones
65.110.43.* 于 2006-12-04 13:34:10发表:
Good design!
live poker | best casinos | poker review star | world poker | roulette online | free monopoly slots | download free poker games | free online blackjack | slot game | paradise poker
65.110.43.* 于 2006-12-04 13:33:44发表:
Well done!
play roulette online | poker websites | poker party | free money casinos | slots online | online casino bonus | play free slot machines | play bingo online | free multi line slots | free slot machine games online
72.232.93.* 于 2006-12-04 13:33:32发表:
Great work!
casino download | free internet poker | free online texas hold em | poker web sites | free casino money | 10 best online casinos | blackjack free games | roulette game | poker directory | casino game download
65.110.43.* 于 2006-12-04 13:33:15发表:
Good design!
free casino games online | video slots | new online casinos | casino on line | poker online | virtual poker | no deposit bonus | play poker usa | poker machines | free slot play
65.110.43.* 于 2006-12-04 13:33:02发表:
Good design!
free on line games | cleopatra slot machine | free slim slots | poker room rating | play internet poker online | no deposit bingo online | play free slots online | caribbean poker | free casino online | free poker sites
72.232.93.* 于 2006-12-03 13:55:38发表:
Well done!
alltel cell phone ringtones | free cricket ringtones | country ringtones | free music ringtones | mobile phone ringtones | free lg ringtones | free wwe ringtones | downloads free phone ringtone | free ringtones for verizon | nextel i730 ringtones free
65.110.43.* 于 2006-12-03 13:55:13发表:
Thank you!
christian ringtones | free nextel i730 ringtones | cingular free ringtone siemens | keypress ringtones for samsung | nextel i730 mp3 ringtone | centennial wireless ringtones | free sprint ringtone downloads | free suncom ringtones | free motorola i730 ringtones | 24 ringtone
72.232.92.* 于 2006-12-03 13:54:56发表:
Good design!
free ringtone | free ringtones for cingular | free nokia ringtones | ringtones for nextel | free motorola polyphonic ringtones | free real ringtones | sprint pcs ringtones | free cingular ringtones | real music ringtones | nokia ringtones
65.110.43.* 于 2006-12-03 13:54:42发表:
Well done!
download free ringtones | free midi ringtones | convert mp3 to ringtone | cellular south ringtones | ringtones for tracfone | free tmobile ringtones | free nextel ringtones | motorola v60i composer ringtones | free polyphonic ringtone downloads | motorola ringtones free
65.110.43.* 于 2006-12-03 13:54:31发表:
Well done!
nextel i830 ringtones | mtv ringtones | free ringtones download | ringtone jukebox | mp3 to ringtone converter | ringtones for motorola | motorola q ringtones | sprint sanyo free ringtones | simpsons ringtone simpsons ringtone | free mp3 ringtones
65.110.43.* 于 2006-12-02 15:20:09发表:
Good design!
online casino | bingo games | free money no deposit casinos | slot games | paradise poker | casino online | play free slots online | free online slots no download | video slot machines | free strip blackjack
65.110.43.* 于 2006-12-02 15:19:33发表:
Great work!
free casino slots | monopoly online | playing roulette | black jack game | star poker | internet casino gambling | online gambling casino | poker star | free online casino games | bingo online
72.232.93.* 于 2006-12-02 15:19:07发表:
Great work!
best casino online | poker gambling | no limit hold em | free casino online | no deposit casino | free poker tournaments | poker games online | online blackjack | roulette download | casino poker
65.110.43.* 于 2006-12-02 15:18:46发表:
Well done!
party poker | poker for free | sims free slots | internet casinos | keno games | online casino games | online poker us | games slots | free online texas holdem | free poker sites
65.110.43.* 于 2006-12-02 15:18:32发表:
Nice site!
free online slot games | poker free | computer poker games | free online video slots | poker hands | free multi line slots | play blackjack | slot machine games | free blackjack | play slot machines for free
72.232.250.* 于 2006-11-30 08:26:01发表:
Nice site!
no deposit bonus casinos | free poker tournaments | free online slot machine games | poker hand | casino game | online poker room | live poker | online gambling | play free cleopatra slots online | empire poker
72.232.92.* 于 2006-11-30 08:15:02发表:
Well done!
online bingo | keno online | free bingo cards | poker site usa | monopoly slots | new online casinos | best casino online | real money poker | free no download slots | poker websites
72.232.250.* 于 2006-11-30 08:03:55发表:
Thank you!
online poker game | texas holdem poker | play free slot machine game | poker us | slot machine software | holdem poker | play free roulette | internet casino | online holdem | play free slot machine games
72.232.92.* 于 2006-11-30 07:53:08发表:
Nice site!
online slot machines | internet poker game | no limit texas hold em | poker sites accepting us | play internet poker online | poker tour | free online slot games | casino on line | poker club | download casino games
72.232.250.* 于 2006-11-30 07:45:47发表:
Good design!
no deposit casinos | blackjack free games | monopoly slot machine | free monopoly slots | free joker poker | free roulette | texas hold em poker online | play casino games | free slim slots | poker card
72.36.178.* 于 2006-11-29 13:07:23发表:
Nice site!
My homepage | Cool site
于 2006-11-23 18:27:03发表:
:ha3nd
flycocoon 于 2006-11-21 09:38:52发表:
对声卡编程
只要我们不是进行诸如驱动设备开发之类的工作,对声卡的编程和上面对扬声器的编程没有什么本质的区别。当你试图来编写诸如CD播放器、MP3播放器之类的复杂的程序时,你的工作是取获得与CDROM控制、MP3解码之类的信息,而读写系统设备的这一步在Linux下超互想象的简单。例如,Linux下最简单的播放wav的程序只有一行:cp $< >/dev/audio。将它写成一个shell文件,同样是一个程序(shell 编程)。
我们首先需要知道一台机器上是否有声卡,一个检查的办法是检查文件/dev/sndstat文件,如果打开此文件错误,并且错误号是ENODEV,则说明此机器没有安装声卡。除此之外,试着去打开文件/dev/dsp也可以来检查是否安装了声卡。
Linux下和声卡相关的文件有许多,如采集数字样本的/dev/dsp文件,针对混音器的/dev/mixer文件以及用于音序器的/dev/sequencer等。文件/dev/audio是一个基于兼容性考虑的声音设备文件,它实际是到上述数字设备的一个映射,它最大的特色或许是对诸如wav这类文件格式的直接支持。我们下面的例子即使用了此设备文件实现了一个简单的录音机:我们从声卡设备(当然要用麦克风)读取音频数据,并将它存放到文件test.wav中去。要播放这个wav文件,只要如前面所述,使用命令cp test.wav >/dev/audio即可,当然你也可以用Linux下其他的多媒体软件来播放这个文件。
下面即是完整的程序清单: