source: mainline/uspace/app/wavplay/dplay.c@ 9df99a17

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9df99a17 was 9df99a17, checked in by Jan Vesely <jano.vesely@…>, 13 years ago

dplay: User provided open_default function instead of hardcoding device path

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