解决mini2440声卡全双工问题 实现同时录音及播放


  1. #include <unistd.h>  
  2. #include <fcntl.h>  
  3. #include <sys/types.h>  
  4. #include <sys/ioctl.h>  
  5. #include <stdlib.h>  
  6. #include <stdio.h>  
  7. #include <linux/soundcard.h>  
  8. #include <pthread.h>  
  9. #define LENGTH 3  
  10. #define RATE 8000  
  11. #define SIZE 8  
  12. #define CHANNELS 1   
  13. unsigned char buf[LENGTH * RATE * SIZE * CHANNELS /8];   
  14. int main()   
  15. {   
  16.     int fd_r,fd_w;   
  17.     int arg;   
  18.     int status;   
  19.     pid_t pid;   
  20.     int recing = 1,playing = 0;   
  21.     pid = fork();   
  22.     if(pid < 0){   
  23.         perror("error in fork\n");   
  24.     }   
  25.     if(pid == 0){   
  26.         fd_r = open("/dev/dsp",O_RDONLY);   
  27.         if(fd_r < 0){   
  28.             perror("open read of /dev/dsp failed");   
  29.             exit(1);   
  30.         }   
  31.            
  32.         arg = SIZE;   
  33.         status = ioctl(fd_r,SOUND_PCM_WRITE_BITS,&arg);   
  34.         if(status == -1)   
  35.             perror("SOUND_PCM_WRITE_BITS ioctl failed");   
  36.         if(arg != SIZE)   
  37.             perror("unable to set sample size");   
  38.            
  39.            
  40.         arg = CHANNELS;   
  41.         status = ioctl(fd_r,SOUND_PCM_WRITE_CHANNELS,&arg);   
  42.         if(status == -1)   
  43.             perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");   
  44.         if(arg != CHANNELS)   
  45.             perror("unable to set number of channels");   
  46.            
  47.         arg = RATE;   
  48.         status = ioctl(fd_r,SOUND_PCM_WRITE_RATE,&arg);   
  49.         if(status == -1)   
  50.             perror("SOUND_PCM_WRITE_WRITE ioctl failed");   
  51.        
  52.         while(1)   
  53.         {   
  54.             if(playing== 0){   
  55.                 recing = 1;   
  56.                 printf("Say something:");   
  57.                 status = read(fd_r,buf,sizeof(buf));   
  58.                 /* recording*/  
  59.                 if(status != sizeof(buf))   
  60.                     perror("read wrong number of bytes");   
  61.                 recing = 0;   
  62.             }   
  63.         }   
  64.     }   
  65.     else {   
  66.         fd_w = open("/dev/dsp",O_WRONLY);   
  67.         if(fd_w < 0)   
  68.         {   
  69.             perror("open write of /dev/dsp failed");   
  70.             exit(1);   
  71.         }   
  72.            
  73.         arg = SIZE;   
  74.         status = ioctl(fd_w,SOUND_PCM_WRITE_BITS,&arg);   
  75.         if(status == -1)   
  76.             perror("SOUND_PCM_WRITE_BITS ioctl failed");   
  77.         if(arg != SIZE)   
  78.             perror("unable to set sample size");   
  79.            
  80.            
  81.         arg = CHANNELS;   
  82.         status = ioctl(fd_w,SOUND_PCM_WRITE_CHANNELS,&arg);   
  83.         if(status == -1)   
  84.             perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");   
  85.         if(arg != CHANNELS)   
  86.             perror("unable to set number of channels");   
  87.            
  88.         arg = RATE;   
  89.         status = ioctl(fd_w,SOUND_PCM_WRITE_RATE,&arg);   
  90.         if(status == -1)   
  91.             perror("SOUND_PCM_WRITE_WRITE ioctl failed");   
  92.         while(1)   
  93.         {   
  94.             if(recing == 0){   
  95.                 playing = 1;   
  96.                 status = write(fd_w,buf,sizeof(buf));   
  97.                 /* playback */  
  98.                 if(status != sizeof(buf))   
  99.                     perror("wrote wrong number of bytes");   
  100.                 status = ioctl(fd_w,SOUND_PCM_SYNC,0);   
  101.                 if(status == -1)   
  102.                     perror("SOUND_PCM_SYNC ioctl failed");   
  103.                 playing = 0;   
  104.             }   
  105.         }   
  106.     }   
  107. }  

相关内容