]> git.tuebingen.mpg.de Git - paraslash.git/blob - alsa_writer.c
039c9817a99f13caa5980247e1d481615755680b
[paraslash.git] / alsa_writer.c
1 /*
2  * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 /** \file alsa_writer.c paraslash's alsa output plugin */
20
21 /*
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.
25  */
26
27 #include "para.h"
28 #include "fd.h"
29 #include "string.h"
30 #include "write.h"
31
32 #include <alsa/asoundlib.h>
33
34 #include "write.cmdline.h"
35 #include "error.h"
36
37 extern struct gengetopt_args_info conf;
38
39 #define FORMAT SND_PCM_FORMAT_S16_LE
40
41 /** data specific to the alsa writer */
42 struct private_alsa_data {
43         snd_pcm_t *handle;
44         size_t bytes_per_frame;
45 };
46
47 /*
48  * open and prepare the PCM handle for writing
49  *
50  * Install PCM software and hardware configuration. Exit on errors.
51  */
52 static int alsa_open(struct writer_node *w)
53 {
54         snd_pcm_hw_params_t *hwparams;
55         snd_pcm_sw_params_t *swparams;
56         snd_pcm_uframes_t buffer_size, xfer_align, start_threshold,
57                 stop_threshold;
58         unsigned buffer_time = 0;
59         int err;
60         snd_pcm_info_t *info;
61         snd_output_t *log;
62         snd_pcm_uframes_t period_size;
63         struct private_alsa_data *pad = para_malloc(sizeof(struct private_alsa_data));
64         w->private_data = pad;
65
66         snd_pcm_info_alloca(&info);
67         if (snd_output_stdio_attach(&log, stderr, 0) < 0)
68                 return -E_ALSA_LOG;
69         err = snd_pcm_open(&pad->handle, conf.device_arg,
70                 SND_PCM_STREAM_PLAYBACK, 0);
71         if (err < 0)
72                 return -E_PCM_OPEN;
73         if ((err = snd_pcm_info(pad->handle, info)) < 0)
74                 return -E_SND_PCM_INFO;
75
76         snd_pcm_hw_params_alloca(&hwparams);
77         snd_pcm_sw_params_alloca(&swparams);
78         if (snd_pcm_hw_params_any(pad->handle, hwparams) < 0)
79                 return -E_BROKEN_CONF;
80         if (snd_pcm_hw_params_set_access(pad->handle, hwparams,
81                         SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
82                 return -E_ACCESS_TYPE;
83         if (snd_pcm_hw_params_set_format(pad->handle, hwparams, FORMAT) < 0)
84                 return -E_SAMPLE_FORMAT;
85         if (snd_pcm_hw_params_set_channels(pad->handle, hwparams,
86                         conf.channels_arg) < 0)
87                 return -E_CHANNEL_COUNT;
88         if (snd_pcm_hw_params_set_rate_near(pad->handle, hwparams,
89                         (unsigned int*) &conf.sample_rate_arg, 0) < 0)
90                 return -E_SET_RATE;
91         err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &buffer_time, 0);
92         if (err < 0 || !buffer_time)
93                 return -E_GET_BUFFER_TIME;
94         PARA_DEBUG_LOG("buffer time: %d\n", buffer_time);
95         if (snd_pcm_hw_params_set_buffer_time_near(pad->handle, hwparams,
96                         &buffer_time, 0) < 0)
97                 return -E_SET_BUFFER_TIME;
98         if (snd_pcm_hw_params(pad->handle, hwparams) < 0)
99                 return -E_HW_PARAMS;
100         snd_pcm_hw_params_get_period_size(hwparams, &period_size, 0);
101         snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
102         PARA_DEBUG_LOG("buffer size: %lu, period_size: %lu\n", buffer_size,
103                 period_size);
104         if (period_size == buffer_size)
105                 return -E_BAD_PERIOD;
106         snd_pcm_sw_params_current(pad->handle, swparams);
107         err = snd_pcm_sw_params_get_xfer_align(swparams, &xfer_align);
108         if (err < 0 || !xfer_align)
109                 return -E_GET_XFER;
110         snd_pcm_sw_params_set_avail_min(pad->handle, swparams, period_size);
111         /* round to closest transfer boundary */
112         start_threshold = (buffer_size / xfer_align) * xfer_align;
113         if (start_threshold < 1)
114                 start_threshold = 1;
115         if (snd_pcm_sw_params_set_start_threshold(pad->handle, swparams,
116                         start_threshold) < 0)
117                 return -E_START_THRESHOLD;
118         stop_threshold = buffer_size;
119         if (snd_pcm_sw_params_set_stop_threshold(pad->handle, swparams,
120                         stop_threshold) < 0)
121                 return -E_STOP_THRESHOLD;
122         if (snd_pcm_sw_params_set_xfer_align(pad->handle, swparams,
123                         xfer_align) < 0)
124                 return -E_SET_XFER;
125         if (snd_pcm_sw_params(pad->handle, swparams) < 0)
126                 return -E_SW_PARAMS;
127         pad->bytes_per_frame = snd_pcm_format_physical_width(FORMAT)
128                 * conf.channels_arg / 8;
129         return period_size * pad->bytes_per_frame;
130 }
131
132 /**
133  * push out pcm frames
134  * \param data pointer do data to be written
135  * \param nbytes number of bytes (not frames)
136  *
137  * \return Number of bytes written, -E_ALSA_WRITE on errors.
138  */
139 static int alsa_write(char *data, size_t nbytes, struct writer_node *wn)
140 {
141         struct private_alsa_data *pad = wn->private_data;
142         size_t frames = nbytes / pad->bytes_per_frame;
143         unsigned char *d = (unsigned char*)data;
144         snd_pcm_sframes_t r, result = 0;
145
146         while (frames > 0) {
147                 /* write interleaved frames */
148                 r = snd_pcm_writei(pad->handle, d, frames);
149                 if (r < 0)
150                         PARA_ERROR_LOG("write error: %s\n", snd_strerror(r));
151                 if (r == -EAGAIN || (r >= 0 && r < frames))
152                         snd_pcm_wait(pad->handle, 1);
153                 else if (r == -EPIPE)
154                         snd_pcm_prepare(pad->handle);
155                 else if (r < 0)
156                         return -E_ALSA_WRITE;
157                 if (r > 0) {
158                         result += r;
159                         frames -= r;
160                         d += r * pad->bytes_per_frame;
161                 }
162         }
163         return result * pad->bytes_per_frame;
164 }
165
166 static void alsa_close(struct writer_node *wn)
167 {
168         struct private_alsa_data *pad = wn->private_data;
169         snd_pcm_drain(pad->handle);
170         snd_pcm_close(pad->handle);
171         snd_config_update_free_global();
172         free(pad);
173 }
174
175 void alsa_writer_init(struct writer *w)
176 {
177         w->open = alsa_open;
178         w->write = alsa_write;
179         w->close = alsa_close;
180         w->shutdown = NULL; /* nothing to do */
181 }