Pool.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. * Copyright (c) 2006-2015 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "Pool.h"
  21. #include "Source.h"
  22. namespace love
  23. {
  24. namespace audio
  25. {
  26. namespace openal
  27. {
  28. Pool::Pool()
  29. : sources()
  30. , totalSources(0)
  31. , mutex(nullptr)
  32. {
  33. // Clear errors.
  34. alGetError();
  35. // Generate sources.
  36. for (int i = 0; i < MAX_SOURCES; i++)
  37. {
  38. alGenSources(1, &sources[i]);
  39. // We might hit an implementation-dependent limit on the total number
  40. // of sources before reaching MAX_SOURCES.
  41. if (alGetError() != AL_NO_ERROR)
  42. break;
  43. totalSources++;
  44. }
  45. if (totalSources < 4)
  46. throw love::Exception("Could not generate sources.");
  47. // Create the mutex.
  48. mutex = thread::newMutex();
  49. #ifdef AL_SOFT_direct_channels
  50. ALboolean hasext = alIsExtensionPresent("AL_SOFT_direct_channels");
  51. #endif
  52. // Make all sources available initially.
  53. for (int i = 0; i < totalSources; i++)
  54. {
  55. #ifdef AL_SOFT_direct_channels
  56. if (hasext)
  57. {
  58. // Bypass virtualization of speakers for multi-channel sources in OpenAL Soft.
  59. alSourcei(sources[i], AL_DIRECT_CHANNELS_SOFT, AL_TRUE);
  60. }
  61. #endif
  62. available.push(sources[i]);
  63. }
  64. }
  65. Pool::~Pool()
  66. {
  67. stop();
  68. delete mutex;
  69. // Free all sources.
  70. alDeleteSources(totalSources, sources);
  71. }
  72. bool Pool::isAvailable() const
  73. {
  74. bool has = false;
  75. {
  76. thread::Lock lock(mutex);
  77. has = !available.empty();
  78. }
  79. return has;
  80. }
  81. bool Pool::isPlaying(Source *s)
  82. {
  83. bool p = false;
  84. {
  85. thread::Lock lock(mutex);
  86. p = (playing.find(s) != playing.end());
  87. }
  88. return p;
  89. }
  90. void Pool::update()
  91. {
  92. thread::Lock lock(mutex);
  93. std::map<Source *, ALuint>::iterator i = playing.begin();
  94. while (i != playing.end())
  95. {
  96. if (!i->first->update())
  97. {
  98. i->first->stopAtomic();
  99. i->first->rewindAtomic();
  100. i->first->release();
  101. available.push(i->second);
  102. playing.erase(i++);
  103. }
  104. else
  105. i++;
  106. }
  107. }
  108. int Pool::getSourceCount() const
  109. {
  110. return (int) playing.size();
  111. }
  112. int Pool::getMaxSources() const
  113. {
  114. return totalSources;
  115. }
  116. bool Pool::play(Source *source, ALuint &out)
  117. {
  118. thread::Lock lock(mutex);
  119. bool ok = true;
  120. out = 0;
  121. bool alreadyPlaying = findSource(source, out);
  122. if (!alreadyPlaying)
  123. {
  124. // Try to play.
  125. if (!available.empty())
  126. {
  127. // Get the first available source.
  128. out = available.front();
  129. // Remove it.
  130. available.pop();
  131. // Insert into map of playing sources.
  132. playing.insert(std::pair<Source *, ALuint>(source, out));
  133. source->retain();
  134. ok = source->playAtomic();
  135. }
  136. else
  137. {
  138. ok = false;
  139. }
  140. }
  141. else
  142. {
  143. ok = true;
  144. }
  145. return ok;
  146. }
  147. void Pool::stop()
  148. {
  149. thread::Lock lock(mutex);
  150. for (const auto &i : playing)
  151. {
  152. i.first->stopAtomic();
  153. i.first->release();
  154. available.push(i.second);
  155. }
  156. playing.clear();
  157. }
  158. void Pool::stop(Source *source)
  159. {
  160. thread::Lock lock(mutex);
  161. removeSource(source);
  162. }
  163. void Pool::pause()
  164. {
  165. thread::Lock lock(mutex);
  166. for (const auto &i : playing)
  167. i.first->pauseAtomic();
  168. }
  169. void Pool::pause(Source *source)
  170. {
  171. thread::Lock lock(mutex);
  172. ALuint out;
  173. if (findSource(source, out))
  174. source->pauseAtomic();
  175. }
  176. void Pool::resume()
  177. {
  178. thread::Lock lock(mutex);
  179. for (const auto &i : playing)
  180. i.first->resumeAtomic();
  181. }
  182. void Pool::resume(Source *source)
  183. {
  184. thread::Lock lock(mutex);
  185. ALuint out;
  186. if (findSource(source, out))
  187. source->resumeAtomic();
  188. }
  189. void Pool::rewind()
  190. {
  191. thread::Lock lock(mutex);
  192. for (const auto &i : playing)
  193. i.first->rewindAtomic();
  194. }
  195. // For those times we don't need it backed.
  196. void Pool::softRewind(Source *source)
  197. {
  198. thread::Lock lock(mutex);
  199. source->rewindAtomic();
  200. }
  201. void Pool::rewind(Source *source)
  202. {
  203. thread::Lock lock(mutex);
  204. source->rewindAtomic();
  205. }
  206. void Pool::release(Source *source)
  207. {
  208. ALuint s = findi(source);
  209. if (s != 0)
  210. {
  211. available.push(s);
  212. playing.erase(source);
  213. }
  214. }
  215. void Pool::seek(Source *source, float offset, void *unit)
  216. {
  217. thread::Lock lock(mutex);
  218. return source->seekAtomic(offset, unit);
  219. }
  220. float Pool::tell(Source *source, void *unit)
  221. {
  222. thread::Lock lock(mutex);
  223. return source->tellAtomic(unit);
  224. }
  225. ALuint Pool::findi(const Source *source) const
  226. {
  227. std::map<Source *, ALuint>::const_iterator i = playing.find((Source *)source);
  228. if (i != playing.end())
  229. return i->second;
  230. return 0;
  231. }
  232. bool Pool::findSource(Source *source, ALuint &out)
  233. {
  234. std::map<Source *, ALuint>::const_iterator i = playing.find((Source *)source);
  235. bool found = i != playing.end();
  236. if (found)
  237. out = i->second;
  238. return found;
  239. }
  240. bool Pool::removeSource(Source *source)
  241. {
  242. std::map<Source *, ALuint>::iterator i = playing.find((Source *)source);
  243. if (i != playing.end())
  244. {
  245. source->stopAtomic();
  246. available.push(i->second);
  247. playing.erase(i++);
  248. source->release();
  249. return true;
  250. }
  251. return false;
  252. }
  253. } // openal
  254. } // audio
  255. } // love