Source.cpp 18 KB

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