AudioManager.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. * @param name[in] Name of the track to play.
  60. */
  61. void MusicPlay(const Ogre::String& name);
  62. /**
  63. * Stops the currently playing music.
  64. *
  65. * Playback can't be resumed.
  66. */
  67. void MusicStop();
  68. /**
  69. * Music structure.
  70. *
  71. * Defines a music track.
  72. */
  73. struct Music{
  74. /**
  75. * The name of the track.
  76. */
  77. Ogre::String name;
  78. /**
  79. * Track filename
  80. */
  81. Ogre::String file;
  82. /**
  83. * Music loop location for continous playback.
  84. *
  85. * @todo How is a loop done with only one value? shouldn't it be
  86. * start and end of loop?
  87. */
  88. float loop;
  89. };
  90. /**
  91. * Adds a music track to the audio manager.
  92. *
  93. * @param music[in] The track to add.
  94. */
  95. void AddMusic(const AudioManager::Music& music);
  96. /**
  97. * Retrieves a muisc track by name.
  98. *
  99. * @param name[in] The track name
  100. * @return The music track, or nullptr if there is no track by that
  101. * name.
  102. */
  103. AudioManager::Music* GetMusic(const Ogre::String& name);
  104. private:
  105. /**
  106. * Initializes the audio manager.
  107. *
  108. * @todo Verify this comment.
  109. */
  110. const bool Init();
  111. /**
  112. * Handles errors
  113. *
  114. * @return nullptr
  115. * @todo Implement and get error info.
  116. */
  117. const char* ALError();
  118. /**
  119. * Handles errors
  120. *
  121. * @return nullptr
  122. * @todo Implement and get error info.
  123. */
  124. const char* ALCError( const ALCdevice* device );
  125. /**
  126. * An audio player
  127. */
  128. class Player{
  129. public:
  130. /**
  131. * Constructor.
  132. *
  133. * @param mutex[in] Mutex to handle concurrent updates.
  134. */
  135. Player(boost::recursive_mutex* mutex);
  136. /**
  137. * Destructor.
  138. */
  139. ~Player();
  140. /**
  141. * Pauses the player.
  142. */
  143. void Pause();
  144. /**
  145. * Plays an audio file.
  146. *
  147. * @param file[in] Path to the file to play
  148. */
  149. void Play(const Ogre::String& file);
  150. /**
  151. * Stops the audio player.
  152. */
  153. void Stop();
  154. /**
  155. * Sets the loop for the current track.
  156. *
  157. * @param loop[in] Loop point
  158. */
  159. void SetLoop(const float loop);
  160. /**
  161. * Updates the audio player.
  162. *
  163. * @param Understand and document.
  164. */
  165. void Update();
  166. /**
  167. * Get the playing position of the current track.
  168. *
  169. * @return Playing position.
  170. * @todo Is this in seconds?
  171. */
  172. float GetPosition();
  173. private:
  174. /**
  175. * Mutex to handle concurrent update on the player.
  176. */
  177. boost::recursive_mutex* update_mutex_;
  178. /**
  179. * Loop point for the current track.
  180. */
  181. float loop_;
  182. /**
  183. * File played or to play.
  184. */
  185. OggVorbis_File vorbis_file_;
  186. /**
  187. * Info about the track.
  188. */
  189. vorbis_info* vorbis_info_;
  190. /**
  191. * Section of the track.
  192. */
  193. int vorbis_section_;
  194. /**
  195. * Indicates if the stream is finished.
  196. */
  197. bool stream_finished_;
  198. /**
  199. * Audio source.
  200. */
  201. ALuint source_;
  202. /**
  203. * Fills the audio buffer.
  204. *
  205. * @return Size of the data added to the buffer.
  206. */
  207. ALsizei FillBuffer();
  208. };
  209. private:
  210. /**
  211. * Indicatesif the audio manager has been initialized.
  212. */
  213. bool initialized_;
  214. /**
  215. * Audio output device.
  216. */
  217. ALCdevice* al_device_;
  218. /**
  219. * Audio context.
  220. */
  221. ALCcontext* al_context_;
  222. /**
  223. * Audio buffer.
  224. */
  225. char* buffer_;
  226. /**
  227. * Mutex to handle concurrent operations.
  228. */
  229. boost::recursive_mutex update_mutex_;
  230. /**
  231. * Thread to handle consurrent operations.
  232. */
  233. boost::thread* update_thread_;
  234. /**
  235. * Flag to indicate if the playback must continue.
  236. */
  237. bool thread_continue_;
  238. /**
  239. * Music player
  240. */
  241. AudioManager::Player music_;
  242. /**
  243. * List of music.
  244. */
  245. std::list<AudioManager::Music> music_list_;
  246. /**
  247. * Size of a channel buffer.
  248. *
  249. * 96Kb are allocated for each channel. ~0.5 seconds of 44100Hz stereo
  250. * 16-bit data can be allocated in each channel. This way, 250ms can
  251. * pass between buffer updates.
  252. */
  253. static ALsizei channel_buffer_size_;
  254. /**
  255. * Number of buffers.
  256. *
  257. * 2 channels.
  258. */
  259. static int channel_buffer_number_;
  260. };