source: mainline/uspace/app/wavplay/dplay.c@ 232bf2c

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 232bf2c was fafb8e5, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Mechanically lowercase IPC_SET_*/IPC_GET_*

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