- int max_fragments = 2;
- int frag_shift = ffs(fft_length / overlap) - 1;
- int fragments = (max_fragments << 16) | frag_shift;
- ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &fragments);
-
- ioctl(3, SNDCTL_DSP_SYNC, 0);
-
- return fd;
+ if ((err = snd_pcm_hw_params_any(handle, hw_params)) < 0) {
+ fprintf(stderr, "cannot initialize hardware parameter structure: %s\n",
+ snd_strerror(err));
+ exit(1);
+ }
+
+ if ((err = snd_pcm_hw_params_set_access(handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
+ fprintf(stderr, "cannot set access type: %s\n",
+ snd_strerror(err));
+ exit(1);
+ }
+
+ if ((err = snd_pcm_hw_params_set_format(handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) { // FIXME
+ fprintf(stderr, "cannot set sample format: %s\n",
+ snd_strerror(err));
+ exit(1);
+ }
+
+ if ((err = snd_pcm_hw_params_set_rate(handle, hw_params, sample_rate, 0)) < 0) {
+ fprintf(stderr, "cannot set sample rate: %s\n",
+ snd_strerror(err));
+ exit(1);
+ }
+
+ if ((err = snd_pcm_hw_params_set_channels(handle, hw_params, 1)) < 0) {
+ fprintf(stderr, "cannot set channel count: %s\n",
+ snd_strerror(err));
+ exit(1);
+ }
+
+ if ((err = snd_pcm_hw_params(handle, hw_params)) < 0) {
+ fprintf(stderr, "cannot set parameters: %s\n",
+ snd_strerror(err));
+ exit(1);
+ }
+
+ snd_pcm_hw_params_free(hw_params);
+
+ if ((err = snd_pcm_prepare(handle)) < 0) {
+ fprintf(stderr, "cannot prepare audio interface for use: %s\n",
+ snd_strerror(err));
+ exit(1);
+ }
+
+ return handle;