Audio.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /**
  2. * Copyright (c) 2006-2022 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_AUDIO_H
  21. #define LOVE_AUDIO_AUDIO_H
  22. // STL
  23. #include <vector>
  24. // LOVE
  25. #include "common/Module.h"
  26. #include "common/StringMap.h"
  27. #include "Source.h"
  28. #include "Effect.h"
  29. #include "RecordingDevice.h"
  30. namespace love
  31. {
  32. namespace sound
  33. {
  34. class Decoder;
  35. class SoundData;
  36. } // sound
  37. namespace audio
  38. {
  39. /*
  40. * In some platforms (notably Android), recording from mic
  41. * requires user permission. This function sets whetever to
  42. * request the permission later or not.
  43. */
  44. void setRequestRecordingPermission(bool rec);
  45. /*
  46. * Gets whetever recording permission will be requested.
  47. */
  48. bool getRequestRecordingPermission();
  49. /*
  50. * Gets whetever recording permission is granted.
  51. */
  52. bool hasRecordingPermission();
  53. /*
  54. * Request recording permission. This is blocking function.
  55. */
  56. void requestRecordingPermission();
  57. /*
  58. * In case recording permission is not granted, this
  59. * function shows the dialog about the recording permission.
  60. */
  61. void showRecordingPermissionMissingDialog();
  62. /**
  63. * The Audio module is responsible for playing back raw sound samples.
  64. **/
  65. class Audio : public Module
  66. {
  67. public:
  68. /**
  69. * Attenuation by distance.
  70. */
  71. enum DistanceModel
  72. {
  73. DISTANCE_NONE,
  74. DISTANCE_INVERSE,
  75. DISTANCE_INVERSE_CLAMPED,
  76. DISTANCE_LINEAR,
  77. DISTANCE_LINEAR_CLAMPED,
  78. DISTANCE_EXPONENT,
  79. DISTANCE_EXPONENT_CLAMPED,
  80. DISTANCE_MAX_ENUM
  81. };
  82. static bool getConstant(const char *in, DistanceModel &out);
  83. static bool getConstant(DistanceModel in, const char *&out);
  84. static std::vector<std::string> getConstants(DistanceModel);
  85. virtual ~Audio() {}
  86. // Implements Module.
  87. virtual ModuleType getModuleType() const { return M_AUDIO; }
  88. virtual Source *newSource(love::sound::Decoder *decoder) = 0;
  89. virtual Source *newSource(love::sound::SoundData *soundData) = 0;
  90. virtual Source *newSource(int sampleRate, int bitDepth, int channels, int buffers) = 0;
  91. /**
  92. * Gets the current number of simultaneous playing sources.
  93. * @return The current number of simultaneous playing sources.
  94. **/
  95. virtual int getActiveSourceCount() const = 0;
  96. /**
  97. * Gets the maximum supported number of simultaneous playing sources.
  98. * @return The maximum supported number of simultaneous playing sources.
  99. **/
  100. virtual int getMaxSources() const = 0;
  101. /**
  102. * Play the specified Source.
  103. * @param source The Source to play.
  104. **/
  105. virtual bool play(Source *source) = 0;
  106. /**
  107. * Play the specified Sources.
  108. * @param sources The Sources to play.
  109. **/
  110. virtual bool play(const std::vector<Source*> &sources) = 0;
  111. /**
  112. * Stops playback on the specified source.
  113. * @param source The source on which to stop the playback.
  114. **/
  115. virtual void stop(Source *source) = 0;
  116. /**
  117. * Stops playback on the specified sources.
  118. * @param sources The sources on which to stop the playback.
  119. **/
  120. virtual void stop(const std::vector<Source*> &sources) = 0;
  121. /**
  122. * Stops all playing audio.
  123. **/
  124. virtual void stop() = 0;
  125. /**
  126. * Pauses playback on the specified source.
  127. * @param source The source on which to pause the playback.
  128. **/
  129. virtual void pause(Source *source) = 0;
  130. /**
  131. * Pauses playback on the specified sources.
  132. * @param sources The sources on which to pause the playback.
  133. **/
  134. virtual void pause(const std::vector<Source*> &sources) = 0;
  135. /**
  136. * Pauses all audio.
  137. **/
  138. virtual std::vector<Source*> pause() = 0;
  139. /**
  140. * Sets the master volume, where 0.0f is min (off) and 1.0f is max.
  141. * @param volume The new master volume.
  142. **/
  143. virtual void setVolume(float volume) = 0;
  144. /**
  145. * Gets the master volume.
  146. * @return The current master volume.
  147. **/
  148. virtual float getVolume() const = 0;
  149. /**
  150. * Gets the position of the listener.
  151. * @param v A float array of size 3 containing (x,y,z) in that order.
  152. **/
  153. virtual void getPosition(float *v) const = 0;
  154. /**
  155. * Sets the position of the listener.
  156. * @param v A float array of size 3 containing [x,y,z] in that order.
  157. **/
  158. virtual void setPosition(float *v) = 0;
  159. /**
  160. * Gets the orientation of the listener.
  161. * @param v A float array of size 6 containing [x,y,z] for the forward
  162. * vector, followed by [x,y,z] for the up vector.
  163. **/
  164. virtual void getOrientation(float *v) const = 0;
  165. /**
  166. * Sets the orientation of the listener.
  167. * @param v A float array of size 6 containing [x,y,z] for the forward
  168. * vector, followed by [x,y,z] for the up vector.
  169. **/
  170. virtual void setOrientation(float *v) = 0;
  171. /**
  172. * Gets the velocity of the listener.
  173. * @param v A float array of size 3 containing [x,y,z] in that order.
  174. **/
  175. virtual void getVelocity(float *v) const = 0;
  176. /**
  177. * Sets the velocity of the listener.
  178. * @param v A float array of size 3 containing [x,y,z] in that order.
  179. **/
  180. virtual void setVelocity(float *v) = 0;
  181. virtual void setDopplerScale(float scale) = 0;
  182. virtual float getDopplerScale() const = 0;
  183. //virtual void setMeter(float scale) = 0;
  184. //virtual float getMeter() const = 0;
  185. /**
  186. * @return Reference to a vector of pointers to recording devices. May be empty.
  187. **/
  188. virtual const std::vector<RecordingDevice*> &getRecordingDevices() = 0;
  189. /**
  190. * Gets the distance model used for attenuation.
  191. * @return Distance model.
  192. */
  193. virtual DistanceModel getDistanceModel() const = 0;
  194. /**
  195. * Sets the distance model used for attenuation.
  196. * @param distanceModel Distance model.
  197. */
  198. virtual void setDistanceModel(DistanceModel distanceModel) = 0;
  199. /**
  200. * Sets scene EFX effect.
  201. * @param name Effect name to use.
  202. * @param fxparams Effect description table.
  203. * @return true if successful, false otherwise.
  204. */
  205. virtual bool setEffect(const char *name, std::map<Effect::Parameter, float> &params) = 0;
  206. /**
  207. * Removes scene EFX effect.
  208. * @param name Effect name to clear.
  209. * @return true if successful, false otherwise.
  210. */
  211. virtual bool unsetEffect(const char *name) = 0;
  212. /**
  213. * Gets scene EFX effect.
  214. * @param name Effect name to get data from.
  215. * @param fxparams Effect description table.
  216. * @return true if effect was present, false otherwise.
  217. */
  218. virtual bool getEffect(const char *name, std::map<Effect::Parameter, float> &params) = 0;
  219. /**
  220. * Gets list of EFX effect names.
  221. * @param list List of EFX names to fill.
  222. * @return true if effect was present, false otherwise.
  223. */
  224. virtual bool getActiveEffects(std::vector<std::string> &list) const = 0;
  225. /**
  226. * Gets maximum number of scene EFX effects.
  227. * @return number of effects.
  228. */
  229. virtual int getMaxSceneEffects() const = 0;
  230. /**
  231. * Gets maximum number of source EFX effects.
  232. * @return number of effects.
  233. */
  234. virtual int getMaxSourceEffects() const = 0;
  235. /**
  236. * Gets EFX (or analog) availability.
  237. * @return true if supported.
  238. */
  239. virtual bool isEFXsupported() const = 0;
  240. /**
  241. * Sets whether audio from other apps mixes with love.audio or is muted,
  242. * on supported platforms.
  243. **/
  244. static bool setMixWithSystem(bool mix);
  245. /**
  246. * Pause/resume audio context
  247. */
  248. virtual void pauseContext() = 0;
  249. virtual void resumeContext() = 0;
  250. /**
  251. * Get current playback device name.
  252. */
  253. virtual std::string getPlaybackDevice() = 0;
  254. /**
  255. * Retrieve list of available playback devices.
  256. */
  257. virtual void getPlaybackDevices(std::vector<std::string> &list) = 0;
  258. /**
  259. * Set the current playback device to specified device name.
  260. */
  261. virtual void setPlaybackDevice(const char *name);
  262. private:
  263. static StringMap<DistanceModel, DISTANCE_MAX_ENUM>::Entry distanceModelEntries[];
  264. static StringMap<DistanceModel, DISTANCE_MAX_ENUM> distanceModels;
  265. }; // Audio
  266. } // audio
  267. } // love
  268. #endif // LOVE_AUDIO_AUDIO_H