- snd_pcm_hw_params_t *hwparams;
- snd_pcm_sw_params_t *swparams;
- snd_pcm_uframes_t buffer_size, xfer_align, start_threshold,
- stop_threshold;
- int err;
- snd_pcm_info_t *info;
- snd_pcm_uframes_t period_size;
- struct private_alsa_write_data *pad = para_calloc(sizeof(struct
- private_alsa_write_data));
- struct alsa_write_args_info *conf = w->conf;
- struct writer_node_group *wng = w->wng;
-
- if (!conf->samplerate_given && wng->samplerate)
- pad->samplerate = *wng->samplerate;
- else
- pad->samplerate = conf->samplerate_arg;
- if (!conf->channels_given && wng->channels)
- pad->channels = *wng->channels;
- else
- pad->channels = conf->channels_arg;
- PARA_INFO_LOG("%d channel(s), %dHz\n", pad->channels, pad->samplerate);
- w->private_data = pad;
- snd_pcm_info_alloca(&info);
- err = snd_pcm_open(&pad->handle, conf->device_arg,
- SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
- if (err < 0)
- return -E_PCM_OPEN;
- if ((err = snd_pcm_info(pad->handle, info)) < 0)
- return -E_SND_PCM_INFO;
-
- snd_pcm_hw_params_alloca(&hwparams);
- snd_pcm_sw_params_alloca(&swparams);
- if (snd_pcm_hw_params_any(pad->handle, hwparams) < 0)
- return -E_BROKEN_CONF;
- if (snd_pcm_hw_params_set_access(pad->handle, hwparams,
- SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
- return -E_ACCESS_TYPE;
- if (snd_pcm_hw_params_set_format(pad->handle, hwparams, FORMAT) < 0)
- return -E_SAMPLE_FORMAT;
- if (snd_pcm_hw_params_set_channels(pad->handle, hwparams,
- pad->channels) < 0)
- return -E_CHANNEL_COUNT;
- if (snd_pcm_hw_params_set_rate_near(pad->handle, hwparams,
- &pad->samplerate, 0) < 0)
- return -E_SET_RATE;
- err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &pad->buffer_time, 0);
- if (err < 0 || !pad->buffer_time)
- return -E_GET_BUFFER_TIME;
- PARA_INFO_LOG("buffer time: %d\n", pad->buffer_time);
- if (snd_pcm_hw_params_set_buffer_time_near(pad->handle, hwparams,
- &pad->buffer_time, 0) < 0)
- return -E_SET_BUFFER_TIME;
- if (snd_pcm_hw_params(pad->handle, hwparams) < 0)
- return -E_HW_PARAMS;
- snd_pcm_hw_params_get_period_size(hwparams, &period_size, 0);
+ switch (sf) {
+ case SF_S8: return SND_PCM_FORMAT_S8;
+ case SF_U8: return SND_PCM_FORMAT_U8;
+ case SF_S16_LE: return SND_PCM_FORMAT_S16_LE;
+ case SF_S16_BE: return SND_PCM_FORMAT_S16_BE;
+ case SF_U16_LE: return SND_PCM_FORMAT_U16_LE;
+ case SF_U16_BE: return SND_PCM_FORMAT_U16_BE;
+ default: return SND_PCM_FORMAT_S16_LE;
+ }
+}
+
+/* Install PCM software and hardware configuration. */
+static int alsa_init(struct writer_node *wn)
+{
+ struct private_alsa_write_data *pad = wn->private_data;
+ snd_pcm_hw_params_t *hwparams = NULL;
+ snd_pcm_sw_params_t *swparams = NULL;
+ snd_pcm_uframes_t start_threshold, stop_threshold;
+ snd_pcm_uframes_t buffer_size, period_size;
+ snd_output_t *output_log;
+ int ret;
+ const char *msg, *dev = WRITE_CMD_OPT_STRING_VAL(ALSA, DEVICE, wn->lpr);
+ unsigned period_time;
+
+ PARA_INFO_LOG("opening %s\n", dev);
+ msg = "unable to open pcm";
+ ret = snd_pcm_open(&pad->handle, dev, SND_PCM_STREAM_PLAYBACK,
+ SND_PCM_NONBLOCK);
+ if (ret < 0)
+ goto fail;
+ ret = snd_pcm_hw_params_malloc(&hwparams);
+ assert(ret >= 0);
+ msg = "Broken alsa configuration";
+ ret = snd_pcm_hw_params_any(pad->handle, hwparams);
+ if (ret < 0)
+ goto fail;
+ msg = "access type not available";
+ ret = snd_pcm_hw_params_set_access(pad->handle, hwparams,
+ SND_PCM_ACCESS_RW_INTERLEAVED);
+ if (ret < 0)
+ goto fail;
+ msg = "sample format not available";
+ ret = snd_pcm_hw_params_set_format(pad->handle, hwparams,
+ pad->sample_format);
+ if (ret < 0)
+ goto fail;
+ msg = "channels count not available";
+ ret = snd_pcm_hw_params_set_channels(pad->handle, hwparams,
+ pad->channels);
+ if (ret < 0)
+ goto fail;
+ msg = "could not set sample rate";
+ ret = snd_pcm_hw_params_set_rate_near(pad->handle, hwparams,
+ &pad->sample_rate, NULL);
+ if (ret < 0)
+ goto fail;
+ /* alsa wants microseconds */
+ pad->buffer_time = 1000U * WRITE_CMD_OPT_UINT32_VAL(ALSA, BUFFER_TIME,
+ wn->lpr);
+ msg = "could not set buffer time";
+ ret = snd_pcm_hw_params_set_buffer_time_near(pad->handle, hwparams,
+ &pad->buffer_time, NULL);
+ if (ret < 0)
+ goto fail;
+ pad->buffer_time /= 1000; /* we prefer milliseconds */
+ period_time = pad->buffer_time * 250; /* buffer time / 4 */
+ msg = "could not set period time";
+ ret = snd_pcm_hw_params_set_period_time_near(pad->handle, hwparams,
+ &period_time, NULL);
+ if (ret < 0)
+ goto fail;
+
+ msg = "unable to install hw params";
+ ret = snd_pcm_hw_params(pad->handle, hwparams);
+ if (ret < 0)
+ goto fail;
+ snd_pcm_hw_params_get_period_size(hwparams, &period_size, NULL);