source: mainline/uspace/app/wavplay/dplay.c@ 84239b1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 84239b1 was 850fd32, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Fix mischievious semicolons.

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