AudioManager.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. #include "core/AudioManager.h"
  2. #include <list>
  3. #include <boost/thread.hpp>
  4. #include "core/XmlMusicsFile.h"
  5. #include "core/Logger.h"
  6. template<>AudioManager *Ogre::Singleton< AudioManager >::msSingleton = NULL;
  7. ALsizei AudioManager::m_ChannelBufferNumber = 2;
  8. int AudioManager::m_ChannelBufferSize = 96 * 1024;
  9. AudioManager::AudioManager():
  10. m_Initialized( false ),
  11. m_ThreadContinue( true ),
  12. m_UpdateMutex(),
  13. m_Music( &m_UpdateMutex )
  14. {
  15. m_ALDevice = alcOpenDevice( NULL );
  16. if( m_ALDevice != NULL )
  17. {
  18. m_ALContext = alcCreateContext( m_ALDevice, NULL );
  19. if( m_ALContext != NULL )
  20. {
  21. alcMakeContextCurrent( m_ALContext );
  22. // listeners
  23. ALfloat position[ 3 ] = { 0.0f, 0.0f, 0.0f };
  24. ALfloat velocity[ 3 ] = { 0.0f, 0.0f, 0.0f };
  25. ALfloat orientation[ 6 ] = { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f };
  26. alListenerfv( AL_POSITION, position );
  27. alListenerfv( AL_VELOCITY, velocity );
  28. alListenerfv( AL_ORIENTATION, orientation );
  29. m_Initialized = true;
  30. m_Buffer = new char[ m_ChannelBufferSize ];
  31. m_UpdateThread = new boost::thread( boost::ref( *this ) );
  32. LOG_TRIVIAL( "AudioManager initialised." );
  33. }
  34. else
  35. {
  36. LOG_ERROR( "AudioManager failed to initialised. Could not create context for sound device." );
  37. }
  38. }
  39. else
  40. {
  41. LOG_ERROR( "AudioManager failed to initialised. There's no default sound device." );
  42. }
  43. // Load musics
  44. XmlMusicsFile musics( "./data/musics.xml" );
  45. musics.LoadMusics();
  46. }
  47. AudioManager::~AudioManager()
  48. {
  49. MusicStop();
  50. if( m_Initialized )
  51. {
  52. m_ThreadContinue = false;
  53. m_UpdateThread->join();
  54. delete m_UpdateThread;
  55. delete[] m_Buffer;
  56. alcMakeContextCurrent( NULL );
  57. alcDestroyContext( m_ALContext );
  58. alcCloseDevice( m_ALDevice );
  59. LOG_TRIVIAL( "AudioManager destroyed." );
  60. }
  61. }
  62. void
  63. AudioManager::operator()()
  64. {
  65. while( m_ThreadContinue )
  66. {
  67. Update();
  68. boost::xtime xt;
  69. boost::xtime_get( &xt, boost::TIME_UTC_ );
  70. xt.nsec += 250000000; // sleep for 250 ms
  71. boost::thread::sleep( xt );
  72. m_UpdateThread->yield();
  73. }
  74. }
  75. void
  76. AudioManager::Update()
  77. {
  78. boost::recursive_mutex::scoped_lock lock( m_UpdateMutex );
  79. m_Music.Update();
  80. }
  81. void
  82. AudioManager::MusicPause()
  83. {
  84. boost::recursive_mutex::scoped_lock lock( m_UpdateMutex );
  85. m_Music.Pause();
  86. }
  87. void
  88. AudioManager::MusicPlay( const Ogre::String& name )
  89. {
  90. if( m_Initialized )
  91. {
  92. boost::recursive_mutex::scoped_lock lock( m_UpdateMutex );
  93. AudioManager::Music* music = GetMusic( name );
  94. if( music == NULL )
  95. {
  96. LOG_ERROR( "No music found with name \"" + name + "\"." );
  97. return;
  98. }
  99. m_Music.SetLoop( music->loop );
  100. m_Music.Play( music->file );
  101. }
  102. }
  103. void
  104. AudioManager::MusicStop()
  105. {
  106. boost::recursive_mutex::scoped_lock lock( m_UpdateMutex );
  107. m_Music.Stop();
  108. }
  109. void
  110. AudioManager::AddMusic( const AudioManager::Music& music )
  111. {
  112. boost::recursive_mutex::scoped_lock lock( m_UpdateMutex );
  113. for( std::list< AudioManager::Music >::iterator it = m_MusicList.begin(); it != m_MusicList.end(); ++it )
  114. {
  115. if( it->name == music.name )
  116. {
  117. LOG_ERROR( "Music with name '" + music.name + "' already exists." );
  118. return;
  119. }
  120. }
  121. m_MusicList.push_back( music );
  122. }
  123. AudioManager::Music*
  124. AudioManager::GetMusic( const Ogre::String& name )
  125. {
  126. boost::recursive_mutex::scoped_lock lock( m_UpdateMutex );
  127. for( auto it = m_MusicList.begin(); it != m_MusicList.end(); ++it )
  128. {
  129. if( it->name == name )
  130. {
  131. return &( *it );
  132. }
  133. }
  134. return NULL;
  135. }
  136. const char*
  137. AudioManager::ALError()
  138. {
  139. //ALenum error_code = alGetError();
  140. //if( error_code == AL_NO_ERROR )
  141. {
  142. return NULL;
  143. }
  144. //else
  145. //{
  146. //return alGetString( error_code );
  147. //}
  148. }
  149. const char*
  150. AudioManager::ALCError( const ALCdevice* device )
  151. {
  152. //const ALCdevice *alc_device = ( ( device == NULL ) ? const_cast< ALCdevice* >( m_ALDevice ) : const_cast< ALCdevice* >( device ) );
  153. //ALCenum error_code = alcGetError( const_cast< ALCdevice* >( alc_device ) );
  154. //if( error_code == ALC_NO_ERROR )
  155. {
  156. return NULL;
  157. }
  158. //else
  159. //{
  160. //return alcGetString( const_cast< ALCdevice* >( device ), error_code );
  161. //}
  162. }
  163. AudioManager::Player::Player( boost::recursive_mutex* mutex ):
  164. m_Loop( -1.0 ),
  165. m_VorbisInfo( NULL ),
  166. m_VorbisSection( 0 ),
  167. m_StreamFinished( false ),
  168. m_UpdateMutex( mutex )
  169. {
  170. }
  171. AudioManager::Player::~Player()
  172. {
  173. Stop();
  174. }
  175. void
  176. AudioManager::Player::Pause()
  177. {
  178. boost::recursive_mutex::scoped_lock lock( *m_UpdateMutex );
  179. alSourcePause( m_Source );
  180. }
  181. void
  182. AudioManager::Player::Play( const Ogre::String &file )
  183. {
  184. boost::recursive_mutex::scoped_lock lock( *m_UpdateMutex );
  185. // open vorbis file
  186. if( ov_fopen( const_cast< char* >( file.c_str() ), &m_VorbisFile ) )
  187. {
  188. LOG_ERROR( "Can't play file \"" + file + "\"." );
  189. return;
  190. }
  191. // get file info
  192. m_VorbisInfo = ov_info( &m_VorbisFile, -1.0f );
  193. // create sound source
  194. alGenSources( 1, &m_Source );
  195. // create buffers
  196. ALuint buffers[ m_ChannelBufferNumber ];
  197. alGenBuffers( m_ChannelBufferNumber, buffers );
  198. // set source parameters
  199. alSourcei( m_Source, AL_LOOPING, AL_FALSE );
  200. // fill buffers
  201. for( int i = 0; i < m_ChannelBufferNumber; ++i )
  202. {
  203. ALsizei buffer_size = FillBuffer();
  204. if( buffer_size )
  205. {
  206. alBufferData( buffers[ i ], m_VorbisInfo->channels == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, ( const ALvoid* )AudioManager::getSingleton().m_Buffer, ( ALsizei )buffer_size, ( ALsizei )m_VorbisInfo->rate );
  207. alSourceQueueBuffers( m_Source, 1, &buffers[ i ] );
  208. }
  209. else
  210. {
  211. m_StreamFinished = true;
  212. alDeleteBuffers( 1, &buffers[ i ] );
  213. }
  214. }
  215. // start playback
  216. alSourcePlay( m_Source );
  217. }
  218. void
  219. AudioManager::Player::Stop()
  220. {
  221. boost::recursive_mutex::scoped_lock lock( *m_UpdateMutex );
  222. // stop source
  223. alSourceStop( m_Source );
  224. // get source buffers
  225. int queued_count = 0;
  226. alGetSourcei( m_Source, AL_BUFFERS_QUEUED, &queued_count );
  227. // unqueue and delete buffers
  228. for( int i = 0; i < queued_count; ++i )
  229. {
  230. ALuint buffer_id;
  231. alSourceUnqueueBuffers( m_Source, 1, &buffer_id );
  232. alDeleteBuffers( 1, &buffer_id );
  233. }
  234. // delete source
  235. alDeleteSources( 1, &m_Source );
  236. // rest cleanup
  237. ov_clear( &m_VorbisFile );
  238. }
  239. void
  240. AudioManager::Player::SetLoop( const float loop )
  241. {
  242. boost::recursive_mutex::scoped_lock lock( *m_UpdateMutex );
  243. m_Loop = loop;
  244. }
  245. void
  246. AudioManager::Player::Update()
  247. {
  248. // try to fill processed buffers
  249. {
  250. int processed;
  251. alGetSourcei( m_Source, AL_BUFFERS_PROCESSED, &processed );
  252. for( int i = 0; i < processed; ++i )
  253. {
  254. // try fill buffer
  255. ALsizei buffer_size = FillBuffer();
  256. if( buffer_size )
  257. {
  258. ALuint buffer_id;
  259. alSourceUnqueueBuffers( m_Source, 1, &buffer_id );
  260. alBufferData( buffer_id, m_VorbisInfo->channels == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, ( const ALvoid* )AudioManager::getSingleton().m_Buffer, ( ALsizei )buffer_size, ( ALsizei )m_VorbisInfo->rate );
  261. alSourceQueueBuffers( m_Source, 1, &buffer_id );
  262. }
  263. // finished reading stream
  264. else
  265. {
  266. m_StreamFinished = true;
  267. break;
  268. }
  269. }
  270. }
  271. // manage source state
  272. {
  273. int processed;
  274. alGetSourcei( m_Source, AL_BUFFERS_PROCESSED, &processed );
  275. int queued;
  276. alGetSourcei( m_Source, AL_BUFFERS_QUEUED, &queued );
  277. if( m_StreamFinished && processed == queued )
  278. {
  279. Stop();
  280. }
  281. else
  282. {
  283. ALenum source_state;
  284. alGetSourcei( m_Source, AL_SOURCE_STATE, &source_state );
  285. if( source_state == AL_STOPPED )
  286. {
  287. alSourcePlay( m_Source );
  288. }
  289. }
  290. }
  291. }
  292. ALsizei
  293. AudioManager::Player::FillBuffer()
  294. {
  295. ALsizei read = 0;
  296. if( m_StreamFinished )
  297. {
  298. return read;
  299. }
  300. char *&buffer = AudioManager::getSingleton().m_Buffer;
  301. bool finished = false;
  302. do
  303. {
  304. long result = ov_read( &m_VorbisFile, buffer + read, m_ChannelBufferSize - read, 0, 2, 1, &m_VorbisSection );
  305. switch( result )
  306. {
  307. // error
  308. case OV_HOLE:
  309. case OV_EBADLINK:
  310. case OV_EINVAL:
  311. {
  312. finished = true;
  313. }
  314. break;
  315. // end of stream
  316. case 0:
  317. {
  318. // if there isn't loop point or can't seek
  319. if( m_Loop < 0.0f || ov_time_seek( &m_VorbisFile, m_Loop ) )
  320. {
  321. finished = true;
  322. }
  323. }
  324. break;
  325. // readed "result" bytes
  326. default:
  327. {
  328. read += result;
  329. if( read == m_ChannelBufferSize )
  330. {
  331. finished = true;
  332. }
  333. }
  334. }
  335. }
  336. while( finished == false );
  337. return read;
  338. }
  339. float
  340. AudioManager::Player::GetPosition()
  341. {
  342. boost::recursive_mutex::scoped_lock lock( *m_UpdateMutex );
  343. int play_offset;
  344. alGetSourcei( m_Source, AL_SAMPLE_OFFSET, &play_offset );
  345. return ( ov_pcm_tell( &m_VorbisFile ) - ( m_ChannelBufferNumber * m_ChannelBufferSize / m_VorbisInfo->channels / sizeof( int16_t ) - play_offset ) ) * 1000 / m_VorbisInfo->rate;
  346. }