source: mainline/uspace/app/wavplay/dplay.c@ d26233c

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

dplay: Rename BUFFER_PARTS ⇒ DEFAULT_FRAGMENTS

remove redundant header

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