2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file alsa_writer.c Data specific to the alsa writer */
22 * Based in parts on aplay.c from the alsa-utils-1.0.8 package,
23 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>, which is
24 * based on the vplay program by Michael Beck.
32 #include <alsa/asoundlib.h>
34 #include "write.cmdline.h"
37 extern struct gengetopt_args_info conf;
39 #define FORMAT SND_PCM_FORMAT_S16_LE
40 struct private_alsa_data {
42 size_t bytes_per_frame;
46 * open and prepare the PCM handle for writing
48 * Install PCM software and hardware configuration. Exit on errors.
50 static int alsa_open(struct writer_node *w)
52 snd_pcm_hw_params_t *hwparams;
53 snd_pcm_sw_params_t *swparams;
54 snd_pcm_uframes_t buffer_size, xfer_align, start_threshold,
56 unsigned buffer_time = 0;
60 snd_pcm_uframes_t period_size;
61 struct private_alsa_data *pad = para_malloc(sizeof(struct private_alsa_data));
62 w->private_data = pad;
64 snd_pcm_info_alloca(&info);
65 if (snd_output_stdio_attach(&log, stderr, 0) < 0)
67 err = snd_pcm_open(&pad->handle, conf.device_arg,
68 SND_PCM_STREAM_PLAYBACK, 0);
71 if ((err = snd_pcm_info(pad->handle, info)) < 0)
72 return -E_SND_PCM_INFO;
74 snd_pcm_hw_params_alloca(&hwparams);
75 snd_pcm_sw_params_alloca(&swparams);
76 if (snd_pcm_hw_params_any(pad->handle, hwparams) < 0)
77 return -E_BROKEN_CONF;
78 if (snd_pcm_hw_params_set_access(pad->handle, hwparams,
79 SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
80 return -E_ACCESS_TYPE;
81 if (snd_pcm_hw_params_set_format(pad->handle, hwparams, FORMAT) < 0)
82 return -E_SAMPLE_FORMAT;
83 if (snd_pcm_hw_params_set_channels(pad->handle, hwparams,
84 conf.channels_arg) < 0)
85 return -E_CHANNEL_COUNT;
86 if (snd_pcm_hw_params_set_rate_near(pad->handle, hwparams,
87 (unsigned int*) &conf.sample_rate_arg, 0) < 0)
89 err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &buffer_time, 0);
90 if (err < 0 || !buffer_time)
91 return -E_GET_BUFFER_TIME;
92 PARA_DEBUG_LOG("buffer time: %d\n", buffer_time);
93 if (snd_pcm_hw_params_set_buffer_time_near(pad->handle, hwparams,
95 return -E_SET_BUFFER_TIME;
96 if (snd_pcm_hw_params(pad->handle, hwparams) < 0)
98 snd_pcm_hw_params_get_period_size(hwparams, &period_size, 0);
99 snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
100 PARA_DEBUG_LOG("buffer size: %lu, period_size: %lu\n", buffer_size,
102 if (period_size == buffer_size)
103 return -E_BAD_PERIOD;
104 snd_pcm_sw_params_current(pad->handle, swparams);
105 err = snd_pcm_sw_params_get_xfer_align(swparams, &xfer_align);
106 if (err < 0 || !xfer_align)
108 snd_pcm_sw_params_set_avail_min(pad->handle, swparams, period_size);
109 /* round to closest transfer boundary */
110 start_threshold = (buffer_size / xfer_align) * xfer_align;
111 if (start_threshold < 1)
113 if (snd_pcm_sw_params_set_start_threshold(pad->handle, swparams,
114 start_threshold) < 0)
115 return -E_START_THRESHOLD;
116 stop_threshold = buffer_size;
117 if (snd_pcm_sw_params_set_stop_threshold(pad->handle, swparams,
119 return -E_STOP_THRESHOLD;
120 if (snd_pcm_sw_params_set_xfer_align(pad->handle, swparams,
123 if (snd_pcm_sw_params(pad->handle, swparams) < 0)
125 pad->bytes_per_frame = snd_pcm_format_physical_width(FORMAT)
126 * conf.channels_arg / 8;
127 return period_size * pad->bytes_per_frame;
131 * push out pcm frames
132 * \param data pointer do data to be written
133 * \param nbytes number of bytes (not frames)
135 * \return Number of bytes written, -E_ALSA_WRITE on errors.
137 static int alsa_write(char *data, size_t nbytes, struct writer_node *wn)
139 struct private_alsa_data *pad = wn->private_data;
140 size_t frames = nbytes / pad->bytes_per_frame;
141 unsigned char *d = data;
142 snd_pcm_sframes_t r, result = 0;
145 /* write interleaved frames */
146 r = snd_pcm_writei(pad->handle, d, frames);
148 PARA_ERROR_LOG("write error: %s\n", snd_strerror(r));
149 if (r == -EAGAIN || (r >= 0 && r < frames))
150 snd_pcm_wait(pad->handle, 1);
151 else if (r == -EPIPE)
152 snd_pcm_prepare(pad->handle);
154 return -E_ALSA_WRITE;
158 d += r * pad->bytes_per_frame;
161 return result * pad->bytes_per_frame;
164 static void alsa_close(struct writer_node *wn)
166 struct private_alsa_data *pad = wn->private_data;
167 snd_pcm_drain(pad->handle);
168 snd_pcm_close(pad->handle);
169 snd_config_update_free_global();
173 void alsa_writer_init(struct writer *w)
176 w->write = alsa_write;
177 w->close = alsa_close;
178 w->shutdown = NULL; /* nothing to do */