| 1 |
/** |
| 2 |
* Originally digi.c from allegro wiki |
| 3 |
* Original authors: KC/Milan |
| 4 |
* |
| 5 |
* Converted to allegro5 by Ryan Dickie |
| 6 |
*/ |
| 7 |
|
| 8 |
/* Title: Stream functions |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <stdio.h> |
| 12 |
|
| 13 |
#include "allegro5/allegro_audio.h" |
| 14 |
#include "allegro5/internal/aintern_audio.h" |
| 15 |
#include "allegro5/internal/aintern_audio_cfg.h" |
| 16 |
|
| 17 |
ALLEGRO_DEBUG_CHANNEL("audio") |
| 18 |
|
| 19 |
/* |
| 20 |
* The highest quality interpolator is a cubic interpolator requiring |
| 21 |
* four sample points. In the streaming case we lag the true sample |
| 22 |
* position by three. |
| 23 |
*/ |
| 24 |
#define MAX_LAG (3) |
| 25 |
|
| 26 |
|
| 27 |
/* |
| 28 |
* To avoid deadlocks, unlock the mutex returned by this function, rather than |
| 29 |
* whatever you passed as the argument. |
| 30 |
*/ |
| 31 |
static ALLEGRO_MUTEX *maybe_lock_mutex(ALLEGRO_MUTEX *mutex) |
| 32 |
{ |
| 33 |
if (mutex) { |
| 34 |
al_lock_mutex(mutex); |
| 35 |
} |
| 36 |
return mutex; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
static void maybe_unlock_mutex(ALLEGRO_MUTEX *mutex) |
| 41 |
{ |
| 42 |
if (mutex) { |
| 43 |
al_unlock_mutex(mutex); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/* Function: al_create_audio_stream |
| 48 |
*/ |
| 49 |
ALLEGRO_AUDIO_STREAM *al_create_audio_stream(size_t fragment_count, |
| 50 |
unsigned int frag_samples, unsigned int freq, ALLEGRO_AUDIO_DEPTH depth, |
| 51 |
ALLEGRO_CHANNEL_CONF chan_conf) |
| 52 |
{ |
| 53 |
ALLEGRO_AUDIO_STREAM *stream; |
| 54 |
unsigned long bytes_per_sample; |
| 55 |
unsigned long bytes_per_frag_buf; |
| 56 |
size_t i; |
| 57 |
|
| 58 |
if (!fragment_count) { |
| 59 |
_al_set_error(ALLEGRO_INVALID_PARAM, |
| 60 |
"Attempted to create stream with no buffers"); |
| 61 |
return NULL; |
| 62 |
} |
| 63 |
if (!frag_samples) { |
| 64 |
_al_set_error(ALLEGRO_INVALID_PARAM, |
| 65 |
"Attempted to create stream with no buffer size"); |
| 66 |
return NULL; |
| 67 |
} |
| 68 |
if (!freq) { |
| 69 |
_al_set_error(ALLEGRO_INVALID_PARAM, |
| 70 |
"Attempted to create stream with no frequency"); |
| 71 |
return NULL; |
| 72 |
} |
| 73 |
|
| 74 |
bytes_per_sample = al_get_channel_count(chan_conf) * |
| 75 |
al_get_audio_depth_size(depth); |
| 76 |
bytes_per_frag_buf = frag_samples * bytes_per_sample; |
| 77 |
|
| 78 |
stream = al_calloc(1, sizeof(*stream)); |
| 79 |
if (!stream) { |
| 80 |
_al_set_error(ALLEGRO_GENERIC_ERROR, |
| 81 |
"Out of memory allocating stream object"); |
| 82 |
return NULL; |
| 83 |
} |
| 84 |
|
| 85 |
stream->spl.is_playing = true; |
| 86 |
stream->is_draining = false; |
| 87 |
|
| 88 |
stream->spl.loop = _ALLEGRO_PLAYMODE_STREAM_ONCE; |
| 89 |
stream->spl.spl_data.depth = depth; |
| 90 |
stream->spl.spl_data.chan_conf = chan_conf; |
| 91 |
stream->spl.spl_data.frequency = freq; |
| 92 |
stream->spl.speed = 1.0f; |
| 93 |
stream->spl.gain = 1.0f; |
| 94 |
stream->spl.pan = 0.0f; |
| 95 |
|
| 96 |
stream->spl.step = 0; |
| 97 |
stream->spl.pos = frag_samples; |
| 98 |
stream->spl.spl_data.len = stream->spl.pos; |
| 99 |
|
| 100 |
stream->buf_count = fragment_count; |
| 101 |
|
| 102 |
stream->used_bufs = al_calloc(1, fragment_count * sizeof(void *) * 2); |
| 103 |
if (!stream->used_bufs) { |
| 104 |
al_free(stream->used_bufs); |
| 105 |
al_free(stream); |
| 106 |
_al_set_error(ALLEGRO_GENERIC_ERROR, |
| 107 |
"Out of memory allocating stream buffer pointers"); |
| 108 |
return NULL; |
| 109 |
} |
| 110 |
stream->pending_bufs = stream->used_bufs + fragment_count; |
| 111 |
|
| 112 |
/* The main_buffer holds all the buffer fragments in contiguous memory. |
| 113 |
* To support interpolation across buffer fragments, we allocate extra |
| 114 |
* MAX_LAG samples at the start of each buffer fragment, to hold the |
| 115 |
* last few sample values which came before that fragment. |
| 116 |
*/ |
| 117 |
stream->main_buffer = al_calloc(1, |
| 118 |
(MAX_LAG * bytes_per_sample + bytes_per_frag_buf) * fragment_count); |
| 119 |
if (!stream->main_buffer) { |
| 120 |
al_free(stream->used_bufs); |
| 121 |
al_free(stream); |
| 122 |
_al_set_error(ALLEGRO_GENERIC_ERROR, |
| 123 |
"Out of memory allocating stream buffer"); |
| 124 |
return NULL; |
| 125 |
} |
| 126 |
|
| 127 |
for (i = 0; i < fragment_count; i++) { |
| 128 |
char *buffer = (char *)stream->main_buffer |
| 129 |
+ i * (MAX_LAG * bytes_per_sample + bytes_per_frag_buf); |
| 130 |
al_fill_silence(buffer, MAX_LAG, depth, chan_conf); |
| 131 |
stream->used_bufs[i] = buffer + MAX_LAG * bytes_per_sample; |
| 132 |
} |
| 133 |
|
| 134 |
al_init_user_event_source(&stream->spl.es); |
| 135 |
|
| 136 |
/* This can lead to deadlocks on shutdown, hence we don't do it. */ |
| 137 |
/* stream->dtor_item = _al_kcm_register_destructor(stream, (void (*)(void *)) al_destroy_audio_stream); */ |
| 138 |
|
| 139 |
return stream; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
/* Function: al_destroy_audio_stream |
| 144 |
*/ |
| 145 |
void al_destroy_audio_stream(ALLEGRO_AUDIO_STREAM *stream) |
| 146 |
{ |
| 147 |
if (stream) { |
| 148 |
if (stream->feed_thread) { |
| 149 |
stream->unload_feeder(stream); |
| 150 |
} |
| 151 |
/* See commented out call to _al_kcm_register_destructor. */ |
| 152 |
/* _al_kcm_unregister_destructor(stream->dtor_item); */ |
| 153 |
_al_kcm_detach_from_parent(&stream->spl); |
| 154 |
|
| 155 |
al_destroy_user_event_source(&stream->spl.es); |
| 156 |
al_free(stream->main_buffer); |
| 157 |
al_free(stream->used_bufs); |
| 158 |
al_free(stream); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
/* Function: al_drain_audio_stream |
| 164 |
*/ |
| 165 |
void al_drain_audio_stream(ALLEGRO_AUDIO_STREAM *stream) |
| 166 |
{ |
| 167 |
bool playing; |
| 168 |
|
| 169 |
if (!al_get_audio_stream_attached(stream)) { |
| 170 |
al_set_audio_stream_playing(stream, false); |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
stream->is_draining = true; |
| 175 |
do { |
| 176 |
al_rest(0.01); |
| 177 |
playing = al_get_audio_stream_playing(stream); |
| 178 |
} while (playing); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
/* Function: al_get_audio_stream_frequency |
| 183 |
*/ |
| 184 |
unsigned int al_get_audio_stream_frequency(const ALLEGRO_AUDIO_STREAM *stream) |
| 185 |
{ |
| 186 |
ASSERT(stream); |
| 187 |
|
| 188 |
return stream->spl.spl_data.frequency; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
/* Function: al_get_audio_stream_length |
| 193 |
*/ |
| 194 |
unsigned int al_get_audio_stream_length(const ALLEGRO_AUDIO_STREAM *stream) |
| 195 |
{ |
| 196 |
ASSERT(stream); |
| 197 |
|
| 198 |
return stream->spl.spl_data.len; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
/* Function: al_get_audio_stream_fragments |
| 203 |
*/ |
| 204 |
unsigned int al_get_audio_stream_fragments(const ALLEGRO_AUDIO_STREAM *stream) |
| 205 |
{ |
| 206 |
ASSERT(stream); |
| 207 |
|
| 208 |
return stream->buf_count; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
/* Function: al_get_available_audio_stream_fragments |
| 213 |
*/ |
| 214 |
unsigned int al_get_available_audio_stream_fragments( |
| 215 |
const ALLEGRO_AUDIO_STREAM *stream) |
| 216 |
{ |
| 217 |
unsigned int i; |
| 218 |
ASSERT(stream); |
| 219 |
|
| 220 |
for (i = 0; i < stream->buf_count && stream->used_bufs[i]; i++) |
| 221 |
; |
| 222 |
return i; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
/* Function: al_get_audio_stream_speed |
| 227 |
*/ |
| 228 |
float al_get_audio_stream_speed(const ALLEGRO_AUDIO_STREAM *stream) |
| 229 |
{ |
| 230 |
ASSERT(stream); |
| 231 |
|
| 232 |
return stream->spl.speed; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
/* Function: al_get_audio_stream_gain |
| 237 |
*/ |
| 238 |
float al_get_audio_stream_gain(const ALLEGRO_AUDIO_STREAM *stream) |
| 239 |
{ |
| 240 |
ASSERT(stream); |
| 241 |
|
| 242 |
return stream->spl.gain; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
/* Function: al_get_audio_stream_pan |
| 247 |
*/ |
| 248 |
float al_get_audio_stream_pan(const ALLEGRO_AUDIO_STREAM *stream) |
| 249 |
{ |
| 250 |
ASSERT(stream); |
| 251 |
|
| 252 |
return stream->spl.pan; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
/* Function: al_get_audio_stream_channels |
| 257 |
*/ |
| 258 |
ALLEGRO_CHANNEL_CONF al_get_audio_stream_channels( |
| 259 |
const ALLEGRO_AUDIO_STREAM *stream) |
| 260 |
{ |
| 261 |
ASSERT(stream); |
| 262 |
|
| 263 |
return stream->spl.spl_data.chan_conf; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
/* Function: al_get_audio_stream_depth |
| 268 |
*/ |
| 269 |
ALLEGRO_AUDIO_DEPTH al_get_audio_stream_depth( |
| 270 |
const ALLEGRO_AUDIO_STREAM *stream) |
| 271 |
{ |
| 272 |
ASSERT(stream); |
| 273 |
|
| 274 |
return stream->spl.spl_data.depth; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
/* Function: al_get_audio_stream_playmode |
| 279 |
*/ |
| 280 |
ALLEGRO_PLAYMODE al_get_audio_stream_playmode( |
| 281 |
const ALLEGRO_AUDIO_STREAM *stream) |
| 282 |
{ |
| 283 |
ASSERT(stream); |
| 284 |
|
| 285 |
return stream->spl.loop; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
/* Function: al_get_audio_stream_playing |
| 290 |
*/ |
| 291 |
bool al_get_audio_stream_playing(const ALLEGRO_AUDIO_STREAM *stream) |
| 292 |
{ |
| 293 |
ASSERT(stream); |
| 294 |
|
| 295 |
return stream->spl.is_playing; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
/* Function: al_get_audio_stream_attached |
| 300 |
*/ |
| 301 |
bool al_get_audio_stream_attached(const ALLEGRO_AUDIO_STREAM *stream) |
| 302 |
{ |
| 303 |
ASSERT(stream); |
| 304 |
|
| 305 |
return (stream->spl.parent.u.ptr != NULL); |
| 306 |
} |
| 307 |
|
| 308 |
/* Function: al_get_audio_stream_played_samples |
| 309 |
*/ |
| 310 |
uint64_t al_get_audio_stream_played_samples(const ALLEGRO_AUDIO_STREAM *stream) |
| 311 |
{ |
| 312 |
uint64_t result; |
| 313 |
ALLEGRO_MUTEX *stream_mutex; |
| 314 |
ASSERT(stream); |
| 315 |
|
| 316 |
stream_mutex = maybe_lock_mutex(stream->spl.mutex); |
| 317 |
if (stream->spl.spl_data.buffer.ptr) { |
| 318 |
result = stream->consumed_fragments * stream->spl.spl_data.len + |
| 319 |
stream->spl.pos; |
| 320 |
} |
| 321 |
else { |
| 322 |
result = 0; |
| 323 |
} |
| 324 |
maybe_unlock_mutex(stream_mutex); |
| 325 |
|
| 326 |
return result; |
| 327 |
} |
| 328 |
|
| 329 |
/* Function: al_get_audio_stream_fragment |
| 330 |
*/ |
| 331 |
void *al_get_audio_stream_fragment(const ALLEGRO_AUDIO_STREAM *stream) |
| 332 |
{ |
| 333 |
size_t i; |
| 334 |
void *fragment; |
| 335 |
ALLEGRO_MUTEX *stream_mutex; |
| 336 |
ASSERT(stream); |
| 337 |
|
| 338 |
stream_mutex = maybe_lock_mutex(stream->spl.mutex); |
| 339 |
|
| 340 |
if (!stream->used_bufs[0]) { |
| 341 |
/* No free fragments are available. */ |
| 342 |
fragment = NULL; |
| 343 |
} |
| 344 |
else { |
| 345 |
fragment = stream->used_bufs[0]; |
| 346 |
for (i = 0; i < stream->buf_count-1 && stream->used_bufs[i]; i++) { |
| 347 |
stream->used_bufs[i] = stream->used_bufs[i+1]; |
| 348 |
} |
| 349 |
stream->used_bufs[i] = NULL; |
| 350 |
} |
| 351 |
|
| 352 |
maybe_unlock_mutex(stream_mutex); |
| 353 |
|
| 354 |
return fragment; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
/* Function: al_set_audio_stream_speed |
| 359 |
*/ |
| 360 |
bool al_set_audio_stream_speed(ALLEGRO_AUDIO_STREAM *stream, float val) |
| 361 |
{ |
| 362 |
ASSERT(stream); |
| 363 |
|
| 364 |
if (val <= 0.0f) { |
| 365 |
_al_set_error(ALLEGRO_INVALID_PARAM, |
| 366 |
"Attempted to set stream speed to a zero or negative value"); |
| 367 |
return false; |
| 368 |
} |
| 369 |
|
| 370 |
if (stream->spl.parent.u.ptr && stream->spl.parent.is_voice) { |
| 371 |
_al_set_error(ALLEGRO_GENERIC_ERROR, |
| 372 |
"Could not set voice playback speed"); |
| 373 |
return false; |
| 374 |
} |
| 375 |
|
| 376 |
stream->spl.speed = val; |
| 377 |
if (stream->spl.parent.u.mixer) { |
| 378 |
ALLEGRO_MIXER *mixer = stream->spl.parent.u.mixer; |
| 379 |
ALLEGRO_MUTEX *stream_mutex = maybe_lock_mutex(stream->spl.mutex); |
| 380 |
|
| 381 |
stream->spl.step = (stream->spl.spl_data.frequency) * stream->spl.speed; |
| 382 |
stream->spl.step_denom = mixer->ss.spl_data.frequency; |
| 383 |
/* Don't wanna be trapped with a step value of 0 */ |
| 384 |
if (stream->spl.step == 0) { |
| 385 |
stream->spl.step = 1; |
| 386 |
} |
| 387 |
|
| 388 |
maybe_unlock_mutex(stream_mutex); |
| 389 |
} |
| 390 |
|
| 391 |
return true; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
/* Function: al_set_audio_stream_gain |
| 396 |
*/ |
| 397 |
bool al_set_audio_stream_gain(ALLEGRO_AUDIO_STREAM *stream, float val) |
| 398 |
{ |
| 399 |
ASSERT(stream); |
| 400 |
|
| 401 |
if (stream->spl.parent.u.ptr && stream->spl.parent.is_voice) { |
| 402 |
_al_set_error(ALLEGRO_GENERIC_ERROR, |
| 403 |
"Could not set gain of stream attached to voice"); |
| 404 |
return false; |
| 405 |
} |
| 406 |
|
| 407 |
if (stream->spl.gain != val) { |
| 408 |
stream->spl.gain = val; |
| 409 |
|
| 410 |
/* If attached to a mixer already, need to recompute the sample |
| 411 |
* matrix to take into account the gain. |
| 412 |
*/ |
| 413 |
if (stream->spl.parent.u.mixer) { |
| 414 |
ALLEGRO_MIXER *mixer = stream->spl.parent.u.mixer; |
| 415 |
ALLEGRO_MUTEX *stream_mutex = maybe_lock_mutex(stream->spl.mutex); |
| 416 |
_al_kcm_mixer_rejig_sample_matrix(mixer, &stream->spl); |
| 417 |
maybe_unlock_mutex(stream_mutex); |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
return true; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
/* Function: al_set_audio_stream_pan |
| 426 |
*/ |
| 427 |
bool al_set_audio_stream_pan(ALLEGRO_AUDIO_STREAM *stream, float val) |
| 428 |
{ |
| 429 |
ASSERT(stream); |
| 430 |
|
| 431 |
if (stream->spl.parent.u.ptr && stream->spl.parent.is_voice) { |
| 432 |
_al_set_error(ALLEGRO_GENERIC_ERROR, |
| 433 |
"Could not set gain of stream attached to voice"); |
| 434 |
return false; |
| 435 |
} |
| 436 |
if (val != ALLEGRO_AUDIO_PAN_NONE && (val < -1.0 || val > 1.0)) { |
| 437 |
_al_set_error(ALLEGRO_GENERIC_ERROR, "Invalid pan value"); |
| 438 |
return false; |
| 439 |
} |
| 440 |
|
| 441 |
if (stream->spl.pan != val) { |
| 442 |
stream->spl.pan = val; |
| 443 |
|
| 444 |
/* If attached to a mixer already, need to recompute the sample |
| 445 |
* matrix to take into account the panning. |
| 446 |
*/ |
| 447 |
if (stream->spl.parent.u.mixer) { |
| 448 |
ALLEGRO_MIXER *mixer = stream->spl.parent.u.mixer; |
| 449 |
ALLEGRO_MUTEX *stream_mutex = maybe_lock_mutex(stream->spl.mutex); |
| 450 |
_al_kcm_mixer_rejig_sample_matrix(mixer, &stream->spl); |
| 451 |
maybe_unlock_mutex(stream_mutex); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
return true; |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
/* Function: al_set_audio_stream_playmode |
| 460 |
*/ |
| 461 |
bool al_set_audio_stream_playmode(ALLEGRO_AUDIO_STREAM *stream, |
| 462 |
ALLEGRO_PLAYMODE val) |
| 463 |
{ |
| 464 |
ASSERT(stream); |
| 465 |
bool ret = false; |
| 466 |
|
| 467 |
if (val == ALLEGRO_PLAYMODE_ONCE) { |
| 468 |
stream->spl.loop = _ALLEGRO_PLAYMODE_STREAM_ONCE; |
| 469 |
ret = true; |
| 470 |
} |
| 471 |
if (val == ALLEGRO_PLAYMODE_LOOP_ONCE) { |
| 472 |
stream->spl.loop = _ALLEGRO_PLAYMODE_STREAM_LOOP_ONCE; |
| 473 |
ret = true; |
| 474 |
} |
| 475 |
else if (val == ALLEGRO_PLAYMODE_LOOP) { |
| 476 |
/* Only streams creating by al_load_audio_stream() support |
| 477 |
* looping. */ |
| 478 |
if (!stream->feeder) { |
| 479 |
ret = false; |
| 480 |
} else { |
| 481 |
stream->spl.loop = _ALLEGRO_PLAYMODE_STREAM_ONEDIR; |
| 482 |
ret = true; |
| 483 |
} |
| 484 |
} |
| 485 |
if (ret) { |
| 486 |
stream->is_draining = false; |
| 487 |
} |
| 488 |
|
| 489 |
// XXX _al_set_error |
| 490 |
return ret; |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
static void reset_stopped_stream(ALLEGRO_AUDIO_STREAM *stream) |
| 495 |
{ |
| 496 |
const int bytes_per_sample = |
| 497 |
al_get_channel_count(stream->spl.spl_data.chan_conf) * |
| 498 |
al_get_audio_depth_size(stream->spl.spl_data.depth); |
| 499 |
const int fragment_buffer_size = |
| 500 |
bytes_per_sample * (stream->spl.spl_data.len + MAX_LAG); |
| 501 |
size_t i, n; |
| 502 |
|
| 503 |
/* Write silence to the "invisible" part in between fragment buffers to |
| 504 |
* avoid interpolation artifacts. It's tempting to zero the complete |
| 505 |
* memory block in one go but some of the buffers might be getting |
| 506 |
* refilled. So they are currently "owned" by the library user and |
| 507 |
* should not be overwritten. But zeroing the parts not visible to the |
| 508 |
* user should be OK. |
| 509 |
*/ |
| 510 |
for (i = 0; i < stream->buf_count; ++i) { |
| 511 |
al_fill_silence((char *)stream->main_buffer + i * fragment_buffer_size, |
| 512 |
MAX_LAG, stream->spl.spl_data.depth, stream->spl.spl_data.chan_conf); |
| 513 |
} |
| 514 |
|
| 515 |
/* Get the current number of entries in the used_buf list. */ |
| 516 |
for (n = 0; n < stream->buf_count && stream->used_bufs[n]; n++) |
| 517 |
; |
| 518 |
|
| 519 |
/* Move everything from pending_bufs to used_bufs. */ |
| 520 |
i = 0; |
| 521 |
while (i < stream->buf_count && |
| 522 |
n < stream->buf_count && |
| 523 |
stream->pending_bufs[i]) |
| 524 |
{ |
| 525 |
stream->used_bufs[n] = stream->pending_bufs[i]; |
| 526 |
stream->pending_bufs[i] = NULL; |
| 527 |
n++; |
| 528 |
i++; |
| 529 |
} |
| 530 |
|
| 531 |
/* No fragment buffer is currently playing. */ |
| 532 |
stream->spl.spl_data.buffer.ptr = NULL; |
| 533 |
stream->spl.pos = stream->spl.spl_data.len; |
| 534 |
stream->spl.pos_bresenham_error = 0; |
| 535 |
stream->consumed_fragments = 0; |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
/* Function: al_set_audio_stream_playing |
| 540 |
*/ |
| 541 |
bool al_set_audio_stream_playing(ALLEGRO_AUDIO_STREAM *stream, bool val) |
| 542 |
{ |
| 543 |
bool rc = true; |
| 544 |
ALLEGRO_MUTEX *stream_mutex; |
| 545 |
ASSERT(stream); |
| 546 |
|
| 547 |
if (stream->spl.parent.u.ptr && stream->spl.parent.is_voice) { |
| 548 |
ALLEGRO_VOICE *voice = stream->spl.parent.u.voice; |
| 549 |
if (val != stream->spl.is_playing) { |
| 550 |
rc = _al_kcm_set_voice_playing(voice, voice->mutex, val); |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
stream_mutex = maybe_lock_mutex(stream->spl.mutex); |
| 555 |
|
| 556 |
stream->spl.is_playing = rc && val; |
| 557 |
|
| 558 |
if (stream->spl.is_playing) { |
| 559 |
/* We usually emit fragment events when a fragment is used up, but if the |
| 560 |
* stream has zero pending fragments to begin with then no events would |
| 561 |
* ever be emitted. |
| 562 |
*/ |
| 563 |
_al_kcm_emit_stream_events(stream); |
| 564 |
} |
| 565 |
else if (!val) { |
| 566 |
reset_stopped_stream(stream); |
| 567 |
} |
| 568 |
|
| 569 |
maybe_unlock_mutex(stream_mutex); |
| 570 |
|
| 571 |
return rc; |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
/* Function: al_detach_audio_stream |
| 576 |
*/ |
| 577 |
bool al_detach_audio_stream(ALLEGRO_AUDIO_STREAM *stream) |
| 578 |
{ |
| 579 |
ASSERT(stream); |
| 580 |
|
| 581 |
_al_kcm_detach_from_parent(&stream->spl); |
| 582 |
ASSERT(stream->spl.spl_read == NULL); |
| 583 |
return !al_get_audio_stream_attached(stream); |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
/* Function: al_set_audio_stream_fragment |
| 588 |
*/ |
| 589 |
bool al_set_audio_stream_fragment(ALLEGRO_AUDIO_STREAM *stream, void *val) |
| 590 |
{ |
| 591 |
size_t i; |
| 592 |
bool ret; |
| 593 |
ALLEGRO_MUTEX *stream_mutex; |
| 594 |
ASSERT(stream); |
| 595 |
|
| 596 |
stream_mutex = maybe_lock_mutex(stream->spl.mutex); |
| 597 |
|
| 598 |
for (i = 0; i < stream->buf_count && stream->pending_bufs[i] ; i++) |
| 599 |
; |
| 600 |
if (i < stream->buf_count) { |
| 601 |
stream->pending_bufs[i] = val; |
| 602 |
ret = true; |
| 603 |
} |
| 604 |
else { |
| 605 |
_al_set_error(ALLEGRO_INVALID_OBJECT, |
| 606 |
"Attempted to set a stream buffer with a full pending list"); |
| 607 |
ret = false; |
| 608 |
} |
| 609 |
|
| 610 |
maybe_unlock_mutex(stream_mutex); |
| 611 |
|
| 612 |
return ret; |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
/* _al_kcm_refill_stream: |
| 617 |
* Called by the mixer when the current buffer has been used up. It should |
| 618 |
* point to the next pending buffer and adjust the sample position to reflect |
| 619 |
* the buffer being updated. It may be necessary to call this function multiple |
| 620 |
* times if the sample position is so far ahead that multiple buffers need to |
| 621 |
* be consumed. |
| 622 |
* Returns true if the next buffer is available and set up. |
| 623 |
* Otherwise returns false. |
| 624 |
*/ |
| 625 |
bool _al_kcm_refill_stream(ALLEGRO_AUDIO_STREAM *stream) |
| 626 |
{ |
| 627 |
ALLEGRO_SAMPLE_INSTANCE *spl = &stream->spl; |
| 628 |
void *old_buf = spl->spl_data.buffer.ptr; |
| 629 |
void *new_buf; |
| 630 |
size_t i; |
| 631 |
int new_pos = spl->pos - spl->spl_data.len; |
| 632 |
|
| 633 |
if (old_buf) { |
| 634 |
/* Slide the buffers down one position and put the |
| 635 |
* completed buffer into the used array to be refilled. |
| 636 |
*/ |
| 637 |
for (i = 0; |
| 638 |
i < stream->buf_count-1 && stream->pending_bufs[i]; |
| 639 |
i++) { |
| 640 |
stream->pending_bufs[i] = stream->pending_bufs[i+1]; |
| 641 |
} |
| 642 |
stream->pending_bufs[i] = NULL; |
| 643 |
|
| 644 |
for (i = 0; stream->used_bufs[i]; i++) |
| 645 |
; |
| 646 |
stream->used_bufs[i] = old_buf; |
| 647 |
} |
| 648 |
|
| 649 |
new_buf = stream->pending_bufs[0]; |
| 650 |
stream->spl.spl_data.buffer.ptr = new_buf; |
| 651 |
if (!new_buf) { |
| 652 |
ALLEGRO_WARN("Out of buffers\n"); |
| 653 |
return false; |
| 654 |
} |
| 655 |
|
| 656 |
/* Copy the last MAX_LAG sample values to the front of the new buffer |
| 657 |
* for interpolation. |
| 658 |
*/ |
| 659 |
if (old_buf) { |
| 660 |
const int bytes_per_sample = |
| 661 |
al_get_channel_count(spl->spl_data.chan_conf) * |
| 662 |
al_get_audio_depth_size(spl->spl_data.depth); |
| 663 |
|
| 664 |
memcpy( |
| 665 |
(char *) new_buf - bytes_per_sample * MAX_LAG, |
| 666 |
(char *) old_buf + bytes_per_sample * (spl->pos-MAX_LAG-new_pos), |
| 667 |
bytes_per_sample * MAX_LAG); |
| 668 |
|
| 669 |
stream->consumed_fragments++; |
| 670 |
} |
| 671 |
|
| 672 |
stream->spl.pos = new_pos; |
| 673 |
|
| 674 |
return true; |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
/* _al_kcm_feed_stream: |
| 679 |
* A routine running in another thread that feeds the stream buffers as |
| 680 |
* necessary, usually getting data from some file reader backend. |
| 681 |
*/ |
| 682 |
void *_al_kcm_feed_stream(ALLEGRO_THREAD *self, void *vstream) |
| 683 |
{ |
| 684 |
ALLEGRO_AUDIO_STREAM *stream = vstream; |
| 685 |
ALLEGRO_EVENT_QUEUE *queue; |
| 686 |
bool finished_event_sent = false; |
| 687 |
bool prefill = true; |
| 688 |
(void)self; |
| 689 |
|
| 690 |
ALLEGRO_DEBUG("Stream feeder thread started.\n"); |
| 691 |
|
| 692 |
queue = al_create_event_queue(); |
| 693 |
al_register_event_source(queue, &stream->spl.es); |
| 694 |
|
| 695 |
stream->quit_feed_thread = false; |
| 696 |
|
| 697 |
while (!stream->quit_feed_thread) { |
| 698 |
char *fragment; |
| 699 |
ALLEGRO_EVENT event; |
| 700 |
|
| 701 |
if (!prefill) |
| 702 |
al_wait_for_event(queue, &event); |
| 703 |
|
| 704 |
if ((prefill || event.type == ALLEGRO_EVENT_AUDIO_STREAM_FRAGMENT) |
| 705 |
&& !stream->is_draining) { |
| 706 |
unsigned long bytes; |
| 707 |
unsigned long bytes_written; |
| 708 |
ALLEGRO_MUTEX *stream_mutex; |
| 709 |
|
| 710 |
fragment = al_get_audio_stream_fragment(stream); |
| 711 |
if (!fragment) { |
| 712 |
/* This is not an error. */ |
| 713 |
continue; |
| 714 |
} |
| 715 |
|
| 716 |
bytes = (stream->spl.spl_data.len) * |
| 717 |
al_get_channel_count(stream->spl.spl_data.chan_conf) * |
| 718 |
al_get_audio_depth_size(stream->spl.spl_data.depth); |
| 719 |
|
| 720 |
stream_mutex = maybe_lock_mutex(stream->spl.mutex); |
| 721 |
bytes_written = stream->feeder(stream, fragment, bytes); |
| 722 |
maybe_unlock_mutex(stream_mutex); |
| 723 |
|
| 724 |
if (stream->spl.loop == _ALLEGRO_PLAYMODE_STREAM_ONEDIR) { |
| 725 |
/* Keep rewinding until the fragment is filled. */ |
| 726 |
while (bytes_written < bytes && |
| 727 |
stream->spl.loop == _ALLEGRO_PLAYMODE_STREAM_ONEDIR) { |
| 728 |
size_t bw; |
| 729 |
al_rewind_audio_stream(stream); |
| 730 |
stream_mutex = maybe_lock_mutex(stream->spl.mutex); |
| 731 |
bw = stream->feeder(stream, fragment + bytes_written, |
| 732 |
bytes - bytes_written); |
| 733 |
bytes_written += bw; |
| 734 |
maybe_unlock_mutex(stream_mutex); |
| 735 |
} |
| 736 |
} |
| 737 |
else if (bytes_written < bytes) { |
| 738 |
/* Fill the rest of the fragment with silence. */ |
| 739 |
int silence_samples = (bytes - bytes_written) / |
| 740 |
(al_get_channel_count(stream->spl.spl_data.chan_conf) * |
| 741 |
al_get_audio_depth_size(stream->spl.spl_data.depth)); |
| 742 |
al_fill_silence(fragment + bytes_written, silence_samples, |
| 743 |
stream->spl.spl_data.depth, stream->spl.spl_data.chan_conf); |
| 744 |
} |
| 745 |
|
| 746 |
if (!al_set_audio_stream_fragment(stream, fragment)) { |
| 747 |
ALLEGRO_ERROR("Error setting stream buffer.\n"); |
| 748 |
continue; |
| 749 |
} |
| 750 |
|
| 751 |
/* The streaming source doesn't feed any more, so drain the stream. |
| 752 |
* Don't quit in case the user decides to seek and then restart the |
| 753 |
* stream. */ |
| 754 |
if (bytes_written != bytes && |
| 755 |
(stream->spl.loop == _ALLEGRO_PLAYMODE_STREAM_ONCE || |
| 756 |
stream->spl.loop == _ALLEGRO_PLAYMODE_STREAM_LOOP_ONCE)) { |
| 757 |
/* Why not al_drain_audio_stream? We don't want to block on draining |
| 758 |
* because the user might adjust the stream loop points and restart |
| 759 |
* the stream. */ |
| 760 |
stream->is_draining = true; |
| 761 |
|
| 762 |
if (!finished_event_sent) { |
| 763 |
ALLEGRO_EVENT fin_event; |
| 764 |
fin_event.user.type = ALLEGRO_EVENT_AUDIO_STREAM_FINISHED; |
| 765 |
fin_event.user.timestamp = al_get_time(); |
| 766 |
al_emit_user_event(&stream->spl.es, &fin_event, NULL); |
| 767 |
finished_event_sent = true; |
| 768 |
} |
| 769 |
} else { |
| 770 |
finished_event_sent = false; |
| 771 |
} |
| 772 |
} |
| 773 |
else if (event.type == _KCM_STREAM_FEEDER_QUIT_EVENT_TYPE) { |
| 774 |
ALLEGRO_EVENT fin_event; |
| 775 |
stream->quit_feed_thread = true; |
| 776 |
|
| 777 |
fin_event.user.type = ALLEGRO_EVENT_AUDIO_STREAM_FINISHED; |
| 778 |
fin_event.user.timestamp = al_get_time(); |
| 779 |
al_emit_user_event(&stream->spl.es, &fin_event, NULL); |
| 780 |
} |
| 781 |
if (prefill) { |
| 782 |
al_lock_mutex(stream->feed_thread_started_mutex); |
| 783 |
stream->feed_thread_started = true; |
| 784 |
al_broadcast_cond(stream->feed_thread_started_cond); |
| 785 |
al_unlock_mutex(stream->feed_thread_started_mutex); |
| 786 |
} |
| 787 |
prefill = false; |
| 788 |
} |
| 789 |
|
| 790 |
al_destroy_event_queue(queue); |
| 791 |
|
| 792 |
ALLEGRO_DEBUG("Stream feeder thread finished.\n"); |
| 793 |
|
| 794 |
return NULL; |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
void _al_kcm_emit_stream_events(ALLEGRO_AUDIO_STREAM *stream) |
| 799 |
{ |
| 800 |
/* Emit one event for each stream fragment available right now. |
| 801 |
* |
| 802 |
* There may already be an event corresponding to an available fragment in |
| 803 |
* some event queue, but there's nothing we can do about that. Streams may |
| 804 |
* be added and removed from queues, events may be lost by the user, etc. |
| 805 |
* so it would be dangerous to assume that each fragment event would be |
| 806 |
* responded to, once and exactly once. |
| 807 |
* |
| 808 |
* Having said that, event queues are empty in the steady state so it is |
| 809 |
* relatively rare that this situation occurs. |
| 810 |
*/ |
| 811 |
int count = al_get_available_audio_stream_fragments(stream); |
| 812 |
|
| 813 |
while (count--) { |
| 814 |
ALLEGRO_EVENT event; |
| 815 |
event.user.type = ALLEGRO_EVENT_AUDIO_STREAM_FRAGMENT; |
| 816 |
event.user.timestamp = al_get_time(); |
| 817 |
al_emit_user_event(&stream->spl.es, &event, NULL); |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
/* Function: al_rewind_audio_stream |
| 823 |
*/ |
| 824 |
bool al_rewind_audio_stream(ALLEGRO_AUDIO_STREAM *stream) |
| 825 |
{ |
| 826 |
bool ret; |
| 827 |
|
| 828 |
if (stream->rewind_feeder) { |
| 829 |
ALLEGRO_MUTEX *stream_mutex = maybe_lock_mutex(stream->spl.mutex); |
| 830 |
ret = stream->rewind_feeder(stream); |
| 831 |
stream->is_draining = false; |
| 832 |
maybe_unlock_mutex(stream_mutex); |
| 833 |
return ret; |
| 834 |
} |
| 835 |
|
| 836 |
return false; |
| 837 |
} |
| 838 |
|
| 839 |
|
| 840 |
/* Function: al_seek_audio_stream_secs |
| 841 |
*/ |
| 842 |
bool al_seek_audio_stream_secs(ALLEGRO_AUDIO_STREAM *stream, double time) |
| 843 |
{ |
| 844 |
bool ret; |
| 845 |
|
| 846 |
if (stream->seek_feeder) { |
| 847 |
ALLEGRO_MUTEX *stream_mutex = maybe_lock_mutex(stream->spl.mutex); |
| 848 |
ret = stream->seek_feeder(stream, time); |
| 849 |
stream->is_draining = false; |
| 850 |
maybe_unlock_mutex(stream_mutex); |
| 851 |
return ret; |
| 852 |
} |
| 853 |
|
| 854 |
return false; |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
/* Function: al_get_audio_stream_position_secs |
| 859 |
*/ |
| 860 |
double al_get_audio_stream_position_secs(ALLEGRO_AUDIO_STREAM *stream) |
| 861 |
{ |
| 862 |
double ret; |
| 863 |
|
| 864 |
if (stream->get_feeder_position) { |
| 865 |
ALLEGRO_MUTEX *stream_mutex = maybe_lock_mutex(stream->spl.mutex); |
| 866 |
ret = stream->get_feeder_position(stream); |
| 867 |
maybe_unlock_mutex(stream_mutex); |
| 868 |
return ret; |
| 869 |
} |
| 870 |
|
| 871 |
return 0.0; |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
/* Function: al_get_audio_stream_length_secs |
| 876 |
*/ |
| 877 |
double al_get_audio_stream_length_secs(ALLEGRO_AUDIO_STREAM *stream) |
| 878 |
{ |
| 879 |
double ret; |
| 880 |
|
| 881 |
if (stream->get_feeder_length) { |
| 882 |
ALLEGRO_MUTEX *stream_mutex = maybe_lock_mutex(stream->spl.mutex); |
| 883 |
ret = stream->get_feeder_length(stream); |
| 884 |
maybe_unlock_mutex(stream_mutex); |
| 885 |
return ret; |
| 886 |
} |
| 887 |
|
| 888 |
return 0.0; |
| 889 |
} |
| 890 |
|
| 891 |
|
| 892 |
/* Function: al_set_audio_stream_loop_secs |
| 893 |
*/ |
| 894 |
bool al_set_audio_stream_loop_secs(ALLEGRO_AUDIO_STREAM *stream, |
| 895 |
double start, double end) |
| 896 |
{ |
| 897 |
bool ret; |
| 898 |
|
| 899 |
if (start >= end) |
| 900 |
return false; |
| 901 |
|
| 902 |
if (stream->set_feeder_loop) { |
| 903 |
ALLEGRO_MUTEX *stream_mutex = maybe_lock_mutex(stream->spl.mutex); |
| 904 |
ret = stream->set_feeder_loop(stream, start, end); |
| 905 |
stream->is_draining = false; |
| 906 |
maybe_unlock_mutex(stream_mutex); |
| 907 |
return ret; |
| 908 |
} |
| 909 |
|
| 910 |
return false; |
| 911 |
} |
| 912 |
|
| 913 |
|
| 914 |
/* Function: al_get_audio_stream_event_source |
| 915 |
*/ |
| 916 |
ALLEGRO_EVENT_SOURCE *al_get_audio_stream_event_source( |
| 917 |
ALLEGRO_AUDIO_STREAM *stream) |
| 918 |
{ |
| 919 |
return &stream->spl.es; |
| 920 |
} |
| 921 |
|
| 922 |
bool al_set_audio_stream_channel_matrix(ALLEGRO_AUDIO_STREAM *stream, const float *matrix) |
| 923 |
{ |
| 924 |
ASSERT(stream); |
| 925 |
|
| 926 |
if (stream->spl.parent.u.ptr && stream->spl.parent.is_voice) { |
| 927 |
_al_set_error(ALLEGRO_GENERIC_ERROR, |
| 928 |
"Could not set channel matrix of stream attached to voice"); |
| 929 |
return false; |
| 930 |
} |
| 931 |
|
| 932 |
return al_set_sample_instance_channel_matrix(&stream->spl, matrix); |
| 933 |
} |
| 934 |
|
| 935 |
/* vim: set sts=3 sw=3 et: */ |