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.
20 * Based in parts on aplay.c from the alsa-utils-1.0.8 package,
21 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>, which is
22 * based on the vplay program by Michael Beck.
25 #define WAV_HEADER_LEN 44
26 #include <sys/time.h> /* gettimeofday */
28 #include "play.cmdline.h"
29 #include <alsa/asoundlib.h>
31 enum { E_BROKEN_CONF, /* Broken configuration for this PCM */
32 E_ACCESS_TYPE, /* Access type not available */
33 E_SAMPLE_FORMAT, /* Sample format not available */
34 E_CHANNEL_COUNT, /* Channels count not available */
35 E_HW_PARAMS, /* Unable to install hw params */
36 E_SW_PARAMS, /* Unable to install sw params */
37 E_BAD_PERIOD, /* Can't use period equal to buffer size */
38 E_GET_XFER, /* Unable to obtain xfer align */
39 E_SET_XFER, /* snd_pcm_sw_params_set_xfer_align failed */
40 E_MEM, /* not enough memory */
41 E_READ, /* read error */
42 E_WRITE, /* write error */
43 E_PIPE, /* write to pipe with other side closed */
44 E_PCM_OPEN, /* unable to open pcm */
45 E_SND_PCM_INFO, /* pcm info error */
46 E_GET_BUFFER_TIME, /* snd_pcm_hw_params_get_buffer_time_max failed */
47 E_SET_BUFFER_TIME, /* snd_pcm_hw_params_set_buffer_time_near failed */
48 E_SET_RATE, /* snd_pcm_hw_params_set_rate_near failed */
49 E_START_THRESHOLD, /* snd_pcm_sw_params_set_start_threshold failed */
50 E_STOP_THRESHOLD, /* snd_pcm_sw_params_set_stop_threshold failed */
51 E_LOG, /* snd_output_stdio_attach failed */
52 E_SYNTAX /* could not parse start_time option */
55 #define FORMAT SND_PCM_FORMAT_S16_LE
59 fprintf (stderr, "error: " #EXP "\n"); exit(EXP);} \
62 static snd_pcm_t *handle;
63 static unsigned char *audiobuf;
64 static snd_pcm_uframes_t chunk_size;
65 static size_t bytes_per_frame;
66 static struct timeval *start_time;
67 static struct gengetopt_args_info conf;
70 * read_wav_header - read WAV_HEADER_LEN bytes from stdin to audio buffer
72 * Exit on errors and on eof before WAV_HEADER_LEN could be read.
74 static void read_wav_header(void)
76 ssize_t ret, count = 0;
78 while (count < WAV_HEADER_LEN) {
79 ret = read(STDIN_FILENO, audiobuf + count, WAV_HEADER_LEN - count);
87 * set_alsa_params - Prepare the PCM handle for writing
89 * Install PCM software and hardware configuration. Exit on errors.
91 static void set_alsa_params(void)
93 snd_pcm_hw_params_t *hwparams;
94 snd_pcm_sw_params_t *swparams;
95 snd_pcm_uframes_t buffer_size, xfer_align, start_threshold,
97 unsigned buffer_time = 0;
100 snd_pcm_hw_params_alloca(&hwparams);
101 snd_pcm_sw_params_alloca(&swparams);
102 if (snd_pcm_hw_params_any(handle, hwparams) < 0)
104 if (snd_pcm_hw_params_set_access(handle, hwparams,
105 SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
107 if (snd_pcm_hw_params_set_format(handle, hwparams, FORMAT) < 0)
108 EXIT(E_SAMPLE_FORMAT);
109 if (snd_pcm_hw_params_set_channels(handle, hwparams, conf.channels_arg) < 0)
110 EXIT(E_CHANNEL_COUNT);
111 if (snd_pcm_hw_params_set_rate_near(handle, hwparams,
112 (unsigned int*) &conf.sample_rate_arg, 0) < 0)
114 err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &buffer_time, 0);
115 if (err < 0 || !buffer_time)
116 EXIT(E_GET_BUFFER_TIME);
117 fprintf(stderr, "buffer time: %d\n", buffer_time);
118 if (snd_pcm_hw_params_set_buffer_time_near(handle, hwparams,
119 &buffer_time, 0) < 0)
120 EXIT(E_SET_BUFFER_TIME);
121 if (snd_pcm_hw_params(handle, hwparams) < 0)
123 snd_pcm_hw_params_get_period_size(hwparams, &chunk_size, 0);
124 snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
125 fprintf(stderr, "buffer size: %lu, period_size: %lu\n", buffer_size, chunk_size);
126 if (chunk_size == buffer_size)
128 snd_pcm_sw_params_current(handle, swparams);
129 err = snd_pcm_sw_params_get_xfer_align(swparams, &xfer_align);
130 if (err < 0 || !xfer_align)
132 // snd_pcm_sw_params_set_sleep_min(handle, swparams, 0);
133 snd_pcm_sw_params_set_avail_min(handle, swparams, chunk_size);
134 /* round to closest transfer boundary */
135 start_threshold = (buffer_size / xfer_align) * xfer_align;
136 if (start_threshold < 1)
138 if (snd_pcm_sw_params_set_start_threshold(handle, swparams,
139 start_threshold) < 0)
140 EXIT(E_START_THRESHOLD);
141 stop_threshold = buffer_size;
142 if (snd_pcm_sw_params_set_stop_threshold(handle, swparams,
144 EXIT(E_STOP_THRESHOLD);
145 if (snd_pcm_sw_params_set_xfer_align(handle, swparams, xfer_align) < 0)
147 if (snd_pcm_sw_params(handle, swparams) < 0)
149 bytes_per_frame = snd_pcm_format_physical_width(FORMAT) * conf.channels_arg / 8;
153 * pcm_write - push out pcm frames
154 * \param data pointer do data to be written
155 * \param count number of frames
157 * \return Number of bytes written. Exit on errors.
159 static snd_pcm_sframes_t pcm_write(u_char *data, size_t count)
161 snd_pcm_sframes_t r, result = 0;
163 /* write interleaved frames */
164 r = snd_pcm_writei(handle, data, count);
166 fprintf(stderr, "write error: %s\n", snd_strerror(r));
167 if (r == -EAGAIN || (r >= 0 && r < count))
168 snd_pcm_wait(handle, 1);
169 else if (r == -EPIPE)
170 snd_pcm_prepare(handle);
176 data += r * bytes_per_frame;
183 * start_time_in_future - check if current time is later than start_time
184 * \param diff pointer to write remaining time to
186 * If start_time was not given, or current time is later than given
187 * start_time, return 0. Otherwise, return 1 and write the time
188 * difference between current time and start_time to diff. diff may be
192 static int start_time_in_future(struct timeval *diff)
196 if (!conf.start_time_given)
198 gettimeofday(&now, NULL);
199 return tv_diff(start_time, &now, diff) > 0? 1 : 0;
203 * do_initial_delay - sleep until time given at command line
205 * This is called if the initial buffer is filled. It returns
206 * immediately if no start_time was given at the command line
207 * or if the given start time is in the past.
210 static void do_initial_delay(struct timeval *delay)
212 // fprintf(stderr, "sleeping %lums\n", tv2ms(delay));
214 select(1, NULL, NULL, NULL, delay);
215 while (start_time_in_future(delay));
219 * play_pcm - play raw pcm data
220 * \param loaded number of bytes already loaded
222 * If start_time was given, prebuffer data until buffer is full or
223 * start_time is reached. In any case, do not start playing before
226 static void play_pcm(size_t loaded)
228 size_t chunk_bytes, bufsize, written = 0, prebuf_size;
231 struct timeval delay;
234 chunk_bytes = chunk_size * bytes_per_frame;
235 bufsize = (conf.bufsize_arg * 1024 / chunk_bytes) * chunk_bytes;
236 audiobuf = realloc(audiobuf, bufsize);
239 prebuf_size = conf.prebuffer_arg * bufsize / 100;
242 if (loaded < prebuf_size)
244 if (start_time && start_time_in_future(&delay)) {
245 do_initial_delay(&delay);
250 while (loaded >= chunk_bytes) {
251 // fprintf(stderr, "write (loaded = %d)\n", loaded);
252 ret = pcm_write(p, chunk_size) * bytes_per_frame;
257 if (loaded && p != audiobuf) {
258 // fprintf(stderr, "memcpy: %d@%d\n", loaded, p - audiobuf);
259 memcpy(audiobuf, p, loaded);
262 ret = read(STDIN_FILENO, audiobuf + loaded, bufsize - loaded);
269 snd_pcm_drain(handle);
273 * check_wave - test if audio buffer contains a valid wave header
275 * If not, return 0, otherwise, store number of channels and sample rate
276 * in struct conf and return WAV_HEADER_LEN.
278 static size_t check_wave(void)
280 unsigned char *a = audiobuf;
281 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F')
282 return WAV_HEADER_LEN;
283 conf.channels_arg = (unsigned) a[22];
284 conf.sample_rate_arg = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
285 // fprintf(stderr, "channels: %d, rate: %d\n", conf.channels_arg,
286 // conf.sample_rate_arg);
290 int main(int argc, char *argv[])
292 snd_pcm_info_t *info;
297 cmdline_parser(argc, argv, &conf);
298 if (conf.start_time_given) {
299 if (sscanf(conf.start_time_arg, "%lu:%lu",
300 &tv.tv_sec, &tv.tv_usec) != 2)
304 // fprintf(stderr, "argc=%d, argv[1]=%s\n",argc, argv[1]);
305 snd_pcm_info_alloca(&info);
306 if (snd_output_stdio_attach(&log, stderr, 0) < 0)
308 err = snd_pcm_open(&handle, conf.device_arg,
309 SND_PCM_STREAM_PLAYBACK, 0);
312 if ((err = snd_pcm_info(handle, info)) < 0)
313 EXIT(E_SND_PCM_INFO);
314 audiobuf = malloc(WAV_HEADER_LEN);
316 play_pcm(check_wave());
317 snd_pcm_close(handle);
319 // snd_output_close(log);
320 snd_config_update_free_global();