Example #1
0
int VoiceStreamer::setparams_c(snd_pcm_t *chandle, int *bufsize) {
   int err, last_bufsize = *bufsize;
   snd_pcm_hw_params_t *ct_params; // templates w/ rate, format and channels
   snd_pcm_hw_params_t *c_params;
   snd_pcm_sw_params_t *c_swparams;
   snd_pcm_uframes_t c_size, c_psize;
   unsigned int val;

   snd_pcm_hw_params_alloca(&c_params);
   snd_pcm_hw_params_alloca(&ct_params);
   snd_pcm_sw_params_alloca(&c_swparams);
   if((err = setparams_stream(chandle, ct_params, "capture")) < 0) {
      printf("Unable to set parameters for capture stream: %s\n", 
            snd_strerror(err));
      exit(EXIT_FAILURE);
   }

   bool again = true;
   if (buffer_size > 0) {
      *bufsize = buffer_size;
      again = false;
   }

   for (;; again = true) {
      if (again) {
         if (buffer_size > 0)
            return -1;
         if (last_bufsize == *bufsize)
            *bufsize +=4;
         last_bufsize = *bufsize;
         if (*bufsize > latency_max)
            return -1;
      }
      if ((err = setparams_bufsize(chandle, c_params, ct_params, *bufsize,
                  "capture")) < 0) {
         printf("Unable to set sw parameters for capture stream: %s\n",
               snd_strerror(err));
         exit(EXIT_FAILURE);
      }

      snd_pcm_hw_params_get_period_size(c_params, &c_psize, NULL);
      if (c_psize > (unsigned int) *bufsize)
         *bufsize = c_psize;

      snd_pcm_hw_params_get_buffer_size(c_params, &c_size);
      if (c_psize*4 < c_size) {
         snd_pcm_hw_params_get_periods_min(c_params, &val, NULL);
         if (val > 4) {
            printf("Capture device does not support 4 periods per buffer\n");
            exit(EXIT_FAILURE);
         }
         continue;
      }
      
      break;
   }

   if ((err = setparams_set(chandle, c_params, c_swparams, "capture")) < 0) {
      printf("Unable to set sw parameters for capture stream: %s\n", 
            snd_strerror(err));
      exit(EXIT_FAILURE);
   }

   fflush(stdout);
   return 0;
}
Example #2
0
int setparams(snd_pcm_t *phandle, snd_pcm_t *chandle, int *bufsize)
{
        int err, last_bufsize = *bufsize;
        snd_pcm_hw_params_t *pt_params, *ct_params;     /* templates with rate, format and channels */
        snd_pcm_hw_params_t *p_params, *c_params;
        snd_pcm_sw_params_t *p_swparams, *c_swparams;
        snd_pcm_uframes_t p_size, c_size, p_psize, c_psize;
        unsigned int p_time, c_time;
        unsigned int val;
        snd_pcm_hw_params_alloca(&p_params);
        snd_pcm_hw_params_alloca(&c_params);
        snd_pcm_hw_params_alloca(&pt_params);
        snd_pcm_hw_params_alloca(&ct_params);
        snd_pcm_sw_params_alloca(&p_swparams);
        snd_pcm_sw_params_alloca(&c_swparams);
        if ((err = setparams_stream(phandle, pt_params, "playback")) < 0) {
                printf("Unable to set parameters for playback stream: %s\n", snd_strerror(err));
                exit(0);
        }
        if ((err = setparams_stream(chandle, ct_params, "capture")) < 0) {
                printf("Unable to set parameters for capture stream: %s\n", snd_strerror(err));
                exit(0);
        }
        if (buffer_size > 0) {
                *bufsize = buffer_size;
                goto __set_it;
        }
      __again:
        if (buffer_size > 0)
                return -1;
        if (last_bufsize == *bufsize)
                *bufsize += 4;
        last_bufsize = *bufsize;
        if (*bufsize > latency_max)
                return -1;
      __set_it:
        if ((err = setparams_bufsize(phandle, p_params, pt_params, *bufsize, "playback")) < 0) {
                printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err));
                exit(0);
        }
        if ((err = setparams_bufsize(chandle, c_params, ct_params, *bufsize, "capture")) < 0) {
                printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err));
                exit(0);
        }
        snd_pcm_hw_params_get_period_size(p_params, &p_psize, NULL);
        if (p_psize > (unsigned int)*bufsize)
                *bufsize = p_psize;
        snd_pcm_hw_params_get_period_size(c_params, &c_psize, NULL);
        if (c_psize > (unsigned int)*bufsize)
                *bufsize = c_psize;
        snd_pcm_hw_params_get_period_time(p_params, &p_time, NULL);
        snd_pcm_hw_params_get_period_time(c_params, &c_time, NULL);
        if (p_time != c_time)
                goto __again;
        snd_pcm_hw_params_get_buffer_size(p_params, &p_size);
        if (p_psize * 2 < p_size) {
                snd_pcm_hw_params_get_periods_min(p_params, &val, NULL);
                if (val > 2) {
                        printf("playback device does not support 2 periods per buffer\n");
                        exit(0);
                }
                goto __again;
        }
        snd_pcm_hw_params_get_buffer_size(c_params, &c_size);
        if (c_psize * 2 < c_size) {
                snd_pcm_hw_params_get_periods_min(c_params, &val, NULL);
                if (val > 2 ) {
                        printf("capture device does not support 2 periods per buffer\n");
                        exit(0);
                }
                goto __again;
        }
        if ((err = setparams_set(phandle, p_params, p_swparams, "playback")) < 0) {
                printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err));
                exit(0);
        }
        if ((err = setparams_set(chandle, c_params, c_swparams, "capture")) < 0) {
                printf("Unable to set sw parameters for playback stream: %s\n", snd_strerror(err));
                exit(0);
        }
        if ((err = snd_pcm_prepare(phandle)) < 0) {
                printf("Prepare error: %s\n", snd_strerror(err));
                exit(0);
        }
        snd_pcm_dump(phandle, output);
        snd_pcm_dump(chandle, output);
        fflush(stdout);
        return 0;
}
Example #3
0
int VoiceStreamer::setparams_p(snd_pcm_t *phandle, int *bufsize) {
   int err, last_bufsize = *bufsize;
   snd_pcm_hw_params_t *pt_params;
   snd_pcm_hw_params_t *p_params;
   snd_pcm_sw_params_t *p_swparams;
   snd_pcm_uframes_t p_size, p_psize;
   unsigned int val;

   snd_pcm_hw_params_alloca(&p_params);
   snd_pcm_hw_params_alloca(&pt_params);
   snd_pcm_sw_params_alloca(&p_swparams);
   if ((err = setparams_stream(phandle, pt_params, "playback")) < 0) {
      printf("Unable to set parameters for playback stream: %s\n",
            snd_strerror(err));
      exit(EXIT_FAILURE);
   }

   bool again = true;
   if (buffer_size > 0) {
      *bufsize = buffer_size;
      again = false;
   }

   for (;; again = true) {
      if (again) {
         if (buffer_size > 0)
            return -1;
         if (last_bufsize == *bufsize)
            *bufsize +=4;
         last_bufsize = *bufsize;
         if (*bufsize > latency_max)
            return -1;
      }
      if ((err = setparams_bufsize(phandle, p_params, pt_params, *bufsize,
                  "playback")) < 0) {
         printf("Unable to set sw parameters for playback stream: %s\n",
               snd_strerror(err));
         exit(EXIT_FAILURE);
      }

      snd_pcm_hw_params_get_period_size(p_params, &p_psize, NULL);
      if (p_psize > (unsigned int) *bufsize)
         *bufsize = p_psize;

      snd_pcm_hw_params_get_buffer_size(p_params, &p_size);
      if (p_psize*4 < p_size) {
         snd_pcm_hw_params_get_periods_min(p_params, &val, NULL);
         if (val > 4) {
            printf("Playback device does not support 4 periods per buffer\n");
            exit(EXIT_FAILURE);
         }
         continue;
      }
      
      break;
   }

   if ((err = setparams_set(phandle, p_params, p_swparams, "playback")) < 0) {
      printf("Unable to set sw parameters for playback stream: %s\n", 
            snd_strerror(err));
      exit(EXIT_FAILURE);
   }

   fflush(stdout);
   return 0;

}