Source.cpp 7.3 KB

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