audio_server.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*************************************************************************/
  2. /* audio_server.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef AUDIO_SERVER_H
  31. #define AUDIO_SERVER_H
  32. #include "audio_frame.h"
  33. #include "object.h"
  34. #include "servers/audio/audio_effect.h"
  35. #include "variant.h"
  36. class AudioDriverDummy;
  37. class AudioDriver {
  38. static AudioDriver *singleton;
  39. uint64_t _last_mix_time;
  40. uint64_t _mix_amount;
  41. protected:
  42. void audio_server_process(int p_frames, int32_t *p_buffer, bool p_update_mix_time = true);
  43. void update_mix_time(int p_frames);
  44. public:
  45. double get_mix_time() const; //useful for video -> audio sync
  46. enum SpeakerMode {
  47. SPEAKER_MODE_STEREO,
  48. SPEAKER_SURROUND_31,
  49. SPEAKER_SURROUND_51,
  50. SPEAKER_SURROUND_71,
  51. };
  52. static const int DEFAULT_MIX_RATE = 44100;
  53. static const int DEFAULT_OUTPUT_LATENCY = 15;
  54. static AudioDriver *get_singleton();
  55. void set_singleton();
  56. virtual const char *get_name() const = 0;
  57. virtual Error init() = 0;
  58. virtual void start() = 0;
  59. virtual int get_mix_rate() const = 0;
  60. virtual SpeakerMode get_speaker_mode() const = 0;
  61. virtual void lock() = 0;
  62. virtual void unlock() = 0;
  63. virtual void finish() = 0;
  64. virtual float get_latency() { return 0; }
  65. SpeakerMode get_speaker_mode_by_total_channels(int p_channels) const;
  66. int get_total_channels_by_speaker_mode(SpeakerMode) const;
  67. AudioDriver();
  68. virtual ~AudioDriver() {}
  69. };
  70. class AudioDriverManager {
  71. enum {
  72. MAX_DRIVERS = 10
  73. };
  74. static AudioDriver *drivers[MAX_DRIVERS];
  75. static int driver_count;
  76. static AudioDriverDummy dummy_driver;
  77. public:
  78. static void add_driver(AudioDriver *p_driver);
  79. static void initialize(int p_driver);
  80. static int get_driver_count();
  81. static AudioDriver *get_driver(int p_driver);
  82. };
  83. class AudioBusLayout;
  84. class AudioServer : public Object {
  85. GDCLASS(AudioServer, Object)
  86. public:
  87. //re-expose this her, as AudioDriver is not exposed to script
  88. enum SpeakerMode {
  89. SPEAKER_MODE_STEREO,
  90. SPEAKER_SURROUND_31,
  91. SPEAKER_SURROUND_51,
  92. SPEAKER_SURROUND_71,
  93. };
  94. enum {
  95. AUDIO_DATA_INVALID_ID = -1
  96. };
  97. typedef void (*AudioCallback)(void *p_userdata);
  98. private:
  99. uint32_t buffer_size;
  100. uint64_t mix_count;
  101. uint64_t mix_frames;
  102. float channel_disable_threshold_db;
  103. uint32_t channel_disable_frames;
  104. int to_mix;
  105. struct Bus {
  106. StringName name;
  107. bool solo;
  108. bool mute;
  109. bool bypass;
  110. bool soloed;
  111. //Each channel is a stereo pair.
  112. struct Channel {
  113. bool used;
  114. bool active;
  115. AudioFrame peak_volume;
  116. Vector<AudioFrame> buffer;
  117. Vector<Ref<AudioEffectInstance> > effect_instances;
  118. uint64_t last_mix_with_audio;
  119. Channel() {
  120. last_mix_with_audio = 0;
  121. used = false;
  122. active = false;
  123. peak_volume = AudioFrame(0, 0);
  124. }
  125. };
  126. Vector<Channel> channels;
  127. struct Effect {
  128. Ref<AudioEffect> effect;
  129. bool enabled;
  130. };
  131. Vector<Effect> effects;
  132. float volume_db;
  133. StringName send;
  134. int index_cache;
  135. };
  136. Vector<Vector<AudioFrame> > temp_buffer; //temp_buffer for each level
  137. Vector<Bus *> buses;
  138. Map<StringName, Bus *> bus_map;
  139. void _update_bus_effects(int p_bus);
  140. static AudioServer *singleton;
  141. // TODO create an audiodata pool to optimize memory
  142. Map<void *, uint32_t> audio_data;
  143. size_t audio_data_total_mem;
  144. size_t audio_data_max_mem;
  145. Mutex *audio_data_lock;
  146. void _mix_step();
  147. struct CallbackItem {
  148. AudioCallback callback;
  149. void *userdata;
  150. bool operator<(const CallbackItem &p_item) const {
  151. return (callback == p_item.callback ? userdata < p_item.userdata : callback < p_item.callback);
  152. }
  153. };
  154. Set<CallbackItem> callbacks;
  155. friend class AudioDriver;
  156. void _driver_process(int p_frames, int32_t *p_buffer);
  157. protected:
  158. static void _bind_methods();
  159. public:
  160. _FORCE_INLINE_ int get_channel_count() const {
  161. switch (get_speaker_mode()) {
  162. case SPEAKER_MODE_STEREO: return 1;
  163. case SPEAKER_SURROUND_31: return 2;
  164. case SPEAKER_SURROUND_51: return 3;
  165. case SPEAKER_SURROUND_71: return 4;
  166. }
  167. ERR_FAIL_V(1);
  168. }
  169. //do not use from outside audio thread
  170. AudioFrame *thread_get_channel_mix_buffer(int p_bus, int p_buffer);
  171. int thread_get_mix_buffer_size() const;
  172. int thread_find_bus_index(const StringName &p_name);
  173. void set_bus_count(int p_count);
  174. int get_bus_count() const;
  175. void remove_bus(int p_index);
  176. void add_bus(int p_at_pos = -1);
  177. void move_bus(int p_bus, int p_to_pos);
  178. void set_bus_name(int p_bus, const String &p_name);
  179. String get_bus_name(int p_bus) const;
  180. int get_bus_index(const StringName &p_bus_name) const;
  181. void set_bus_volume_db(int p_bus, float p_volume_db);
  182. float get_bus_volume_db(int p_bus) const;
  183. void set_bus_send(int p_bus, const StringName &p_send);
  184. StringName get_bus_send(int p_bus) const;
  185. void set_bus_solo(int p_bus, bool p_enable);
  186. bool is_bus_solo(int p_bus) const;
  187. void set_bus_mute(int p_bus, bool p_enable);
  188. bool is_bus_mute(int p_bus) const;
  189. void set_bus_bypass_effects(int p_bus, bool p_enable);
  190. bool is_bus_bypassing_effects(int p_bus) const;
  191. void add_bus_effect(int p_bus, const Ref<AudioEffect> &p_effect, int p_at_pos = -1);
  192. void remove_bus_effect(int p_bus, int p_effect);
  193. int get_bus_effect_count(int p_bus);
  194. Ref<AudioEffect> get_bus_effect(int p_bus, int p_effect);
  195. void swap_bus_effects(int p_bus, int p_effect, int p_by_effect);
  196. void set_bus_effect_enabled(int p_bus, int p_effect, bool p_enabled);
  197. bool is_bus_effect_enabled(int p_bus, int p_effect) const;
  198. float get_bus_peak_volume_left_db(int p_bus, int p_channel) const;
  199. float get_bus_peak_volume_right_db(int p_bus, int p_channel) const;
  200. bool is_bus_channel_active(int p_bus, int p_channel) const;
  201. virtual void init();
  202. virtual void finish();
  203. virtual void update();
  204. virtual void load_default_bus_layout();
  205. /* MISC config */
  206. virtual void lock();
  207. virtual void unlock();
  208. virtual SpeakerMode get_speaker_mode() const;
  209. virtual float get_mix_rate() const;
  210. virtual float read_output_peak_db() const;
  211. static AudioServer *get_singleton();
  212. virtual double get_mix_time() const; //useful for video -> audio sync
  213. virtual double get_output_delay() const;
  214. void *audio_data_alloc(uint32_t p_data_len, const uint8_t *p_from_data = NULL);
  215. void audio_data_free(void *p_data);
  216. size_t audio_data_get_total_memory_usage() const;
  217. size_t audio_data_get_max_memory_usage() const;
  218. void add_callback(AudioCallback p_callback, void *p_userdata);
  219. void remove_callback(AudioCallback p_callback, void *p_userdata);
  220. void set_bus_layout(const Ref<AudioBusLayout> &p_bus_layout);
  221. Ref<AudioBusLayout> generate_bus_layout() const;
  222. AudioServer();
  223. virtual ~AudioServer();
  224. };
  225. VARIANT_ENUM_CAST(AudioServer::SpeakerMode)
  226. class AudioBusLayout : public Resource {
  227. GDCLASS(AudioBusLayout, Resource)
  228. friend class AudioServer;
  229. struct Bus {
  230. StringName name;
  231. bool solo;
  232. bool mute;
  233. bool bypass;
  234. struct Effect {
  235. Ref<AudioEffect> effect;
  236. bool enabled;
  237. };
  238. Vector<Effect> effects;
  239. float volume_db;
  240. StringName send;
  241. Bus() {
  242. solo = false;
  243. mute = false;
  244. bypass = false;
  245. volume_db = 0;
  246. }
  247. };
  248. Vector<Bus> buses;
  249. protected:
  250. bool _set(const StringName &p_name, const Variant &p_value);
  251. bool _get(const StringName &p_name, Variant &r_ret) const;
  252. void _get_property_list(List<PropertyInfo> *p_list) const;
  253. public:
  254. AudioBusLayout();
  255. };
  256. typedef AudioServer AS;
  257. #endif // AUDIO_SERVER_H