| 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: Sample Instance functions |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <math.h> |
| 12 |
#include <stdio.h> |
| 13 |
|
| 14 |
#include "allegro5/allegro_audio.h" |
| 15 |
#include "allegro5/internal/aintern_audio.h" |
| 16 |
#include "allegro5/internal/aintern_audio_cfg.h" |
| 17 |
|
| 18 |
|
| 19 |
static void maybe_lock_mutex(ALLEGRO_MUTEX *mutex) |
| 20 |
{ |
| 21 |
if (mutex) { |
| 22 |
al_lock_mutex(mutex); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
static void maybe_unlock_mutex(ALLEGRO_MUTEX *mutex) |
| 28 |
{ |
| 29 |
if (mutex) { |
| 30 |
al_unlock_mutex(mutex); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
/* _al_kcm_stream_set_mutex: |
| 36 |
* This function sets a sample's mutex pointer to the specified value. It is |
| 37 |
* ALLEGRO_MIXER aware, and will recursively set any attached streams' mutex |
| 38 |
* to the same value. |
| 39 |
*/ |
| 40 |
void _al_kcm_stream_set_mutex(ALLEGRO_SAMPLE_INSTANCE *stream, ALLEGRO_MUTEX *mutex) |
| 41 |
{ |
| 42 |
ASSERT(stream); |
| 43 |
|
| 44 |
if (stream->mutex == mutex) |
| 45 |
return; |
| 46 |
stream->mutex = mutex; |
| 47 |
|
| 48 |
/* If this is a mixer, we need to make sure all the attached streams also |
| 49 |
* set the same mutex. |
| 50 |
*/ |
| 51 |
if (stream->is_mixer) { |
| 52 |
ALLEGRO_MIXER *mixer = (ALLEGRO_MIXER *)stream; |
| 53 |
int i; |
| 54 |
|
| 55 |
for (i = _al_vector_size(&mixer->streams) - 1; i >= 0; i--) { |
| 56 |
ALLEGRO_SAMPLE_INSTANCE **slot = _al_vector_ref(&mixer->streams, i); |
| 57 |
ALLEGRO_SAMPLE_INSTANCE *spl = *slot; |
| 58 |
_al_kcm_stream_set_mutex(spl, mutex); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
/* stream_free: |
| 65 |
* This function is ALLEGRO_MIXER aware and frees the memory associated with |
| 66 |
* the sample or mixer, and detaches any attached streams or mixers. |
| 67 |
*/ |
| 68 |
static void stream_free(ALLEGRO_SAMPLE_INSTANCE *spl) |
| 69 |
{ |
| 70 |
if (spl) { |
| 71 |
/* Make sure we free the mixer buffer and de-reference the attached |
| 72 |
* streams if this is a mixer stream. |
| 73 |
*/ |
| 74 |
if (spl->is_mixer) { |
| 75 |
ALLEGRO_MIXER *mixer = (ALLEGRO_MIXER *)spl; |
| 76 |
int i; |
| 77 |
|
| 78 |
_al_kcm_stream_set_mutex(&mixer->ss, NULL); |
| 79 |
|
| 80 |
for (i = _al_vector_size(&mixer->streams) - 1; i >= 0; i--) { |
| 81 |
ALLEGRO_SAMPLE_INSTANCE **slot = _al_vector_ref(&mixer->streams, i); |
| 82 |
ALLEGRO_SAMPLE_INSTANCE *spl = *slot; |
| 83 |
|
| 84 |
spl->parent.u.ptr = NULL; |
| 85 |
spl->spl_read = NULL; |
| 86 |
al_free(spl->matrix); |
| 87 |
spl->matrix = NULL; |
| 88 |
} |
| 89 |
|
| 90 |
_al_vector_free(&mixer->streams); |
| 91 |
|
| 92 |
if (spl->spl_data.buffer.ptr) { |
| 93 |
ASSERT(spl->spl_data.free_buf); |
| 94 |
al_free(spl->spl_data.buffer.ptr); |
| 95 |
spl->spl_data.buffer.ptr = NULL; |
| 96 |
} |
| 97 |
spl->spl_data.free_buf = false; |
| 98 |
} |
| 99 |
|
| 100 |
ASSERT(! spl->spl_data.free_buf); |
| 101 |
|
| 102 |
al_free(spl); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
/* _al_kcm_detach_from_parent: |
| 108 |
* This detaches the sample, stream, or mixer from anything it may be attached |
| 109 |
* to. |
| 110 |
*/ |
| 111 |
void _al_kcm_detach_from_parent(ALLEGRO_SAMPLE_INSTANCE *spl) |
| 112 |
{ |
| 113 |
ALLEGRO_MIXER *mixer; |
| 114 |
int i; |
| 115 |
|
| 116 |
if (!spl || !spl->parent.u.ptr) |
| 117 |
return; |
| 118 |
|
| 119 |
if (spl->parent.is_voice) { |
| 120 |
al_detach_voice(spl->parent.u.voice); |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
mixer = spl->parent.u.mixer; |
| 125 |
|
| 126 |
/* Search through the streams and check for this one */ |
| 127 |
maybe_lock_mutex(mixer->ss.mutex); |
| 128 |
for (i = _al_vector_size(&mixer->streams) - 1; i >= 0; i--) { |
| 129 |
ALLEGRO_SAMPLE_INSTANCE **slot = _al_vector_ref(&mixer->streams, i); |
| 130 |
|
| 131 |
if (*slot == spl) { |
| 132 |
_al_vector_delete_at(&mixer->streams, i); |
| 133 |
spl->parent.u.mixer = NULL; |
| 134 |
_al_kcm_stream_set_mutex(spl, NULL); |
| 135 |
|
| 136 |
spl->spl_read = NULL; |
| 137 |
break; |
| 138 |
} |
| 139 |
} |
| 140 |
maybe_unlock_mutex(mixer->ss.mutex); |
| 141 |
|
| 142 |
al_free(spl->matrix); |
| 143 |
spl->matrix = NULL; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
/* Function: al_create_sample_instance |
| 148 |
*/ |
| 149 |
ALLEGRO_SAMPLE_INSTANCE *al_create_sample_instance(ALLEGRO_SAMPLE *sample_data) |
| 150 |
{ |
| 151 |
ALLEGRO_SAMPLE_INSTANCE *spl; |
| 152 |
|
| 153 |
spl = al_calloc(1, sizeof(*spl)); |
| 154 |
if (!spl) { |
| 155 |
_al_set_error(ALLEGRO_GENERIC_ERROR, |
| 156 |
"Out of memory allocating sample object"); |
| 157 |
return NULL; |
| 158 |
} |
| 159 |
|
| 160 |
if (sample_data) { |
| 161 |
spl->spl_data = *sample_data; |
| 162 |
} |
| 163 |
spl->spl_data.free_buf = false; |
| 164 |
|
| 165 |
spl->loop = ALLEGRO_PLAYMODE_ONCE; |
| 166 |
spl->speed = 1.0f; |
| 167 |
spl->gain = 1.0f; |
| 168 |
spl->pan = 0.0f; |
| 169 |
spl->pos = 0; |
| 170 |
spl->loop_start = 0; |
| 171 |
spl->loop_end = sample_data ? sample_data->len : 0; |
| 172 |
spl->step = 0; |
| 173 |
|
| 174 |
spl->matrix = NULL; |
| 175 |
|
| 176 |
spl->is_mixer = false; |
| 177 |
spl->spl_read = NULL; |
| 178 |
|
| 179 |
spl->mutex = NULL; |
| 180 |
spl->parent.u.ptr = NULL; |
| 181 |
|
| 182 |
spl->dtor_item = _al_kcm_register_destructor("sample_instance", spl, |
| 183 |
(void (*)(void *))al_destroy_sample_instance); |
| 184 |
|
| 185 |
return spl; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
/* This function is ALLEGRO_MIXER aware */ |
| 190 |
/* Function: al_destroy_sample_instance |
| 191 |
*/ |
| 192 |
void al_destroy_sample_instance(ALLEGRO_SAMPLE_INSTANCE *spl) |
| 193 |
{ |
| 194 |
_al_kcm_destroy_sample(spl, true); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
/* Internal function: _al_kcm_destroy_sample |
| 199 |
*/ |
| 200 |
void _al_kcm_destroy_sample(ALLEGRO_SAMPLE_INSTANCE *spl, bool unregister) |
| 201 |
{ |
| 202 |
if (spl) { |
| 203 |
if (unregister) { |
| 204 |
_al_kcm_unregister_destructor(spl->dtor_item); |
| 205 |
} |
| 206 |
|
| 207 |
_al_kcm_detach_from_parent(spl); |
| 208 |
stream_free(spl); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
/* Function: al_play_sample_instance |
| 214 |
*/ |
| 215 |
bool al_play_sample_instance(ALLEGRO_SAMPLE_INSTANCE *spl) |
| 216 |
{ |
| 217 |
ASSERT(spl); |
| 218 |
|
| 219 |
return al_set_sample_instance_playing(spl, true); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
/* Function: al_stop_sample_instance |
| 224 |
*/ |
| 225 |
bool al_stop_sample_instance(ALLEGRO_SAMPLE_INSTANCE *spl) |
| 226 |
{ |
| 227 |
ASSERT(spl); |
| 228 |
|
| 229 |
return al_set_sample_instance_playing(spl, false); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
/* Function: al_get_sample_instance_frequency |
| 234 |
*/ |
| 235 |
unsigned int al_get_sample_instance_frequency(const ALLEGRO_SAMPLE_INSTANCE *spl) |
| 236 |
{ |
| 237 |
ASSERT(spl); |
| 238 |
|
| 239 |
return spl->spl_data.frequency; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
/* Function: al_get_sample_instance_length |
| 244 |
*/ |
| 245 |
unsigned int al_get_sample_instance_length(const ALLEGRO_SAMPLE_INSTANCE *spl) |
| 246 |
{ |
| 247 |
ASSERT(spl); |
| 248 |
|
| 249 |
return spl->spl_data.len; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
/* Function: al_get_sample_instance_position |
| 254 |
*/ |
| 255 |
unsigned int al_get_sample_instance_position(const ALLEGRO_SAMPLE_INSTANCE *spl) |
| 256 |
{ |
| 257 |
ASSERT(spl); |
| 258 |
|
| 259 |
if (spl->parent.u.ptr && spl->parent.is_voice) { |
| 260 |
ALLEGRO_VOICE *voice = spl->parent.u.voice; |
| 261 |
return al_get_voice_position(voice); |
| 262 |
} |
| 263 |
|
| 264 |
return spl->pos; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
/* Function: al_get_sample_instance_speed |
| 269 |
*/ |
| 270 |
float al_get_sample_instance_speed(const ALLEGRO_SAMPLE_INSTANCE *spl) |
| 271 |
{ |
| 272 |
ASSERT(spl); |
| 273 |
|
| 274 |
return spl->speed; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
/* Function: al_get_sample_instance_gain |
| 279 |
*/ |
| 280 |
float al_get_sample_instance_gain(const ALLEGRO_SAMPLE_INSTANCE *spl) |
| 281 |
{ |
| 282 |
ASSERT(spl); |
| 283 |
|
| 284 |
return spl->gain; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
/* Function: al_get_sample_instance_pan |
| 289 |
*/ |
| 290 |
float al_get_sample_instance_pan(const ALLEGRO_SAMPLE_INSTANCE *spl) |
| 291 |
{ |
| 292 |
ASSERT(spl); |
| 293 |
|
| 294 |
return spl->pan; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
/* Function: al_get_sample_instance_time |
| 299 |
*/ |
| 300 |
float al_get_sample_instance_time(const ALLEGRO_SAMPLE_INSTANCE *spl) |
| 301 |
{ |
| 302 |
ASSERT(spl); |
| 303 |
|
| 304 |
return (float)(spl->spl_data.len) |
| 305 |
/ (float)spl->spl_data.frequency; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
/* Function: al_get_sample_instance_depth |
| 310 |
*/ |
| 311 |
ALLEGRO_AUDIO_DEPTH al_get_sample_instance_depth(const ALLEGRO_SAMPLE_INSTANCE *spl) |
| 312 |
{ |
| 313 |
ASSERT(spl); |
| 314 |
|
| 315 |
return spl->spl_data.depth; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
/* Function: al_get_sample_instance_channels |
| 320 |
*/ |
| 321 |
ALLEGRO_CHANNEL_CONF al_get_sample_instance_channels( |
| 322 |
const ALLEGRO_SAMPLE_INSTANCE *spl) |
| 323 |
{ |
| 324 |
ASSERT(spl); |
| 325 |
|
| 326 |
return spl->spl_data.chan_conf; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
/* Function: al_get_sample_instance_playmode |
| 331 |
*/ |
| 332 |
ALLEGRO_PLAYMODE al_get_sample_instance_playmode(const ALLEGRO_SAMPLE_INSTANCE *spl) |
| 333 |
{ |
| 334 |
ASSERT(spl); |
| 335 |
|
| 336 |
return spl->loop; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
/* Function: al_get_sample_instance_playing |
| 341 |
*/ |
| 342 |
bool al_get_sample_instance_playing(const ALLEGRO_SAMPLE_INSTANCE *spl) |
| 343 |
{ |
| 344 |
ASSERT(spl); |
| 345 |
|
| 346 |
if (spl->parent.u.ptr && spl->parent.is_voice) { |
| 347 |
ALLEGRO_VOICE *voice = spl->parent.u.voice; |
| 348 |
return al_get_voice_playing(voice); |
| 349 |
} |
| 350 |
|
| 351 |
return spl->is_playing; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
/* Function: al_get_sample_instance_attached |
| 356 |
*/ |
| 357 |
bool al_get_sample_instance_attached(const ALLEGRO_SAMPLE_INSTANCE *spl) |
| 358 |
{ |
| 359 |
ASSERT(spl); |
| 360 |
|
| 361 |
return (spl->parent.u.ptr != NULL); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
/* Function: al_set_sample_instance_position |
| 366 |
*/ |
| 367 |
bool al_set_sample_instance_position(ALLEGRO_SAMPLE_INSTANCE *spl, |
| 368 |
unsigned int val) |
| 369 |
{ |
| 370 |
ASSERT(spl); |
| 371 |
|
| 372 |
if (spl->parent.u.ptr && spl->parent.is_voice) { |
| 373 |
ALLEGRO_VOICE *voice = spl->parent.u.voice; |
| 374 |
if (!al_set_voice_position(voice, val)) |
| 375 |
return false; |
| 376 |
} |
| 377 |
else { |
| 378 |
maybe_lock_mutex(spl->mutex); |
| 379 |
spl->pos = val; |
| 380 |
maybe_unlock_mutex(spl->mutex); |
| 381 |
} |
| 382 |
|
| 383 |
return true; |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
/* Function: al_set_sample_instance_length |
| 388 |
*/ |
| 389 |
bool al_set_sample_instance_length(ALLEGRO_SAMPLE_INSTANCE *spl, |
| 390 |
unsigned int val) |
| 391 |
{ |
| 392 |
ASSERT(spl); |
| 393 |
|
| 394 |
if (spl->is_playing) { |
| 395 |
_al_set_error(ALLEGRO_INVALID_OBJECT, |
| 396 |
"Attempted to change the length of a playing sample"); |
| 397 |
return false; |
| 398 |
} |
| 399 |
|
| 400 |
spl->spl_data.len = val; |
| 401 |
spl->loop_end = val; |
| 402 |
return true; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
/* Function: al_set_sample_instance_speed |
| 407 |
*/ |
| 408 |
bool al_set_sample_instance_speed(ALLEGRO_SAMPLE_INSTANCE *spl, float val) |
| 409 |
{ |
| 410 |
ASSERT(spl); |
| 411 |
|
| 412 |
if (fabsf(val) < (1.0f/64.0f)) { |
| 413 |
_al_set_error(ALLEGRO_INVALID_PARAM, |
| 414 |
"Attempted to set zero speed"); |
| 415 |
return false; |
| 416 |
} |
| 417 |
|
| 418 |
if (spl->parent.u.ptr && spl->parent.is_voice) { |
| 419 |
_al_set_error(ALLEGRO_GENERIC_ERROR, |
| 420 |
"Could not set voice playback speed"); |
| 421 |
return false; |
| 422 |
} |
| 423 |
|
| 424 |
spl->speed = val; |
| 425 |
if (spl->parent.u.mixer) { |
| 426 |
ALLEGRO_MIXER *mixer = spl->parent.u.mixer; |
| 427 |
|
| 428 |
maybe_lock_mutex(spl->mutex); |
| 429 |
|
| 430 |
spl->step = (spl->spl_data.frequency) * spl->speed; |
| 431 |
spl->step_denom = mixer->ss.spl_data.frequency; |
| 432 |
/* Don't wanna be trapped with a step value of 0 */ |
| 433 |
if (spl->step == 0) { |
| 434 |
if (spl->speed > 0.0f) |
| 435 |
spl->step = 1; |
| 436 |
else |
| 437 |
spl->step = -1; |
| 438 |
} |
| 439 |
|
| 440 |
maybe_unlock_mutex(spl->mutex); |
| 441 |
} |
| 442 |
|
| 443 |
return true; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
/* Function: al_set_sample_instance_gain |
| 448 |
*/ |
| 449 |
bool al_set_sample_instance_gain(ALLEGRO_SAMPLE_INSTANCE *spl, float val) |
| 450 |
{ |
| 451 |
ASSERT(spl); |
| 452 |
|
| 453 |
if (spl->parent.u.ptr && spl->parent.is_voice) { |
| 454 |
_al_set_error(ALLEGRO_GENERIC_ERROR, |
| 455 |
"Could not set gain of sample attached to voice"); |
| 456 |
return false; |
| 457 |
} |
| 458 |
|
| 459 |
if (spl->gain != val) { |
| 460 |
spl->gain = val; |
| 461 |
|
| 462 |
/* If attached to a mixer already, need to recompute the sample |
| 463 |
* matrix to take into account the gain. |
| 464 |
*/ |
| 465 |
if (spl->parent.u.mixer) { |
| 466 |
ALLEGRO_MIXER *mixer = spl->parent.u.mixer; |
| 467 |
|
| 468 |
maybe_lock_mutex(spl->mutex); |
| 469 |
_al_kcm_mixer_rejig_sample_matrix(mixer, spl); |
| 470 |
maybe_unlock_mutex(spl->mutex); |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
return true; |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
/* Function: al_set_sample_instance_pan |
| 479 |
*/ |
| 480 |
bool al_set_sample_instance_pan(ALLEGRO_SAMPLE_INSTANCE *spl, float val) |
| 481 |
{ |
| 482 |
ASSERT(spl); |
| 483 |
|
| 484 |
if (spl->parent.u.ptr && spl->parent.is_voice) { |
| 485 |
_al_set_error(ALLEGRO_GENERIC_ERROR, |
| 486 |
"Could not set panning of sample attached to voice"); |
| 487 |
return false; |
| 488 |
} |
| 489 |
if (val != ALLEGRO_AUDIO_PAN_NONE && (val < -1.0 || val > 1.0)) { |
| 490 |
_al_set_error(ALLEGRO_GENERIC_ERROR, "Invalid pan value"); |
| 491 |
return false; |
| 492 |
} |
| 493 |
|
| 494 |
if (spl->pan != val) { |
| 495 |
spl->pan = val; |
| 496 |
|
| 497 |
/* If attached to a mixer already, need to recompute the sample |
| 498 |
* matrix to take into account the panning. |
| 499 |
*/ |
| 500 |
if (spl->parent.u.mixer) { |
| 501 |
ALLEGRO_MIXER *mixer = spl->parent.u.mixer; |
| 502 |
|
| 503 |
maybe_lock_mutex(spl->mutex); |
| 504 |
_al_kcm_mixer_rejig_sample_matrix(mixer, spl); |
| 505 |
maybe_unlock_mutex(spl->mutex); |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
return true; |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
/* Function: al_set_sample_instance_playmode |
| 514 |
*/ |
| 515 |
bool al_set_sample_instance_playmode(ALLEGRO_SAMPLE_INSTANCE *spl, |
| 516 |
ALLEGRO_PLAYMODE val) |
| 517 |
{ |
| 518 |
ASSERT(spl); |
| 519 |
|
| 520 |
if (val < ALLEGRO_PLAYMODE_ONCE || val > ALLEGRO_PLAYMODE_BIDIR) { |
| 521 |
_al_set_error(ALLEGRO_INVALID_PARAM, |
| 522 |
"Invalid loop mode"); |
| 523 |
return false; |
| 524 |
} |
| 525 |
|
| 526 |
maybe_lock_mutex(spl->mutex); |
| 527 |
|
| 528 |
spl->loop = val; |
| 529 |
if (spl->loop != ALLEGRO_PLAYMODE_ONCE) { |
| 530 |
if (spl->pos < spl->loop_start) |
| 531 |
spl->pos = spl->loop_start; |
| 532 |
else if (spl->pos > spl->loop_end-1) |
| 533 |
spl->pos = spl->loop_end-1; |
| 534 |
} |
| 535 |
|
| 536 |
maybe_unlock_mutex(spl->mutex); |
| 537 |
|
| 538 |
return true; |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
/* Function: al_set_sample_instance_playing |
| 543 |
*/ |
| 544 |
bool al_set_sample_instance_playing(ALLEGRO_SAMPLE_INSTANCE *spl, bool val) |
| 545 |
{ |
| 546 |
ASSERT(spl); |
| 547 |
|
| 548 |
if (!spl->parent.u.ptr || !spl->spl_data.buffer.ptr) { |
| 549 |
spl->is_playing = val; |
| 550 |
return true; |
| 551 |
} |
| 552 |
|
| 553 |
if (spl->parent.is_voice) { |
| 554 |
/* parent is voice */ |
| 555 |
ALLEGRO_VOICE *voice = spl->parent.u.voice; |
| 556 |
|
| 557 |
return al_set_voice_playing(voice, val); |
| 558 |
} |
| 559 |
|
| 560 |
/* parent is mixer */ |
| 561 |
maybe_lock_mutex(spl->mutex); |
| 562 |
spl->is_playing = val; |
| 563 |
if (!val) |
| 564 |
spl->pos = 0; |
| 565 |
maybe_unlock_mutex(spl->mutex); |
| 566 |
return true; |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
/* Function: al_detach_sample_instance |
| 571 |
*/ |
| 572 |
bool al_detach_sample_instance(ALLEGRO_SAMPLE_INSTANCE *spl) |
| 573 |
{ |
| 574 |
ASSERT(spl); |
| 575 |
|
| 576 |
_al_kcm_detach_from_parent(spl); |
| 577 |
ASSERT(spl->spl_read == NULL); |
| 578 |
return true; |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
/* Function: al_set_sample |
| 583 |
*/ |
| 584 |
bool al_set_sample(ALLEGRO_SAMPLE_INSTANCE *spl, ALLEGRO_SAMPLE *data) |
| 585 |
{ |
| 586 |
sample_parent_t old_parent; |
| 587 |
bool need_reattach; |
| 588 |
|
| 589 |
ASSERT(spl); |
| 590 |
|
| 591 |
/* Stop the sample if it is playing. */ |
| 592 |
if (spl->is_playing) { |
| 593 |
if (!al_set_sample_instance_playing(spl, false)) { |
| 594 |
/* Shouldn't happen. */ |
| 595 |
ASSERT(false); |
| 596 |
return false; |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
if (!data) { |
| 601 |
if (spl->parent.u.ptr) { |
| 602 |
_al_kcm_detach_from_parent(spl); |
| 603 |
} |
| 604 |
spl->spl_data.buffer.ptr = NULL; |
| 605 |
return true; |
| 606 |
} |
| 607 |
|
| 608 |
/* Have data. */ |
| 609 |
|
| 610 |
need_reattach = false; |
| 611 |
if (spl->parent.u.ptr != NULL) { |
| 612 |
if (spl->spl_data.frequency != data->frequency || |
| 613 |
spl->spl_data.depth != data->depth || |
| 614 |
spl->spl_data.chan_conf != data->chan_conf) { |
| 615 |
old_parent = spl->parent; |
| 616 |
need_reattach = true; |
| 617 |
_al_kcm_detach_from_parent(spl); |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
spl->spl_data = *data; |
| 622 |
spl->spl_data.free_buf = false; |
| 623 |
spl->pos = 0; |
| 624 |
spl->loop_start = 0; |
| 625 |
spl->loop_end = data->len; |
| 626 |
/* Should we reset the loop mode? */ |
| 627 |
|
| 628 |
if (need_reattach) { |
| 629 |
if (old_parent.is_voice) { |
| 630 |
if (!al_attach_sample_instance_to_voice(spl, old_parent.u.voice)) { |
| 631 |
spl->spl_data.buffer.ptr = NULL; |
| 632 |
return false; |
| 633 |
} |
| 634 |
} |
| 635 |
else { |
| 636 |
if (!al_attach_sample_instance_to_mixer(spl, old_parent.u.mixer)) { |
| 637 |
spl->spl_data.buffer.ptr = NULL; |
| 638 |
return false; |
| 639 |
} |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
return true; |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
/* Function: al_get_sample |
| 648 |
*/ |
| 649 |
ALLEGRO_SAMPLE *al_get_sample(ALLEGRO_SAMPLE_INSTANCE *spl) |
| 650 |
{ |
| 651 |
ASSERT(spl); |
| 652 |
|
| 653 |
return &(spl->spl_data); |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
/* Function: al_set_sample_instance_channel_matrix |
| 658 |
*/ |
| 659 |
bool al_set_sample_instance_channel_matrix(ALLEGRO_SAMPLE_INSTANCE *spl, const float *matrix) |
| 660 |
{ |
| 661 |
ASSERT(spl); |
| 662 |
ASSERT(matrix); |
| 663 |
|
| 664 |
if (spl->parent.u.ptr && spl->parent.is_voice) { |
| 665 |
_al_set_error(ALLEGRO_GENERIC_ERROR, |
| 666 |
"Could not set channel matrix of sample attached to voice"); |
| 667 |
return false; |
| 668 |
} |
| 669 |
|
| 670 |
if (spl->parent.u.mixer) { |
| 671 |
ALLEGRO_MIXER *mixer = spl->parent.u.mixer; |
| 672 |
size_t dst_chans = al_get_channel_count(mixer->ss.spl_data.chan_conf); |
| 673 |
size_t src_chans = al_get_channel_count(spl->spl_data.chan_conf); |
| 674 |
ASSERT(spl->matrix); |
| 675 |
|
| 676 |
maybe_lock_mutex(spl->mutex); |
| 677 |
|
| 678 |
memcpy(spl->matrix, matrix, dst_chans * src_chans * sizeof(float)); |
| 679 |
|
| 680 |
maybe_unlock_mutex(spl->mutex); |
| 681 |
} |
| 682 |
|
| 683 |
return true; |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
/* vim: set sts=3 sw=3 et: */ |