Source.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**
  2. * Copyright (c) 2006-2023 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. #ifndef LOVE_AUDIO_OPENAL_SOURCE_H
  21. #define LOVE_AUDIO_OPENAL_SOURCE_H
  22. // LOVE
  23. #include "common/config.h"
  24. #include "common/Object.h"
  25. #include "audio/Source.h"
  26. #include "audio/Filter.h"
  27. #include "sound/SoundData.h"
  28. #include "sound/Decoder.h"
  29. #include "Audio.h"
  30. #include "Filter.h"
  31. // STL
  32. #include <vector>
  33. #include <stack>
  34. // C
  35. #include <float.h>
  36. // OpenAL
  37. #ifdef LOVE_APPLE_USE_FRAMEWORKS
  38. #ifdef LOVE_IOS
  39. #include <OpenAL/alc.h>
  40. #include <OpenAL/al.h>
  41. #else
  42. #include <OpenAL-Soft/alc.h>
  43. #include <OpenAL-Soft/al.h>
  44. #endif
  45. #else
  46. #include <AL/alc.h>
  47. #include <AL/al.h>
  48. #endif
  49. namespace love
  50. {
  51. namespace audio
  52. {
  53. namespace openal
  54. {
  55. #ifdef LOVE_IOS
  56. // OpenAL on iOS barfs if the max distance is +inf.
  57. static const float MAX_ATTENUATION_DISTANCE = 1000000.0f;
  58. #else
  59. static const float MAX_ATTENUATION_DISTANCE = FLT_MAX;
  60. #endif
  61. class Audio;
  62. class Pool;
  63. // Basically just a reference-counted non-streaming OpenAL buffer object.
  64. class StaticDataBuffer : public love::Object
  65. {
  66. public:
  67. StaticDataBuffer(ALenum format, const ALvoid *data, ALsizei size, ALsizei freq);
  68. virtual ~StaticDataBuffer();
  69. inline ALuint getBuffer() const
  70. {
  71. return buffer;
  72. }
  73. inline ALsizei getSize() const
  74. {
  75. return size;
  76. }
  77. private:
  78. ALuint buffer;
  79. ALsizei size;
  80. }; // StaticDataBuffer
  81. class Source : public love::audio::Source
  82. {
  83. public:
  84. Source(Pool *pool, love::sound::SoundData *soundData);
  85. Source(Pool *pool, love::sound::Decoder *decoder);
  86. Source(Pool *pool, int sampleRate, int bitDepth, int channels, int buffers);
  87. Source(const Source &s);
  88. virtual ~Source();
  89. virtual love::audio::Source *clone();
  90. virtual bool play();
  91. virtual void stop();
  92. virtual void pause();
  93. virtual bool isPlaying() const;
  94. virtual bool isFinished() const;
  95. virtual bool update();
  96. virtual void setPitch(float pitch);
  97. virtual float getPitch() const;
  98. virtual void setVolume(float volume);
  99. virtual float getVolume() const;
  100. virtual void seek(double offset, Unit unit);
  101. virtual double tell(Unit unit);
  102. virtual double getDuration(Unit unit);
  103. virtual void setPosition(float *v);
  104. virtual void getPosition(float *v) const;
  105. virtual void setVelocity(float *v);
  106. virtual void getVelocity(float *v) const;
  107. virtual void setDirection(float *v);
  108. virtual void getDirection(float *v) const;
  109. virtual void setCone(float innerAngle, float outerAngle, float outerVolume, float outerHighGain);
  110. virtual void getCone(float &innerAngle, float &outerAngle, float &outerVolume, float &outerHighGain) const;
  111. virtual void setRelative(bool enable);
  112. virtual bool isRelative() const;
  113. void setLooping(bool looping);
  114. bool isLooping() const;
  115. virtual void setMinVolume(float volume);
  116. virtual float getMinVolume() const;
  117. virtual void setMaxVolume(float volume);
  118. virtual float getMaxVolume() const;
  119. virtual void setReferenceDistance(float distance);
  120. virtual float getReferenceDistance() const;
  121. virtual void setRolloffFactor(float factor);
  122. virtual float getRolloffFactor() const;
  123. virtual void setMaxDistance(float distance);
  124. virtual float getMaxDistance() const;
  125. virtual void setAirAbsorptionFactor(float factor);
  126. virtual float getAirAbsorptionFactor() const;
  127. virtual int getChannelCount() const;
  128. virtual bool setFilter(const std::map<Filter::Parameter, float> &params);
  129. virtual bool setFilter();
  130. virtual bool getFilter(std::map<Filter::Parameter, float> &params);
  131. virtual bool setEffect(const char *effect);
  132. virtual bool setEffect(const char *effect, const std::map<Filter::Parameter, float> &params);
  133. virtual bool unsetEffect(const char *effect);
  134. virtual bool getEffect(const char *effect, std::map<Filter::Parameter, float> &params);
  135. virtual bool getActiveEffects(std::vector<std::string> &list) const;
  136. virtual int getFreeBufferCount() const;
  137. virtual bool queue(void *data, size_t length, int dataSampleRate, int dataBitDepth, int dataChannels);
  138. void prepareAtomic();
  139. void teardownAtomic();
  140. bool playAtomic(ALuint source);
  141. void stopAtomic();
  142. void pauseAtomic();
  143. void resumeAtomic();
  144. static bool play(const std::vector<love::audio::Source*> &sources);
  145. static void stop(const std::vector<love::audio::Source*> &sources);
  146. static void pause(const std::vector<love::audio::Source*> &sources);
  147. static std::vector<love::audio::Source*> pause(Pool *pool);
  148. static void stop(Pool *pool);
  149. private:
  150. void reset();
  151. void setFloatv(float *dst, const float *src) const;
  152. int streamAtomic(ALuint buffer, love::sound::Decoder *d);
  153. Pool *pool = nullptr;
  154. ALuint source = 0;
  155. bool valid = false;
  156. const static int DEFAULT_BUFFERS = 8;
  157. const static int MAX_BUFFERS = 64;
  158. std::queue<ALuint> streamBuffers;
  159. std::stack<ALuint> unusedBuffers;
  160. StrongRef<StaticDataBuffer> staticBuffer;
  161. float pitch = 1.0f;
  162. float volume = 1.0f;
  163. float position[3];
  164. float velocity[3];
  165. float direction[3];
  166. bool relative = false;
  167. bool looping = false;
  168. float minVolume = 0.0f;
  169. float maxVolume = 1.0f;
  170. float referenceDistance = 1.0f;
  171. float rolloffFactor = 1.0f;
  172. float absorptionFactor = 0.0f;
  173. float maxDistance = MAX_ATTENUATION_DISTANCE;
  174. struct Cone
  175. {
  176. int innerAngle = 360; // degrees
  177. int outerAngle = 360; // degrees
  178. float outerVolume = 0.0f;
  179. float outerHighGain = 1.0f;
  180. } cone;
  181. int offsetSamples = 0;
  182. int sampleRate = 0;
  183. int channels = 0;
  184. int bitDepth = 0;
  185. StrongRef<love::sound::Decoder> decoder;
  186. unsigned int toLoop = 0;
  187. ALsizei bufferedBytes = 0;
  188. int buffers = 0;
  189. Filter *directfilter = nullptr;
  190. struct EffectMapStorage
  191. {
  192. Filter *filter;
  193. ALuint slot, target;
  194. };
  195. std::map<std::string, EffectMapStorage> effectmap;
  196. std::stack<ALuint> slotlist;
  197. }; // Source
  198. } // openal
  199. } // audio
  200. } // love
  201. #endif // LOVE_AUDIO_OPENAL_SOURCE_H