]> code.delx.au - pulseaudio/blob - src/utils/paplay.c
Updated catalan po
[pulseaudio] / src / utils / paplay.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <signal.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <assert.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <getopt.h>
35 #include <locale.h>
36
37 #include <sndfile.h>
38
39 #include <pulse/pulseaudio.h>
40 #include <pulse/i18n.h>
41
42 static pa_context *context = NULL;
43 static pa_stream *stream = NULL;
44 static pa_mainloop_api *mainloop_api = NULL;
45
46 static char *stream_name = NULL, *client_name = NULL, *device = NULL;
47
48 static int verbose = 0;
49 static pa_volume_t volume = PA_VOLUME_NORM;
50
51 static SNDFILE* sndfile = NULL;
52
53 static pa_sample_spec sample_spec = { 0, 0, 0 };
54 static pa_channel_map channel_map;
55 static int channel_map_set = 0;
56
57 static sf_count_t (*readf_function)(SNDFILE *_sndfile, void *ptr, sf_count_t frames) = NULL;
58
59 /* A shortcut for terminating the application */
60 static void quit(int ret) {
61 assert(mainloop_api);
62 mainloop_api->quit(mainloop_api, ret);
63 }
64
65 /* Connection draining complete */
66 static void context_drain_complete(pa_context*c, void *userdata) {
67 pa_context_disconnect(c);
68 }
69
70 /* Stream draining complete */
71 static void stream_drain_complete(pa_stream*s, int success, void *userdata) {
72 pa_operation *o;
73
74 if (!success) {
75 fprintf(stderr, _("Failed to drain stream: %s\n"), pa_strerror(pa_context_errno(context)));
76 quit(1);
77 }
78
79 if (verbose)
80 fprintf(stderr, _("Playback stream drained.\n"));
81
82 pa_stream_disconnect(stream);
83 pa_stream_unref(stream);
84 stream = NULL;
85
86 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
87 pa_context_disconnect(context);
88 else {
89 pa_operation_unref(o);
90
91 if (verbose)
92 fprintf(stderr, _("Draining connection to server.\n"));
93 }
94 }
95
96 /* This is called whenever new data may be written to the stream */
97 static void stream_write_callback(pa_stream *s, size_t length, void *userdata) {
98 sf_count_t bytes;
99 void *data;
100 assert(s && length);
101
102 if (!sndfile)
103 return;
104
105 data = pa_xmalloc(length);
106
107 if (readf_function) {
108 size_t k = pa_frame_size(&sample_spec);
109
110 if ((bytes = readf_function(sndfile, data, (sf_count_t) (length/k))) > 0)
111 bytes *= (sf_count_t) k;
112
113 } else
114 bytes = sf_read_raw(sndfile, data, (sf_count_t) length);
115
116 if (bytes > 0)
117 pa_stream_write(s, data, (size_t) bytes, pa_xfree, 0, PA_SEEK_RELATIVE);
118 else
119 pa_xfree(data);
120
121 if (bytes < (sf_count_t) length) {
122 sf_close(sndfile);
123 sndfile = NULL;
124 pa_operation_unref(pa_stream_drain(s, stream_drain_complete, NULL));
125 }
126 }
127
128 /* This routine is called whenever the stream state changes */
129 static void stream_state_callback(pa_stream *s, void *userdata) {
130 assert(s);
131
132 switch (pa_stream_get_state(s)) {
133 case PA_STREAM_CREATING:
134 case PA_STREAM_TERMINATED:
135 break;
136
137 case PA_STREAM_READY:
138 if (verbose)
139 fprintf(stderr, _("Stream successfully created\n"));
140 break;
141
142 case PA_STREAM_FAILED:
143 default:
144 fprintf(stderr, _("Stream errror: %s\n"), pa_strerror(pa_context_errno(pa_stream_get_context(s))));
145 quit(1);
146 }
147 }
148
149 /* This is called whenever the context status changes */
150 static void context_state_callback(pa_context *c, void *userdata) {
151 assert(c);
152
153 switch (pa_context_get_state(c)) {
154 case PA_CONTEXT_CONNECTING:
155 case PA_CONTEXT_AUTHORIZING:
156 case PA_CONTEXT_SETTING_NAME:
157 break;
158
159 case PA_CONTEXT_READY: {
160 pa_cvolume cv;
161
162 assert(c && !stream);
163
164 if (verbose)
165 fprintf(stderr, _("Connection established.\n"));
166
167 stream = pa_stream_new(c, stream_name, &sample_spec, channel_map_set ? &channel_map : NULL);
168 assert(stream);
169
170 pa_stream_set_state_callback(stream, stream_state_callback, NULL);
171 pa_stream_set_write_callback(stream, stream_write_callback, NULL);
172 pa_stream_connect_playback(stream, device, NULL, 0, pa_cvolume_set(&cv, sample_spec.channels, volume), NULL);
173
174 break;
175 }
176
177 case PA_CONTEXT_TERMINATED:
178 quit(0);
179 break;
180
181 case PA_CONTEXT_FAILED:
182 default:
183 fprintf(stderr, _("Connection failure: %s\n"), pa_strerror(pa_context_errno(c)));
184 quit(1);
185 }
186 }
187
188 /* UNIX signal to quit recieved */
189 static void exit_signal_callback(pa_mainloop_api*m, pa_signal_event *e, int sig, void *userdata) {
190 if (verbose)
191 fprintf(stderr, _("Got SIGINT, exiting.\n"));
192 quit(0);
193
194 }
195
196 static void help(const char *argv0) {
197
198 printf(_("%s [options] [FILE]\n\n"
199 " -h, --help Show this help\n"
200 " --version Show version\n\n"
201 " -v, --verbose Enable verbose operation\n\n"
202 " -s, --server=SERVER The name of the server to connect to\n"
203 " -d, --device=DEVICE The name of the sink to connect to\n"
204 " -n, --client-name=NAME How to call this client on the server\n"
205 " --stream-name=NAME How to call this stream on the server\n"
206 " --volume=VOLUME Specify the initial (linear) volume in range 0...65536\n"
207 " --channel-map=CHANNELMAP Set the channel map to the use\n"),
208 argv0);
209 }
210
211 enum {
212 ARG_VERSION = 256,
213 ARG_STREAM_NAME,
214 ARG_VOLUME,
215 ARG_CHANNELMAP
216 };
217
218 int main(int argc, char *argv[]) {
219 pa_mainloop* m = NULL;
220 int ret = 1, r, c;
221 char *bn, *server = NULL;
222 const char *filename;
223 SF_INFO sfinfo;
224
225 static const struct option long_options[] = {
226 {"device", 1, NULL, 'd'},
227 {"server", 1, NULL, 's'},
228 {"client-name", 1, NULL, 'n'},
229 {"stream-name", 1, NULL, ARG_STREAM_NAME},
230 {"version", 0, NULL, ARG_VERSION},
231 {"help", 0, NULL, 'h'},
232 {"verbose", 0, NULL, 'v'},
233 {"volume", 1, NULL, ARG_VOLUME},
234 {"channel-map", 1, NULL, ARG_CHANNELMAP},
235 {NULL, 0, NULL, 0}
236 };
237
238 setlocale(LC_ALL, "");
239 bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
240
241 if (!(bn = strrchr(argv[0], '/')))
242 bn = argv[0];
243 else
244 bn++;
245
246 while ((c = getopt_long(argc, argv, "d:s:n:hv", long_options, NULL)) != -1) {
247
248 switch (c) {
249 case 'h' :
250 help(bn);
251 ret = 0;
252 goto quit;
253
254 case ARG_VERSION:
255 printf(_("paplay %s\nCompiled with libpulse %s\n"
256 "Linked with libpulse %s\n"), PACKAGE_VERSION, pa_get_headers_version(), pa_get_library_version());
257 ret = 0;
258 goto quit;
259
260 case 'd':
261 pa_xfree(device);
262 device = pa_xstrdup(optarg);
263 break;
264
265 case 's':
266 pa_xfree(server);
267 server = pa_xstrdup(optarg);
268 break;
269
270 case 'n':
271 pa_xfree(client_name);
272 client_name = pa_xstrdup(optarg);
273 break;
274
275 case ARG_STREAM_NAME:
276 pa_xfree(stream_name);
277 stream_name = pa_xstrdup(optarg);
278 break;
279
280 case 'v':
281 verbose = 1;
282 break;
283
284 case ARG_VOLUME: {
285 int v = atoi(optarg);
286 volume = v < 0 ? 0U : (pa_volume_t) v;
287 break;
288 }
289
290 case ARG_CHANNELMAP:
291 if (!pa_channel_map_parse(&channel_map, optarg)) {
292 fprintf(stderr, _("Invalid channel map\n"));
293 goto quit;
294 }
295
296 channel_map_set = 1;
297 break;
298
299 default:
300 goto quit;
301 }
302 }
303
304 filename = optind < argc ? argv[optind] : "STDIN";
305
306 memset(&sfinfo, 0, sizeof(sfinfo));
307
308 if (optind < argc)
309 sndfile = sf_open(filename, SFM_READ, &sfinfo);
310 else
311 sndfile = sf_open_fd(STDIN_FILENO, SFM_READ, &sfinfo, 0);
312
313 if (!sndfile) {
314 fprintf(stderr, _("Failed to open file '%s'\n"), filename);
315 goto quit;
316 }
317
318 sample_spec.rate = (uint32_t) sfinfo.samplerate;
319 sample_spec.channels = (uint8_t) sfinfo.channels;
320
321 readf_function = NULL;
322
323 switch (sfinfo.format & 0xFF) {
324 case SF_FORMAT_PCM_16:
325 case SF_FORMAT_PCM_U8:
326 case SF_FORMAT_PCM_S8:
327 sample_spec.format = PA_SAMPLE_S16NE;
328 readf_function = (sf_count_t (*)(SNDFILE *_sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
329 break;
330
331 case SF_FORMAT_ULAW:
332 sample_spec.format = PA_SAMPLE_ULAW;
333 break;
334
335 case SF_FORMAT_ALAW:
336 sample_spec.format = PA_SAMPLE_ALAW;
337 break;
338
339 case SF_FORMAT_FLOAT:
340 case SF_FORMAT_DOUBLE:
341 default:
342 sample_spec.format = PA_SAMPLE_FLOAT32NE;
343 readf_function = (sf_count_t (*)(SNDFILE *_sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
344 break;
345 }
346
347 assert(pa_sample_spec_valid(&sample_spec));
348
349 if (channel_map_set && channel_map.channels != sample_spec.channels) {
350 fprintf(stderr, _("Channel map doesn't match file.\n"));
351 goto quit;
352 }
353
354 if (!client_name) {
355 client_name = pa_locale_to_utf8(bn);
356 if (!client_name)
357 client_name = pa_utf8_filter(bn);
358 }
359
360 if (!stream_name) {
361 const char *n;
362
363 n = sf_get_string(sndfile, SF_STR_TITLE);
364
365 if (!n)
366 n = filename;
367
368 stream_name = pa_locale_to_utf8(n);
369 if (!stream_name)
370 stream_name = pa_utf8_filter(n);
371 }
372
373 if (verbose) {
374 char t[PA_SAMPLE_SPEC_SNPRINT_MAX];
375 pa_sample_spec_snprint(t, sizeof(t), &sample_spec);
376 fprintf(stderr, _("Using sample spec '%s'\n"), t);
377 }
378
379 /* Set up a new main loop */
380 if (!(m = pa_mainloop_new())) {
381 fprintf(stderr, _("pa_mainloop_new() failed.\n"));
382 goto quit;
383 }
384
385 mainloop_api = pa_mainloop_get_api(m);
386
387 r = pa_signal_init(mainloop_api);
388 assert(r == 0);
389 pa_signal_new(SIGINT, exit_signal_callback, NULL);
390 #ifdef SIGPIPE
391 signal(SIGPIPE, SIG_IGN);
392 #endif
393
394 /* Create a new connection context */
395 if (!(context = pa_context_new(mainloop_api, client_name))) {
396 fprintf(stderr, _("pa_context_new() failed.\n"));
397 goto quit;
398 }
399
400 pa_context_set_state_callback(context, context_state_callback, NULL);
401
402 /* Connect the context */
403 if (pa_context_connect(context, server, 0, NULL) < 0) {
404 fprintf(stderr, _("pa_context_connect() failed: %s"), pa_strerror(pa_context_errno(context)));
405 goto quit;
406 }
407
408 /* Run the main loop */
409 if (pa_mainloop_run(m, &ret) < 0) {
410 fprintf(stderr, _("pa_mainloop_run() failed.\n"));
411 goto quit;
412 }
413
414 quit:
415 if (stream)
416 pa_stream_unref(stream);
417
418 if (context)
419 pa_context_unref(context);
420
421 if (m) {
422 pa_signal_done();
423 pa_mainloop_free(m);
424 }
425
426 pa_xfree(server);
427 pa_xfree(device);
428 pa_xfree(client_name);
429 pa_xfree(stream_name);
430
431 if (sndfile)
432 sf_close(sndfile);
433
434 return ret;
435 }