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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9c514be was a46e56b, checked in by Jakub Jermar <jakub@…>, 7 years ago

Prefer handle over ID in naming handle variables

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