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 dplay
|
---|
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 <sys/mman.h>
|
---|
44 | #include <sys/time.h>
|
---|
45 |
|
---|
46 | #include <stdio.h>
|
---|
47 | #include <macros.h>
|
---|
48 |
|
---|
49 | #include "wave.h"
|
---|
50 | #include "dplay.h"
|
---|
51 |
|
---|
52 | #define DEFAULT_FRAGMENTS 2
|
---|
53 |
|
---|
54 | typedef struct {
|
---|
55 | struct {
|
---|
56 | void *base;
|
---|
57 | size_t size;
|
---|
58 | void* write_ptr;
|
---|
59 | } buffer;
|
---|
60 | pcm_format_t f;
|
---|
61 | FILE* source;
|
---|
62 | volatile bool playing;
|
---|
63 | fibril_mutex_t mutex;
|
---|
64 | fibril_condvar_t cv;
|
---|
65 | audio_pcm_sess_t *device;
|
---|
66 | } playback_t;
|
---|
67 |
|
---|
68 | static void playback_initialize(playback_t *pb, audio_pcm_sess_t *sess)
|
---|
69 | {
|
---|
70 | assert(sess);
|
---|
71 | assert(pb);
|
---|
72 | pb->buffer.base = NULL;
|
---|
73 | pb->buffer.size = 0;
|
---|
74 | pb->buffer.write_ptr = NULL;
|
---|
75 | pb->playing = false;
|
---|
76 | pb->source = NULL;
|
---|
77 | pb->device = sess;
|
---|
78 | fibril_mutex_initialize(&pb->mutex);
|
---|
79 | fibril_condvar_initialize(&pb->cv);
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | static void device_event_callback(ipc_callid_t iid, ipc_call_t *icall, void* arg)
|
---|
84 | {
|
---|
85 | async_answer_0(iid, EOK);
|
---|
86 | playback_t *pb = arg;
|
---|
87 | const size_t fragment_size = pb->buffer.size / DEFAULT_FRAGMENTS;
|
---|
88 | while (1) {
|
---|
89 | ipc_call_t call;
|
---|
90 | ipc_callid_t callid = async_get_call(&call);
|
---|
91 | switch(IPC_GET_IMETHOD(call)) {
|
---|
92 | case PCM_EVENT_PLAYBACK_STARTED:
|
---|
93 | case PCM_EVENT_FRAMES_PLAYED:
|
---|
94 | printf("%u frames: ", IPC_GET_ARG1(call));
|
---|
95 | async_answer_0(callid, EOK);
|
---|
96 | break;
|
---|
97 | case PCM_EVENT_PLAYBACK_TERMINATED:
|
---|
98 | printf("Playback terminated\n");
|
---|
99 | fibril_mutex_lock(&pb->mutex);
|
---|
100 | pb->playing = false;
|
---|
101 | fibril_condvar_signal(&pb->cv);
|
---|
102 | async_answer_0(callid, EOK);
|
---|
103 | fibril_mutex_unlock(&pb->mutex);
|
---|
104 | return;
|
---|
105 | default:
|
---|
106 | printf("Unknown event %d.\n", IPC_GET_IMETHOD(call));
|
---|
107 | async_answer_0(callid, ENOTSUP);
|
---|
108 | continue;
|
---|
109 |
|
---|
110 | }
|
---|
111 | const size_t bytes = fread(pb->buffer.write_ptr,
|
---|
112 | sizeof(uint8_t), fragment_size, pb->source);
|
---|
113 | printf("Copied from position %p size %zu/%zu\n",
|
---|
114 | pb->buffer.write_ptr, bytes, fragment_size);
|
---|
115 | if (bytes == 0) {
|
---|
116 | audio_pcm_last_playback_fragment(pb->device);
|
---|
117 | }
|
---|
118 | /* any constant is silence */
|
---|
119 | bzero(pb->buffer.write_ptr + bytes, fragment_size - bytes);
|
---|
120 | pb->buffer.write_ptr += fragment_size;
|
---|
121 |
|
---|
122 | if (pb->buffer.write_ptr >= (pb->buffer.base + pb->buffer.size))
|
---|
123 | pb->buffer.write_ptr -= pb->buffer.size;
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | static void play_fragment(playback_t *pb)
|
---|
128 | {
|
---|
129 | assert(pb);
|
---|
130 | assert(pb->device);
|
---|
131 | const size_t fragment_size = pb->buffer.size / DEFAULT_FRAGMENTS;
|
---|
132 | printf("Registering event callback\n");
|
---|
133 | int ret = audio_pcm_register_event_callback(pb->device,
|
---|
134 | device_event_callback, pb);
|
---|
135 | if (ret != EOK) {
|
---|
136 | printf("Failed to register event callback.\n");
|
---|
137 | return;
|
---|
138 | }
|
---|
139 | printf("Playing: %dHz, %s, %d channel(s).\n", pb->f.sampling_rate,
|
---|
140 | pcm_sample_format_str(pb->f.sample_format), pb->f.channels);
|
---|
141 | const size_t bytes = fread(pb->buffer.base, sizeof(uint8_t),
|
---|
142 | fragment_size, pb->source);
|
---|
143 | if (bytes != fragment_size)
|
---|
144 | bzero(pb->buffer.base + bytes, fragment_size - bytes);
|
---|
145 | printf("Initial: Copied from position %p size %zu/%zu\n",
|
---|
146 | pb->buffer.base, bytes, fragment_size);
|
---|
147 | pb->buffer.write_ptr = pb->buffer.base + fragment_size;
|
---|
148 | fibril_mutex_lock(&pb->mutex);
|
---|
149 | const unsigned frames =
|
---|
150 | pcm_format_size_to_frames(fragment_size, &pb->f);
|
---|
151 | ret = audio_pcm_start_playback_fragment(pb->device, frames,
|
---|
152 | pb->f.channels, pb->f.sampling_rate, pb->f.sample_format);
|
---|
153 | if (ret != EOK) {
|
---|
154 | fibril_mutex_unlock(&pb->mutex);
|
---|
155 | printf("Failed to start playback: %s.\n", str_error(ret));
|
---|
156 | audio_pcm_unregister_event_callback(pb->device);
|
---|
157 | return;
|
---|
158 | }
|
---|
159 |
|
---|
160 | for (pb->playing = true; pb->playing;
|
---|
161 | fibril_condvar_wait(&pb->cv, &pb->mutex));
|
---|
162 |
|
---|
163 | fibril_mutex_unlock(&pb->mutex);
|
---|
164 | printf("\n");
|
---|
165 | audio_pcm_unregister_event_callback(pb->device);
|
---|
166 | }
|
---|
167 |
|
---|
168 | static size_t buffer_occupied(const playback_t *pb, size_t pos)
|
---|
169 | {
|
---|
170 | assert(pb);
|
---|
171 | void *read_ptr = pb->buffer.base + pos;
|
---|
172 | if (read_ptr > pb->buffer.write_ptr)
|
---|
173 | return pb->buffer.write_ptr + pb->buffer.size - read_ptr;
|
---|
174 | return pb->buffer.write_ptr - read_ptr;
|
---|
175 |
|
---|
176 | }
|
---|
177 |
|
---|
178 | static size_t buffer_avail(const playback_t *pb, size_t pos)
|
---|
179 | {
|
---|
180 | assert(pb);
|
---|
181 | void *read_ptr = pb->buffer.base + pos;
|
---|
182 | if (read_ptr <= pb->buffer.write_ptr)
|
---|
183 | return read_ptr + pb->buffer.size - pb->buffer.write_ptr - 1;
|
---|
184 | return (read_ptr - pb->buffer.write_ptr) - 1;
|
---|
185 | }
|
---|
186 |
|
---|
187 | static size_t buffer_remain(const playback_t *pb)
|
---|
188 | {
|
---|
189 | assert(pb);
|
---|
190 | return (pb->buffer.base + pb->buffer.size) - pb->buffer.write_ptr;
|
---|
191 | }
|
---|
192 |
|
---|
193 | static void buffer_advance(playback_t *pb, size_t bytes)
|
---|
194 | {
|
---|
195 | assert(pb);
|
---|
196 | pb->buffer.write_ptr += bytes;
|
---|
197 | while (pb->buffer.write_ptr >= (pb->buffer.base + pb->buffer.size))
|
---|
198 | pb->buffer.write_ptr -= pb->buffer.size;
|
---|
199 | }
|
---|
200 |
|
---|
201 | #define DPRINTF(f, ...) \
|
---|
202 | printf("%.2lu:%.6lu "f, time.tv_sec % 100, time.tv_usec, __VA_ARGS__)
|
---|
203 |
|
---|
204 |
|
---|
205 | static void play(playback_t *pb)
|
---|
206 | {
|
---|
207 | assert(pb);
|
---|
208 | assert(pb->device);
|
---|
209 | pb->buffer.write_ptr = pb->buffer.base;
|
---|
210 | printf("Playing: %dHz, %s, %d channel(s).\n", pb->f.sampling_rate,
|
---|
211 | pcm_sample_format_str(pb->f.sample_format), pb->f.channels);
|
---|
212 | useconds_t work_time = 20000; /* 20 ms */
|
---|
213 | bool started = false;
|
---|
214 | size_t pos = 0;
|
---|
215 | struct timeval time = { 0 };
|
---|
216 | getuptime(&time);
|
---|
217 | do {
|
---|
218 | size_t available = buffer_avail(pb, pos);
|
---|
219 | /* Writing might need wrap around the end */
|
---|
220 | size_t bytes = fread(pb->buffer.write_ptr, sizeof(uint8_t),
|
---|
221 | min(available, buffer_remain(pb)), pb->source);
|
---|
222 | buffer_advance(pb, bytes);
|
---|
223 | DPRINTF("POS %zu: %zu bytes free in buffer, read %zu, wp %zu\n",
|
---|
224 | pos, available, bytes,
|
---|
225 | pb->buffer.write_ptr - pb->buffer.base);
|
---|
226 | available -= bytes;
|
---|
227 | if (available) {
|
---|
228 | bytes = fread(pb->buffer.write_ptr,
|
---|
229 | sizeof(uint8_t), min(available, buffer_remain(pb)),
|
---|
230 | pb->source);
|
---|
231 | buffer_advance(pb, bytes);
|
---|
232 | DPRINTF("POS %zu: %zu bytes still free in buffer, "
|
---|
233 | "read %zu, wp %zu\n", pos, available, bytes,
|
---|
234 | pb->buffer.write_ptr - pb->buffer.base);
|
---|
235 | available -= bytes;
|
---|
236 | }
|
---|
237 |
|
---|
238 | if (!started) {
|
---|
239 | int ret = audio_pcm_start_playback(pb->device,
|
---|
240 | pb->f.channels, pb->f.sampling_rate,
|
---|
241 | pb->f.sample_format);
|
---|
242 | if (ret != EOK) {
|
---|
243 | printf("Failed to start playback\n");
|
---|
244 | return;
|
---|
245 | }
|
---|
246 | started = true;
|
---|
247 | ret = audio_pcm_get_buffer_pos(pb->device, &pos);
|
---|
248 | if (ret != EOK) {
|
---|
249 | printf("Failed to update position indicator\n");
|
---|
250 | }
|
---|
251 | }
|
---|
252 | const size_t to_play = buffer_occupied(pb, pos);
|
---|
253 | const useconds_t usecs =
|
---|
254 | pcm_format_size_to_usec(to_play, &pb->f);
|
---|
255 |
|
---|
256 | const useconds_t real_delay = (usecs > work_time)
|
---|
257 | ? usecs - work_time : 0;
|
---|
258 | DPRINTF("POS %zu: %u usecs (%u) to play %zu bytes.\n",
|
---|
259 | pos, usecs, real_delay, to_play);
|
---|
260 | if (real_delay)
|
---|
261 | async_usleep(real_delay);
|
---|
262 | const int ret = audio_pcm_get_buffer_pos(pb->device, &pos);
|
---|
263 | if (ret != EOK) {
|
---|
264 | printf("Failed to update position indicator\n");
|
---|
265 | }
|
---|
266 | getuptime(&time);
|
---|
267 | if (available)
|
---|
268 | break;
|
---|
269 |
|
---|
270 | } while (1);
|
---|
271 | audio_pcm_stop_playback(pb->device);
|
---|
272 | }
|
---|
273 |
|
---|
274 | int dplay(const char *device, const char *file)
|
---|
275 | {
|
---|
276 | int ret = EOK;
|
---|
277 | audio_pcm_sess_t *session = NULL;
|
---|
278 | if (str_cmp(device, "default") == 0) {
|
---|
279 | session = audio_pcm_open_default();
|
---|
280 | } else {
|
---|
281 | session = audio_pcm_open(device);
|
---|
282 | }
|
---|
283 | if (!session) {
|
---|
284 | printf("Failed to connect to device %s.\n", device);
|
---|
285 | return 1;
|
---|
286 | }
|
---|
287 | printf("Playing on device: %s.\n", device);
|
---|
288 | if (audio_pcm_query_cap(session, AUDIO_CAP_PLAYBACK) <= 0) {
|
---|
289 | printf("Device %s does not support playback\n", device);
|
---|
290 | ret = ENOTSUP;
|
---|
291 | goto close_session;
|
---|
292 | }
|
---|
293 |
|
---|
294 | const char* info = NULL;
|
---|
295 | ret = audio_pcm_get_info_str(session, &info);
|
---|
296 | if (ret != EOK) {
|
---|
297 | printf("Failed to get PCM info.\n");
|
---|
298 | goto close_session;
|
---|
299 | }
|
---|
300 | printf("Playing on %s.\n", info);
|
---|
301 | free(info);
|
---|
302 |
|
---|
303 | playback_t pb;
|
---|
304 | playback_initialize(&pb, session);
|
---|
305 |
|
---|
306 | ret = audio_pcm_get_buffer(pb.device, &pb.buffer.base, &pb.buffer.size);
|
---|
307 | if (ret != EOK) {
|
---|
308 | printf("Failed to get PCM buffer: %s.\n", str_error(ret));
|
---|
309 | goto close_session;
|
---|
310 | }
|
---|
311 | printf("Buffer: %p %zu.\n", pb.buffer.base, pb.buffer.size);
|
---|
312 |
|
---|
313 | pb.source = fopen(file, "rb");
|
---|
314 | if (pb.source == NULL) {
|
---|
315 | ret = ENOENT;
|
---|
316 | printf("Failed to open file: %s.\n", file);
|
---|
317 | goto cleanup;
|
---|
318 | }
|
---|
319 |
|
---|
320 | wave_header_t header;
|
---|
321 | fread(&header, sizeof(header), 1, pb.source);
|
---|
322 | const char *error;
|
---|
323 | ret = wav_parse_header(&header, NULL, NULL,
|
---|
324 | &pb.f.channels, &pb.f.sampling_rate, &pb.f.sample_format, &error);
|
---|
325 | if (ret != EOK) {
|
---|
326 | printf("Error parsing wav header: %s.\n", error);
|
---|
327 | goto cleanup;
|
---|
328 | }
|
---|
329 | if (audio_pcm_query_cap(pb.device, AUDIO_CAP_BUFFER_POS) > 0) {
|
---|
330 | play(&pb);
|
---|
331 | } else {
|
---|
332 | if (audio_pcm_query_cap(pb.device, AUDIO_CAP_INTERRUPT) > 0)
|
---|
333 | play_fragment(&pb);
|
---|
334 | else
|
---|
335 | printf("Neither playing method is supported");
|
---|
336 | }
|
---|
337 |
|
---|
338 | cleanup:
|
---|
339 | fclose(pb.source);
|
---|
340 | munmap(pb.buffer.base, pb.buffer.size);
|
---|
341 | audio_pcm_release_buffer(pb.device);
|
---|
342 | close_session:
|
---|
343 | audio_pcm_close(session);
|
---|
344 | return ret == EOK ? 0 : 1;
|
---|
345 | }
|
---|
346 | /**
|
---|
347 | * @}
|
---|
348 | */
|
---|