主题 : micro2240的音频驱动调用问题 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 17007
精华: 0
发帖: 2
金钱: 10 两
威望: 2 点
贡献值: 0 点
综合积分: 4 分
注册时间: 2010-03-24
最后登录: 2010-05-04
楼主  发表于: 2010-04-03 15:57

 micro2240的音频驱动调用问题

之前买了一块micro2440来学习嵌入式linux
目前在做到音频这块的时候,遇到了问题就是无法实现播放,并且在录音程序中生成的文件,在板子自带的播放器以及拷贝到window上都无法播放
这是源码,希望给以指教

放音:
    fd = open("/dev/dsp", O_RDWR);//soundcard  device
    if (fd < 0)
    {                                      
        perror("open of /dev/dsp failed");              
        exit(1);                                        
    }  
arg = SIZE;                                        
        status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);    
        if (status == -1)                                  
            perror("SOUND_PCM_WRITE_BITS ioctl failed");    
        if (arg != SIZE)                                  
            perror("unable to set sample size");            
         /* 设置采样时的声道数目 */                        
        arg = CHANNELS;                                    
        status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);
        if (status == -1)                                  
            perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");
        if (arg != CHANNELS)                              
            perror("unable to set number of channels");      
      /* 设置采样时的采样频率 */                        
      arg = RATE;                                        
      status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);    
      if (status == -1)                                  
        perror("SOUND_PCM_WRITE_WRITE ioctl failed");    
      /* 循环,直到按下Control-C */                    

        //////////////////////////////////////////////
        //
      fdfile = open("11.wav", O_RDONLY|O_NONBLOCK);                    
      if (fdfile < 0) {                                      
            perror("open of /dev/dsp failed");              
            exit(1);                                        
      }              
      while(read(fdfile,buf1,buflen))
      {

    //////////////////////////////////////////////
        //
            status = write(fd, buf1, buflen);
            if (status != sizeof(buf))                      
              perror("wrote wrong number of bytes");        
          /* 在继续录音前等待回放结束 */                  
            //status = ioctl(fd, SOUND_PCM_SYNC, 0);          
            //if (status == -1)                                
              //perror("SOUND_PCM_SYNC ioctl failed");        

      }
录音:
arg1=FORMAT;
      int result = ioctl(fd, SNDCTL_DSP_SETFMT, &arg1);
      if ( result == -1 )
       {
            perror("ioctl sample format");
            return -1;
       }
       printf("the format:%d\n",arg);
      // 设置采样时的量化位数
      arg1 = SIZE;
      status1 = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg1);
      if (status1 == -1)
            perror("SOUND_PCM_WRITE_BITS ioctl failed");
      if (arg1 != SIZE)
            perror("unable to set sample size");
      printf( "bits:%d \n",arg);
     // 设置采样时的声道数目
      arg1 = CHANNELS;
      status1 = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg1);
    if (status1 == -1)
            perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");
      if (arg != CHANNELS)
            perror("unable to set number of  channels");\
    printf( " channels:%d \n",arg);
     // 设置采样时的采样频率
      arg1 = RATE;
      status1 = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg1);
      if (status1 == -1)
            perror("SOUND_PCM_WRITE_WRITE ioctl failed");
  
      FILE * in;
      in=fopen("zzz.wav","wb");
    
      while(  status = read(fd, buf2,buflen))
      {
          if (status != sizeof(buf))
            perror("read wrong number of bytes");

          //保存声音到文件
          fwrite(buf2,buflen,1,in);
      }
  
      fclose(in);