source: mainline/uspace/app/wavplay/dplay.c@ 39a379a

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

dplay: 80 column

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