Source.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /**
  2. * Copyright (c) 2006-2011 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 "Source.h"
  21. #include "Pool.h"
  22. // STD
  23. #include <iostream>
  24. namespace love
  25. {
  26. namespace audio
  27. {
  28. namespace openal
  29. {
  30. Source::Source(Pool * pool, love::sound::SoundData * soundData)
  31. : love::audio::Source(Source::TYPE_STATIC), pool(pool), valid(false),
  32. pitch(1.0f), volume(1.0f), looping(false), decoder(0)
  33. {
  34. alGenBuffers(1, buffers);
  35. ALenum fmt = getFormat(soundData->getChannels(), soundData->getBits());
  36. alBufferData(buffers[0], fmt, soundData->getData(), soundData->getSize(), soundData->getSampleRate());
  37. static float z[3] = {0, 0, 0};
  38. setFloatv(position, z);
  39. setFloatv(velocity, z);
  40. setFloatv(direction, z);
  41. }
  42. Source::Source(Pool * pool, love::sound::Decoder * decoder)
  43. : love::audio::Source(Source::TYPE_STREAM), pool(pool), valid(false),
  44. pitch(1.0f), volume(1.0f), looping(false), decoder(decoder)
  45. {
  46. decoder->retain();
  47. alGenBuffers(MAX_BUFFERS, buffers);
  48. static float z[3] = {0, 0, 0};
  49. setFloatv(position, z);
  50. setFloatv(velocity, z);
  51. setFloatv(direction, z);
  52. }
  53. Source::~Source()
  54. {
  55. stop();
  56. alDeleteBuffers((type == TYPE_STATIC) ? 1 : MAX_BUFFERS, buffers);
  57. }
  58. love::audio::Source * Source::copy()
  59. {
  60. return 0;
  61. }
  62. void Source::play()
  63. {
  64. valid = pool->play(this, source);
  65. if(valid)
  66. reset(source);
  67. }
  68. void Source::stop()
  69. {
  70. if (!isStopped())
  71. pool->stop(this);
  72. }
  73. void Source::pause()
  74. {
  75. pool->pause(this);
  76. }
  77. void Source::resume()
  78. {
  79. pool->resume(this);
  80. }
  81. void Source::rewind()
  82. {
  83. pool->rewind(this);
  84. }
  85. bool Source::isStopped() const
  86. {
  87. if(valid)
  88. {
  89. ALenum state;
  90. alGetSourcei(source, AL_SOURCE_STATE, &state);
  91. return (state == AL_STOPPED);
  92. }
  93. return true;
  94. }
  95. bool Source::isPaused() const
  96. {
  97. if(valid)
  98. {
  99. ALenum state;
  100. alGetSourcei(source, AL_SOURCE_STATE, &state);
  101. return (state == AL_PAUSED);
  102. }
  103. return false;
  104. }
  105. bool Source::isFinished() const
  106. {
  107. return type == TYPE_STATIC ? isStopped() : isStopped() && !isLooping() && decoder->isFinished();
  108. }
  109. void Source::update()
  110. {
  111. if(valid && type == TYPE_STATIC)
  112. {
  113. // Looping mode could have changed.
  114. alSourcei(source, AL_LOOPING, isLooping() ? AL_TRUE : AL_FALSE);
  115. }
  116. else if(valid && type == TYPE_STREAM)
  117. {
  118. // Number of processed buffers.
  119. ALint processed;
  120. alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
  121. while(processed--)
  122. {
  123. ALuint buffer;
  124. // Get a free buffer.
  125. alSourceUnqueueBuffers(source, 1, &buffer);
  126. if(streamAtomic(buffer, decoder) > 0)
  127. alSourceQueueBuffers(source, 1, &buffer);
  128. }
  129. }
  130. }
  131. void Source::setPitch(float pitch)
  132. {
  133. if(valid)
  134. alSourcef(source, AL_PITCH, pitch);
  135. this->pitch = pitch;
  136. }
  137. float Source::getPitch() const
  138. {
  139. if(valid)
  140. {
  141. ALfloat f;
  142. alGetSourcef(source, AL_PITCH, &f);
  143. return f;
  144. }
  145. // In case the Source isn't playing.
  146. return pitch;
  147. }
  148. void Source::setVolume(float volume)
  149. {
  150. if(valid)
  151. {
  152. alSourcef(source, AL_GAIN, volume);
  153. }
  154. this->volume = volume;
  155. }
  156. float Source::getVolume() const
  157. {
  158. if(valid)
  159. {
  160. ALfloat f;
  161. alGetSourcef(source, AL_GAIN, &f);
  162. return f;
  163. }
  164. // In case the Source isn't playing.
  165. return volume;
  166. }
  167. void Source::setPosition(float * v)
  168. {
  169. if(valid)
  170. alSourcefv(source, AL_POSITION, v);
  171. setFloatv(position, v);
  172. }
  173. void Source::getPosition(float * v) const
  174. {
  175. if(valid)
  176. alGetSourcefv(source, AL_POSITION, v);
  177. else
  178. setFloatv(v, position);
  179. }
  180. void Source::setVelocity(float * v)
  181. {
  182. if(valid)
  183. alSourcefv(source, AL_VELOCITY, v);
  184. setFloatv(velocity, v);
  185. }
  186. void Source::getVelocity(float * v) const
  187. {
  188. if(valid)
  189. alGetSourcefv(source, AL_VELOCITY, v);
  190. else
  191. setFloatv(v, velocity);
  192. }
  193. void Source::setDirection(float * v)
  194. {
  195. if(valid)
  196. alSourcefv(source, AL_DIRECTION, v);
  197. else
  198. setFloatv(direction, v);
  199. }
  200. void Source::getDirection(float * v) const
  201. {
  202. if(valid)
  203. alGetSourcefv(source, AL_DIRECTION, v);
  204. else
  205. setFloatv(v, direction);
  206. }
  207. void Source::setLooping(bool looping)
  208. {
  209. if(valid && type == TYPE_STATIC)
  210. alSourcei(source, AL_LOOPING, looping ? AL_TRUE : AL_FALSE);
  211. this->looping = looping;
  212. }
  213. bool Source::isLooping() const
  214. {
  215. return looping;
  216. }
  217. void Source::playAtomic()
  218. {
  219. if(type == TYPE_STATIC)
  220. {
  221. alSourcei(source, AL_BUFFER, buffers[0]);
  222. }
  223. else if(type == TYPE_STREAM)
  224. {
  225. int usedBuffers = 0;
  226. for(unsigned int i = 0; i < MAX_BUFFERS; i++)
  227. {
  228. int decoded = streamAtomic(buffers[i], decoder);
  229. ++usedBuffers;
  230. if(decoded < decoder->getSize())
  231. break;
  232. }
  233. if(usedBuffers > 0)
  234. alSourceQueueBuffers(source, usedBuffers, buffers);
  235. }
  236. // Set these properties. These may have changed while we've
  237. // been without an AL source.
  238. alSourcef(source, AL_PITCH, pitch);
  239. alSourcef(source, AL_GAIN, volume);
  240. alSourcePlay(source);
  241. valid = true; //if it fails it will be set to false again
  242. //but this prevents a horrible, horrible bug
  243. }
  244. void Source::stopAtomic()
  245. {
  246. if(valid)
  247. {
  248. if(type == TYPE_STATIC)
  249. {
  250. alSourceStop(source);
  251. }
  252. else if(type == TYPE_STREAM)
  253. {
  254. alSourceStop(source);
  255. int queued = 0;
  256. alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
  257. while(queued--)
  258. {
  259. ALuint buffer;
  260. alSourceUnqueueBuffers(source, 1, &buffer);
  261. }
  262. }
  263. alSourcei(source, AL_BUFFER, AL_NONE);
  264. }
  265. rewindAtomic();
  266. valid = false;
  267. }
  268. void Source::pauseAtomic()
  269. {
  270. if(valid)
  271. {
  272. alSourcePause(source);
  273. }
  274. }
  275. void Source::resumeAtomic()
  276. {
  277. if(valid)
  278. {
  279. alSourcePlay(source);
  280. }
  281. }
  282. void Source::rewindAtomic()
  283. {
  284. if(valid && type == TYPE_STATIC)
  285. {
  286. alSourceRewind(source);
  287. }
  288. else if(valid && type == TYPE_STREAM)
  289. {
  290. decoder->rewind();
  291. }
  292. }
  293. void Source::reset(ALenum source)
  294. {
  295. alSourcefv(source, AL_POSITION, position);
  296. alSourcefv(source, AL_VELOCITY, velocity);
  297. alSourcefv(source, AL_DIRECTION, direction);
  298. alSourcef(source, AL_PITCH, pitch);
  299. alSourcef(source, AL_GAIN, volume);
  300. }
  301. void Source::setFloatv(float * dst, const float * src) const
  302. {
  303. dst[0] = src[0];
  304. dst[1] = src[1];
  305. dst[2] = src[2];
  306. }
  307. ALenum Source::getFormat(int channels, int bits) const
  308. {
  309. if(channels == 1 && bits == 8)
  310. return AL_FORMAT_MONO8;
  311. else if(channels == 1 && bits == 16)
  312. return AL_FORMAT_MONO16;
  313. else if(channels == 2 && bits == 8)
  314. return AL_FORMAT_STEREO8;
  315. else if(channels == 2 && bits == 16)
  316. return AL_FORMAT_STEREO16;
  317. else
  318. return 0;
  319. }
  320. int Source::streamAtomic(ALuint buffer, love::sound::Decoder * d)
  321. {
  322. // Get more sound data.
  323. int decoded = d->decode();
  324. int fmt = getFormat(d->getChannels(), d->getBits());
  325. if(decoded > 0 && fmt != 0)
  326. alBufferData(buffer, fmt, d->getBuffer(), decoded, d->getSampleRate());
  327. if(decoded < d->getSize() && isLooping())
  328. d->rewind();
  329. return decoded;
  330. }
  331. bool Source::isStatic() const
  332. {
  333. return (type == TYPE_STATIC);
  334. }
  335. } // openal
  336. } // audio
  337. } // love