Source.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /**
  2. * Copyright (c) 2006-2012 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. #include <float.h>
  25. namespace love
  26. {
  27. namespace audio
  28. {
  29. namespace openal
  30. {
  31. Source::Source(Pool * pool, love::sound::SoundData * soundData)
  32. : love::audio::Source(Source::TYPE_STATIC), pool(pool), valid(false),
  33. pitch(1.0f), volume(1.0f), looping(false), paused(false), minVolume(0.0f),
  34. maxVolume(1.0f), referenceDistance(1.0f), rolloffFactor(1.0f), maxDistance(FLT_MAX),
  35. offsetSamples(0), offsetSeconds(0), decoder(0), toLoop(0)
  36. {
  37. alGenBuffers(1, buffers);
  38. ALenum fmt = getFormat(soundData->getChannels(), soundData->getBits());
  39. alBufferData(buffers[0], fmt, soundData->getData(), soundData->getSize(), soundData->getSampleRate());
  40. static float z[3] = {0, 0, 0};
  41. setFloatv(position, z);
  42. setFloatv(velocity, z);
  43. setFloatv(direction, z);
  44. }
  45. Source::Source(Pool * pool, love::sound::Decoder * decoder)
  46. : love::audio::Source(Source::TYPE_STREAM), pool(pool), valid(false),
  47. pitch(1.0f), volume(1.0f), looping(false), paused(false), minVolume(0.0f),
  48. maxVolume(1.0f), referenceDistance(1.0f), rolloffFactor(1.0f), maxDistance(FLT_MAX),
  49. offsetSamples(0), offsetSeconds(0), decoder(decoder), toLoop(0)
  50. {
  51. decoder->retain();
  52. alGenBuffers(MAX_BUFFERS, buffers);
  53. static float z[3] = {0, 0, 0};
  54. setFloatv(position, z);
  55. setFloatv(velocity, z);
  56. setFloatv(direction, z);
  57. }
  58. Source::~Source()
  59. {
  60. if (valid)
  61. pool->stop(this);
  62. alDeleteBuffers((type == TYPE_STATIC) ? 1 : MAX_BUFFERS, buffers);
  63. if (decoder)
  64. decoder->release();
  65. }
  66. love::audio::Source * Source::copy()
  67. {
  68. return 0;
  69. }
  70. void Source::play()
  71. {
  72. if (valid && paused)
  73. {
  74. pool->resume(this);
  75. return;
  76. }
  77. valid = pool->play(this, source);
  78. if (valid)
  79. reset(source);
  80. }
  81. void Source::stop()
  82. {
  83. if (!isStopped())
  84. {
  85. pool->stop(this);
  86. pool->softRewind(this);
  87. }
  88. }
  89. void Source::pause()
  90. {
  91. pool->pause(this);
  92. }
  93. void Source::resume()
  94. {
  95. pool->resume(this);
  96. }
  97. void Source::rewind()
  98. {
  99. pool->rewind(this);
  100. }
  101. bool Source::isStopped() const
  102. {
  103. if (valid)
  104. {
  105. ALenum state;
  106. alGetSourcei(source, AL_SOURCE_STATE, &state);
  107. return (state == AL_STOPPED);
  108. }
  109. return true;
  110. }
  111. bool Source::isPaused() const
  112. {
  113. if (valid)
  114. {
  115. ALenum state;
  116. alGetSourcei(source, AL_SOURCE_STATE, &state);
  117. return (state == AL_PAUSED);
  118. }
  119. return false;
  120. }
  121. bool Source::isFinished() const
  122. {
  123. return type == TYPE_STATIC ? isStopped() : isStopped() && !isLooping() && decoder->isFinished();
  124. }
  125. bool Source::update()
  126. {
  127. if (!valid)
  128. return false;
  129. if (type == TYPE_STATIC)
  130. {
  131. // Looping mode could have changed.
  132. alSourcei(source, AL_LOOPING, isLooping() ? AL_TRUE : AL_FALSE);
  133. return !isStopped();
  134. }
  135. else if (type == TYPE_STREAM && (isLooping() || !isFinished()))
  136. {
  137. // Number of processed buffers.
  138. ALint processed = 0;
  139. alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
  140. while (processed--)
  141. {
  142. ALuint buffer;
  143. float curOffsetSamples, curOffsetSecs;
  144. alGetSourcef(source, AL_SAMPLE_OFFSET, &curOffsetSamples);
  145. ALint b;
  146. alGetSourcei(source, AL_BUFFER, &b);
  147. int freq;
  148. alGetBufferi(b, AL_FREQUENCY, &freq);
  149. curOffsetSecs = curOffsetSamples / freq;
  150. // Get a free buffer.
  151. alSourceUnqueueBuffers(source, 1, &buffer);
  152. float newOffsetSamples, newOffsetSecs;
  153. alGetSourcef(source, AL_SAMPLE_OFFSET, &newOffsetSamples);
  154. newOffsetSecs = newOffsetSamples / freq;
  155. offsetSamples += (curOffsetSamples - newOffsetSamples);
  156. offsetSeconds += (curOffsetSecs - newOffsetSecs);
  157. streamAtomic(buffer, decoder);
  158. alSourceQueueBuffers(source, 1, &buffer);
  159. }
  160. return true;
  161. }
  162. return false;
  163. }
  164. void Source::setPitch(float pitch)
  165. {
  166. if (valid)
  167. alSourcef(source, AL_PITCH, pitch);
  168. this->pitch = pitch;
  169. }
  170. float Source::getPitch() const
  171. {
  172. if (valid)
  173. {
  174. ALfloat f;
  175. alGetSourcef(source, AL_PITCH, &f);
  176. return f;
  177. }
  178. // In case the Source isn't playing.
  179. return pitch;
  180. }
  181. void Source::setVolume(float volume)
  182. {
  183. if (valid)
  184. {
  185. alSourcef(source, AL_GAIN, volume);
  186. }
  187. this->volume = volume;
  188. }
  189. float Source::getVolume() const
  190. {
  191. if (valid)
  192. {
  193. ALfloat f;
  194. alGetSourcef(source, AL_GAIN, &f);
  195. return f;
  196. }
  197. // In case the Source isn't playing.
  198. return volume;
  199. }
  200. void Source::seekAtomic(float offset, void * unit)
  201. {
  202. if (valid)
  203. {
  204. switch (*((Source::Unit*) unit)) {
  205. case Source::UNIT_SAMPLES:
  206. if (type == TYPE_STREAM)
  207. {
  208. offsetSamples = offset;
  209. ALint buffer;
  210. alGetSourcei(source, AL_BUFFER, &buffer);
  211. int freq;
  212. alGetBufferi(buffer, AL_FREQUENCY, &freq);
  213. offset /= freq;
  214. offsetSeconds = offset;
  215. decoder->seek(offset);
  216. }
  217. else
  218. {
  219. alSourcef(source, AL_SAMPLE_OFFSET, offset);
  220. }
  221. break;
  222. case Source::UNIT_SECONDS:
  223. default:
  224. if (type == TYPE_STREAM)
  225. {
  226. offsetSeconds = offset;
  227. decoder->seek(offset);
  228. ALint buffer;
  229. alGetSourcei(source, AL_BUFFER, &buffer);
  230. int freq;
  231. alGetBufferi(buffer, AL_FREQUENCY, &freq);
  232. offsetSamples = offset*freq;
  233. }
  234. else
  235. {
  236. alSourcef(source, AL_SEC_OFFSET, offset);
  237. }
  238. break;
  239. }
  240. if (type == TYPE_STREAM)
  241. {
  242. bool waspaused = paused;
  243. // Because we still have old data
  244. // from before the seek in the buffers
  245. // let's empty them.
  246. stopAtomic();
  247. playAtomic();
  248. if (waspaused)
  249. pauseAtomic();
  250. }
  251. }
  252. }
  253. void Source::seek(float offset, Source::Unit unit)
  254. {
  255. return pool->seek(this, offset, &unit);
  256. }
  257. float Source::tellAtomic(void * unit) const
  258. {
  259. if (valid)
  260. {
  261. float offset;
  262. switch (*((Source::Unit*) unit)) {
  263. case Source::UNIT_SAMPLES:
  264. alGetSourcef(source, AL_SAMPLE_OFFSET, &offset);
  265. if (type == TYPE_STREAM) offset += offsetSamples;
  266. break;
  267. case Source::UNIT_SECONDS:
  268. default:
  269. alGetSourcef(source, AL_SAMPLE_OFFSET, &offset);
  270. ALint buffer;
  271. alGetSourcei(source, AL_BUFFER, &buffer);
  272. int freq;
  273. alGetBufferi(buffer, AL_FREQUENCY, &freq);
  274. offset /= freq;
  275. if (type == TYPE_STREAM) offset += offsetSeconds;
  276. break;
  277. }
  278. return offset;
  279. }
  280. return 0.0f;
  281. }
  282. float Source::tell(Source::Unit unit)
  283. {
  284. return pool->tell(this, &unit);
  285. }
  286. void Source::setPosition(float * v)
  287. {
  288. if (valid)
  289. alSourcefv(source, AL_POSITION, v);
  290. setFloatv(position, v);
  291. }
  292. void Source::getPosition(float * v) const
  293. {
  294. if (valid)
  295. alGetSourcefv(source, AL_POSITION, v);
  296. else
  297. setFloatv(v, position);
  298. }
  299. void Source::setVelocity(float * v)
  300. {
  301. if (valid)
  302. alSourcefv(source, AL_VELOCITY, v);
  303. setFloatv(velocity, v);
  304. }
  305. void Source::getVelocity(float * v) const
  306. {
  307. if (valid)
  308. alGetSourcefv(source, AL_VELOCITY, v);
  309. else
  310. setFloatv(v, velocity);
  311. }
  312. void Source::setDirection(float * v)
  313. {
  314. if (valid)
  315. alSourcefv(source, AL_DIRECTION, v);
  316. else
  317. setFloatv(direction, v);
  318. }
  319. void Source::getDirection(float * v) const
  320. {
  321. if (valid)
  322. alGetSourcefv(source, AL_DIRECTION, v);
  323. else
  324. setFloatv(v, direction);
  325. }
  326. void Source::setLooping(bool looping)
  327. {
  328. if (valid && type == TYPE_STATIC)
  329. alSourcei(source, AL_LOOPING, looping ? AL_TRUE : AL_FALSE);
  330. this->looping = looping;
  331. }
  332. bool Source::isLooping() const
  333. {
  334. return looping;
  335. }
  336. void Source::playAtomic()
  337. {
  338. if (type == TYPE_STATIC)
  339. {
  340. alSourcei(source, AL_BUFFER, buffers[0]);
  341. }
  342. else if (type == TYPE_STREAM)
  343. {
  344. int usedBuffers = 0;
  345. for (unsigned int i = 0; i < MAX_BUFFERS; i++)
  346. {
  347. streamAtomic(buffers[i], decoder);
  348. ++usedBuffers;
  349. if (decoder->isFinished())
  350. break;
  351. }
  352. if (usedBuffers > 0)
  353. alSourceQueueBuffers(source, usedBuffers, buffers);
  354. }
  355. // Set these properties. These may have changed while we've
  356. // been without an AL source.
  357. alSourcef(source, AL_PITCH, pitch);
  358. alSourcef(source, AL_GAIN, volume);
  359. alSourcef(source, AL_MIN_GAIN, minVolume);
  360. alSourcef(source, AL_MAX_GAIN, maxVolume);
  361. alSourcef(source, AL_REFERENCE_DISTANCE, referenceDistance);
  362. alSourcef(source, AL_ROLLOFF_FACTOR, rolloffFactor);
  363. alSourcef(source, AL_MAX_DISTANCE, maxDistance);
  364. alSourcePlay(source);
  365. valid = true; //if it fails it will be set to false again
  366. //but this prevents a horrible, horrible bug
  367. }
  368. void Source::stopAtomic()
  369. {
  370. if (valid)
  371. {
  372. if (type == TYPE_STATIC)
  373. {
  374. alSourceStop(source);
  375. }
  376. else if (type == TYPE_STREAM)
  377. {
  378. alSourceStop(source);
  379. int queued = 0;
  380. alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
  381. while (queued--)
  382. {
  383. ALuint buffer;
  384. alSourceUnqueueBuffers(source, 1, &buffer);
  385. }
  386. }
  387. alSourcei(source, AL_BUFFER, AL_NONE);
  388. }
  389. toLoop = 0;
  390. valid = false;
  391. }
  392. void Source::pauseAtomic()
  393. {
  394. if (valid)
  395. {
  396. alSourcePause(source);
  397. paused = true;
  398. }
  399. }
  400. void Source::resumeAtomic()
  401. {
  402. if (valid && paused)
  403. {
  404. alSourcePlay(source);
  405. paused = false;
  406. }
  407. }
  408. void Source::rewindAtomic()
  409. {
  410. if (valid && type == TYPE_STATIC)
  411. {
  412. alSourceRewind(source);
  413. if (!paused)
  414. alSourcePlay(source);
  415. }
  416. else if (valid && type == TYPE_STREAM)
  417. {
  418. bool waspaused = paused;
  419. decoder->rewind();
  420. // Because we still have old data
  421. // from before the seek in the buffers
  422. // let's empty them.
  423. stopAtomic();
  424. playAtomic();
  425. if (waspaused)
  426. pauseAtomic();
  427. offsetSamples = 0;
  428. offsetSeconds = 0;
  429. }
  430. else if (type == TYPE_STREAM)
  431. {
  432. decoder->rewind();
  433. offsetSamples = 0;
  434. offsetSeconds = 0;
  435. }
  436. }
  437. void Source::reset(ALenum source)
  438. {
  439. alSourcefv(source, AL_POSITION, position);
  440. alSourcefv(source, AL_VELOCITY, velocity);
  441. alSourcefv(source, AL_DIRECTION, direction);
  442. alSourcef(source, AL_PITCH, pitch);
  443. alSourcef(source, AL_GAIN, volume);
  444. alSourcef(source, AL_MIN_GAIN, minVolume);
  445. alSourcef(source, AL_MAX_GAIN, maxVolume);
  446. alSourcef(source, AL_REFERENCE_DISTANCE, referenceDistance);
  447. alSourcef(source, AL_ROLLOFF_FACTOR, rolloffFactor);
  448. alSourcef(source, AL_MAX_DISTANCE, maxDistance);
  449. }
  450. void Source::setFloatv(float * dst, const float * src) const
  451. {
  452. dst[0] = src[0];
  453. dst[1] = src[1];
  454. dst[2] = src[2];
  455. }
  456. ALenum Source::getFormat(int channels, int bits) const
  457. {
  458. if (channels == 1 && bits == 8)
  459. return AL_FORMAT_MONO8;
  460. else if (channels == 1 && bits == 16)
  461. return AL_FORMAT_MONO16;
  462. else if (channels == 2 && bits == 8)
  463. return AL_FORMAT_STEREO8;
  464. else if (channels == 2 && bits == 16)
  465. return AL_FORMAT_STEREO16;
  466. else
  467. return 0;
  468. }
  469. int Source::streamAtomic(ALuint buffer, love::sound::Decoder * d)
  470. {
  471. // Get more sound data.
  472. int decoded = d->decode();
  473. int fmt = getFormat(d->getChannels(), d->getBits());
  474. if (fmt != 0)
  475. alBufferData(buffer, fmt, d->getBuffer(), decoded, d->getSampleRate());
  476. if (decoder->isFinished() && isLooping())
  477. {
  478. int queued, processed;
  479. alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
  480. alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
  481. if (queued > processed)
  482. toLoop = queued-processed;
  483. else
  484. toLoop = MAX_BUFFERS-processed;
  485. d->rewind();
  486. }
  487. if (toLoop > 0)
  488. {
  489. if (--toLoop == 0)
  490. {
  491. offsetSamples = 0;
  492. offsetSeconds = 0;
  493. }
  494. }
  495. return decoded;
  496. }
  497. bool Source::isStatic() const
  498. {
  499. return (type == TYPE_STATIC);
  500. }
  501. void Source::setMinVolume(float volume)
  502. {
  503. if (valid)
  504. {
  505. alSourcef(source, AL_MIN_GAIN, volume);
  506. }
  507. this->minVolume = volume;
  508. }
  509. float Source::getMinVolume() const
  510. {
  511. if (valid)
  512. {
  513. ALfloat f;
  514. alGetSourcef(source, AL_MIN_GAIN, &f);
  515. return f;
  516. }
  517. // In case the Source isn't playing.
  518. return this->minVolume;
  519. }
  520. void Source::setMaxVolume(float volume)
  521. {
  522. if (valid)
  523. {
  524. alSourcef(source, AL_MAX_GAIN, volume);
  525. }
  526. this->maxVolume = volume;
  527. }
  528. float Source::getMaxVolume() const
  529. {
  530. if (valid)
  531. {
  532. ALfloat f;
  533. alGetSourcef(source, AL_MAX_GAIN, &f);
  534. return f;
  535. }
  536. // In case the Source isn't playing.
  537. return this->maxVolume;
  538. }
  539. void Source::setReferenceDistance(float distance)
  540. {
  541. if (valid)
  542. {
  543. alSourcef(source, AL_REFERENCE_DISTANCE, distance);
  544. }
  545. this->referenceDistance = distance;
  546. }
  547. float Source::getReferenceDistance() const
  548. {
  549. if (valid)
  550. {
  551. ALfloat f;
  552. alGetSourcef(source, AL_REFERENCE_DISTANCE, &f);
  553. return f;
  554. }
  555. // In case the Source isn't playing.
  556. return this->referenceDistance;
  557. }
  558. void Source::setRolloffFactor(float factor)
  559. {
  560. if (valid)
  561. {
  562. alSourcef(source, AL_ROLLOFF_FACTOR, factor);
  563. }
  564. this->rolloffFactor = factor;
  565. }
  566. float Source::getRolloffFactor() const
  567. {
  568. if (valid)
  569. {
  570. ALfloat f;
  571. alGetSourcef(source, AL_ROLLOFF_FACTOR, &f);
  572. return f;
  573. }
  574. // In case the Source isn't playing.
  575. return this->rolloffFactor;
  576. }
  577. void Source::setMaxDistance(float distance)
  578. {
  579. if (valid)
  580. {
  581. alSourcef(source, AL_MAX_DISTANCE, distance);
  582. }
  583. this->maxDistance = distance;
  584. }
  585. float Source::getMaxDistance() const
  586. {
  587. if (valid)
  588. {
  589. ALfloat f;
  590. alGetSourcef(source, AL_MAX_DISTANCE, &f);
  591. return f;
  592. }
  593. // In case the Source isn't playing.
  594. return this->maxDistance;
  595. }
  596. } // openal
  597. } // audio
  598. } // love