linux下的音频设备文件编程

来源:互联网 发布:淘宝网天猫浇花的水壶 编辑:程序博客网 时间:2024/06/02 07:29

1. Linux下的音频设备文件

/dev/console:与扬声器相关的设备文件。

 

/dev/dsp:与声卡设备上的DSP相关的设备文件,提供了数字采样和数字录音的功能。声卡设备通过DSP实现模拟信号和数字信号的转换。向该设备写入数据将激活声卡上的数模转换器播放声音。而从该设备上读取数据,则会激活声卡上的模数转换进行录音操作。

 

/dev/audio:与/dev/dsp类似。使用的编码方式为mu-law。

 

/dev/mixer:声卡中混音器的软件接口,用于将多个声音信号组合或进行叠加。对混音器的编程包括如何设置增益,以及如何在不同的音源之间进行切换。

 

/dev/sequencer:用于提供对声卡中的波表合成器的支持,主要用于计算机音乐软件上。

 

2. 实例

实例1:让扬声器发生

view plain
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <unistd.h>  
  4. #include <sys/types.h>  
  5. #include <sys/stat.h>  
  6. #include <fcntl.h>  
  7. #include <sys/ioctl.h>  
  8. #include <linux/kd.h>  
  9.   
  10. #define SPEAKER_DEVICE  "/dev/console"  
  11.   
  12. int main(int argc, char *argv[])  
  13. {  
  14.     int fd;  
  15.     int freq;  
  16.       
  17.     if(argc !=2)  
  18.     {  
  19.         printf("Usage: %s frequence /n", argv[0]);  
  20.         return 1;  
  21.     }  
  22.       
  23.     freq = atoi(argv[1]);  
  24.     if(freq <=0 || freq > 10000)  
  25.     {  
  26.         printf("the frequence must be in the range from 0 to 10000./n");  
  27.         return 1;  
  28.     }  
  29.       
  30.     fd = open(SPEAKER_DEVICE, O_WRONLY);  
  31.   
  32.     if(fd == -1)  
  33.     {  
  34.         perror("connot open device./n");   
  35.         return 1;  
  36.     }  
  37.       
  38.     int i;  
  39.     int cnt;  
  40.     for(i = 0; i<1000; ++i)  
  41.     {  
  42.         int set_freq = 1190000/freq;  
  43.         ioctl(fd, KIOCSOUND, set_freq);  
  44.         usleep(200);  
  45.         ioctl(fd, KIOCSOUND, 0);  
  46.         usleep(100);  
  47.     }  
  48.     return 0;