4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <asoundlib.h>
38 #include "sample-util.h"
39 #include "alsa-util.h"
42 snd_pcm_t
*pcm_handle
;
45 unsigned n_io_sources
;
47 size_t frame_size
, fragment_size
;
48 struct pa_memchunk memchunk
, silence
;
51 static const char* const valid_modargs
[] = {
62 #define DEFAULT_SINK_NAME "alsa_output"
63 #define DEFAULT_DEVICE "plughw:0,0"
65 static void xrun_recovery(struct userdata
*u
) {
68 fprintf(stderr
, "*** ALSA-XRUN (playback) ***\n");
70 if (snd_pcm_prepare(u
->pcm_handle
) < 0)
71 fprintf(stderr
, "snd_pcm_prepare() failed\n");
74 static void do_write(struct userdata
*u
) {
78 struct pa_memchunk
*memchunk
= NULL
;
79 snd_pcm_sframes_t frames
;
81 if (u
->memchunk
.memblock
)
82 memchunk
= &u
->memchunk
;
84 if (pa_sink_render(u
->sink
, u
->fragment_size
, &u
->memchunk
) < 0)
85 memchunk
= &u
->silence
;
87 memchunk
= &u
->memchunk
;
90 assert(memchunk
->memblock
&& memchunk
->memblock
->data
&& memchunk
->length
&& memchunk
->memblock
->length
&& (memchunk
->length
% u
->frame_size
) == 0);
92 if ((frames
= snd_pcm_writei(u
->pcm_handle
, memchunk
->memblock
->data
+ memchunk
->index
, memchunk
->length
/ u
->frame_size
)) < 0) {
93 if (frames
== -EAGAIN
)
96 if (frames
== -EPIPE
) {
101 fprintf(stderr
, "snd_pcm_writei() failed\n");
105 if (memchunk
== &u
->memchunk
) {
106 size_t l
= frames
* u
->frame_size
;
107 memchunk
->index
+= l
;
108 memchunk
->length
-= l
;
110 if (memchunk
->length
== 0) {
111 pa_memblock_unref(memchunk
->memblock
);
112 memchunk
->memblock
= NULL
;
113 memchunk
->index
= memchunk
->length
= 0;
121 static void io_callback(struct pa_mainloop_api
*a
, void *id
, int fd
, enum pa_mainloop_api_io_events events
, void *userdata
) {
122 struct userdata
*u
= userdata
;
123 assert(u
&& a
&& id
);
125 if (snd_pcm_state(u
->pcm_handle
) == SND_PCM_STATE_XRUN
)
131 static uint32_t sink_get_latency_cb(struct pa_sink
*s
) {
132 struct userdata
*u
= s
->userdata
;
133 snd_pcm_sframes_t frames
;
134 assert(s
&& u
&& u
->sink
);
136 if (snd_pcm_delay(u
->pcm_handle
, &frames
) < 0) {
137 fprintf(stderr
, __FILE__
": failed to get delay\n");
138 s
->get_latency
= NULL
;
145 return pa_samples_usec(frames
* u
->frame_size
, &s
->sample_spec
);
148 int pa_module_init(struct pa_core
*c
, struct pa_module
*m
) {
149 struct pa_modargs
*ma
= NULL
;
151 struct userdata
*u
= NULL
;
153 struct pa_sample_spec ss
;
154 unsigned periods
, fragsize
;
155 snd_pcm_uframes_t buffer_size
;
158 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
159 fprintf(stderr
, __FILE__
": failed to parse module arguments\n");
163 ss
= c
->default_sample_spec
;
164 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0) {
165 fprintf(stderr
, __FILE__
": failed to parse sample specification\n");
168 frame_size
= pa_sample_size(&ss
);
172 if (pa_modargs_get_value_u32(ma
, "fragments", &periods
) < 0 || pa_modargs_get_value_u32(ma
, "fragment_size", &fragsize
) < 0) {
173 fprintf(stderr
, __FILE__
": failed to parse buffer metrics\n");
176 buffer_size
= fragsize
/frame_size
*periods
;
178 u
= malloc(sizeof(struct userdata
));
180 memset(u
, 0, sizeof(struct userdata
));
183 if (snd_pcm_open(&u
->pcm_handle
, dev
= pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
), SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
) < 0) {
184 fprintf(stderr
, __FILE__
": Error opening PCM device %s\n", dev
);
188 if (pa_alsa_set_hw_params(u
->pcm_handle
, &ss
, &periods
, &buffer_size
) < 0) {
189 fprintf(stderr
, __FILE__
": Failed to set hardware parameters\n");
193 u
->sink
= pa_sink_new(c
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
);
196 u
->sink
->get_latency
= sink_get_latency_cb
;
197 u
->sink
->userdata
= u
;
198 pa_sink_set_owner(u
->sink
, m
);
199 u
->sink
->description
= pa_sprintf_malloc("Advanced Linux Sound Architecture PCM on '%s'", dev
);
201 if (pa_create_io_sources(u
->pcm_handle
, c
->mainloop
, &u
->io_sources
, &u
->n_io_sources
, io_callback
, u
) < 0) {
202 fprintf(stderr
, __FILE__
": failed to obtain file descriptors\n");
206 u
->frame_size
= frame_size
;
207 u
->fragment_size
= buffer_size
*u
->frame_size
/periods
;
209 fprintf(stderr
, __FILE__
": using %u fragments of size %u bytes.\n", periods
, u
->fragment_size
);
211 u
->silence
.memblock
= pa_memblock_new(u
->silence
.length
= u
->fragment_size
);
212 assert(u
->silence
.memblock
);
213 pa_silence_memblock(u
->silence
.memblock
, &ss
);
214 u
->silence
.index
= 0;
216 u
->memchunk
.memblock
= NULL
;
217 u
->memchunk
.index
= u
->memchunk
.length
= 0;
230 pa_module_done(c
, m
);
235 void pa_module_done(struct pa_core
*c
, struct pa_module
*m
) {
239 if ((u
= m
->userdata
)) {
241 pa_sink_free(u
->sink
);
244 pa_free_io_sources(c
->mainloop
, u
->io_sources
, u
->n_io_sources
);
247 snd_pcm_drop(u
->pcm_handle
);
248 snd_pcm_close(u
->pcm_handle
);
251 if (u
->memchunk
.memblock
)
252 pa_memblock_unref(u
->memchunk
.memblock
);
253 if (u
->silence
.memblock
)
254 pa_memblock_unref(u
->silence
.memblock
);