Source.cpp 9.8 KB

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