ALSA错误的文件描述符和机器人语音

时间:2016-05-27 15:03:43

标签: audio alsa libalsa

我正在使用linux嵌入式设备,每次我开始播放音频流时,调用snd_pcm_start,返回-77:这意味着"文件描述符处于错误状态"。

#define EBADFD      77  /* File descriptor in bad state */

我用一些printf重建了alsa lib,我发现使用命令aplay不会产生那个错误。

即使出现这个错误,我也可以播放声音,但每10次我会得到一种粗糙的金属机器人声音。

问题的原因是什么?我按照了打开声音设备时发现的所有指示。

这是我的代码的相关部分:

在捕获/回放线程之前,我以这种方式启动音频,这里有错误:

void alsa_device_start(AlsaDevice *dev)
{

   int i;
   int rval = 0;

   short pcm[dev->period*dev->channels];
   for (i=0;i<dev->period*dev->channels;i++)
      pcm[i] = 0;

   alsa_device_write(dev, pcm, dev->period);
   alsa_device_write(dev, pcm, dev->period);

   rval = snd_pcm_start(dev->capture_handle); //<<-- returns -77
}

在我按照以下方式启动设备之前:

AlsaDevice *alsa_device_open(pj_pool_t* pool, char *device_name, unsigned int rate, int channels, int period)
{
   int dir;
   int err;
   snd_pcm_hw_params_t *hw_params;
   snd_pcm_sw_params_t *sw_params;
   snd_pcm_uframes_t period_size = period;
   snd_pcm_uframes_t buffer_size = 2*period;
   static snd_output_t *jcd_out;


   hw_params = NULL;
   sw_params = NULL;

   //AlsaDevice *dev =  pj_pool_alloc(pool, sizeof(AlsaDevice)); //malloc(sizeof(*dev));
   AlsaDevice *dev =  malloc(sizeof(*dev));

   if (!dev)
      return NULL;
   dev->device_name =  pj_pool_alloc(pool, 1+strlen(device_name)); //malloc(1+strlen(device_name));
   if (!dev->device_name)
   {
      free(dev);
      return NULL;
   }
   strcpy(dev->device_name, device_name);
   dev->channels = channels;
   dev->period = period;

   dev->read_buff = pj_pool_alloc(pool, period * sizeof(short));
   dev->read_buff_tmp = pj_pool_alloc(pool, period * sizeof(short));

   err = snd_output_stdio_attach(&jcd_out, stdout, 0);
   if ((err = snd_pcm_open (&dev->capture_handle, dev->device_name, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot open audio device %s (%s)\n", dev->device_name,
                  snd_strerror (err)));
      assert(0);
   }

   if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot allocate hardware parameter structure (%s)\n",
                      snd_strerror (err)));
      assert(0);
   }

   if ((err = snd_pcm_hw_params_any (dev->capture_handle, hw_params)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot initialize hardware parameter structure (%s)\n",
                          snd_strerror (err)));
      assert(0);
   }

   if ((err = snd_pcm_hw_params_set_access (dev->capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot set access type (%s)\n",
                              snd_strerror (err)));
      assert(0);
   }

   if ((err = snd_pcm_hw_params_set_format (dev->capture_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot set sample format (%s)\n",
               snd_strerror (err)));
       assert(0);
   }

   if ((err = snd_pcm_hw_params_set_rate_near (dev->capture_handle, hw_params, &rate, 0)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot set sample rate (%s)\n",
                   snd_strerror (err)));
      assert(0);
   }
   /*fprintf (stderr, "rate = %d\n", rate);*/

   if ((err = snd_pcm_hw_params_set_channels (dev->capture_handle, hw_params, channels)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot set channel count (%s)\n",
                       snd_strerror (err)));
      assert(0);
   }

   period_size = period;
   dir = 0;
   if ((err = snd_pcm_hw_params_set_period_size_near (dev->capture_handle, hw_params, &period_size, &dir)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot set period size (%s)\n",
                           snd_strerror (err)));
      assert(0);
   }

   if ((err = snd_pcm_hw_params_set_periods (dev->capture_handle, hw_params, ALSA_PERIODS, 0)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot set number of periods (%s)\n",
                               snd_strerror (err)));
      assert(0);
   }

   buffer_size = period_size * 2;
   dir=0;
   if ((err = snd_pcm_hw_params_set_buffer_size_near (dev->capture_handle, hw_params, &buffer_size)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot set buffer time (%s)\n",
                                   snd_strerror (err)));
      assert(0);
   }

   if ((err = snd_pcm_hw_params (dev->capture_handle, hw_params)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot set capture parameters (%s)\n",
                                       snd_strerror (err)));
      assert(0);
   }
   /*snd_pcm_dump_setup(dev->capture_handle, jcd_out);*/
   snd_pcm_hw_params_free (hw_params);
   hw_params = NULL; //ermes

   if ((err = snd_pcm_sw_params_malloc (&sw_params)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot allocate software parameters structure (%s)\n",
                                           snd_strerror (err)));
      assert(0);
   }
   if ((err = snd_pcm_sw_params_current (dev->capture_handle, sw_params)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot initialize software parameters structure (%s)\n",
                                               snd_strerror (err)));
      assert(0);
   }
   if ((err = snd_pcm_sw_params_set_avail_min (dev->capture_handle, sw_params, period)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot set minimum available count (%s)\n",
                                                   snd_strerror (err)));
      assert(0);
   }
   if ((err = snd_pcm_sw_params (dev->capture_handle, sw_params)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot set software parameters (%s)\n",
                                                       snd_strerror (err)));
      assert(0);
   }

   if (sw_params) snd_pcm_sw_params_free(sw_params); //ermes
   sw_params = NULL; //ermes


   if ((err = snd_pcm_open (&dev->playback_handle, dev->device_name, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {

       PJ_LOG (4,(THIS_FILE, "cannot open audio device %s (%s)\n",
                  dev->device_name,
                  snd_strerror (err)));
      assert(0);
   }

   if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot allocate hardware parameter structure (%s)\n",
               snd_strerror (err)));
      assert(0);
   }

   if ((err = snd_pcm_hw_params_any (dev->playback_handle, hw_params)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot initialize hardware parameter structure (%s)\n",
               snd_strerror (err)));
      assert(0);
   }

   if ((err = snd_pcm_hw_params_set_access (dev->playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot set access type (%s)\n",
                   snd_strerror (err)));
      assert(0);
   }

   if ((err = snd_pcm_hw_params_set_format (dev->playback_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot set sample format (%s)\n",
                       snd_strerror (err)));
      assert(0);
   }

   if ((err = snd_pcm_hw_params_set_rate_near (dev->playback_handle, hw_params, &rate, 0)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot set sample rate (%s)\n",
                           snd_strerror (err)));
      assert(0);
   }


   if ((err = snd_pcm_hw_params_set_channels (dev->playback_handle, hw_params, channels)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot set channel count (%s)\n",
               snd_strerror (err)));
      assert(0);
   }

   period_size = period;
   dir = 0;
   if ((err = snd_pcm_hw_params_set_period_size_near (dev->playback_handle, hw_params, &period_size, &dir)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot set period size (%s)\n",
               snd_strerror (err)));
      assert(0);
   }
   if ((err = snd_pcm_hw_params_set_periods (dev->playback_handle, hw_params, ALSA_PERIODS, 0)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot set number of periods (%s)\n",
               snd_strerror (err)));
      assert(0);
   }
   buffer_size = period_size * 2;
   dir=0;
   if ((err = snd_pcm_hw_params_set_buffer_size_near (dev->playback_handle, hw_params, &buffer_size)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot set buffer time (%s)\n",
               snd_strerror (err)));
      assert(0);
   }


   if ((err = snd_pcm_hw_params (dev->playback_handle, hw_params)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot set playback parameters (%s)\n",
                   snd_strerror (err)));
      assert(0);
   }

   /*snd_pcm_dump_setup(dev->playback_handle, jcd_out);*/
   snd_pcm_hw_params_free (hw_params);
   hw_params = NULL; //ermes



   if ((err = snd_pcm_sw_params_malloc (&sw_params)) < 0) {
      PJ_LOG (4,(THIS_FILE, "cannot allocate software parameters structure (%s)\n",
                       snd_strerror (err)));
      assert(0);
   }

   if ((err = snd_pcm_sw_params_current (dev->playback_handle, sw_params)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot initialize software parameters structure (%s)\n",
                           snd_strerror (err)));
      assert(0);
   }
   if ((err = snd_pcm_sw_params_set_avail_min (dev->playback_handle, sw_params, period)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot set minimum available count (%s)\n",
                               snd_strerror (err)));
      assert(0);
   }
   if ((err = snd_pcm_sw_params_set_start_threshold (dev->playback_handle, sw_params, 2*period)) < 0) {
      PJ_LOG (4,(THIS_FILE, "cannot set start mode (%s)\n",
                                   snd_strerror (err)));
      assert(0);
   }
   if ((err = snd_pcm_sw_params (dev->playback_handle, sw_params)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot set software parameters (%s)\n",
                                       snd_strerror (err)));
      assert(0);
   }

   if (sw_params) snd_pcm_sw_params_free(sw_params); 
   sw_params = NULL; 


   snd_pcm_link(dev->capture_handle, dev->playback_handle);
   if ((err = snd_pcm_prepare (dev->capture_handle)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot prepare audio interface for use (%s)\n",
                                           snd_strerror (err)));
      assert(0);
   }
   if ((err = snd_pcm_prepare (dev->playback_handle)) < 0) {
       PJ_LOG (4,(THIS_FILE, "cannot prepare audio interface for use (%s)\n",
                                       snd_strerror (err)));
      assert(0);
   }


   dev->readN = snd_pcm_poll_descriptors_count(dev->capture_handle);
   dev->writeN = snd_pcm_poll_descriptors_count(dev->playback_handle);

   dev->read_fd = pj_pool_alloc(pool, dev->readN*sizeof(*dev->read_fd)); 

   if (snd_pcm_poll_descriptors(dev->capture_handle, dev->read_fd, dev->readN) != dev->readN)
   {
       PJ_LOG (4,(THIS_FILE,  "cannot obtain capture file descriptors (%s)\n",
                                           snd_strerror (err)));
      assert(0);
   }

   dev->write_fd = pj_pool_alloc(pool, dev->writeN*sizeof(*dev->read_fd)); // malloc(dev->writeN*sizeof(*dev->read_fd));
   if (snd_pcm_poll_descriptors(dev->playback_handle, dev->write_fd, dev->writeN) != dev->writeN)
   {
      PJ_LOG (4,(THIS_FILE,  "cannot obtain playback file descriptors (%s)\n",
                                               snd_strerror (err)));
      assert(0);
   }

   return dev;
}

0 个答案:

没有答案