Source.cpp 18 KB

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