1. Linux下的音频设备文件
/dev/console:与扬声器相关的设备文件。
/dev/dsp:与声卡设备上的DSP相关的设备文件,提供了数字采样和数字录音的功能。声卡设备通过DSP实现模拟信号和数字信号的转换。向该设备写入数据将激活声卡上的数模转换器播放声音。而从该设备上读取数据,则会激活声卡上的模数转换进行录音操作。
/dev/audio:与/dev/dsp类似。使用的编码方式为mu-law。
/dev/mixer:声卡中混音器的软件接口,用于将多个声音信号组合或进行叠加。对混音器的编程包括如何设置增益,以及如何在不同的音源之间进行切换。
/dev/sequencer:用于提供对声卡中的波表合成器的支持,主要用于计算机音乐软件上。
2. 实例
实例1:让扬声器发生
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <sys/ioctl.h>
- #include <linux/kd.h>
- #define SPEAKER_DEVICE "/dev/console"
- int main(int argc, char *argv[])
- {
- int fd;
- int freq;
- if(argc !=2)
- {
- printf("Usage: %s frequence /n", argv[0]);
- return 1;
- }
- freq = atoi(argv[1]);
- if(freq <=0 || freq > 10000)
- {
- printf("the frequence must be in the range from 0 to 10000./n");
- return 1;
- }
- fd = open(SPEAKER_DEVICE, O_WRONLY);
- if(fd == -1)
- {
- perror("connot open device./n");
- return 1;
- }
- int i;
- int cnt;
- for(i = 0; i<1000; ++i)
- {
- int set_freq = 1190000/freq;
- ioctl(fd, KIOCSOUND, set_freq);
- usleep(200);
- ioctl(fd, KIOCSOUND, 0);
- usleep(100);
- }
- return 0;
- }