[linux-audio-dev] select() ?? before snd_pcm_writei(handle, buffer, frames);

kurmisk kurmisk at inbox.lv
Fri Mar 24 14:42:36 UTC 2006


Hi Developers.

I have write my small _alsa_test_program_.  
[C code ckunkz see below]
It works good but now i wanna before call
rc = snd_pcm_writei(handle, buffer, frames);
somehow check - is sound device free for this call or not.

For OSS i have such problem solve with  select() :
 fdo = open("/dev/dsp", O_WRONLY );
.....
      FD_ZERO(&wfds);
      FD_SET( fdo , &wfds);     Zeit.tv_sec=0;   Zeit.tv_usec=1;
      response=select(fdo+1, NULL, &wfds, NULL, & Zeit );


Tnx in advance
Alf



int alsa_snd_pcm_open_write(unsigned int sample_rate__, 
unsigned int chan__ , snd_pcm_uframes_t * frames_p , snd_pcm_t 
** handle)
{
  int dir;                   // snd_pcm_t *handle;
  snd_pcm_hw_params_t *params;
  unsigned int val;           int ret_fun;
  int chan_nr;                snd_pcm_uframes_t frames;
  /* Open PCM device for playback. */
  ret_fun = snd_pcm_open(* &handle, "default", 
SND_PCM_STREAM_PLAYBACK, 0);
  if (ret_fun < 0){  fprintf(stderr, "unable to open pcm device: %s\n", 
snd_strerror(ret_fun) );  exit(1); }
  /* Allocate a hardware parameters object. */
  snd_pcm_hw_params_alloca(&params); /* Fill it in with default 
values. */
  snd_pcm_hw_params_any(* handle, params); /* Set the desired 
hardware parameters. */
  snd_pcm_hw_params_set_access(* handle, params,  
SND_PCM_ACCESS_RW_INTERLEAVED);   /* Interleaved mode 
*/
  snd_pcm_hw_params_set_format(* handle, params,
SND_PCM_FORMAT_S16_LE);   /* Signed 16-bit little-endian 
format */
  //snd_pcm_hw_params_set_format(handle, params,
SND_PCM_FORMAT_S16_BE);
  chan_nr  = chan__;
  snd_pcm_hw_params_set_channels(* handle, params, chan_nr);
  val = sample_rate__;
  snd_pcm_hw_params_set_rate_near(* handle, params,  &val, &dir
);
  frames = * frames_p; //frames = 2048;    /* Set period size to  __nr__ 
 frames. */
  snd_pcm_hw_params_set_period_size_near(* handle, params, 
&frames, &dir);
   * frames_p = frames;
  /* Write the parameters to the driver */
  ret_fun = snd_pcm_hw_params(* handle, params);
  if (ret_fun < 0)
  { fprintf(stderr, "unable to set hw parameters: %s\n", 
snd_strerror(ret_fun)); exit(1); }
  /* Use a buffer large enough to hold one period */
   snd_pcm_hw_params_get_period_size(params, &frames,  &dir);
   snd_pcm_hw_params_get_period_time(params, &val, &dir);
}


int main(int argc, char **argv)
{
........
 snd_pcm_uframes_t frames;
unsigned int sample_rate;   unsigned int chan;
  snd_pcm_t *handle;
//----

 sample_rate=44100;    chan=2;
  frames=1023;  // 4096
//  frames=2047;  // 8192
//  frames=4095;  // 16384
//  frames=8191;  // 32768

alsa_snd_pcm_open_write( sample_rate , chan , & frames , & 
handle);
.......
   while (1)
   {
  .......
     rc = snd_pcm_writei(handle, buffer, frames);
     if (rc == -EPIPE) {
      /* EPIPE means underrun */
       fprintf(stderr, "underrun occurred\n");
       snd_pcm_prepare(handle);
     } else if (rc < 0) 
     {  fprintf(stderr, "error from writei: %s\n",     snd_strerror(rc));
     }  else if (rc != (int)frames) {
       fprintf(stderr,"short write, write %d frames\n", rc);
     }
 .......
  }

   snd_pcm_drain(handle);
   snd_pcm_close(handle);

}// main
------





More information about the Linux-audio-dev mailing list