sound_world_al.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "sound_world.h"
  24. #include "id_array.h"
  25. #include "vector3.h"
  26. #include "matrix4x4.h"
  27. #include "sound_resource.h"
  28. #include "temp_allocator.h"
  29. #include "log.h"
  30. #include "audio.h"
  31. #include <AL/al.h>
  32. #include <AL/alc.h>
  33. namespace crown
  34. {
  35. //-----------------------------------------------------------------------------
  36. #if defined(CROWN_DEBUG)
  37. static const char* al_error_to_string(ALenum error)
  38. {
  39. switch (error)
  40. {
  41. case AL_INVALID_ENUM: return "AL_INVALID_ENUM";
  42. case AL_INVALID_VALUE: return "AL_INVALID_VALUE";
  43. case AL_INVALID_OPERATION: return "AL_INVALID_OPERATION";
  44. case AL_OUT_OF_MEMORY: return "AL_OUT_OF_MEMORY";
  45. default: return "UNKNOWN_AL_ERROR";
  46. }
  47. }
  48. #define AL_CHECK(function)\
  49. function;\
  50. do { ALenum error; CE_ASSERT((error = alGetError()) == AL_NO_ERROR,\
  51. "OpenAL error: %s", al_error_to_string(error)); } while (0)
  52. #else
  53. #define AL_CHECK(function) function;
  54. #endif
  55. /// Global audio-related functions
  56. namespace audio_globals
  57. {
  58. static ALCdevice* s_al_device;
  59. static ALCcontext* s_al_context;
  60. void init()
  61. {
  62. s_al_device = alcOpenDevice(NULL);
  63. CE_ASSERT(s_al_device, "Cannot open OpenAL audio device");
  64. s_al_context = alcCreateContext(s_al_device, NULL);
  65. CE_ASSERT(s_al_context, "Cannot create OpenAL context");
  66. AL_CHECK(alcMakeContextCurrent(s_al_context));
  67. CE_LOGD("OpenAL Vendor : %s", alGetString(AL_VENDOR));
  68. CE_LOGD("OpenAL Version : %s", alGetString(AL_VERSION));
  69. CE_LOGD("OpenAL Renderer : %s", alGetString(AL_RENDERER));
  70. AL_CHECK(alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED));
  71. AL_CHECK(alDopplerFactor(1.0f));
  72. AL_CHECK(alDopplerVelocity(343.0f));
  73. }
  74. void shutdown()
  75. {
  76. alcDestroyContext(s_al_context);
  77. alcCloseDevice(s_al_device);
  78. }
  79. }
  80. //-----------------------------------------------------------------------------
  81. struct SoundInstance
  82. {
  83. void create(SoundResource* sr, const Vector3& pos)
  84. {
  85. AL_CHECK(alGenSources(1, &m_source));
  86. CE_ASSERT(alIsSource(m_source), "Bad OpenAL source");
  87. // AL_CHECK(alSourcef(m_source, AL_PITCH, 1.0f));
  88. // AL_CHECK(alSourcef(m_source, AL_REFERENCE_DISTANCE, 0.1f));
  89. // AL_CHECK(alSourcef(m_source, AL_MAX_DISTANCE, 1000.0f));
  90. // Generates AL buffers
  91. AL_CHECK(alGenBuffers(1, &m_buffer));
  92. CE_ASSERT(alIsBuffer(m_buffer), "Bad OpenAL buffer");
  93. ALenum format;
  94. switch (sr->bits_ps())
  95. {
  96. case 8: format = sr->channels() > 1 ? AL_FORMAT_STEREO8 : AL_FORMAT_MONO8; break;
  97. case 16: format = sr->channels() > 1 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16; break;
  98. default: CE_FATAL("Number of bits per sample not supported."); break;
  99. }
  100. AL_CHECK(alBufferData(m_buffer, format, sr->data(), sr->size(), sr->sample_rate()));
  101. m_resource = sr;
  102. set_position(pos);
  103. }
  104. void destroy()
  105. {
  106. stop();
  107. AL_CHECK(alSourcei(m_source, AL_BUFFER, 0));
  108. AL_CHECK(alDeleteBuffers(1, &m_buffer));
  109. AL_CHECK(alDeleteSources(1, &m_source));
  110. }
  111. void reload(SoundResource* new_sr)
  112. {
  113. destroy();
  114. create(new_sr, position());
  115. }
  116. void play(bool loop, float volume)
  117. {
  118. set_volume(volume);
  119. AL_CHECK(alSourcei(m_source, AL_LOOPING, (loop ? AL_TRUE : AL_FALSE)));
  120. AL_CHECK(alSourceQueueBuffers(m_source, 1, &m_buffer));
  121. AL_CHECK(alSourcePlay(m_source));
  122. }
  123. void pause()
  124. {
  125. AL_CHECK(alSourcePause(m_source));
  126. }
  127. void resume()
  128. {
  129. AL_CHECK(alSourcePlay(m_source));
  130. }
  131. void stop()
  132. {
  133. AL_CHECK(alSourceStop(m_source));
  134. AL_CHECK(alSourceRewind(m_source)); // Workaround
  135. ALint processed;
  136. AL_CHECK(alGetSourcei(m_source, AL_BUFFERS_PROCESSED, &processed));
  137. if (processed > 0)
  138. {
  139. ALuint removed;
  140. AL_CHECK(alSourceUnqueueBuffers(m_source, 1, &removed));
  141. }
  142. }
  143. bool is_playing()
  144. {
  145. ALint state;
  146. AL_CHECK(alGetSourcei(m_source, AL_SOURCE_STATE, &state));
  147. return state == AL_PLAYING;
  148. }
  149. bool finished()
  150. {
  151. ALint state;
  152. AL_CHECK(alGetSourcei(m_source, AL_SOURCE_STATE, &state));
  153. return (state != AL_PLAYING && state != AL_PAUSED);
  154. }
  155. Vector3 position()
  156. {
  157. ALfloat pos[3];
  158. AL_CHECK(alGetSourcefv(m_source, AL_POSITION, pos));
  159. return Vector3(pos[0], pos[1], pos[2]);
  160. }
  161. void set_position(const Vector3& pos)
  162. {
  163. AL_CHECK(alSourcefv(m_source, AL_POSITION, vector3::to_float_ptr(pos)));
  164. }
  165. void set_range(float range)
  166. {
  167. AL_CHECK(alSourcef(m_source, AL_MAX_DISTANCE, range));
  168. }
  169. void set_volume(float volume)
  170. {
  171. AL_CHECK(alSourcef(m_source, AL_GAIN, volume));
  172. }
  173. SoundResource* resource()
  174. {
  175. return m_resource;
  176. }
  177. public:
  178. SoundInstanceId m_id;
  179. SoundResource* m_resource;
  180. ALuint m_buffer;
  181. ALuint m_source;
  182. };
  183. class ALSoundWorld : public SoundWorld
  184. {
  185. public:
  186. ALSoundWorld()
  187. {
  188. set_listener_pose(matrix4x4::IDENTITY);
  189. }
  190. virtual ~ALSoundWorld()
  191. {
  192. }
  193. virtual SoundInstanceId play(SoundResource* sr, bool loop, float volume, const Vector3& pos)
  194. {
  195. SoundInstance instance;
  196. instance.create(sr, pos);
  197. SoundInstanceId id = id_array::create(m_playing_sounds, instance);
  198. id_array::get(m_playing_sounds, id).m_id = id;
  199. instance.play(loop, volume);
  200. return id;
  201. }
  202. virtual void stop(SoundInstanceId id)
  203. {
  204. SoundInstance& instance = id_array::get(m_playing_sounds, id);
  205. instance.destroy();
  206. id_array::destroy(m_playing_sounds, id);
  207. }
  208. virtual bool is_playing(SoundInstanceId id)
  209. {
  210. return id_array::has(m_playing_sounds, id) && id_array::get(m_playing_sounds, id).is_playing();
  211. }
  212. virtual void stop_all()
  213. {
  214. for (uint32_t i = 0; i < id_array::size(m_playing_sounds); i++)
  215. {
  216. m_playing_sounds[i].stop();
  217. }
  218. }
  219. virtual void pause_all()
  220. {
  221. for (uint32_t i = 0; i < id_array::size(m_playing_sounds); i++)
  222. {
  223. m_playing_sounds[i].pause();
  224. }
  225. }
  226. virtual void resume_all()
  227. {
  228. for (uint32_t i = 0; i < id_array::size(m_playing_sounds); i++)
  229. {
  230. m_playing_sounds[i].resume();
  231. }
  232. }
  233. virtual void set_sound_positions(uint32_t num, const SoundInstanceId* ids, const Vector3* positions)
  234. {
  235. for (uint32_t i = 0; i < num; i++)
  236. {
  237. id_array::get(m_playing_sounds, ids[i]).set_position(positions[i]);
  238. }
  239. }
  240. virtual void set_sound_ranges(uint32_t num, const SoundInstanceId* ids, const float* ranges)
  241. {
  242. for (uint32_t i = 0; i < num; i++)
  243. {
  244. id_array::get(m_playing_sounds, ids[i]).set_range(ranges[i]);
  245. }
  246. }
  247. virtual void set_sound_volumes(uint32_t num, const SoundInstanceId* ids, const float* volumes)
  248. {
  249. for (uint32_t i = 0; i < num; i++)
  250. {
  251. id_array::get(m_playing_sounds, ids[i]).set_volume(volumes[i]);
  252. }
  253. }
  254. virtual void reload_sounds(SoundResource* old_sr, SoundResource* new_sr)
  255. {
  256. for (uint32_t i = 0; i < id_array::size(m_playing_sounds); i++)
  257. {
  258. if (m_playing_sounds[i].resource() == old_sr)
  259. {
  260. m_playing_sounds[i].reload(new_sr);
  261. }
  262. }
  263. }
  264. virtual void set_listener_pose(const Matrix4x4& pose)
  265. {
  266. const Vector3 pos = matrix4x4::translation(pose);
  267. const Vector3 up = matrix4x4::y(pose);
  268. const Vector3 at = matrix4x4::z(pose);
  269. AL_CHECK(alListener3f(AL_POSITION, pos.x, pos.y, pos.z));
  270. //AL_CHECK(alListener3f(AL_VELOCITY, vel.x, vel.y, vel.z));
  271. const ALfloat orientation[] = { up.x, up.y, up.z, at.x, at.y, at.z };
  272. AL_CHECK(alListenerfv(AL_ORIENTATION, orientation));
  273. m_listener_pose = pose;
  274. }
  275. virtual void update()
  276. {
  277. TempAllocator256 alloc;
  278. Array<SoundInstanceId> to_delete(alloc);
  279. // Check what sounds finished playing
  280. for (uint32_t i = 0; i < id_array::size(m_playing_sounds); i++)
  281. {
  282. SoundInstance& instance = m_playing_sounds[i];
  283. if (instance.finished())
  284. {
  285. array::push_back(to_delete, instance.m_id);
  286. }
  287. }
  288. // Destroy instances which finished playing
  289. for (uint32_t i = 0; i < array::size(to_delete); i++)
  290. {
  291. stop(to_delete[i]);
  292. }
  293. }
  294. private:
  295. IdArray<CE_MAX_SOUND_INSTANCES, SoundInstance> m_playing_sounds;
  296. Matrix4x4 m_listener_pose;
  297. };
  298. SoundWorld* SoundWorld::create(Allocator& a)
  299. {
  300. return CE_NEW(a, ALSoundWorld)();
  301. }
  302. void SoundWorld::destroy(Allocator& a, SoundWorld* sw)
  303. {
  304. CE_DELETE(a, sw);
  305. }
  306. } // namespace crown