ALRenderer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 "SoundRenderer.h"
  24. #include "ALRenderer.h"
  25. #include "SoundResource.h"
  26. #include "StringUtils.h"
  27. #include "Log.h"
  28. namespace crown
  29. {
  30. // ALRenderer
  31. class SoundRendererImpl
  32. {
  33. public:
  34. //-----------------------------------------------------------------------------
  35. SoundRendererImpl() :
  36. m_device(NULL),
  37. m_context(NULL)
  38. {
  39. }
  40. //-----------------------------------------------------------------------------
  41. void init()
  42. {
  43. m_device = alcOpenDevice(NULL);
  44. if (!m_device)
  45. {
  46. CE_ASSERT(false, "Cannot open audio device");
  47. }
  48. m_context = alcCreateContext(m_device, NULL);
  49. if (!m_context)
  50. {
  51. CE_ASSERT(false, "Cannot create context");
  52. }
  53. AL_CHECK(alcMakeContextCurrent(m_context));
  54. AL_CHECK(alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED));
  55. AL_CHECK(alDopplerFactor(1.0f));
  56. AL_CHECK(alDopplerVelocity(343.0f));
  57. // Default listener
  58. Vector3 pos(0.0f, 0.0f, 0.0f);
  59. Vector3 vel(0.0f, 0.0f, 0.0f);
  60. Vector3 at(0.0f, 0.0f, -1.0f);
  61. Vector3 up(0.0f, 1.0f, 0.0f);
  62. set_listener(pos, vel, at, up);
  63. }
  64. //-----------------------------------------------------------------------------
  65. void shutdown()
  66. {
  67. alcDestroyContext(m_context);
  68. alcCloseDevice(m_device);
  69. }
  70. //-----------------------------------------------------------------------------
  71. void set_listener(const Vector3& pos, const Vector3& vel, const Vector3& or_up, const Vector3& or_at)
  72. {
  73. AL_CHECK(alListener3f(AL_POSITION, pos.x, pos.y, pos.z));
  74. AL_CHECK(alListener3f(AL_VELOCITY, vel.x, vel.y, vel.z));
  75. ALfloat orientation[] = { or_up.x, or_up.y, or_up.z,
  76. or_at.x, or_at.y, or_at.z };
  77. AL_CHECK(alListenerfv(AL_ORIENTATION, orientation));
  78. }
  79. private:
  80. ALCdevice* m_device;
  81. ALCcontext* m_context;
  82. SoundBuffer m_buffers[MAX_SOUND_BUFFERS];
  83. SoundSource m_sources[MAX_SOUND_SOURCES];
  84. private:
  85. friend class SoundRenderer;
  86. };
  87. //-----------------------------------------------------------------------------
  88. SoundRenderer::SoundRenderer(Allocator& allocator)
  89. : m_allocator(allocator)
  90. {
  91. m_impl = CE_NEW(m_allocator, SoundRendererImpl);
  92. }
  93. //-----------------------------------------------------------------------------
  94. SoundRenderer::~SoundRenderer()
  95. {
  96. if (m_impl)
  97. {
  98. CE_DELETE(m_allocator, m_impl);
  99. }
  100. }
  101. //-----------------------------------------------------------------------------
  102. void SoundRenderer::init()
  103. {
  104. m_impl->init();
  105. }
  106. //-----------------------------------------------------------------------------
  107. void SoundRenderer::shutdown()
  108. {
  109. m_impl->shutdown();
  110. }
  111. //-----------------------------------------------------------------------------
  112. void SoundRenderer::frame()
  113. {
  114. }
  115. //-----------------------------------------------------------------------------
  116. void SoundRenderer::set_listener(const Vector3& pos, const Vector3& vel, const Vector3& or_up, const Vector3& or_at) const
  117. {
  118. m_impl->set_listener(pos, vel, or_up, or_at);
  119. }
  120. //-----------------------------------------------------------------------------
  121. SoundBufferId SoundRenderer::create_sound_buffer(void* data, size_t size, uint32_t sample_rate, uint32_t num_channels, uint16_t bits_ps)
  122. {
  123. SoundBufferId id = m_buffers_id_table.create();
  124. m_impl->m_buffers[id.index].create(sample_rate, num_channels, bits_ps);
  125. m_impl->m_buffers[id.index].update(data, size);
  126. return id;
  127. }
  128. //-----------------------------------------------------------------------------
  129. void SoundRenderer::destroy_sound_buffer(SoundBufferId id)
  130. {
  131. CE_ASSERT(m_buffers_id_table.has(id), "SoundBuffer does not exists");
  132. m_impl->m_buffers[id.index].destroy();
  133. m_buffers_id_table.destroy(id);
  134. }
  135. //-----------------------------------------------------------------------------
  136. SoundSourceId SoundRenderer::create_sound_source()
  137. {
  138. SoundSourceId id = m_sources_id_table.create();
  139. m_impl->m_sources[id.index].create();
  140. return id;
  141. }
  142. //-----------------------------------------------------------------------------
  143. void SoundRenderer::destroy_sound_source(SoundSourceId id)
  144. {
  145. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  146. m_impl->m_sources[id.index].destroy();
  147. m_sources_id_table.destroy(id);
  148. }
  149. //-----------------------------------------------------------------------------
  150. void SoundRenderer::bind_buffer(SoundBufferId buffer, SoundSourceId source)
  151. {
  152. CE_ASSERT(m_buffers_id_table.has(buffer), "SoundBuffer does not exists");
  153. CE_ASSERT(m_sources_id_table.has(source), "SoundSource does not exists");
  154. m_impl->m_sources[source.index].bind_buffer(m_impl->m_buffers[buffer.index].m_id);
  155. }
  156. //-----------------------------------------------------------------------------
  157. void SoundRenderer::unbind_buffer(SoundSourceId id)
  158. {
  159. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  160. m_impl->m_sources[id.index].unbind_buffer();
  161. }
  162. //-----------------------------------------------------------------------------
  163. void SoundRenderer::play_sound(SoundSourceId id)
  164. {
  165. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  166. m_impl->m_sources[id.index].play();
  167. }
  168. //-----------------------------------------------------------------------------
  169. void SoundRenderer::pause_sound(SoundSourceId id)
  170. {
  171. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  172. m_impl->m_sources[id.index].pause();
  173. }
  174. //-----------------------------------------------------------------------------
  175. void SoundRenderer::set_sound_loop(SoundSourceId id, bool loop)
  176. {
  177. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  178. m_impl->m_sources[id.index].loop(loop);
  179. }
  180. //-----------------------------------------------------------------------------
  181. void SoundRenderer::set_sound_min_distance(SoundSourceId id, const float min_distance)
  182. {
  183. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  184. m_impl->m_sources[id.index].set_min_distance(min_distance);
  185. }
  186. //-----------------------------------------------------------------------------
  187. void SoundRenderer::set_sound_max_distance(SoundSourceId id, const float max_distance)
  188. {
  189. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  190. m_impl->m_sources[id.index].set_max_distance(max_distance);
  191. }
  192. //-----------------------------------------------------------------------------
  193. void SoundRenderer::set_sound_position(SoundSourceId id, const Vector3& pos)
  194. {
  195. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  196. m_impl->m_sources[id.index].set_position(pos);
  197. }
  198. //-----------------------------------------------------------------------------
  199. void SoundRenderer::set_sound_velocity(SoundSourceId id, const Vector3& vel)
  200. {
  201. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  202. m_impl->m_sources[id.index].set_velocity(vel);
  203. }
  204. //-----------------------------------------------------------------------------
  205. void SoundRenderer::set_sound_direction(SoundSourceId id, const Vector3& dir)
  206. {
  207. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  208. m_impl->m_sources[id.index].set_direction(dir);
  209. }
  210. //-----------------------------------------------------------------------------
  211. void SoundRenderer::set_sound_pitch(SoundSourceId id, const float pitch)
  212. {
  213. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  214. m_impl->m_sources[id.index].set_pitch(pitch);
  215. }
  216. //-----------------------------------------------------------------------------
  217. void SoundRenderer::set_sound_gain(SoundSourceId id, const float gain)
  218. {
  219. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  220. m_impl->m_sources[id.index].set_gain(gain);
  221. }
  222. //-----------------------------------------------------------------------------
  223. void SoundRenderer::set_sound_rolloff(SoundSourceId id, const float rolloff)
  224. {
  225. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  226. m_impl->m_sources[id.index].set_rolloff(rolloff);
  227. }
  228. //-----------------------------------------------------------------------------
  229. float SoundRenderer::sound_min_distance(SoundSourceId id) const
  230. {
  231. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  232. return m_impl->m_sources[id.index].min_distance();
  233. }
  234. //-----------------------------------------------------------------------------
  235. float SoundRenderer::sound_max_distance(SoundSourceId id) const
  236. {
  237. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  238. return m_impl->m_sources[id.index].max_distance();
  239. }
  240. //-----------------------------------------------------------------------------
  241. Vector3 SoundRenderer::sound_position(SoundSourceId id) const
  242. {
  243. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  244. return m_impl->m_sources[id.index].position();
  245. }
  246. //-----------------------------------------------------------------------------
  247. Vector3 SoundRenderer::sound_velocity(SoundSourceId id) const
  248. {
  249. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  250. return m_impl->m_sources[id.index].velocity();
  251. }
  252. //-----------------------------------------------------------------------------
  253. Vector3 SoundRenderer::sound_direction(SoundSourceId id) const
  254. {
  255. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  256. return m_impl->m_sources[id.index].direction();
  257. }
  258. //-----------------------------------------------------------------------------
  259. float SoundRenderer::sound_pitch(SoundSourceId id) const
  260. {
  261. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  262. return m_impl->m_sources[id.index].pitch();
  263. }
  264. //-----------------------------------------------------------------------------
  265. float SoundRenderer::sound_gain(SoundSourceId id) const
  266. {
  267. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  268. return m_impl->m_sources[id.index].gain();
  269. }
  270. //-----------------------------------------------------------------------------
  271. float SoundRenderer::sound_rolloff(SoundSourceId id) const
  272. {
  273. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  274. return m_impl->m_sources[id.index].rolloff();
  275. }
  276. //-----------------------------------------------------------------------------
  277. int32_t SoundRenderer::sound_queued_buffers(SoundSourceId id) const
  278. {
  279. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  280. return m_impl->m_sources[id.index].queued_buffers();
  281. }
  282. //-----------------------------------------------------------------------------
  283. int32_t SoundRenderer::sound_processed_buffers(SoundSourceId id) const
  284. {
  285. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  286. return m_impl->m_sources[id.index].processed_buffers();
  287. }
  288. //-----------------------------------------------------------------------------
  289. bool SoundRenderer::sound_playing(SoundSourceId id)
  290. {
  291. CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exists");
  292. return m_impl->m_sources[id.index].is_playing();
  293. }
  294. } // namespace crown