00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "OSDLMusic.h"
00028
00029 #include "OSDLAudio.h"
00030 #include "OSDLFileTags.h"
00031 #include "OSDLUtils.h"
00032
00033
00034 #ifdef OSDL_USES_CONFIG_H
00035 #include "OSDLConfig.h"
00036 #endif // OSDL_USES_CONFIG_H
00037
00038
00039 #if OSDL_ARCH_NINTENDO_DS
00040 #include "OSDLConfigForNintendoDS.h"
00041 #endif // OSDL_ARCH_NINTENDO_DS
00042
00043
00044
00045 #if OSDL_USES_SDL
00046 #include "SDL.h"
00047 #endif // OSDL_USES_SDL
00048
00049 #if OSDL_USES_SDL_MIXER
00050 #include "SDL_mixer.h"
00051 #endif // OSDL_USES_SDL_MIXER
00052
00053
00054
00055
00056 #if OSDL_DEBUG_AUDIO_PLAYBACK
00057
00058 #define LOG_DEBUG_AUDIO(message) LogPlug::debug(message)
00059 #define LOG_TRACE_AUDIO(message) LogPlug::trace(message)
00060 #define LOG_WARNING_AUDIO(message) LogPlug::warning(message)
00061
00062 #else // OSDL_DEBUG_AUDIO_PLAYBACK
00063
00064 #define LOG_DEBUG_AUDIO(message)
00065 #define LOG_TRACE_AUDIO(message)
00066 #define LOG_WARNING_AUDIO(message)
00067
00068 #endif // OSDL_DEBUG_AUDIO_PLAYBACK
00069
00070
00071
00072 using std::string ;
00073
00074 using Ceylan::System::Millisecond ;
00075
00076
00077 using namespace Ceylan::Log ;
00078 using namespace Ceylan::System ;
00079
00080 using namespace OSDL::Audio ;
00081
00082
00083
00084
00147 extern const BitrateType OSDL::Audio::CBR = 1 ;
00148 extern const BitrateType OSDL::Audio::VBR = 2 ;
00149
00150
00151
00152 MusicException::MusicException( const string & reason ) :
00153 AudibleException( reason )
00154 {
00155
00156 }
00157
00158
00159
00160 MusicException::~MusicException() throw()
00161 {
00162
00163 }
00164
00165
00166
00167
00169 Music * Music::_CurrentMusic = 0 ;
00170
00171
00172
00173
00174 #if OSDL_ARCH_NINTENDO_DS
00175
00176 const OSDL::CommandManagerSettings * Music::_CommandManagerSettings = 0 ;
00177
00178 #endif // OSDL_ARCH_NINTENDO_DS
00179
00180
00181
00182
00183 Music::Music( const std::string & musicFilename, bool preload ) :
00184 Audible( false ),
00185 Ceylan::LoadableWithContent<LowLevelMusic>( musicFilename ),
00186 _dataStream( 0 ),
00187 _isPlaying( false )
00188 {
00189
00190 if ( ! AudioModule::IsAudioInitialized() )
00191 throw MusicException( "Music constructor failed: "
00192 "audio module not already initialized" ) ;
00193
00194
00195
00196 #if ! OSDL_ARCH_NINTENDO_DS
00197
00198 if ( preload )
00199 {
00200
00201 try
00202 {
00203
00204 load() ;
00205
00206 }
00207 catch( const Ceylan::LoadableException & e )
00208 {
00209
00210 throw MusicException( "Music constructor failed while preloading: "
00211 + e.toString() ) ;
00212
00213 }
00214
00215 }
00216
00217 #endif // OSDL_ARCH_NINTENDO_DS
00218
00219 }
00220
00221
00222
00223 Music::~Music() throw()
00224 {
00225
00226 if ( _CurrentMusic == this )
00227 {
00228
00229 try
00230 {
00231 manageNoMoreCurrent() ;
00232
00233 }
00234 catch( const AudioException & e )
00235 {
00236
00237 LogPlug::error( "Music destructor failed "
00238 "while unsetting 'current music' status: " + e.toString() ) ;
00239
00240 }
00241
00242 }
00243
00244
00245 try
00246 {
00247
00248 if ( hasContent() )
00249 unload() ;
00250
00251 }
00252 catch( const Ceylan::LoadableException & e )
00253 {
00254
00255 LogPlug::error( "Music destructor failed while unloading: "
00256 + e.toString() ) ;
00257
00258 }
00259
00260
00261
00262 }
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272 bool Music::load()
00273 {
00274
00275 #if OSDL_ARCH_NINTENDO_DS
00276
00277 #ifdef OSDL_RUNS_ON_ARM7
00278
00279 throw Ceylan::LoadableException( "Music::load failed: "
00280 "not supported on the ARM7" ) ;
00281
00282 #elif defined(OSDL_RUNS_ON_ARM9)
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299 _content = new LowLevelMusic() ;
00300
00301 LowLevelMusic & music = *_content ;
00302
00303
00304 try
00305 {
00306
00307
00308
00309
00310 music._musicFile = & File::Open( _contentPath ) ;
00311
00312
00313 FileTag readTag = music._musicFile->readUint16() ;
00314
00315 if ( readTag != MusicTag )
00316 throw Ceylan::LoadableException(
00317 "Music::load: expected music tag not found ("
00318 + Ceylan::toString( MusicTag ) + "), read instead "
00319 + Ceylan::toString( readTag ) + ", which corresponds to: "
00320 + DescribeFileTag( readTag ) ) ;
00321
00322 LOG_DEBUG_AUDIO( "Music::load: correct tag found." ) ;
00323
00324
00325 music._frequency = music._musicFile->readUint16() ;
00326
00327
00328 music._bitDepth = 16 ;
00329
00330
00331 music._mode = music._musicFile->readUint16() ;
00332
00333
00334 music._musicFile->readUint8() ;
00335
00336 music._size = music._musicFile->size() ;
00337
00338
00339 music._frameSizeUpperBound = music._musicFile->readUint16() ;
00340
00341
00342
00343 if ( _CommandManagerSettings->_bufferSize <=
00344 music._frameSizeUpperBound )
00345 throw Ceylan::LoadableException( "Music::load: "
00346 "buffer to small compared to frame size upper bound." ) ;
00347
00348
00349 music._startAfterDelta = _CommandManagerSettings->_doubleBuffer
00350 + music._frameSizeUpperBound ;
00351
00352 music._firstActualRefillSize =
00353 _CommandManagerSettings->_bufferSize - music._frameSizeUpperBound ;
00354
00355
00356
00357
00358
00359
00360 fillFirstBuffer() ;
00361
00362 music._requestFillOfSecondBuffer = true ;
00363
00364
00365 music._playbackCount = 1 ;
00366
00367 }
00368 catch( const Ceylan::System::SystemException & e )
00369 {
00370
00371 throw Ceylan::LoadableException( "Music::load failed: "
00372 + e.toString() ) ;
00373
00374 }
00375
00376 LOG_DEBUG_AUDIO( "Music::load: read first chunk of " + _contentPath +
00377 + " (" + Ceylan::toString( music._firstActualRefillSize)
00378 + " out of a total of " + Ceylan::toString( music._size )
00379 + " bytes)." ) ;
00380
00381
00382 _convertedToOutputFormat = true ;
00383
00384 return true ;
00385
00386 #endif // OSDL_RUNS_ON_ARM7
00387
00388 #else // OSDL_ARCH_NINTENDO_DS
00389
00390 #if OSDL_USES_SDL_MIXER
00391
00392
00393
00394
00395
00396
00397
00398
00399 if ( hasContent() )
00400 return false ;
00401
00402 try
00403 {
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417 Ceylan::System::File & musicFile = File::Open( _contentPath ) ;
00418
00419
00420
00421
00422
00423
00424
00425
00426 _dataStream = & Utils::createDataStreamFrom( musicFile ) ;
00427
00428 _content = ::Mix_LoadMUS_RW( _dataStream ) ;
00429
00430
00431
00432 }
00433 catch( const Ceylan::Exception & e )
00434 {
00435
00436 throw Ceylan::LoadableException( "Music::load failed: "
00437 "unable to load from '" + _contentPath + "': " + e.toString() ) ;
00438
00439 }
00440
00441 if ( _content == 0 )
00442 throw Ceylan::LoadableException( "Music::load failed: "
00443 + string( ::Mix_GetError() ) ) ;
00444
00445 _convertedToOutputFormat = true ;
00446
00447 return true ;
00448
00449 #else // OSDL_USES_SDL_MIXER
00450
00451 throw Ceylan::LoadableException(
00452 "Music::load failed: no SDL_mixer support available." ) ;
00453
00454 #endif // OSDL_USES_SDL_MIXER
00455
00456 #endif // OSDL_ARCH_NINTENDO_DS
00457
00458 }
00459
00460
00461
00462 bool Music::unload()
00463 {
00464
00465 if ( ! hasContent() )
00466 return false ;
00467
00468
00469 #if OSDL_ARCH_NINTENDO_DS
00470
00471 #ifdef OSDL_RUNS_ON_ARM7
00472
00473 throw Ceylan::LoadableException( "Music::unload failed: "
00474 "not supported on the ARM7" ) ;
00475
00476 #elif defined(OSDL_RUNS_ON_ARM9)
00477
00478
00479 if ( _content->_musicFile != 0 )
00480 delete _content->_musicFile ;
00481
00482 delete _content ;
00483
00484 #endif // OSDL_RUNS_ON_ARM7
00485
00486 #else // OSDL_ARCH_NINTENDO_DS
00487
00488 #if OSDL_USES_SDL_MIXER
00489
00490
00491 ::Mix_FreeMusic( _content ) ;
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517 _dataStream = 0 ;
00518
00519 #else // OSDL_USES_SDL_MIXER
00520
00521 throw Ceylan::LoadableException(
00522 "Music::unload failed: no SDL_mixer support available." ) ;
00523
00524 #endif // OSDL_USES_SDL_MIXER
00525
00526 #endif // OSDL_ARCH_NINTENDO_DS
00527
00528 _content = 0 ;
00529
00530 return true ;
00531
00532 }
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542 Volume Music::getVolume() const
00543 {
00544
00545 #if OSDL_USES_SDL_MIXER
00546
00547 try
00548 {
00549
00550 if ( hasContent() )
00551 return::Mix_VolumeMusic( -1 ) ;
00552 else
00553 throw MusicException(
00554 "Music::getVolume failed: no loaded music available." ) ;
00555
00556 }
00557 catch( const Ceylan::LoadableException & e )
00558 {
00559
00560 throw MusicException( "Music::getVolume failed: " + e.toString() ) ;
00561
00562 }
00563
00564 #else // OSDL_USES_SDL_MIXER
00565
00566 throw MusicException(
00567 "Music::getVolume failed: no SDL_mixer support available." ) ;
00568
00569 #endif // OSDL_USES_SDL_MIXER
00570
00571 }
00572
00573
00574
00575 void Music::setVolume( Volume newVolume )
00576 {
00577
00578 #if OSDL_ARCH_NINTENDO_DS
00579
00580 #ifdef OSDL_RUNS_ON_ARM7
00581
00582 throw MusicException(
00583 "Music::setVolume failed: not supported on the ARM7" ) ;
00584
00585 #elif defined(OSDL_RUNS_ON_ARM9)
00586
00587
00588 _CommandManagerSettings->_commandManager->setMusicVolume( newVolume ) ;
00589
00590
00591 #endif // OSDL_RUNS_ON_ARM7
00592
00593
00594 #else // OSDL_ARCH_NINTENDO_DS
00595
00596
00597 #if OSDL_USES_SDL_MIXER
00598
00599 try
00600 {
00601
00602 if ( hasContent() )
00603 ::Mix_VolumeMusic( static_cast<int>( newVolume ) ) ;
00604 else
00605 throw MusicException(
00606 "Music::setVolume failed: no loaded music available." ) ;
00607
00608 }
00609 catch( const Ceylan::LoadableException & e )
00610 {
00611
00612
00613 throw MusicException( "Music::setVolume failed: " + e.toString() ) ;
00614
00615 }
00616
00617 #else // OSDL_USES_SDL_MIXER
00618
00619 throw MusicException(
00620 "Music::setVolume failed: no SDL_mixer support available." ) ;
00621
00622 #endif // OSDL_USES_SDL_MIXER
00623
00624 #endif // OSDL_ARCH_NINTENDO_DS
00625
00626 }
00627
00628
00629
00630 MusicType Music::getType() const
00631 {
00632
00633 #if OSDL_USES_SDL_MIXER
00634
00635 return GetTypeOf( this ) ;
00636
00637 #else // OSDL_USES_SDL_MIXER
00638
00639 throw MusicException(
00640 "Music::setVolume failed: no SDL_mixer support available." ) ;
00641
00642 #endif // OSDL_USES_SDL_MIXER
00643
00644 }
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657 void Music::play( PlaybackCount playCount )
00658 {
00659
00660 if ( playCount == 0 )
00661 return ;
00662
00663 _isPlaying = true ;
00664
00665 #if OSDL_ARCH_NINTENDO_DS
00666
00667 #ifdef OSDL_RUNS_ON_ARM7
00668
00669 throw MusicException( "Music::play failed: not supported on the ARM7" ) ;
00670
00671 #elif defined(OSDL_RUNS_ON_ARM9)
00672
00673 if ( hasContent() )
00674 unload() ;
00675
00676 load() ;
00677
00678 _content->_playbackCount = playCount ;
00679
00680 try
00681 {
00682
00683 if ( _content->_playbackCount != Loop )
00684 _content->_playbackCount-- ;
00685
00686 _CommandManagerSettings->_commandManager->playMusic( *this ) ;
00687
00688 }
00689 catch( const CommandException & e )
00690 {
00691
00692 throw MusicException( "Music::play failed: " + e.toString() ) ;
00693
00694 }
00695
00696 return ;
00697
00698 #endif // OSDL_RUNS_ON_ARM7
00699
00700 #else // OSDL_ARCH_NINTENDO_DS
00701
00702 #if OSDL_USES_SDL_MIXER
00703
00704 if ( ! hasContent() )
00705 throw MusicException( "Music::play failed: "
00706 "no loaded music available" ) ;
00707
00708 if ( ::Mix_PlayMusic( _content, GetLoopsForPlayCount( playCount ) ) == -1 )
00709 throw MusicException( "Music::play failed: "
00710 + string( ::Mix_GetError() ) ) ;
00711
00712 #else // OSDL_USES_SDL_MIXER
00713
00714 throw MusicException(
00715 "Music::play failed: no SDL_mixer support available." ) ;
00716
00717 #endif // OSDL_USES_SDL_MIXER
00718
00719 #endif // OSDL_ARCH_NINTENDO_DS
00720
00721 }
00722
00723
00724
00725
00726
00727
00728
00729 void Music::playWithFadeIn( Ceylan::System::Millisecond fadeInMaxDuration,
00730 PlaybackCount playCount )
00731 {
00732
00733 _isPlaying = true ;
00734
00735 #if OSDL_ARCH_NINTENDO_DS
00736
00737 #ifdef OSDL_RUNS_ON_ARM7
00738
00739 throw MusicException(
00740 "Music::playWithFadeIn failed: not supported on the ARM7" ) ;
00741
00742 #elif defined(OSDL_RUNS_ON_ARM9)
00743
00744 if ( hasContent() )
00745 unload() ;
00746
00747 load() ;
00748
00749 _content->_playbackCount = playCount ;
00750
00751 try
00752 {
00753
00754 if ( _content->_playbackCount != Loop )
00755 _content->_playbackCount-- ;
00756
00757 _CommandManagerSettings->_commandManager->playMusicWithFadeIn( *this,
00758 fadeInMaxDuration ) ;
00759
00760 }
00761 catch( const CommandException & e )
00762 {
00763
00764 throw MusicException( "Music::playWithFadeIn failed: "
00765 + e.toString() ) ;
00766
00767 }
00768
00769 return ;
00770
00771 #endif // OSDL_RUNS_ON_ARM7
00772
00773 #else // OSDL_ARCH_NINTENDO_DS
00774
00775
00776 #if OSDL_USES_SDL_MIXER
00777
00778 if ( ! hasContent() )
00779 throw AudibleException( "Music::playWithFadeIn failed: "
00780 "no loaded music available" ) ;
00781
00782 if ( ::Mix_FadeInMusic( _content, GetLoopsForPlayCount( playCount ),
00783 fadeInMaxDuration ) == -1 )
00784 throw MusicException( "Music::playWithFadeIn failed: "
00785 + string( ::Mix_GetError() ) ) ;
00786
00787 #else // OSDL_USES_SDL_MIXER
00788
00789 throw MusicException(
00790 "Music::playWithFadeIn failed: no SDL_mixer support available." ) ;
00791
00792 #endif // OSDL_USES_SDL_MIXER
00793
00794 #endif // OSDL_ARCH_NINTENDO_DS
00795
00796 }
00797
00798
00799
00800 void Music::playWithFadeInFromPosition(
00801 Ceylan::System::Millisecond fadeInMaxDuration,
00802 MusicPosition position, PlaybackCount playCount )
00803 {
00804
00805 _isPlaying = true ;
00806
00807 #if OSDL_USES_SDL_MIXER
00808
00809 if ( ! hasContent() )
00810 throw AudibleException( "Music::playWithFadeInFromPosition failed: "
00811 "no loaded music available" ) ;
00812
00813 if ( ::Mix_FadeInMusicPos( _content, GetLoopsForPlayCount( playCount ),
00814 fadeInMaxDuration, static_cast<double>( position ) ) == -1 )
00815 throw MusicException( "Music::playWithFadeInFromPosition failed: "
00816 + string( ::Mix_GetError() ) ) ;
00817
00818 #else // OSDL_USES_SDL_MIXER
00819
00820 throw MusicException( "Music::playWithFadeInFromPosition failed: "
00821 "no SDL_mixer support available." ) ;
00822
00823 #endif // OSDL_USES_SDL_MIXER
00824
00825 }
00826
00827
00828
00829 bool Music::isPlaying()
00830 {
00831
00832 return _isPlaying ;
00833
00834 }
00835
00836
00837
00838 void Music::pause()
00839 {
00840
00841 #if OSDL_ARCH_NINTENDO_DS
00842
00843
00844 #ifdef OSDL_RUNS_ON_ARM7
00845
00846 throw MusicException( "Music::pause failed: "
00847 "not supported on the ARM7" ) ;
00848
00849 #elif defined(OSDL_RUNS_ON_ARM9)
00850
00851 try
00852 {
00853 if ( hasContent() )
00854 _CommandManagerSettings->_commandManager->pauseMusic() ;
00855
00856 }
00857 catch( const CommandException & e )
00858 {
00859
00860 throw MusicException( "Music::pause failed: " + e.toString() ) ;
00861
00862 }
00863
00864 return ;
00865
00866 #endif // OSDL_RUNS_ON_ARM7
00867
00868
00869 #else // OSDL_ARCH_NINTENDO_DS
00870
00871 #if OSDL_USES_SDL_MIXER
00872
00873
00874 ::Mix_PauseMusic() ;
00875
00876 #else // OSDL_USES_SDL_MIXER
00877
00878 throw MusicException( "Music::pause failed: "
00879 "no SDL_mixer support available." ) ;
00880
00881 #endif // OSDL_USES_SDL_MIXER
00882
00883 #endif // OSDL_ARCH_NINTENDO_DS
00884
00885 }
00886
00887
00888
00889 void Music::unpause()
00890 {
00891
00892 #if OSDL_ARCH_NINTENDO_DS
00893
00894
00895 #ifdef OSDL_RUNS_ON_ARM7
00896
00897 throw MusicException( "Music::unpause failed: "
00898 "not supported on the ARM7" ) ;
00899
00900 #elif defined(OSDL_RUNS_ON_ARM9)
00901
00902
00903 try
00904 {
00905
00906 if ( hasContent() )
00907 _CommandManagerSettings->_commandManager->unpauseMusic() ;
00908
00909 }
00910 catch( const CommandException & e )
00911 {
00912
00913 throw MusicException( "Music::unpause failed: " + e.toString() ) ;
00914
00915 }
00916
00917 return ;
00918
00919 #endif // OSDL_RUNS_ON_ARM7
00920
00921
00922 #else // OSDL_ARCH_NINTENDO_DS
00923
00924 #if OSDL_USES_SDL_MIXER
00925
00926
00927 ::Mix_ResumeMusic() ;
00928
00929 #else // OSDL_USES_SDL_MIXER
00930
00931 throw MusicException( "Music::unpause failed: "
00932 "no SDL_mixer support available." ) ;
00933
00934 #endif // OSDL_USES_SDL_MIXER
00935
00936 #endif // OSDL_ARCH_NINTENDO_DS
00937
00938 }
00939
00940
00941
00942 void Music::rewind()
00943 {
00944
00945 #if OSDL_USES_SDL_MIXER
00946
00947
00948 ::Mix_RewindMusic() ;
00949
00950 #else // OSDL_USES_SDL_MIXER
00951
00952 throw MusicException( "Music::rewind failed: "
00953 "no SDL_mixer support available." ) ;
00954
00955 #endif // OSDL_USES_SDL_MIXER
00956
00957 }
00958
00959
00960
00961 void Music::setPosition( MusicPosition newPosition )
00962 {
00963
00964 #if OSDL_USES_SDL_MIXER
00965
00966
00967 if ( ::Mix_SetMusicPosition( static_cast<double>( newPosition ) ) == -1 )
00968 throw MusicException( "Music::setPosition failed: "
00969 + string( ::Mix_GetError() ) ) ;
00970
00971 #else // OSDL_USES_SDL_MIXER
00972
00973 throw MusicException( "Music::setPosition failed: "
00974 "no SDL_mixer support available." ) ;
00975
00976 #endif // OSDL_USES_SDL_MIXER
00977
00978 }
00979
00980
00981
00982 void Music::stop()
00983 {
00984
00985 #if OSDL_ARCH_NINTENDO_DS
00986
00987
00988 #ifdef OSDL_RUNS_ON_ARM7
00989
00990 throw MusicException( "Music::stop failed: "
00991 "not supported on the ARM7" ) ;
00992
00993 #elif defined(OSDL_RUNS_ON_ARM9)
00994
00995
00996 try
00997 {
00998
00999 if ( hasContent() )
01000 {
01001
01002 _content->_playbackCount = 0 ;
01003 _CommandManagerSettings->_commandManager->stopMusic() ;
01004
01005 }
01006
01007 }
01008 catch( const CommandException & e )
01009 {
01010
01011 throw MusicException( "Music::stop failed: " + e.toString() ) ;
01012
01013 }
01014
01015 return ;
01016
01017 #endif // OSDL_RUNS_ON_ARM7
01018
01019
01020 #else // OSDL_ARCH_NINTENDO_DS
01021
01022 #if OSDL_USES_SDL_MIXER
01023
01024
01025 ::Mix_HaltMusic() ;
01026
01027 #else // OSDL_USES_SDL_MIXER
01028
01029 throw MusicException( "Music::stop failed: "
01030 "no SDL_mixer support available." ) ;
01031
01032 #endif // OSDL_USES_SDL_MIXER
01033
01034 #endif // OSDL_ARCH_NINTENDO_DS
01035
01036 }
01037
01038
01039
01040 void Music::fadeIn( Ceylan::System::Millisecond fadeInMaxDuration )
01041 {
01042
01043 #if OSDL_ARCH_NINTENDO_DS
01044
01045
01046 #ifdef OSDL_RUNS_ON_ARM7
01047
01048 throw MusicException( "Music::fadeIn failed: "
01049 "not supported on the ARM7" ) ;
01050
01051 #elif defined(OSDL_RUNS_ON_ARM9)
01052
01053 _CommandManagerSettings->_commandManager->fadeInMusic( fadeInMaxDuration ) ;
01054
01055 #endif // OSDL_RUNS_ON_ARM7
01056
01057
01058 #else // OSDL_ARCH_NINTENDO_DS
01059
01060
01061
01062 throw MusicException( "Music::fadeIn failed: "
01063 "not available on this platform." ) ;
01064
01065 #endif // OSDL_ARCH_NINTENDO_DS
01066
01067 }
01068
01069
01070
01071 void Music::fadeOut( Ceylan::System::Millisecond fadeOutMaxDuration )
01072 {
01073
01074 #if OSDL_ARCH_NINTENDO_DS
01075
01076
01077 #ifdef OSDL_RUNS_ON_ARM7
01078
01079 throw MusicException( "Music::fadeOut: "
01080 "not supported on the ARM7" ) ;
01081
01082 #elif defined(OSDL_RUNS_ON_ARM9)
01083
01084 _CommandManagerSettings->_commandManager->fadeOutMusic(
01085 fadeOutMaxDuration ) ;
01086
01087 #endif // OSDL_RUNS_ON_ARM7
01088
01089
01090 #else // OSDL_ARCH_NINTENDO_DS
01091
01092 #if OSDL_USES_SDL_MIXER
01093
01094
01095 if ( ::Mix_FadeOutMusic( fadeOutMaxDuration ) == 0 )
01096 throw MusicException( "Music::fadeOut failed: "
01097 + string( ::Mix_GetError() ) ) ;
01098
01099 #else // OSDL_USES_SDL_MIXER
01100
01101 throw MusicException( "Music::fadeOut failed: "
01102 "no SDL_mixer support available." ) ;
01103
01104 #endif // OSDL_USES_SDL_MIXER
01105
01106 #endif // OSDL_ARCH_NINTENDO_DS
01107
01108 }
01109
01110
01111
01112 void Music::setAsCurrent()
01113 {
01114
01115
01116 if ( _CurrentMusic != 0 && _CurrentMusic != this )
01117 _CurrentMusic->manageNoMoreCurrent() ;
01118
01119
01120
01121
01122
01123
01124 _CurrentMusic = this ;
01125
01126 LOG_DEBUG_AUDIO( "Music::setAsCurrent: " + toString( Ceylan::low ) +
01127 " set as current." ) ;
01128
01129 }
01130
01131
01132
01133 void Music::requestFillOfFirstBuffer()
01134 {
01135
01136 #if ! OSDL_USES_SDL_MIXER
01137
01138 _content->_requestFillOfFirstBuffer = true ;
01139
01140 #endif // OSDL_USES_SDL_MIXER
01141
01142 }
01143
01144
01145
01146 void Music::requestFillOfSecondBuffer()
01147 {
01148
01149 #if ! OSDL_USES_SDL_MIXER
01150
01151 _content->_requestFillOfSecondBuffer = true ;
01152
01153 #endif // OSDL_USES_SDL_MIXER
01154
01155 }
01156
01157
01158
01159 const string Music::toString( Ceylan::VerbosityLevels level ) const
01160 {
01161
01162 if ( level == Ceylan::low )
01163 return ( hasContent() ? string( "Loaded" ) : string( "Not loaded" ) )
01164 + " music, whose content path is '" + _contentPath + "'" ;
01165
01166
01167 try
01168 {
01169
01170 if ( hasContent() )
01171 {
01172
01173 Volume v = getVolume() ;
01174
01175 return "Loaded music from content path '" + _contentPath
01176 + "', whose volume is " + Ceylan::toNumericalString( v )
01177 + " (" + Ceylan::toNumericalString( 100 * v
01178 / ( AudioModule::MaxVolume - AudioModule::MinVolume ) )
01179 + "%) and whose type is " + DescribeMusicType( getType() )
01180 + string( ", currently " )
01181 + ( _isPlaying ? "playing": "not playing" ) ;
01182
01183 }
01184 else
01185 {
01186
01187 return "Music currently not loaded, whose content path is '"
01188 + _contentPath + "'" ;
01189
01190 }
01191
01192 }
01193 catch( const Ceylan::Exception & e )
01194 {
01195
01196 return "Music::toString failed (abnormal): " + e.toString() ;
01197
01198 }
01199
01200 }
01201
01202
01203
01204
01205 #if OSDL_ARCH_NINTENDO_DS
01206
01207 void Music::SetCommandManagerSettings( const CommandManagerSettings & settings )
01208 {
01209
01210 _CommandManagerSettings = & settings ;
01211
01212 }
01213
01214 #endif // OSDL_ARCH_NINTENDO_DS
01215
01216
01217
01218 void Music::ManageCurrentMusic()
01219 {
01220
01221 if ( _CurrentMusic != 0 )
01222 _CurrentMusic->manageBufferRefill() ;
01223
01224 }
01225
01226
01227
01228
01229
01230
01231
01232
01233 MusicType Music::GetTypeOf( const Music * music )
01234 {
01235
01236 #if OSDL_USES_SDL_MIXER
01237
01238 LowLevelMusic * actualMusic ;
01239
01240 if ( music == 0 )
01241 {
01242 actualMusic = 0 ;
01243 }
01244 else
01245 {
01246
01247 if ( music->hasContent() )
01248 actualMusic = const_cast<LowLevelMusic *>(
01249 & music->getExistingContentAsConst() ) ;
01250 else
01251 throw AudioException(
01252 "Music::GetTypeOf failed: no loaded music available" ) ;
01253
01254 }
01255
01256
01257 switch( ::Mix_GetMusicType( actualMusic ) )
01258 {
01259
01260 case MUS_WAV:
01261 return Wave ;
01262 break ;
01263
01264 case MUS_MOD:
01265 return MOD ;
01266 break ;
01267
01268 case MUS_MID:
01269 return MIDI ;
01270 break ;
01271
01272 case MUS_OGG:
01273 return OggVorbis ;
01274 break ;
01275
01276 case MUS_MP3:
01277 return MP3 ;
01278 break ;
01279
01280 case MUS_CMD:
01281 return CommandBased ;
01282 break ;
01283
01284 case MUS_NONE:
01285 return NoMusic ;
01286 break ;
01287
01288 default:
01289 return Unknown ;
01290 break ;
01291
01292 }
01293
01294
01295 #else // OSDL_USES_SDL_MIXER
01296
01297 throw AudioException( "Music::GetTypeOf failed: "
01298 "no SDL_mixer support available." ) ;
01299
01300 #endif // OSDL_USES_SDL_MIXER
01301
01302 }
01303
01304
01305
01306 string Music::DescribeMusicType( MusicType type )
01307 {
01308
01309 switch( type )
01310 {
01311
01312 case Wave:
01313 return "waveform audio format (WAVE/RIFF)" ;
01314 break ;
01315
01316 case MOD:
01317 return "soundtrack Module (MOD)" ;
01318 break ;
01319
01320 case MIDI:
01321 return "Musical Instrument Digital Interface (MIDI)" ;
01322 break ;
01323
01324 case OggVorbis:
01325 return "Vorbis encoding over Ogg container (OggVorbis)" ;
01326 break ;
01327
01328 case MP3:
01329 return "MPEG-1 Audio Layer 3 (MP3)" ;
01330 break ;
01331
01332 case CommandBased:
01333 return "music managed externally by a third-party player" ;
01334 break ;
01335
01336 case NoMusic:
01337 return "no music" ;
01338 break ;
01339
01340 default:
01341 return "unknown music type (abnormal)" ;
01342 break ;
01343
01344
01345 }
01346
01347
01348 }
01349
01350
01351
01352 string Music::DescribeBitrateType( BitrateType type )
01353 {
01354
01355 switch( type )
01356 {
01357
01358 case CBR:
01359 return "CBR (constant bitrate)" ;
01360 break ;
01361
01362 case VBR:
01363 return "VBR (variable bitrate)" ;
01364 break ;
01365
01366 default:
01367 return "unknown bitrate type (abnormal)" ;
01368 break ;
01369
01370 }
01371
01372
01373 }
01374
01375
01376
01377 void Music::onPlaybackEnded()
01378 {
01379
01380
01381 LOG_TRACE_AUDIO( "Music::onPlaybackEnded" ) ;
01382
01383 }
01384
01385
01386
01387 void Music::managePlaybackEnded()
01388 {
01389
01390 _isPlaying = false ;
01391
01392 #if OSDL_ARCH_NINTENDO_DS
01393
01394 #ifdef OSDL_RUNS_ON_ARM7
01395
01396 throw AudioException( "Music::managePlaybackEnded: "
01397 "not supported on the ARM7" ) ;
01398
01399 #elif defined(OSDL_RUNS_ON_ARM9)
01400
01401 PlaybackCount count = _content->_playbackCount ;
01402
01403 unload() ;
01404 onPlaybackEnded() ;
01405
01406 LOG_DEBUG_AUDIO( "Music::managePlaybackEnded: count = "
01407 + Ceylan::toString(count) ) ;
01408
01409
01410 if ( count != 0 )
01411 play( count ) ;
01412 else
01413 manageNoMoreCurrent() ;
01414
01415 #endif // OSDL_RUNS_ON_ARM7
01416
01417 #endif // OSDL_ARCH_NINTENDO_DS
01418
01419 }
01420
01421
01422
01423 void Music::onNoMoreCurrent()
01424 {
01425
01426 LOG_WARNING_AUDIO( "Music::onNoMoreCurrent: " + toString( Ceylan::low )
01427 + " not current anymore." ) ;
01428
01429 }
01430
01431
01432
01433 void Music::manageNoMoreCurrent()
01434 {
01435
01436 LOG_TRACE_AUDIO( "Music::manageNoMoreCurrent" ) ;
01437
01438 #if OSDL_ARCH_NINTENDO_DS
01439
01440 #ifdef OSDL_RUNS_ON_ARM7
01441
01442 throw AudioException( "Music::manageNoMoreCurrent: "
01443 "not supported on the ARM7" ) ;
01444
01445 #elif defined(OSDL_RUNS_ON_ARM9)
01446
01447 _isPlaying = false ;
01448
01449 if ( _CurrentMusic == this )
01450 {
01451
01452 _CurrentMusic = 0 ;
01453 _CommandManagerSettings->_commandManager->unsetCurrentMusic( *this ) ;
01454
01455 }
01456
01457 #endif // OSDL_RUNS_ON_ARM7
01458
01459 #else // OSDL_ARCH_NINTENDO_DS
01460
01461 _isPlaying = false ;
01462
01463 if ( _CurrentMusic == this )
01464 _CurrentMusic = 0 ;
01465
01466 #endif // OSDL_ARCH_NINTENDO_DS
01467
01468 onNoMoreCurrent() ;
01469
01470 }
01471
01472
01473
01474 void Music::manageBufferRefill()
01475 {
01476
01477 #if OSDL_ARCH_NINTENDO_DS
01478
01479 if ( _content->_requestFillOfFirstBuffer )
01480 fillFirstBuffer() ;
01481
01482 if ( _content->_requestFillOfSecondBuffer )
01483 fillSecondBuffer() ;
01484
01485 #endif // OSDL_ARCH_NINTENDO_DS
01486
01487 }
01488
01489
01490
01491 void Music::fillFirstBuffer()
01492 {
01493
01494 #if OSDL_ARCH_NINTENDO_DS
01495
01496 #ifdef OSDL_RUNS_ON_ARM7
01497
01498 throw AudioException( "Music::fillFirstBuffer: "
01499 "not supported on the ARM7" ) ;
01500
01501 #elif defined(OSDL_RUNS_ON_ARM9)
01502
01503
01504 LOG_TRACE_AUDIO( "Music::fillFirstBuffer." ) ;
01505
01506 LowLevelMusic & music = getContent() ;
01507
01508 music._requestFillOfFirstBuffer = false ;
01509
01510 try
01511 {
01512
01513
01514
01515
01516
01517 BufferSize readSize = music._musicFile->read(
01518 music._startAfterDelta,
01519 music._firstActualRefillSize ) ;
01520
01521 LOG_DEBUG_AUDIO( "Music::fillFirstBuffer: read "
01522 + Ceylan::toString( readSize ) + " bytes." ) ;
01523
01524
01525
01526
01527
01528
01529 if ( readSize < music._firstActualRefillSize )
01530 {
01531
01532 LOG_TRACE_AUDIO( "Padding first buffer." ) ;
01533
01534 ::memset(
01535 music._startAfterDelta + readSize,
01536 0,
01537 _CommandManagerSettings->_bufferSize
01538 - music._frameSizeUpperBound - readSize ) ;
01539
01540
01541 DC_FlushRange( (void*) music._startAfterDelta,
01542 music._firstActualRefillSize ) ;
01543
01544 try
01545 {
01546
01547 _CommandManagerSettings->_commandManager->notifyEndOfEncodedStreamReached()
01548 ;
01549
01550 }
01551 catch( const CommandException & e )
01552 {
01553
01554 throw AudioException( "Music::fillFirstBuffer failed: "
01555 + e.toString() ) ;
01556
01557 }
01558
01559 }
01560 else
01561 {
01562
01563
01564 DC_FlushRange( (void*) music._startAfterDelta,
01565 music._firstActualRefillSize ) ;
01566
01567 }
01568
01569
01570 }
01571 catch( const SystemException & e )
01572 {
01573
01574 throw AudioException( "Music::fillFirstBuffer failed: "
01575 + e.toString() ) ;
01576
01577 }
01578
01579 #endif // OSDL_RUNS_ON_ARM7
01580
01581 #endif // OSDL_ARCH_NINTENDO_DS
01582
01583 }
01584
01585
01586
01587 void Music::fillSecondBuffer()
01588 {
01589
01590 #if OSDL_ARCH_NINTENDO_DS
01591
01592 #ifdef OSDL_RUNS_ON_ARM7
01593
01594 throw AudioException( "Music::fillSecondBuffer: "
01595 "not supported on the ARM7" ) ;
01596
01597 #elif defined(OSDL_RUNS_ON_ARM9)
01598
01599 LOG_TRACE_AUDIO( "Music::fillSecondBuffer." ) ;
01600
01601 LowLevelMusic & music = getContent() ;
01602
01603 music._requestFillOfSecondBuffer = false ;
01604
01605 try
01606 {
01607
01608
01609
01610
01611 BufferSize readSize = music._musicFile->read(
01612 _CommandManagerSettings->_secondBuffer,
01613 _CommandManagerSettings->_bufferSize ) ;
01614
01615 LOG_DEBUG_AUDIO( "Music::fillSecondBuffer: read "
01616 + Ceylan::toString( readSize ) + " bytes." ) ;
01617
01618
01619
01620
01621
01622 if ( readSize < _CommandManagerSettings->_bufferSize )
01623 {
01624
01625 LOG_TRACE_AUDIO( "Padding second buffer." ) ;
01626
01627 ::memset(
01628 _CommandManagerSettings->_secondBuffer + readSize,
01629 0,
01630 _CommandManagerSettings->_bufferSize - readSize
01631 ) ;
01632
01633
01634
01635 DC_FlushRange( (void*) _CommandManagerSettings->_secondBuffer,
01636 _CommandManagerSettings->_bufferSize ) ;
01637
01638 try
01639 {
01640
01641 _CommandManagerSettings->_commandManager->notifyEndOfEncodedStreamReached() ;
01642
01643 }
01644 catch( const CommandException & e )
01645 {
01646
01647 throw AudioException( "Music::fillSecondBuffer failed: "
01648 + e.toString() ) ;
01649
01650 }
01651
01652 }
01653 else
01654 {
01655
01656
01657 DC_FlushRange( (void*) _CommandManagerSettings->_secondBuffer,
01658 _CommandManagerSettings->_bufferSize ) ;
01659
01660 }
01661
01662
01663 }
01664 catch( const SystemException & e )
01665 {
01666
01667 throw AudioException( "Music::fillSecondBuffer failed: "
01668 + e.toString() ) ;
01669
01670 }
01671
01672 #endif // OSDL_RUNS_ON_ARM7
01673
01674 #endif // OSDL_ARCH_NINTENDO_DS
01675
01676 }
01677