1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
|
/*
(c) Copyright 2012-2013 DirectFB integrated media GmbH
(c) Copyright 2001-2013 The world wide DirectFB Open Source Community (directfb.org)
(c) Copyright 2000-2004 Convergence (integrated media) GmbH
All rights reserved.
Written by Denis Oliver Kropp <dok@directfb.org>,
Andreas Shimokawa <andi@directfb.org>,
Marek Pikarski <mass@directfb.org>,
Sven Neumann <neo@directfb.org>,
Ville Syrjälä <syrjala@sci.fi> and
Claudio Ciccani <klan@users.sf.net>.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <stdlib.h>
#include <direct/debug.h>
#include <direct/messages.h>
#include <direct/util.h>
#include <fusion/lock.h>
#include <core/core_sound.h>
#include <core/sound_buffer.h>
#include <core/playback.h>
#include <core/playback_internal.h>
#define DOWNMIX_LEVEL_3DB 0.70794578438413791
/******************************************************************************/
static void fs_playback_notify( CorePlayback *playback,
CorePlaybackNotificationFlags flags,
int num );
/******************************************************************************/
static void
playback_destructor( FusionObject *object, bool zombie, void *ctx )
{
CorePlayback *playback = (CorePlayback*) object;
D_DEBUG( "FusionSound/Core: %s (%p, buffer: %p)%s\n", __FUNCTION__,
playback, playback->buffer, zombie ? " ZOMBIE!" : "" );
fs_buffer_unlink( &playback->buffer );
fusion_skirmish_destroy( &playback->lock );
fusion_object_destroy( object );
}
FusionObjectPool *
fs_playback_pool_create( const FusionWorld *world )
{
return fusion_object_pool_create( "Playbacks", sizeof(CorePlayback),
sizeof(CorePlaybackNotification),
playback_destructor, NULL, world );
}
/******************************************************************************/
DirectResult
fs_playback_create( CoreSound *core,
CoreSoundBuffer *buffer,
bool notify,
CorePlayback **ret_playback )
{
CorePlayback *playback;
float volume;
D_ASSERT( buffer != NULL );
D_ASSERT( ret_playback != NULL );
D_DEBUG( "FusionSound/Core: %s (buffer %p, notify %d)\n",
__FUNCTION__, buffer, notify );
/* Create playback object. */
playback = fs_core_create_playback( core );
if (!playback)
return DR_FUSION;
/* Link buffer to playback object. */
if (fs_buffer_link( &playback->buffer, buffer )) {
fusion_object_destroy( &playback->object );
return DR_FUSION;
}
fusion_skirmish_init( &playback->lock, "FusionSound Playback", fs_core_world(core) );
/* Initialize private data. */
playback->core = core;
playback->notify = notify;
playback->pitch = FS_PITCH_ONE;
/* Set default downmixing levels. */
fs_playback_set_downmix( playback, DOWNMIX_LEVEL_3DB, DOWNMIX_LEVEL_3DB );
/* Set default volume levels. */
playback->levels[0] = FSF_ONE;
playback->levels[1] = FSF_ONE;
playback->levels[2] = playback->center;
playback->levels[3] = playback->rear;
playback->levels[4] = playback->rear;
playback->levels[5] = FSF_ONE;
/* Get local volume level. */
fs_core_get_local_volume( core, &volume );
playback->volume = fsf_from_float( volume );
/* Activate playback object. */
fusion_object_activate( &playback->object );
/* Return playback object. */
*ret_playback = playback;
return DR_OK;
}
DirectResult
fs_playback_enable( CorePlayback *playback )
{
DirectResult ret = DR_OK;
D_ASSERT( playback != NULL );
D_ASSERT( playback->buffer != NULL );
/* Lock playback. */
if (fusion_skirmish_prevail( &playback->lock ))
return DR_FUSION;
/* Enable playback. */
playback->disabled = false;
/* Unlock playback. */
fusion_skirmish_dismiss( &playback->lock );
return ret;
}
DirectResult
fs_playback_start( CorePlayback *playback, bool enable )
{
DirectResult ret = DR_OK;
D_ASSERT( playback != NULL );
D_ASSERT( playback->buffer != NULL );
/* Lock playlist. */
if (fs_core_playlist_lock( playback->core ))
return DR_FUSION;
/* Lock playback. */
if (fusion_skirmish_prevail( &playback->lock )) {
fs_core_playlist_unlock( playback->core );
return DR_FUSION;
}
/* If the playback is disabled, it won't begin to play. */
if (enable)
playback->disabled = false;
/* Start the playback if it's not running already. */
if (!playback->running) {
if (playback->disabled) {
ret = DR_TEMPUNAVAIL;
}
else {
ret = fs_core_add_playback( playback->core, playback );
/* Notify listeners about the start of the playback. */
if (ret == DR_OK)
fs_playback_notify( playback, CPNF_START, 0 );
}
}
/* Unlock playback. */
fusion_skirmish_dismiss( &playback->lock );
/* Unlock playlist. */
fs_core_playlist_unlock( playback->core );
return ret;
}
DirectResult
fs_playback_stop( CorePlayback *playback, bool disable )
{
D_ASSERT( playback != NULL );
/* Lock playlist. */
if (fs_core_playlist_lock( playback->core ))
return DR_FUSION;
/* Lock playback. */
if (fusion_skirmish_prevail( &playback->lock )) {
fs_core_playlist_unlock( playback->core );
return DR_FUSION;
}
/* Stop the playback if it's running. */
if (playback->running) {
fs_core_remove_playback( playback->core, playback );
/* Notify listeners about the end of the playback. */
fs_playback_notify( playback, CPNF_STOP, 0 );
}
/* If this the playback is disabled, it can only be started with enable = true. */
if (disable)
playback->disabled = true;
/* Unlock playback. */
fusion_skirmish_dismiss( &playback->lock );
/* Unlock playlist. */
fs_core_playlist_unlock( playback->core );
return DR_OK;
}
DirectResult
fs_playback_set_stop( CorePlayback *playback,
int stop )
{
D_ASSERT( playback != NULL );
D_ASSERT( playback->buffer != NULL );
D_ASSERT( stop <= playback->buffer->length );
/* Lock playback. */
if (fusion_skirmish_prevail( &playback->lock ))
return DR_FUSION;
/* Adjust stop position. */
playback->stop = stop;
/* Unlock playback. */
fusion_skirmish_dismiss( &playback->lock );
return DR_OK;
}
DirectResult
fs_playback_set_position( CorePlayback *playback,
int position )
{
D_ASSERT( playback != NULL );
D_ASSERT( playback->buffer != NULL );
D_ASSERT( position >= 0 );
D_ASSERT( position < playback->buffer->length );
/* Lock playback. */
if (fusion_skirmish_prevail( &playback->lock ))
return DR_FUSION;
/* Adjust the playback position. */
playback->position = position;
/* Unlock playback. */
fusion_skirmish_dismiss( &playback->lock );
return DR_OK;
}
DirectResult
fs_playback_set_downmix( CorePlayback *playback,
float center,
float rear )
{
CoreSoundBuffer *buffer;
CoreSoundDeviceConfig *config;
D_ASSERT( playback != NULL );
D_ASSERT( center >= 0.0f );
D_ASSERT( center <= 1.0f );
D_ASSERT( rear >= 0.0f );
D_ASSERT( rear <= 1.0f );
/* Lock playback. */
if (fusion_skirmish_prevail( &playback->lock ))
return DR_FUSION;
buffer = playback->buffer;
config = fs_core_device_config( playback->core );
if ( FS_MODE_HAS_CENTER(buffer->mode) &&
!FS_MODE_HAS_CENTER(config->mode))
playback->center = fsf_from_float( center );
else
playback->center = FSF_ONE;
if ( FS_MODE_NUM_REARS(buffer->mode) &&
!FS_MODE_NUM_REARS(config->mode))
playback->rear = fsf_from_float( rear );
else
playback->rear = FSF_ONE;
/* Unlock playback. */
fusion_skirmish_dismiss( &playback->lock );
return DR_OK;
}
DirectResult
fs_playback_set_volume( CorePlayback *playback,
float levels[6] )
{
int i;
D_ASSERT( playback != NULL );
D_ASSERT( levels[0] >= 0.0f );
D_ASSERT( levels[0] <= 64.0f );
D_ASSERT( levels[1] >= 0.0f );
D_ASSERT( levels[1] <= 64.0f );
/* Lock playback. */
if (fusion_skirmish_prevail( &playback->lock ))
return DR_FUSION;
/* Adjust volume. */
for (i = 0; i < 6; i++)
playback->levels[i] = fsf_from_float( levels[i] );
/* Apply downmixing levels. */
if (playback->center != FSF_ONE) {
playback->levels[2] = fsf_mul( playback->levels[2], playback->center );
}
if (playback->rear != FSF_ONE) {
playback->levels[3] = fsf_mul( playback->levels[3], playback->rear );
playback->levels[4] = fsf_mul( playback->levels[4], playback->rear );
}
/* Unlock playback. */
fusion_skirmish_dismiss( &playback->lock );
return DR_OK;
}
DirectResult
fs_playback_set_local_volume( CorePlayback *playback,
float level )
{
D_ASSERT( playback != NULL );
D_ASSERT( level >= 0.0f );
D_ASSERT( level <= 1.0f );
/* Lock playback. */
if (fusion_skirmish_prevail( &playback->lock ))
return DR_FUSION;
/* Set local volume level. */
playback->volume = fsf_from_float( level );
/* Unlock playback. */
fusion_skirmish_dismiss( &playback->lock );
return DR_OK;
}
DirectResult
fs_playback_set_pitch( CorePlayback *playback,
int pitch )
{
D_ASSERT( playback != NULL );
D_ASSERT( pitch >= -(64*FS_PITCH_ONE) );
D_ASSERT( pitch <= +(64*FS_PITCH_ONE) );
/* Lock playback. */
if (fusion_skirmish_prevail( &playback->lock ))
return DR_FUSION;
/* Adjust pitch. */
playback->pitch = pitch;
/* Unlock playback. */
fusion_skirmish_dismiss( &playback->lock );
return DR_OK;
}
DirectResult
fs_playback_get_status( CorePlayback *playback,
CorePlaybackStatus *ret_status,
int *ret_position )
{
D_ASSERT( playback != NULL );
/* Lock playback. */
if (fusion_skirmish_prevail( &playback->lock ))
return DR_FUSION;
/* Return status. */
if (ret_status) {
CorePlaybackStatus status = CPS_NONE;
if (playback->running) {
status |= CPS_PLAYING;
if (playback->stop < 0)
status |= CPS_LOOPING;
}
*ret_status = status;
}
/* Return position. */
if (ret_position)
*ret_position = playback->position;
/* Unlock playback. */
fusion_skirmish_dismiss( &playback->lock );
return DR_OK;
}
/******************************************************************************/
DirectResult
fs_playback_mixto( CorePlayback *playback,
__fsf *dest,
int dest_rate,
FSChannelMode dest_mode,
int max_frames,
__fsf volume,
int *ret_samples)
{
DirectResult ret;
int pos;
int num;
__fsf *levels;
int i;
D_ASSERT( playback != NULL );
D_ASSERT( playback->buffer != NULL );
D_ASSERT( dest != NULL );
D_ASSERT( max_frames > 0 );
/* Lock playback. */
if (fusion_skirmish_prevail( &playback->lock ))
return DR_FUSION;
if (volume != FSF_ONE || playback->volume != FSF_ONE) {
levels = alloca( 6 * sizeof(__fsf) );
volume = fsf_mul( volume, playback->volume );
for (i = 0; i < 6; i++)
levels[i] = fsf_mul( playback->levels[i], volume );
}
else {
levels = playback->levels;
}
/* Mix samples... */
ret = fs_buffer_mixto( playback->buffer, dest, dest_rate, dest_mode, max_frames,
playback->position, playback->stop, levels,
playback->pitch, &pos, &num, ret_samples );
if (ret)
playback->running = false;
/* Write back new position. */
playback->position = pos;
/* Unlock playback. */
fusion_skirmish_dismiss( &playback->lock );
/* Notify listeners about the new position and a possible end. */
fs_playback_notify( playback, ret ? (CPNF_ADVANCE | CPNF_STOP) : CPNF_ADVANCE, num );
return ret;
}
/******************************************************************************/
static void
fs_playback_notify( CorePlayback *playback,
CorePlaybackNotificationFlags flags,
int num )
{
CorePlaybackNotification notification;
D_ASSERT( playback != NULL );
D_ASSERT( playback->buffer != NULL );
D_ASSERT( ! (flags & ~(CPNF_START | CPNF_STOP | CPNF_ADVANCE)) );
if (flags & CPNF_START)
playback->running = true;
if (flags & CPNF_STOP)
playback->running = false;
if (!playback->notify)
return;
notification.flags = flags;
notification.playback = playback;
notification.pos = playback->position;
notification.stop = playback->running ? playback->stop : playback->position;
notification.num = num;
fs_playback_dispatch( playback, ¬ification, NULL );
}
|