Source.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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), offsetSamples(0), offsetSeconds(0), 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), offsetSamples(0), offsetSeconds(0), 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. float curOffsetSamples, curOffsetSecs;
  125. alGetSourcef(source, AL_SAMPLE_OFFSET, &curOffsetSamples);
  126. ALint b;
  127. alGetSourcei(source, AL_BUFFER, &b);
  128. int freq;
  129. alGetBufferi(b, AL_FREQUENCY, &freq);
  130. curOffsetSecs = curOffsetSamples / freq;
  131. // Get a free buffer.
  132. alSourceUnqueueBuffers(source, 1, &buffer);
  133. float newOffsetSamples, newOffsetSecs;
  134. alGetSourcef(source, AL_SAMPLE_OFFSET, &newOffsetSamples);
  135. newOffsetSecs = newOffsetSamples / freq;
  136. offsetSamples += (curOffsetSamples - newOffsetSamples);
  137. offsetSeconds += (curOffsetSecs - newOffsetSecs);
  138. if(streamAtomic(buffer, decoder) > 0)
  139. alSourceQueueBuffers(source, 1, &buffer);
  140. }
  141. }
  142. }
  143. void Source::setPitch(float pitch)
  144. {
  145. if(valid)
  146. alSourcef(source, AL_PITCH, pitch);
  147. this->pitch = pitch;
  148. }
  149. float Source::getPitch() const
  150. {
  151. if(valid)
  152. {
  153. ALfloat f;
  154. alGetSourcef(source, AL_PITCH, &f);
  155. return f;
  156. }
  157. // In case the Source isn't playing.
  158. return pitch;
  159. }
  160. void Source::setVolume(float volume)
  161. {
  162. if(valid)
  163. {
  164. alSourcef(source, AL_GAIN, volume);
  165. }
  166. this->volume = volume;
  167. }
  168. float Source::getVolume() const
  169. {
  170. if(valid)
  171. {
  172. ALfloat f;
  173. alGetSourcef(source, AL_GAIN, &f);
  174. return f;
  175. }
  176. // In case the Source isn't playing.
  177. return volume;
  178. }
  179. void Source::seek(float offset, Source::Unit unit)
  180. {
  181. if (valid)
  182. {
  183. switch (unit) {
  184. case Source::UNIT_SAMPLES:
  185. if (type == TYPE_STREAM) {
  186. ALint buffer;
  187. alGetSourcei(source, AL_BUFFER, &buffer);
  188. int freq;
  189. alGetBufferi(buffer, AL_FREQUENCY, &freq);
  190. offset /= freq;
  191. decoder->seek(offset);
  192. } else {
  193. alSourcef(source, AL_SAMPLE_OFFSET, offset);
  194. }
  195. break;
  196. case Source::UNIT_SECONDS:
  197. default:
  198. if (type == TYPE_STREAM) {
  199. decoder->seek(offset);
  200. } else {
  201. alSourcef(source, AL_SEC_OFFSET, offset);
  202. }
  203. break;
  204. }
  205. }
  206. }
  207. float Source::tell(Source::Unit unit) const
  208. {
  209. if (valid)
  210. {
  211. float offset;
  212. switch (unit) {
  213. case Source::UNIT_SAMPLES:
  214. alGetSourcef(source, AL_SAMPLE_OFFSET, &offset);
  215. if (type == TYPE_STREAM) offset += offsetSamples;
  216. break;
  217. case Source::UNIT_SECONDS:
  218. default:
  219. alGetSourcef(source, AL_SAMPLE_OFFSET, &offset);
  220. ALint buffer;
  221. alGetSourcei(source, AL_BUFFER, &buffer);
  222. int freq;
  223. alGetBufferi(buffer, AL_FREQUENCY, &freq);
  224. offset /= freq;
  225. if (type == TYPE_STREAM) offset += offsetSeconds;
  226. break;
  227. }
  228. return offset;
  229. }
  230. return 0.0f;
  231. }
  232. void Source::setPosition(float * v)
  233. {
  234. if(valid)
  235. alSourcefv(source, AL_POSITION, v);
  236. setFloatv(position, v);
  237. }
  238. void Source::getPosition(float * v) const
  239. {
  240. if(valid)
  241. alGetSourcefv(source, AL_POSITION, v);
  242. else
  243. setFloatv(v, position);
  244. }
  245. void Source::setVelocity(float * v)
  246. {
  247. if(valid)
  248. alSourcefv(source, AL_VELOCITY, v);
  249. setFloatv(velocity, v);
  250. }
  251. void Source::getVelocity(float * v) const
  252. {
  253. if(valid)
  254. alGetSourcefv(source, AL_VELOCITY, v);
  255. else
  256. setFloatv(v, velocity);
  257. }
  258. void Source::setDirection(float * v)
  259. {
  260. if(valid)
  261. alSourcefv(source, AL_DIRECTION, v);
  262. else
  263. setFloatv(direction, v);
  264. }
  265. void Source::getDirection(float * v) const
  266. {
  267. if(valid)
  268. alGetSourcefv(source, AL_DIRECTION, v);
  269. else
  270. setFloatv(v, direction);
  271. }
  272. void Source::setLooping(bool looping)
  273. {
  274. if(valid && type == TYPE_STATIC)
  275. alSourcei(source, AL_LOOPING, looping ? AL_TRUE : AL_FALSE);
  276. this->looping = looping;
  277. }
  278. bool Source::isLooping() const
  279. {
  280. return looping;
  281. }
  282. void Source::playAtomic()
  283. {
  284. if(type == TYPE_STATIC)
  285. {
  286. alSourcei(source, AL_BUFFER, buffers[0]);
  287. }
  288. else if(type == TYPE_STREAM)
  289. {
  290. int usedBuffers = 0;
  291. for(unsigned int i = 0; i < MAX_BUFFERS; i++)
  292. {
  293. int decoded = streamAtomic(buffers[i], decoder);
  294. ++usedBuffers;
  295. if(decoded < decoder->getSize())
  296. break;
  297. }
  298. if(usedBuffers > 0)
  299. alSourceQueueBuffers(source, usedBuffers, buffers);
  300. }
  301. // Set these properties. These may have changed while we've
  302. // been without an AL source.
  303. alSourcef(source, AL_PITCH, pitch);
  304. alSourcef(source, AL_GAIN, volume);
  305. alSourcePlay(source);
  306. valid = true; //if it fails it will be set to false again
  307. //but this prevents a horrible, horrible bug
  308. }
  309. void Source::stopAtomic()
  310. {
  311. if(valid)
  312. {
  313. if(type == TYPE_STATIC)
  314. {
  315. alSourceStop(source);
  316. }
  317. else if(type == TYPE_STREAM)
  318. {
  319. alSourceStop(source);
  320. int queued = 0;
  321. alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
  322. while(queued--)
  323. {
  324. ALuint buffer;
  325. alSourceUnqueueBuffers(source, 1, &buffer);
  326. }
  327. }
  328. alSourcei(source, AL_BUFFER, AL_NONE);
  329. }
  330. rewindAtomic();
  331. valid = false;
  332. }
  333. void Source::pauseAtomic()
  334. {
  335. if(valid)
  336. {
  337. alSourcePause(source);
  338. }
  339. }
  340. void Source::resumeAtomic()
  341. {
  342. if(valid)
  343. {
  344. alSourcePlay(source);
  345. }
  346. }
  347. void Source::rewindAtomic()
  348. {
  349. if(valid && type == TYPE_STATIC)
  350. {
  351. alSourceRewind(source);
  352. }
  353. else if(valid && type == TYPE_STREAM)
  354. {
  355. decoder->rewind();
  356. offsetSamples = 0;
  357. offsetSeconds = 0;
  358. }
  359. }
  360. void Source::reset(ALenum source)
  361. {
  362. alSourcefv(source, AL_POSITION, position);
  363. alSourcefv(source, AL_VELOCITY, velocity);
  364. alSourcefv(source, AL_DIRECTION, direction);
  365. alSourcef(source, AL_PITCH, pitch);
  366. alSourcef(source, AL_GAIN, volume);
  367. }
  368. void Source::setFloatv(float * dst, const float * src) const
  369. {
  370. dst[0] = src[0];
  371. dst[1] = src[1];
  372. dst[2] = src[2];
  373. }
  374. ALenum Source::getFormat(int channels, int bits) const
  375. {
  376. if(channels == 1 && bits == 8)
  377. return AL_FORMAT_MONO8;
  378. else if(channels == 1 && bits == 16)
  379. return AL_FORMAT_MONO16;
  380. else if(channels == 2 && bits == 8)
  381. return AL_FORMAT_STEREO8;
  382. else if(channels == 2 && bits == 16)
  383. return AL_FORMAT_STEREO16;
  384. else
  385. return 0;
  386. }
  387. int Source::streamAtomic(ALuint buffer, love::sound::Decoder * d)
  388. {
  389. // Get more sound data.
  390. int decoded = d->decode();
  391. int fmt = getFormat(d->getChannels(), d->getBits());
  392. if(decoded > 0 && fmt != 0)
  393. alBufferData(buffer, fmt, d->getBuffer(), decoded, d->getSampleRate());
  394. if(decoded < d->getSize() && isLooping()) {
  395. offsetSamples = 0;
  396. offsetSeconds = 0;
  397. d->rewind();
  398. }
  399. return decoded;
  400. }
  401. bool Source::isStatic() const
  402. {
  403. return (type == TYPE_STATIC);
  404. }
  405. } // openal
  406. } // audio
  407. } // love