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 <errno.h>
|
---|
38 | #include <str_error.h>
|
---|
39 | #include <str.h>
|
---|
40 | #include <audio_pcm_iface.h>
|
---|
41 | #include <fibril_synch.h>
|
---|
42 | #include <pcm/format.h>
|
---|
43 | #include <as.h>
|
---|
44 | #include <time.h>
|
---|
45 | #include <inttypes.h>
|
---|
46 | #include <stdbool.h>
|
---|
47 | #include <stdio.h>
|
---|
48 | #include <macros.h>
|
---|
49 |
|
---|
50 | #include "wave.h"
|
---|
51 | #include "dplay.h"
|
---|
52 |
|
---|
53 | #define DEFAULT_FRAGMENTS 2
|
---|
54 |
|
---|
55 | /** Playback helper structure */
|
---|
56 | typedef struct {
|
---|
57 | struct {
|
---|
58 | void *base;
|
---|
59 | size_t size;
|
---|
60 | void *write_ptr;
|
---|
61 | } buffer;
|
---|
62 | pcm_format_t f;
|
---|
63 | FILE *source;
|
---|
64 | volatile bool playing;
|
---|
65 | fibril_mutex_t mutex;
|
---|
66 | fibril_condvar_t cv;
|
---|
67 | audio_pcm_sess_t *device;
|
---|
68 | } playback_t;
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Initialize playback helper structure.
|
---|
72 | * @param pb Pointer to helper structure to initialize
|
---|
73 | * @param sess Pointer to audio device IPC session
|
---|
74 | * @return
|
---|
75 | */
|
---|
76 | static void playback_initialize(playback_t *pb, audio_pcm_sess_t *sess)
|
---|
77 | {
|
---|
78 | assert(sess);
|
---|
79 | assert(pb);
|
---|
80 | pb->buffer.base = NULL;
|
---|
81 | pb->buffer.size = 0;
|
---|
82 | pb->buffer.write_ptr = NULL;
|
---|
83 | pb->playing = false;
|
---|
84 | pb->source = NULL;
|
---|
85 | pb->device = sess;
|
---|
86 | fibril_mutex_initialize(&pb->mutex);
|
---|
87 | fibril_condvar_initialize(&pb->cv);
|
---|
88 | }
|
---|
89 |
|
---|
90 | /** Fragment playback callback function.
|
---|
91 | *
|
---|
92 | * @param icall Pointer to the call structure
|
---|
93 | * @param arg Argument, pointer to the playback helper function
|
---|
94 | *
|
---|
95 | */
|
---|
96 | static void device_event_callback(ipc_call_t *icall, void *arg)
|
---|
97 | {
|
---|
98 | playback_t *pb = arg;
|
---|
99 | const size_t fragment_size = pb->buffer.size / DEFAULT_FRAGMENTS;
|
---|
100 |
|
---|
101 | while (true) {
|
---|
102 | ipc_call_t call;
|
---|
103 | async_get_call(&call);
|
---|
104 |
|
---|
105 | switch (ipc_get_imethod(&call)) {
|
---|
106 | case PCM_EVENT_PLAYBACK_STARTED:
|
---|
107 | case PCM_EVENT_FRAMES_PLAYED:
|
---|
108 | printf("%" PRIun " frames: ", ipc_get_arg1(&call));
|
---|
109 | async_answer_0(&call, EOK);
|
---|
110 | break;
|
---|
111 | case PCM_EVENT_PLAYBACK_TERMINATED:
|
---|
112 | printf("Playback terminated\n");
|
---|
113 | fibril_mutex_lock(&pb->mutex);
|
---|
114 | pb->playing = false;
|
---|
115 | fibril_condvar_signal(&pb->cv);
|
---|
116 | async_answer_0(&call, EOK);
|
---|
117 | fibril_mutex_unlock(&pb->mutex);
|
---|
118 | return;
|
---|
119 | default:
|
---|
120 | printf("Unknown event %" PRIun ".\n", ipc_get_imethod(&call));
|
---|
121 | async_answer_0(&call, ENOTSUP);
|
---|
122 | continue;
|
---|
123 |
|
---|
124 | }
|
---|
125 | const size_t bytes = fread(pb->buffer.write_ptr,
|
---|
126 | sizeof(uint8_t), fragment_size, pb->source);
|
---|
127 | printf("Copied from position %p size %zu/%zu\n",
|
---|
128 | pb->buffer.write_ptr, bytes, fragment_size);
|
---|
129 | if (bytes == 0) {
|
---|
130 | audio_pcm_last_playback_fragment(pb->device);
|
---|
131 | }
|
---|
132 | /* any constant is silence */
|
---|
133 | memset(pb->buffer.write_ptr + bytes, 0, fragment_size - bytes);
|
---|
134 | pb->buffer.write_ptr += fragment_size;
|
---|
135 |
|
---|
136 | if (pb->buffer.write_ptr >= (pb->buffer.base + pb->buffer.size))
|
---|
137 | pb->buffer.write_ptr -= pb->buffer.size;
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Start event based playback.
|
---|
143 | * @param pb Playback helper structure.
|
---|
144 | */
|
---|
145 | static void play_fragment(playback_t *pb)
|
---|
146 | {
|
---|
147 | assert(pb);
|
---|
148 | assert(pb->device);
|
---|
149 | const size_t fragment_size = pb->buffer.size / DEFAULT_FRAGMENTS;
|
---|
150 | printf("Registering event callback\n");
|
---|
151 | errno_t ret = audio_pcm_register_event_callback(pb->device,
|
---|
152 | device_event_callback, pb);
|
---|
153 | if (ret != EOK) {
|
---|
154 | printf("Failed to register event callback: %s.\n",
|
---|
155 | str_error(ret));
|
---|
156 | return;
|
---|
157 | }
|
---|
158 | printf("Playing: %dHz, %s, %d channel(s).\n", pb->f.sampling_rate,
|
---|
159 | pcm_sample_format_str(pb->f.sample_format), pb->f.channels);
|
---|
160 | const size_t bytes = fread(pb->buffer.base, sizeof(uint8_t),
|
---|
161 | fragment_size, pb->source);
|
---|
162 | if (bytes != fragment_size)
|
---|
163 | memset(pb->buffer.base + bytes, 0, fragment_size - bytes);
|
---|
164 | printf("Initial: Copied from position %p size %zu/%zu\n",
|
---|
165 | pb->buffer.base, bytes, fragment_size);
|
---|
166 | pb->buffer.write_ptr = pb->buffer.base + fragment_size;
|
---|
167 | fibril_mutex_lock(&pb->mutex);
|
---|
168 | const unsigned frames =
|
---|
169 | pcm_format_size_to_frames(fragment_size, &pb->f);
|
---|
170 | ret = audio_pcm_start_playback_fragment(pb->device, frames,
|
---|
171 | pb->f.channels, pb->f.sampling_rate, pb->f.sample_format);
|
---|
172 | if (ret != EOK) {
|
---|
173 | fibril_mutex_unlock(&pb->mutex);
|
---|
174 | printf("Failed to start playback: %s.\n", str_error(ret));
|
---|
175 | audio_pcm_unregister_event_callback(pb->device);
|
---|
176 | return;
|
---|
177 | }
|
---|
178 |
|
---|
179 | pb->playing = true;
|
---|
180 | while (pb->playing)
|
---|
181 | fibril_condvar_wait(&pb->cv, &pb->mutex);
|
---|
182 |
|
---|
183 | fibril_mutex_unlock(&pb->mutex);
|
---|
184 | printf("\n");
|
---|
185 | audio_pcm_unregister_event_callback(pb->device);
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Count occupied space in a cyclic buffer.
|
---|
190 | * @param pb Playback helper structure.
|
---|
191 | * @param pos read pointer position.
|
---|
192 | * @return Occupied space size.
|
---|
193 | */
|
---|
194 | static size_t buffer_occupied(const playback_t *pb, size_t pos)
|
---|
195 | {
|
---|
196 | assert(pb);
|
---|
197 | void *read_ptr = pb->buffer.base + pos;
|
---|
198 | if (read_ptr > pb->buffer.write_ptr)
|
---|
199 | return pb->buffer.write_ptr + pb->buffer.size - read_ptr;
|
---|
200 | return pb->buffer.write_ptr - read_ptr;
|
---|
201 |
|
---|
202 | }
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Count available space in a cyclic buffer.
|
---|
206 | * @param pb Playback helper structure.
|
---|
207 | * @param pos read pointer position.
|
---|
208 | * @return Free space size.
|
---|
209 | */
|
---|
210 | static size_t buffer_avail(const playback_t *pb, size_t pos)
|
---|
211 | {
|
---|
212 | assert(pb);
|
---|
213 | void *read_ptr = pb->buffer.base + pos;
|
---|
214 | if (read_ptr <= pb->buffer.write_ptr)
|
---|
215 | return read_ptr + pb->buffer.size - pb->buffer.write_ptr - 1;
|
---|
216 | return (read_ptr - pb->buffer.write_ptr) - 1;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Size of the space between write pointer and the end of a cyclic buffer
|
---|
221 | * @param pb Playback helper structure.
|
---|
222 | */
|
---|
223 | static size_t buffer_remain(const playback_t *pb)
|
---|
224 | {
|
---|
225 | assert(pb);
|
---|
226 | return (pb->buffer.base + pb->buffer.size) - pb->buffer.write_ptr;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Move write pointer forward. Wrap around the end.
|
---|
231 | * @param pb Playback helper structure.
|
---|
232 | * @param bytes NUmber of bytes to advance.
|
---|
233 | */
|
---|
234 | static void buffer_advance(playback_t *pb, size_t bytes)
|
---|
235 | {
|
---|
236 | assert(pb);
|
---|
237 | pb->buffer.write_ptr += bytes;
|
---|
238 | while (pb->buffer.write_ptr >= (pb->buffer.base + pb->buffer.size))
|
---|
239 | pb->buffer.write_ptr -= pb->buffer.size;
|
---|
240 | }
|
---|
241 |
|
---|
242 | #define DPRINTF(f, ...) \
|
---|
243 | printf("%.2lld:%.6lld "f, time.tv_sec % 100, \
|
---|
244 | NSEC2USEC(time.tv_nsec), __VA_ARGS__)
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Start playback using buffer position api.
|
---|
248 | * @param pb Playback helper function.
|
---|
249 | */
|
---|
250 | static void play(playback_t *pb)
|
---|
251 | {
|
---|
252 | assert(pb);
|
---|
253 | assert(pb->device);
|
---|
254 | pb->buffer.write_ptr = pb->buffer.base;
|
---|
255 | printf("Playing: %dHz, %s, %d channel(s).\n", pb->f.sampling_rate,
|
---|
256 | pcm_sample_format_str(pb->f.sample_format), pb->f.channels);
|
---|
257 | usec_t work_time = 50000; /* 50 ms */
|
---|
258 | bool started = false;
|
---|
259 | size_t pos = 0;
|
---|
260 | struct timespec time = { 0 };
|
---|
261 | getuptime(&time);
|
---|
262 | while (true) {
|
---|
263 | size_t available = buffer_avail(pb, pos);
|
---|
264 | /*
|
---|
265 | * Writing might need wrap around the end,
|
---|
266 | * read directly to device buffer
|
---|
267 | */
|
---|
268 | size_t bytes = fread(pb->buffer.write_ptr, sizeof(uint8_t),
|
---|
269 | min(available, buffer_remain(pb)), pb->source);
|
---|
270 | buffer_advance(pb, bytes);
|
---|
271 | DPRINTF("POS %zu: %zu bytes free in buffer, read %zu, wp %zu\n",
|
---|
272 | pos, available, bytes,
|
---|
273 | pb->buffer.write_ptr - pb->buffer.base);
|
---|
274 | available -= bytes;
|
---|
275 |
|
---|
276 | /* continue if we wrapped around the end */
|
---|
277 | if (available) {
|
---|
278 | bytes = fread(pb->buffer.write_ptr,
|
---|
279 | sizeof(uint8_t), min(available, buffer_remain(pb)),
|
---|
280 | pb->source);
|
---|
281 | buffer_advance(pb, bytes);
|
---|
282 | DPRINTF("POS %zu: %zu bytes still free in buffer, "
|
---|
283 | "read %zu, wp %zu\n", pos, available, bytes,
|
---|
284 | pb->buffer.write_ptr - pb->buffer.base);
|
---|
285 | available -= bytes;
|
---|
286 | }
|
---|
287 |
|
---|
288 | if (!started) {
|
---|
289 | errno_t ret = audio_pcm_start_playback(pb->device,
|
---|
290 | pb->f.channels, pb->f.sampling_rate,
|
---|
291 | pb->f.sample_format);
|
---|
292 | if (ret != EOK) {
|
---|
293 | printf("Failed to start playback: %s\n",
|
---|
294 | str_error(ret));
|
---|
295 | return;
|
---|
296 | }
|
---|
297 | started = true;
|
---|
298 | ret = audio_pcm_get_buffer_pos(pb->device, &pos);
|
---|
299 | if (ret != EOK) {
|
---|
300 | printf("Failed to update position indicator "
|
---|
301 | "%s\n", str_error(ret));
|
---|
302 | }
|
---|
303 | }
|
---|
304 | const size_t to_play = buffer_occupied(pb, pos);
|
---|
305 | const usec_t usecs = pcm_format_size_to_usec(to_play, &pb->f);
|
---|
306 |
|
---|
307 | /* Compute delay time */
|
---|
308 | const usec_t real_delay = (usecs > work_time) ?
|
---|
309 | usecs - work_time : 0;
|
---|
310 | DPRINTF("POS %zu: %lld usecs (%lld) to play %zu bytes.\n",
|
---|
311 | pos, usecs, real_delay, to_play);
|
---|
312 | if (real_delay)
|
---|
313 | fibril_usleep(real_delay);
|
---|
314 | /* update buffer position */
|
---|
315 | const errno_t ret = audio_pcm_get_buffer_pos(pb->device, &pos);
|
---|
316 | if (ret != EOK) {
|
---|
317 | printf("Failed to update position indicator %s\n",
|
---|
318 | str_error(ret));
|
---|
319 | }
|
---|
320 | getuptime(&time);
|
---|
321 |
|
---|
322 | /*
|
---|
323 | * we did not use all the space we had,
|
---|
324 | * that is the end
|
---|
325 | */
|
---|
326 | if (available)
|
---|
327 | break;
|
---|
328 |
|
---|
329 | }
|
---|
330 | audio_pcm_stop_playback_immediate(pb->device);
|
---|
331 | }
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Play audio file usign direct device access.
|
---|
335 | * @param device The device.
|
---|
336 | * @param file The file.
|
---|
337 | * @return 0 on success, non-zero on failure.
|
---|
338 | */
|
---|
339 | int dplay(const char *device, const char *file)
|
---|
340 | {
|
---|
341 | errno_t ret = EOK;
|
---|
342 | audio_pcm_sess_t *session = NULL;
|
---|
343 | if (str_cmp(device, "default") == 0) {
|
---|
344 | session = audio_pcm_open_default();
|
---|
345 | } else {
|
---|
346 | session = audio_pcm_open(device);
|
---|
347 | }
|
---|
348 | if (!session) {
|
---|
349 | printf("Failed to connect to device %s.\n", device);
|
---|
350 | return 1;
|
---|
351 | }
|
---|
352 | printf("Playing on device: %s.\n", device);
|
---|
353 | sysarg_t val;
|
---|
354 | ret = audio_pcm_query_cap(session, AUDIO_CAP_PLAYBACK, &val);
|
---|
355 | if (ret != EOK || !val) {
|
---|
356 | printf("Device %s does not support playback\n", device);
|
---|
357 | ret = ENOTSUP;
|
---|
358 | goto close_session;
|
---|
359 | }
|
---|
360 |
|
---|
361 | char *info = NULL;
|
---|
362 | ret = audio_pcm_get_info_str(session, &info);
|
---|
363 | if (ret != EOK) {
|
---|
364 | printf("Failed to get PCM info: %s.\n", str_error(ret));
|
---|
365 | goto close_session;
|
---|
366 | }
|
---|
367 | printf("Playing on %s.\n", info);
|
---|
368 | free(info);
|
---|
369 |
|
---|
370 | playback_t pb;
|
---|
371 | playback_initialize(&pb, session);
|
---|
372 |
|
---|
373 | ret = audio_pcm_get_buffer(pb.device, &pb.buffer.base, &pb.buffer.size);
|
---|
374 | if (ret != EOK) {
|
---|
375 | printf("Failed to get PCM buffer: %s.\n", str_error(ret));
|
---|
376 | goto close_session;
|
---|
377 | }
|
---|
378 | printf("Buffer: %p %zu.\n", pb.buffer.base, pb.buffer.size);
|
---|
379 |
|
---|
380 | pb.source = fopen(file, "rb");
|
---|
381 | if (pb.source == NULL) {
|
---|
382 | ret = ENOENT;
|
---|
383 | printf("Failed to open file: %s.\n", file);
|
---|
384 | goto cleanup;
|
---|
385 | }
|
---|
386 |
|
---|
387 | wave_header_t header;
|
---|
388 | fread(&header, sizeof(header), 1, pb.source);
|
---|
389 | const char *error;
|
---|
390 | ret = wav_parse_header(&header, NULL, NULL,
|
---|
391 | &pb.f.channels, &pb.f.sampling_rate, &pb.f.sample_format, &error);
|
---|
392 | if (ret != EOK) {
|
---|
393 | printf("Error parsing wav header: %s.\n", error);
|
---|
394 | goto cleanup;
|
---|
395 | }
|
---|
396 | ret = audio_pcm_query_cap(pb.device, AUDIO_CAP_BUFFER_POS, &val);
|
---|
397 | if (ret == EOK && val) {
|
---|
398 | play(&pb);
|
---|
399 | } else {
|
---|
400 | ret = audio_pcm_query_cap(pb.device, AUDIO_CAP_INTERRUPT, &val);
|
---|
401 | if (ret == EOK && val)
|
---|
402 | play_fragment(&pb);
|
---|
403 | else
|
---|
404 | printf("Neither playing method is supported");
|
---|
405 | }
|
---|
406 |
|
---|
407 | cleanup:
|
---|
408 | fclose(pb.source);
|
---|
409 | as_area_destroy(pb.buffer.base);
|
---|
410 | audio_pcm_release_buffer(pb.device);
|
---|
411 | close_session:
|
---|
412 | audio_pcm_close(session);
|
---|
413 | return ret == EOK ? 0 : 1;
|
---|
414 | }
|
---|
415 | /**
|
---|
416 | * @}
|
---|
417 | */
|
---|