AudioManager.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <OgreSingleton.h>
  17. #include <boost/thread.hpp>
  18. #include <vorbis/vorbisfile.h>
  19. // Include OpenAL
  20. #if defined(__WIN32__) || defined(_WIN32)
  21. #include <al.h>
  22. #include <alc.h>
  23. #else
  24. #include <AL/al.h>
  25. #include <AL/alc.h>
  26. #endif
  27. /**
  28. * The audio manager.
  29. *
  30. * It handles all music and sounds in the application.
  31. */
  32. class AudioManager : public Ogre::Singleton<AudioManager>{
  33. public:
  34. /**
  35. * Constructor.
  36. */
  37. AudioManager();
  38. /**
  39. * Destructor
  40. */
  41. virtual ~AudioManager();
  42. /**
  43. * Used by boost libraries.
  44. */
  45. void operator()();
  46. /**
  47. * Updates the music player.
  48. */
  49. void Update();
  50. /**
  51. * Pauses currently playing music.
  52. *
  53. * @todo How to resume it?
  54. */
  55. void MusicPause();
  56. /**
  57. * Plays a music track.
  58. *
  59. * To be called from Lua scripts
  60. *
  61. * @param[in] name Name of the track to play.
  62. */
  63. void ScriptPlayMusic(const char* name);
  64. /**
  65. * Plays a music track.
  66. *
  67. * @param[in] name Name of the track to play.
  68. */
  69. void MusicPlay(const Ogre::String& name);
  70. /**
  71. * Plays a sound.
  72. *
  73. * To be called from Lua scripts
  74. *
  75. * @param[in] name Name of the sound to play.
  76. */
  77. void ScriptPlaySound(const char* name);
  78. /**
  79. * Plays a sound in a channel.
  80. *
  81. * To be called from Lua scripts
  82. *
  83. * @param[in] name Name of the sound to play.
  84. * @param[in] channel Channel to play the sound in (1-5).
  85. */
  86. void ScriptPlaySound(const char* name, const int channel);
  87. /**
  88. * Plays up to 4 sounds, in 4 different channels.
  89. *
  90. * To be called from Lua scripts.
  91. *
  92. * @param[in] name1 Name of the first sound to play.
  93. * @param[in] name2 Name of the second sound to play.
  94. * @param[in] name3 Name of the third sound to play.
  95. * @param[in] name4 Name of the fourth sound to play.
  96. */
  97. void ScriptPlaySounds(
  98. const char* name1, const char* name2, const char* name3, const char* name4
  99. );
  100. /**
  101. * Plays a sound.
  102. *
  103. * @param[in] name Name of the sound to play.
  104. */
  105. void SoundPlay(const Ogre::String& name);
  106. /**
  107. * Stops the currently playing music.
  108. *
  109. * Playback can't be resumed.
  110. */
  111. void MusicStop();
  112. /**
  113. * Music structure.
  114. *
  115. * Defines a music entry in musics.xml.
  116. */
  117. struct Music{
  118. /**
  119. * The name of the track.
  120. */
  121. Ogre::String name;
  122. /**
  123. * Track filename
  124. */
  125. Ogre::String file;
  126. /**
  127. * Music loop location for continous playback.
  128. *
  129. * @todo How is a loop done with only one value? shouldn't it be
  130. * start and end of loop?
  131. */
  132. float loop;
  133. };
  134. /**
  135. * Music structure.
  136. *
  137. * Defines a sound entry in sound.xml.
  138. */
  139. struct Sound{
  140. /**
  141. * The name of the sound.
  142. */
  143. Ogre::String name;
  144. /**
  145. * Sound filename
  146. */
  147. Ogre::String file;
  148. };
  149. /**
  150. * Adds a music track to the audio manager.
  151. *
  152. * @param[in] music The track to add.
  153. */
  154. void AddMusic(const AudioManager::Music& music);
  155. /**
  156. * Retrieves a music track by name.
  157. *
  158. * @param[in] name The track name.
  159. * @return The music track, or nullptr if there is no track by that name.
  160. */
  161. AudioManager::Music* GetMusic(const Ogre::String& name);
  162. /**
  163. * Adds a sound to the audio manager.
  164. *
  165. * @param[in] sound The sound to add.
  166. */
  167. void AddSound(const AudioManager::Sound& sound);
  168. /**
  169. * Retrieves a sound by name.
  170. *
  171. * @param[in] name The sound name.
  172. * @return The sound track, or nullptr if there is no sound by that name.
  173. */
  174. AudioManager::Sound* GetSound(const Ogre::String& name);
  175. private:
  176. /**
  177. * Initializes the audio manager.
  178. *
  179. * @todo Verify this comment.
  180. */
  181. const bool Init();
  182. /**
  183. * Handles errors
  184. *
  185. * @return nullptr
  186. * @todo Implement and get error info.
  187. */
  188. const char* ALError();
  189. /**
  190. * Handles errors
  191. *
  192. * @return nullptr
  193. * @todo Implement and get error info.
  194. */
  195. const char* ALCError( const ALCdevice* device );
  196. /**
  197. * An audio player
  198. */
  199. class Player{
  200. public:
  201. /**
  202. * Constructor.
  203. *
  204. * @param[in] mutex Mutex to handle concurrent updates.
  205. */
  206. Player(boost::recursive_mutex* mutex);
  207. /**
  208. * Destructor.
  209. */
  210. ~Player();
  211. /**
  212. * Pauses the player.
  213. */
  214. void Pause();
  215. /**
  216. * Plays an audio file.
  217. *
  218. * @param[in] file Path to the file to play
  219. */
  220. void Play(const Ogre::String& file);
  221. /**
  222. * Stops the audio player.
  223. */
  224. void Stop();
  225. /**
  226. * Sets the loop for the current track.
  227. *
  228. * @param[in] loop Loop point
  229. */
  230. void SetLoop(const float loop);
  231. /**
  232. * Updates the audio player.
  233. *
  234. * @todo Understand and document.
  235. */
  236. void Update();
  237. /**
  238. * Get the playing position of the current track.
  239. *
  240. * @return Playing position.
  241. * @todo Is this in seconds?
  242. */
  243. float GetPosition();
  244. private:
  245. /**
  246. * Mutex to handle concurrent update on the player.
  247. */
  248. boost::recursive_mutex* update_mutex_;
  249. /**
  250. * Loop point for the current track.
  251. */
  252. float loop_;
  253. /**
  254. * Name of the file currently being played.
  255. */
  256. Ogre::String file_;
  257. /**
  258. * File played or to play.
  259. */
  260. OggVorbis_File vorbis_file_;
  261. /**
  262. * Info about the track.
  263. */
  264. vorbis_info* vorbis_info_;
  265. /**
  266. * Section of the track.
  267. */
  268. int vorbis_section_;
  269. /**
  270. * Indicates if the stream is finished.
  271. */
  272. bool stream_finished_;
  273. /**
  274. * Audio buffer.
  275. */
  276. char* buffer_;
  277. /**
  278. * Audio source.
  279. */
  280. ALuint source_;
  281. /**
  282. * Fills the audio buffer.
  283. *
  284. * @return Size of the data added to the buffer.
  285. */
  286. ALsizei FillBuffer();
  287. };
  288. private:
  289. /**
  290. * Indicatesif the audio manager has been initialized.
  291. */
  292. bool initialized_;
  293. /**
  294. * Audio output device.
  295. */
  296. ALCdevice* al_device_;
  297. /**
  298. * Audio context.
  299. */
  300. ALCcontext* al_context_;
  301. /**
  302. * Audio buffer.
  303. *
  304. * TODO: Unused? Not there are buffers per player. Remove.
  305. */
  306. char* buffer_;
  307. /**
  308. * Mutex to handle concurrent operations.
  309. */
  310. boost::recursive_mutex update_mutex_;
  311. /**
  312. * Thread to handle concurrent operations.
  313. */
  314. boost::thread* update_thread_;
  315. /**
  316. * Flag to indicate if the playback must continue.
  317. */
  318. bool thread_continue_;
  319. /**
  320. * Music player.
  321. */
  322. AudioManager::Player music_;
  323. /**
  324. * Sound effect player.
  325. *
  326. * TODO: Unused? remove.
  327. */
  328. AudioManager::Player fx_;
  329. /**
  330. * List of music.
  331. */
  332. std::list<AudioManager::Music> music_list_;
  333. /**
  334. * List of music.
  335. */
  336. std::list<AudioManager::Sound> sound_list_;
  337. /**
  338. * Size of a channel buffer.
  339. *
  340. * 96Kb are allocated for each channel. ~0.5 seconds of 44100Hz stereo
  341. * 16-bit data can be allocated in each channel. This way, 250ms can
  342. * pass between buffer updates.
  343. */
  344. static ALsizei channel_buffer_size_;
  345. /**
  346. * Number of buffers.
  347. *
  348. * 2 channels.
  349. */
  350. static int channel_buffer_number_;
  351. };