Source.cpp 14 KB

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