[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>
|
---|
| 40 | #include <devman.h>
|
---|
[90f05b0f] | 41 | #include <audio_pcm_iface.h>
|
---|
[9d5244f] | 42 | #include <fibril_synch.h>
|
---|
[017455e] | 43 | #include <pcm/format.h>
|
---|
[a68a94e] | 44 | #include <sys/mman.h>
|
---|
[b499127] | 45 | #include <sys/time.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 |
|
---|
[ce047249] | 53 | #define DEFAULT_DEVICE "/hw/pci0/00:01.0/sb16/pcm"
|
---|
[57e8b3b] | 54 | #define BUFFER_PARTS 2
|
---|
[a68a94e] | 55 |
|
---|
[4bec78f] | 56 | typedef 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 |
|
---|
[2cc5c835] | 70 | static void playback_initialize(playback_t *pb, audio_pcm_sess_t *sess)
|
---|
[9d5244f] | 71 | {
|
---|
[2cc5c835] | 72 | assert(sess);
|
---|
[9d5244f] | 73 | assert(pb);
|
---|
| 74 | pb->buffer.base = NULL;
|
---|
| 75 | pb->buffer.size = 0;
|
---|
[3e6a975a] | 76 | pb->buffer.write_ptr = NULL;
|
---|
[9d5244f] | 77 | pb->playing = false;
|
---|
| 78 | pb->source = NULL;
|
---|
[2cc5c835] | 79 | pb->device = sess;
|
---|
[9d5244f] | 80 | fibril_mutex_initialize(&pb->mutex);
|
---|
| 81 | fibril_condvar_initialize(&pb->cv);
|
---|
| 82 | }
|
---|
[4bec78f] | 83 |
|
---|
[9d5244f] | 84 |
|
---|
| 85 | static void device_event_callback(ipc_callid_t iid, ipc_call_t *icall, void* arg)
|
---|
[4bec78f] | 86 | {
|
---|
[68e005d] | 87 | async_answer_0(iid, EOK);
|
---|
| 88 | playback_t *pb = arg;
|
---|
[017455e] | 89 | const size_t fragment_size = pb->buffer.size / BUFFER_PARTS;
|
---|
[9d5244f] | 90 | while (1) {
|
---|
| 91 | ipc_call_t call;
|
---|
| 92 | ipc_callid_t callid = async_get_call(&call);
|
---|
[1240bb9] | 93 | switch(IPC_GET_IMETHOD(call)) {
|
---|
[e7bf5f6] | 94 | case PCM_EVENT_PLAYBACK_STARTED:
|
---|
[57e8b3b] | 95 | case PCM_EVENT_FRAMES_PLAYED:
|
---|
[017455e] | 96 | printf("%u frames: ", IPC_GET_ARG1(call));
|
---|
[1240bb9] | 97 | async_answer_0(callid, EOK);
|
---|
[9d5244f] | 98 | break;
|
---|
[1240bb9] | 99 | case PCM_EVENT_PLAYBACK_TERMINATED:
|
---|
[57e8b3b] | 100 | printf("Playback terminated\n");
|
---|
[1240bb9] | 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 |
|
---|
[9d5244f] | 112 | }
|
---|
[3e6a975a] | 113 | const size_t bytes = fread(pb->buffer.write_ptr,
|
---|
| 114 | sizeof(uint8_t), fragment_size, pb->source);
|
---|
[017455e] | 115 | printf("Copied from position %p size %zu/%zu\n",
|
---|
[3e6a975a] | 116 | pb->buffer.write_ptr, bytes, fragment_size);
|
---|
[d01e635] | 117 | if (bytes == 0) {
|
---|
[92b638c] | 118 | audio_pcm_last_playback_fragment(pb->device);
|
---|
[d01e635] | 119 | }
|
---|
[3e6a975a] | 120 | /* any constant is silence */
|
---|
| 121 | bzero(pb->buffer.write_ptr + bytes, fragment_size - bytes);
|
---|
| 122 | pb->buffer.write_ptr += fragment_size;
|
---|
[c5c65b6] | 123 |
|
---|
[3e6a975a] | 124 | if (pb->buffer.write_ptr >= (pb->buffer.base + pb->buffer.size))
|
---|
| 125 | pb->buffer.write_ptr -= pb->buffer.size;
|
---|
[4bec78f] | 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[017455e] | 129 | static void play_fragment(playback_t *pb)
|
---|
[b499127] | 130 | {
|
---|
[4bec78f] | 131 | assert(pb);
|
---|
[9d5244f] | 132 | assert(pb->device);
|
---|
[017455e] | 133 | const size_t fragment_size = pb->buffer.size / BUFFER_PARTS;
|
---|
[018ab50] | 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 | }
|
---|
[017455e] | 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);
|
---|
[4bec78f] | 143 | const size_t bytes = fread(pb->buffer.base, sizeof(uint8_t),
|
---|
[017455e] | 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);
|
---|
[3e6a975a] | 149 | pb->buffer.write_ptr = pb->buffer.base + fragment_size;
|
---|
[9d5244f] | 150 | fibril_mutex_lock(&pb->mutex);
|
---|
[3e6a975a] | 151 | const unsigned frames =
|
---|
| 152 | pcm_format_size_to_frames(fragment_size, &pb->f);
|
---|
[017455e] | 153 | ret = audio_pcm_start_playback_fragment(pb->device, frames,
|
---|
| 154 | pb->f.channels, pb->f.sampling_rate, pb->f.sample_format);
|
---|
[b499127] | 155 | if (ret != EOK) {
|
---|
[9d5244f] | 156 | fibril_mutex_unlock(&pb->mutex);
|
---|
[b499127] | 157 | printf("Failed to start playback: %s.\n", str_error(ret));
|
---|
[017455e] | 158 | audio_pcm_unregister_event_callback(pb->device);
|
---|
[b499127] | 159 | return;
|
---|
| 160 | }
|
---|
[7a5ab20] | 161 |
|
---|
[9d5244f] | 162 | for (pb->playing = true; pb->playing;
|
---|
[68e005d] | 163 | fibril_condvar_wait(&pb->cv, &pb->mutex));
|
---|
[b499127] | 164 |
|
---|
[1240bb9] | 165 | fibril_mutex_unlock(&pb->mutex);
|
---|
[c5c65b6] | 166 | printf("\n");
|
---|
[018ab50] | 167 | audio_pcm_unregister_event_callback(pb->device);
|
---|
[b499127] | 168 | }
|
---|
| 169 |
|
---|
[3e6a975a] | 170 | static 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 |
|
---|
| 180 | static 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 |
|
---|
| 189 | static 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 |
|
---|
| 195 | static 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 |
|
---|
[1bd99785] | 203 | #define DPRINTF(f, ...) \
|
---|
| 204 | printf("%.2lu:%.6lu "f, time.tv_sec % 100, time.tv_usec, __VA_ARGS__)
|
---|
| 205 |
|
---|
[3e6a975a] | 206 |
|
---|
[017455e] | 207 | static void play(playback_t *pb)
|
---|
| 208 | {
|
---|
| 209 | assert(pb);
|
---|
| 210 | assert(pb->device);
|
---|
[3e6a975a] | 211 | pb->buffer.write_ptr = pb->buffer.base;
|
---|
[017455e] | 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);
|
---|
[1bd99785] | 214 | useconds_t work_time = 70000; /* 10 ms */
|
---|
[3e6a975a] | 215 | bool started = false;
|
---|
| 216 | size_t pos = 0;
|
---|
[1bd99785] | 217 | struct timeval time = { 0 };
|
---|
| 218 | getuptime(&time);
|
---|
[017455e] | 219 | do {
|
---|
[3e6a975a] | 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);
|
---|
[1bd99785] | 225 | DPRINTF("POS %zu: %zu bytes free in buffer, read %zu, wp %zu\n",
|
---|
[39a379a] | 226 | pos, available, bytes,
|
---|
| 227 | pb->buffer.write_ptr - pb->buffer.base);
|
---|
[3e6a975a] | 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);
|
---|
[39a379a] | 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);
|
---|
[3e6a975a] | 237 | available -= bytes;
|
---|
[b5d2e57] | 238 | }
|
---|
[017455e] | 239 |
|
---|
[3e6a975a] | 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;
|
---|
[017455e] | 247 | }
|
---|
[3e6a975a] | 248 | started = true;
|
---|
[017455e] | 249 | }
|
---|
[3e6a975a] | 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;
|
---|
[1bd99785] | 256 | DPRINTF("POS %zu: %u usecs (%u) to play %zu bytes.\n",
|
---|
[3e6a975a] | 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");
|
---|
[b5d2e57] | 263 | }
|
---|
[1bd99785] | 264 | getuptime(&time);
|
---|
[3e6a975a] | 265 | if (available)
|
---|
| 266 | break;
|
---|
| 267 |
|
---|
[017455e] | 268 | } while (1);
|
---|
| 269 | audio_pcm_stop_playback(pb->device);
|
---|
| 270 | }
|
---|
| 271 |
|
---|
[aef1799] | 272 | int dplay(const char *device, const char *file)
|
---|
[a68a94e] | 273 | {
|
---|
[017455e] | 274 | int ret = EOK;
|
---|
[aef1799] | 275 | if (str_cmp(device, "default") == 0)
|
---|
| 276 | device = DEFAULT_DEVICE;
|
---|
[2cc5c835] | 277 | audio_pcm_sess_t *session = audio_pcm_open(device);
|
---|
[a68a94e] | 278 | if (!session) {
|
---|
[aef1799] | 279 | printf("Failed to connect to device %s.\n", device);
|
---|
[a68a94e] | 280 | return 1;
|
---|
| 281 | }
|
---|
[aef1799] | 282 | printf("Playing on device: %s.\n", device);
|
---|
[017455e] | 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 | }
|
---|
[a68a94e] | 288 |
|
---|
[4bec78f] | 289 | const char* info = NULL;
|
---|
[017455e] | 290 | ret = audio_pcm_get_info_str(session, &info);
|
---|
[a68a94e] | 291 | if (ret != EOK) {
|
---|
| 292 | printf("Failed to get PCM info.\n");
|
---|
[c5c65b6] | 293 | goto close_session;
|
---|
[a68a94e] | 294 | }
|
---|
| 295 | printf("Playing on %s.\n", info);
|
---|
| 296 | free(info);
|
---|
| 297 |
|
---|
[9d5244f] | 298 | playback_t pb;
|
---|
[2cc5c835] | 299 | playback_initialize(&pb, session);
|
---|
[9d5244f] | 300 |
|
---|
[018ab50] | 301 | ret = audio_pcm_get_buffer(pb.device, &pb.buffer.base, &pb.buffer.size);
|
---|
[a68a94e] | 302 | if (ret != EOK) {
|
---|
| 303 | printf("Failed to get PCM buffer: %s.\n", str_error(ret));
|
---|
[c5c65b6] | 304 | goto close_session;
|
---|
[a68a94e] | 305 | }
|
---|
[b497018] | 306 | printf("Buffer: %p %zu.\n", pb.buffer.base, pb.buffer.size);
|
---|
[3e6a975a] | 307 |
|
---|
[4bec78f] | 308 | pb.source = fopen(file, "rb");
|
---|
| 309 | if (pb.source == NULL) {
|
---|
[c5c65b6] | 310 | ret = ENOENT;
|
---|
[3e6a975a] | 311 | printf("Failed to open file: %s.\n", file);
|
---|
[c5c65b6] | 312 | goto cleanup;
|
---|
[eaa1c28] | 313 | }
|
---|
[3e6a975a] | 314 |
|
---|
[eaa1c28] | 315 | wave_header_t header;
|
---|
[4bec78f] | 316 | fread(&header, sizeof(header), 1, pb.source);
|
---|
[eaa1c28] | 317 | const char *error;
|
---|
[017455e] | 318 | ret = wav_parse_header(&header, NULL, NULL,
|
---|
| 319 | &pb.f.channels, &pb.f.sampling_rate, &pb.f.sample_format, &error);
|
---|
[eaa1c28] | 320 | if (ret != EOK) {
|
---|
| 321 | printf("Error parsing wav header: %s.\n", error);
|
---|
[c5c65b6] | 322 | goto cleanup;
|
---|
[eaa1c28] | 323 | }
|
---|
[017455e] | 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 | }
|
---|
[a68a94e] | 332 |
|
---|
[c5c65b6] | 333 | cleanup:
|
---|
[017455e] | 334 | fclose(pb.source);
|
---|
[4bec78f] | 335 | munmap(pb.buffer.base, pb.buffer.size);
|
---|
[2cc5c835] | 336 | audio_pcm_release_buffer(pb.device);
|
---|
[c5c65b6] | 337 | close_session:
|
---|
[2cc5c835] | 338 | audio_pcm_close(session);
|
---|
[c5c65b6] | 339 | return ret == EOK ? 0 : 1;
|
---|
[a68a94e] | 340 | }
|
---|
[b499127] | 341 | /**
|
---|
| 342 | * @}
|
---|
[a68a94e] | 343 | */
|
---|