audio_server.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*************************************************************************/
  2. /* audio_server.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "core/math/audio_frame.h"
  33. #include "core/object.h"
  34. #include "core/os/os.h"
  35. #include "core/variant.h"
  36. #include "servers/audio/audio_effect.h"
  37. class AudioDriverDummy;
  38. class AudioStream;
  39. class AudioStreamSample;
  40. class AudioDriver {
  41. static AudioDriver *singleton;
  42. uint64_t _last_mix_time;
  43. uint64_t _last_mix_frames;
  44. #ifdef DEBUG_ENABLED
  45. uint64_t prof_ticks;
  46. uint64_t prof_time;
  47. #endif
  48. protected:
  49. Vector<int32_t> input_buffer;
  50. unsigned int input_position;
  51. unsigned int input_size;
  52. void audio_server_process(int p_frames, int32_t *p_buffer, bool p_update_mix_time = true);
  53. void update_mix_time(int p_frames);
  54. void input_buffer_init(int driver_buffer_frames);
  55. void input_buffer_write(int32_t sample);
  56. #ifdef DEBUG_ENABLED
  57. _FORCE_INLINE_ void start_counting_ticks() { prof_ticks = OS::get_singleton()->get_ticks_usec(); }
  58. _FORCE_INLINE_ void stop_counting_ticks() { prof_time += OS::get_singleton()->get_ticks_usec() - prof_ticks; }
  59. #else
  60. _FORCE_INLINE_ void start_counting_ticks() {}
  61. _FORCE_INLINE_ void stop_counting_ticks() {}
  62. #endif
  63. public:
  64. double get_time_since_last_mix(); //useful for video -> audio sync
  65. double get_time_to_next_mix();
  66. enum SpeakerMode {
  67. SPEAKER_MODE_STEREO,
  68. SPEAKER_SURROUND_31,
  69. SPEAKER_SURROUND_51,
  70. SPEAKER_SURROUND_71,
  71. };
  72. static AudioDriver *get_singleton();
  73. void set_singleton();
  74. virtual const char *get_name() const = 0;
  75. virtual Error init() = 0;
  76. virtual void start() = 0;
  77. virtual int get_mix_rate() const = 0;
  78. virtual SpeakerMode get_speaker_mode() const = 0;
  79. virtual Array get_device_list();
  80. virtual String get_device();
  81. virtual void set_device(String device) {}
  82. virtual void lock() = 0;
  83. virtual void unlock() = 0;
  84. virtual void finish() = 0;
  85. virtual Error capture_start() { return FAILED; }
  86. virtual Error capture_stop() { return FAILED; }
  87. virtual void capture_set_device(const String &p_name) {}
  88. virtual String capture_get_device() { return "Default"; }
  89. virtual Array capture_get_device_list(); // TODO: convert this and get_device_list to PoolStringArray
  90. virtual float get_latency() { return 0; }
  91. SpeakerMode get_speaker_mode_by_total_channels(int p_channels) const;
  92. int get_total_channels_by_speaker_mode(SpeakerMode) const;
  93. Vector<int32_t> get_input_buffer() { return input_buffer; }
  94. unsigned int get_input_position() { return input_position; }
  95. unsigned int get_input_size() { return input_size; }
  96. #ifdef DEBUG_ENABLED
  97. uint64_t get_profiling_time() const { return prof_time; }
  98. void reset_profiling_time() { prof_time = 0; }
  99. #endif
  100. AudioDriver();
  101. virtual ~AudioDriver() {}
  102. };
  103. class AudioDriverManager {
  104. enum {
  105. MAX_DRIVERS = 10
  106. };
  107. static const int DEFAULT_MIX_RATE = 44100;
  108. static const int DEFAULT_OUTPUT_LATENCY = 15;
  109. static AudioDriver *drivers[MAX_DRIVERS];
  110. static int driver_count;
  111. static AudioDriverDummy dummy_driver;
  112. public:
  113. static void add_driver(AudioDriver *p_driver);
  114. static void initialize(int p_driver);
  115. static int get_driver_count();
  116. static AudioDriver *get_driver(int p_driver);
  117. };
  118. class AudioBusLayout;
  119. class AudioServer : public Object {
  120. GDCLASS(AudioServer, Object);
  121. public:
  122. //re-expose this here, as AudioDriver is not exposed to script
  123. enum SpeakerMode {
  124. SPEAKER_MODE_STEREO,
  125. SPEAKER_SURROUND_31,
  126. SPEAKER_SURROUND_51,
  127. SPEAKER_SURROUND_71,
  128. };
  129. enum {
  130. AUDIO_DATA_INVALID_ID = -1
  131. };
  132. typedef void (*AudioCallback)(void *p_userdata);
  133. private:
  134. uint64_t mix_time;
  135. int mix_size;
  136. uint32_t buffer_size;
  137. uint64_t mix_count;
  138. uint64_t mix_frames;
  139. #ifdef DEBUG_ENABLED
  140. uint64_t prof_time;
  141. #endif
  142. float channel_disable_threshold_db;
  143. uint32_t channel_disable_frames;
  144. int channel_count;
  145. int to_mix;
  146. float global_rate_scale;
  147. struct Bus {
  148. StringName name;
  149. bool solo;
  150. bool mute;
  151. bool bypass;
  152. bool soloed;
  153. //Each channel is a stereo pair.
  154. struct Channel {
  155. bool used;
  156. bool active;
  157. AudioFrame peak_volume;
  158. Vector<AudioFrame> buffer;
  159. Vector<Ref<AudioEffectInstance>> effect_instances;
  160. uint64_t last_mix_with_audio;
  161. Channel() {
  162. last_mix_with_audio = 0;
  163. used = false;
  164. active = false;
  165. peak_volume = AudioFrame(AUDIO_MIN_PEAK_DB, AUDIO_MIN_PEAK_DB);
  166. }
  167. };
  168. Vector<Channel> channels;
  169. struct Effect {
  170. Ref<AudioEffect> effect;
  171. bool enabled;
  172. #ifdef DEBUG_ENABLED
  173. uint64_t prof_time;
  174. #endif
  175. };
  176. Vector<Effect> effects;
  177. float volume_db;
  178. StringName send;
  179. int index_cache;
  180. };
  181. Vector<Vector<AudioFrame>> temp_buffer; //temp_buffer for each level
  182. Vector<Bus *> buses;
  183. Map<StringName, Bus *> bus_map;
  184. void _update_bus_effects(int p_bus);
  185. static AudioServer *singleton;
  186. // TODO create an audiodata pool to optimize memory
  187. Map<void *, uint32_t> audio_data;
  188. size_t audio_data_total_mem;
  189. size_t audio_data_max_mem;
  190. Mutex audio_data_lock;
  191. void init_channels_and_buffers();
  192. void _mix_step();
  193. struct CallbackItem {
  194. AudioCallback callback;
  195. void *userdata;
  196. bool operator<(const CallbackItem &p_item) const {
  197. return (callback == p_item.callback ? userdata < p_item.userdata : callback < p_item.callback);
  198. }
  199. };
  200. Set<CallbackItem> callbacks;
  201. Set<CallbackItem> update_callbacks;
  202. friend class AudioDriver;
  203. void _driver_process(int p_frames, int32_t *p_buffer);
  204. protected:
  205. static void _bind_methods();
  206. public:
  207. _FORCE_INLINE_ int get_channel_count() const {
  208. switch (get_speaker_mode()) {
  209. case SPEAKER_MODE_STEREO:
  210. return 1;
  211. case SPEAKER_SURROUND_31:
  212. return 2;
  213. case SPEAKER_SURROUND_51:
  214. return 3;
  215. case SPEAKER_SURROUND_71:
  216. return 4;
  217. }
  218. ERR_FAIL_V(1);
  219. }
  220. //do not use from outside audio thread
  221. bool thread_has_channel_mix_buffer(int p_bus, int p_buffer) const;
  222. AudioFrame *thread_get_channel_mix_buffer(int p_bus, int p_buffer);
  223. int thread_get_mix_buffer_size() const;
  224. int thread_find_bus_index(const StringName &p_name);
  225. void set_bus_count(int p_count);
  226. int get_bus_count() const;
  227. void remove_bus(int p_index);
  228. void add_bus(int p_at_pos = -1);
  229. void move_bus(int p_bus, int p_to_pos);
  230. void set_bus_name(int p_bus, const String &p_name);
  231. String get_bus_name(int p_bus) const;
  232. int get_bus_index(const StringName &p_bus_name) const;
  233. int get_bus_channels(int p_bus) const;
  234. void set_bus_volume_db(int p_bus, float p_volume_db);
  235. float get_bus_volume_db(int p_bus) const;
  236. void set_bus_send(int p_bus, const StringName &p_send);
  237. StringName get_bus_send(int p_bus) const;
  238. void set_bus_solo(int p_bus, bool p_enable);
  239. bool is_bus_solo(int p_bus) const;
  240. void set_bus_mute(int p_bus, bool p_enable);
  241. bool is_bus_mute(int p_bus) const;
  242. void set_bus_bypass_effects(int p_bus, bool p_enable);
  243. bool is_bus_bypassing_effects(int p_bus) const;
  244. void add_bus_effect(int p_bus, const Ref<AudioEffect> &p_effect, int p_at_pos = -1);
  245. void remove_bus_effect(int p_bus, int p_effect);
  246. int get_bus_effect_count(int p_bus);
  247. Ref<AudioEffect> get_bus_effect(int p_bus, int p_effect);
  248. Ref<AudioEffectInstance> get_bus_effect_instance(int p_bus, int p_effect, int p_channel = 0);
  249. void swap_bus_effects(int p_bus, int p_effect, int p_by_effect);
  250. void set_bus_effect_enabled(int p_bus, int p_effect, bool p_enabled);
  251. bool is_bus_effect_enabled(int p_bus, int p_effect) const;
  252. float get_bus_peak_volume_left_db(int p_bus, int p_channel) const;
  253. float get_bus_peak_volume_right_db(int p_bus, int p_channel) const;
  254. bool is_bus_channel_active(int p_bus, int p_channel) const;
  255. void set_global_rate_scale(float p_scale);
  256. float get_global_rate_scale() const;
  257. virtual void init();
  258. virtual void finish();
  259. virtual void update();
  260. virtual void load_default_bus_layout();
  261. /* MISC config */
  262. virtual void lock();
  263. virtual void unlock();
  264. virtual SpeakerMode get_speaker_mode() const;
  265. virtual float get_mix_rate() const;
  266. virtual float read_output_peak_db() const;
  267. static AudioServer *get_singleton();
  268. virtual double get_output_latency() const;
  269. virtual double get_time_to_next_mix() const;
  270. virtual double get_time_since_last_mix() const;
  271. void *audio_data_alloc(uint32_t p_data_len, const uint8_t *p_from_data = nullptr);
  272. void audio_data_free(void *p_data);
  273. size_t audio_data_get_total_memory_usage() const;
  274. size_t audio_data_get_max_memory_usage() const;
  275. void add_callback(AudioCallback p_callback, void *p_userdata);
  276. void remove_callback(AudioCallback p_callback, void *p_userdata);
  277. void add_update_callback(AudioCallback p_callback, void *p_userdata);
  278. void remove_update_callback(AudioCallback p_callback, void *p_userdata);
  279. void set_bus_layout(const Ref<AudioBusLayout> &p_bus_layout);
  280. Ref<AudioBusLayout> generate_bus_layout() const;
  281. Array get_device_list();
  282. String get_device();
  283. void set_device(String device);
  284. Array capture_get_device_list();
  285. String capture_get_device();
  286. void capture_set_device(const String &p_name);
  287. AudioServer();
  288. virtual ~AudioServer();
  289. };
  290. VARIANT_ENUM_CAST(AudioServer::SpeakerMode)
  291. class AudioBusLayout : public Resource {
  292. GDCLASS(AudioBusLayout, Resource);
  293. friend class AudioServer;
  294. struct Bus {
  295. StringName name;
  296. bool solo;
  297. bool mute;
  298. bool bypass;
  299. struct Effect {
  300. Ref<AudioEffect> effect;
  301. bool enabled;
  302. };
  303. Vector<Effect> effects;
  304. float volume_db;
  305. StringName send;
  306. Bus() {
  307. solo = false;
  308. mute = false;
  309. bypass = false;
  310. volume_db = 0;
  311. }
  312. };
  313. Vector<Bus> buses;
  314. protected:
  315. bool _set(const StringName &p_name, const Variant &p_value);
  316. bool _get(const StringName &p_name, Variant &r_ret) const;
  317. void _get_property_list(List<PropertyInfo> *p_list) const;
  318. public:
  319. AudioBusLayout();
  320. };
  321. typedef AudioServer AS;
  322. #endif // AUDIO_SERVER_H