1 | /*
|
---|
2 | * Copyright (c) 2012 Jan Vesely
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup wavplay
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file PCM playback audio devices
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <assert.h>
|
---|
37 | #include <stdatomic.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <fibril_synch.h>
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include <str_error.h>
|
---|
42 | #include <stdio.h>
|
---|
43 | #include <hound/client.h>
|
---|
44 | #include <pcm/sample_format.h>
|
---|
45 | #include <getopt.h>
|
---|
46 |
|
---|
47 | #include "dplay.h"
|
---|
48 | #include "drec.h"
|
---|
49 | #include "wave.h"
|
---|
50 |
|
---|
51 | #define READ_SIZE (32 * 1024)
|
---|
52 | #define STREAM_BUFFER_SIZE (64 * 1024)
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Play audio file using a new stream on provided context.
|
---|
56 | * @param ctx Provided context.
|
---|
57 | * @param filename File to play.
|
---|
58 | * @return Error code.
|
---|
59 | */
|
---|
60 | static errno_t hplay_ctx(hound_context_t *ctx, const char *filename)
|
---|
61 | {
|
---|
62 | printf("Hound context playback: %s\n", filename);
|
---|
63 | FILE *source = fopen(filename, "rb");
|
---|
64 | if (!source) {
|
---|
65 | printf("Failed to open file %s\n", filename);
|
---|
66 | return EINVAL;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /* Read and parse WAV header */
|
---|
70 | wave_header_t header;
|
---|
71 | size_t read = fread(&header, sizeof(header), 1, source);
|
---|
72 | if (read != 1) {
|
---|
73 | printf("Failed to read WAV header: %zu\n", read);
|
---|
74 | fclose(source);
|
---|
75 | return EIO;
|
---|
76 | }
|
---|
77 | pcm_format_t format;
|
---|
78 | const char *error;
|
---|
79 | errno_t ret = wav_parse_header(&header, NULL, NULL, &format.channels,
|
---|
80 | &format.sampling_rate, &format.sample_format, &error);
|
---|
81 | if (ret != EOK) {
|
---|
82 | printf("Error parsing `%s' wav header: %s.\n", filename, error);
|
---|
83 | fclose(source);
|
---|
84 | return EINVAL;
|
---|
85 | }
|
---|
86 |
|
---|
87 | printf("File `%s' format: %u channel(s), %uHz, %s.\n", filename,
|
---|
88 | format.channels, format.sampling_rate,
|
---|
89 | pcm_sample_format_str(format.sample_format));
|
---|
90 |
|
---|
91 | /* Allocate buffer and create new context */
|
---|
92 | char *buffer = malloc(READ_SIZE);
|
---|
93 | if (!buffer) {
|
---|
94 | fclose(source);
|
---|
95 | return ENOMEM;
|
---|
96 | }
|
---|
97 | hound_stream_t *stream = hound_stream_create(ctx,
|
---|
98 | HOUND_STREAM_DRAIN_ON_EXIT, format, STREAM_BUFFER_SIZE);
|
---|
99 |
|
---|
100 | /* Read and play */
|
---|
101 | while ((read = fread(buffer, sizeof(char), READ_SIZE, source)) > 0) {
|
---|
102 | ret = hound_stream_write(stream, buffer, read);
|
---|
103 | if (ret != EOK) {
|
---|
104 | printf("Failed to write to hound stream: %s\n",
|
---|
105 | str_error(ret));
|
---|
106 | break;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | /* Cleanup */
|
---|
111 | free(buffer);
|
---|
112 | fclose(source);
|
---|
113 | return ret;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Play audio file via hound server.
|
---|
118 | * @param filename File to play.
|
---|
119 | * @return Error code
|
---|
120 | */
|
---|
121 | static errno_t hplay(const char *filename, const char *target)
|
---|
122 | {
|
---|
123 | printf("Hound playback: %s\n", filename);
|
---|
124 | FILE *source = fopen(filename, "rb");
|
---|
125 | if (!source) {
|
---|
126 | printf("Failed to open file %s\n", filename);
|
---|
127 | return EINVAL;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /* Read and parse WAV header */
|
---|
131 | wave_header_t header;
|
---|
132 | size_t read = fread(&header, sizeof(header), 1, source);
|
---|
133 | if (read != 1) {
|
---|
134 | printf("Failed to read WAV header: %zu\n", read);
|
---|
135 | fclose(source);
|
---|
136 | return EIO;
|
---|
137 | }
|
---|
138 | pcm_format_t format;
|
---|
139 | const char *error;
|
---|
140 | errno_t ret = wav_parse_header(&header, NULL, NULL, &format.channels,
|
---|
141 | &format.sampling_rate, &format.sample_format, &error);
|
---|
142 | if (ret != EOK) {
|
---|
143 | printf("Error parsing `%s' wav header: %s.\n", filename, error);
|
---|
144 | fclose(source);
|
---|
145 | return EINVAL;
|
---|
146 | }
|
---|
147 | printf("File `%s' format: %u channel(s), %uHz, %s.\n", filename,
|
---|
148 | format.channels, format.sampling_rate,
|
---|
149 | pcm_sample_format_str(format.sample_format));
|
---|
150 |
|
---|
151 | /* Connect new playback context */
|
---|
152 | hound_context_t *hound = hound_context_create_playback(filename,
|
---|
153 | format, STREAM_BUFFER_SIZE);
|
---|
154 | if (!hound) {
|
---|
155 | printf("Failed to create HOUND context\n");
|
---|
156 | fclose(source);
|
---|
157 | return ENOMEM;
|
---|
158 | }
|
---|
159 |
|
---|
160 | ret = hound_context_connect_target(hound, target);
|
---|
161 | if (ret != EOK) {
|
---|
162 | printf("Failed to connect to target '%s': %s\n", target,
|
---|
163 | str_error(ret));
|
---|
164 |
|
---|
165 | char **names = NULL;
|
---|
166 | size_t count = 0;
|
---|
167 | ret = hound_context_get_available_targets(hound, &names, &count);
|
---|
168 | if (ret == EOK) {
|
---|
169 | printf("Available targets:\n");
|
---|
170 | for (size_t i = 0; i < count; i++)
|
---|
171 | printf(" - %s\n", names[i]);
|
---|
172 | }
|
---|
173 |
|
---|
174 | hound_context_destroy(hound);
|
---|
175 | fclose(source);
|
---|
176 | return ret;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /* Read and play */
|
---|
180 | static char buffer[READ_SIZE];
|
---|
181 | while ((read = fread(buffer, sizeof(char), READ_SIZE, source)) > 0) {
|
---|
182 | ret = hound_write_main_stream(hound, buffer, read);
|
---|
183 | if (ret != EOK) {
|
---|
184 | printf("Failed to write to main context stream: %s\n",
|
---|
185 | str_error(ret));
|
---|
186 | break;
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | /* Cleanup */
|
---|
191 | hound_context_destroy(hound);
|
---|
192 | fclose(source);
|
---|
193 | return ret;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Helper structure for playback in separate fibrils
|
---|
198 | */
|
---|
199 | typedef struct {
|
---|
200 | hound_context_t *ctx;
|
---|
201 | atomic_int *count;
|
---|
202 | const char *file;
|
---|
203 | } fib_play_t;
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Fibril playback wrapper.
|
---|
207 | * @param arg Argument, pointer to playback helper structure.
|
---|
208 | * @return Error code.
|
---|
209 | */
|
---|
210 | static errno_t play_wrapper(void *arg)
|
---|
211 | {
|
---|
212 | assert(arg);
|
---|
213 | fib_play_t *p = arg;
|
---|
214 | const errno_t ret = hplay_ctx(p->ctx, p->file);
|
---|
215 | atomic_fetch_sub(p->count, 1);
|
---|
216 | free(arg);
|
---|
217 | return ret;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Array of supported commandline options
|
---|
222 | */
|
---|
223 | static const struct option opts[] = {
|
---|
224 | { "device", required_argument, 0, 'd' },
|
---|
225 | { "parallel", no_argument, 0, 'p' },
|
---|
226 | { "record", no_argument, 0, 'r' },
|
---|
227 | { "target", required_argument, 0, 't' },
|
---|
228 | { "help", no_argument, 0, 'h' },
|
---|
229 | { 0, 0, 0, 0 }
|
---|
230 | };
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Print usage help.
|
---|
234 | * @param name Name of the program.
|
---|
235 | */
|
---|
236 | static void print_help(const char *name)
|
---|
237 | {
|
---|
238 | printf("Usage: %s [options] file [files...]\n", name);
|
---|
239 | printf("supported options:\n");
|
---|
240 | printf("\t -h, --help\t Print this help.\n");
|
---|
241 | printf("\t -r, --record\t Start recording instead of playback. "
|
---|
242 | "(Not implemented)\n");
|
---|
243 | printf("\t -d, --device\t Direct output to specified device instead of "
|
---|
244 | "the sound service. Use location path or a special device `default'\n");
|
---|
245 | printf("\t -t, --target\t Output to the specified audio target.\n");
|
---|
246 | printf("\t -p, --parallel\t Play given files in parallel instead of "
|
---|
247 | "sequentially (does not work with -d).\n");
|
---|
248 | }
|
---|
249 |
|
---|
250 | int main(int argc, char *argv[])
|
---|
251 | {
|
---|
252 | const char *device = "default";
|
---|
253 | const char *target = HOUND_DEFAULT_TARGET;
|
---|
254 | int idx = 0;
|
---|
255 | bool direct = false, record = false, parallel = false;
|
---|
256 | optind = 0;
|
---|
257 | int ret = 0;
|
---|
258 |
|
---|
259 | /* Parse command line options */
|
---|
260 | while (ret != -1) {
|
---|
261 | ret = getopt_long(argc, argv, "d:prt:h", opts, &idx);
|
---|
262 | switch (ret) {
|
---|
263 | case 'd':
|
---|
264 | direct = true;
|
---|
265 | device = optarg;
|
---|
266 | break;
|
---|
267 | case 'r':
|
---|
268 | record = true;
|
---|
269 | break;
|
---|
270 | case 'p':
|
---|
271 | parallel = true;
|
---|
272 | break;
|
---|
273 | case 't':
|
---|
274 | target = optarg;
|
---|
275 | break;
|
---|
276 | case 'h':
|
---|
277 | print_help(*argv);
|
---|
278 | return 0;
|
---|
279 | }
|
---|
280 | }
|
---|
281 |
|
---|
282 | if (parallel && direct) {
|
---|
283 | printf("Parallel playback is available only if using sound "
|
---|
284 | "server (no -d)\n");
|
---|
285 | print_help(*argv);
|
---|
286 | return 1;
|
---|
287 | }
|
---|
288 |
|
---|
289 | if (optind == argc) {
|
---|
290 | printf("Not enough arguments.\n");
|
---|
291 | print_help(*argv);
|
---|
292 | return 1;
|
---|
293 | }
|
---|
294 |
|
---|
295 | /* Init parallel playback variables */
|
---|
296 | hound_context_t *hound_ctx = NULL;
|
---|
297 | atomic_int playcount = 0;
|
---|
298 |
|
---|
299 | /* Init parallel playback context if necessary */
|
---|
300 | if (parallel) {
|
---|
301 | hound_ctx = hound_context_create_playback("wavplay",
|
---|
302 | AUDIO_FORMAT_DEFAULT, STREAM_BUFFER_SIZE);
|
---|
303 | if (!hound_ctx) {
|
---|
304 | printf("Failed to create global hound context\n");
|
---|
305 | return 1;
|
---|
306 | }
|
---|
307 | const errno_t ret = hound_context_connect_target(hound_ctx,
|
---|
308 | HOUND_DEFAULT_TARGET);
|
---|
309 | if (ret != EOK) {
|
---|
310 | printf("Failed to connect hound context to default "
|
---|
311 | "target.\n");
|
---|
312 | hound_context_destroy(hound_ctx);
|
---|
313 | return 1;
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | /* play or record all files */
|
---|
318 | for (int i = optind; i < argc; ++i) {
|
---|
319 | const char *file = argv[i];
|
---|
320 |
|
---|
321 | printf("%s (%d/%d) %s\n", record ? "Recording" : "Playing",
|
---|
322 | i - optind + 1, argc - optind, file);
|
---|
323 | if (record) {
|
---|
324 | if (direct) {
|
---|
325 | drecord(device, file);
|
---|
326 | continue;
|
---|
327 | } else {
|
---|
328 | printf("Indirect recording is not supported "
|
---|
329 | "yet.\n");
|
---|
330 | break;
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | if (direct) {
|
---|
335 | dplay(device, file);
|
---|
336 | } else {
|
---|
337 | if (parallel) {
|
---|
338 | /* Start new fibril for parallel playback */
|
---|
339 | fib_play_t *data = malloc(sizeof(fib_play_t));
|
---|
340 | if (!data) {
|
---|
341 | printf("Playback of %s failed.\n",
|
---|
342 | file);
|
---|
343 | continue;
|
---|
344 | }
|
---|
345 | data->file = file;
|
---|
346 | data->count = &playcount;
|
---|
347 | data->ctx = hound_ctx;
|
---|
348 | fid_t fid = fibril_create(play_wrapper, data);
|
---|
349 | atomic_fetch_add(&playcount, 1);
|
---|
350 | fibril_add_ready(fid);
|
---|
351 | } else {
|
---|
352 | hplay(file, target);
|
---|
353 | }
|
---|
354 | }
|
---|
355 | }
|
---|
356 |
|
---|
357 | /* Wait for all fibrils to finish */
|
---|
358 | while (atomic_load(&playcount) > 0)
|
---|
359 | fibril_usleep(1000000);
|
---|
360 |
|
---|
361 | /* Destroy parallel playback context, if initialized */
|
---|
362 | if (hound_ctx)
|
---|
363 | hound_context_destroy(hound_ctx);
|
---|
364 | return 0;
|
---|
365 | }
|
---|
366 | /**
|
---|
367 | * @}
|
---|
368 | */
|
---|