audio_server.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*************************************************************************/
  2. /* audio_server.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 _mix_amount;
  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_mix_time() const; //useful for video -> audio sync
  65. enum SpeakerMode {
  66. SPEAKER_MODE_STEREO,
  67. SPEAKER_SURROUND_31,
  68. SPEAKER_SURROUND_51,
  69. SPEAKER_SURROUND_71,
  70. };
  71. static const int DEFAULT_MIX_RATE = 44100;
  72. static const int DEFAULT_OUTPUT_LATENCY = 15;
  73. static AudioDriver *get_singleton();
  74. void set_singleton();
  75. virtual const char *get_name() const = 0;
  76. virtual Error init() = 0;
  77. virtual void start() = 0;
  78. virtual int get_mix_rate() const = 0;
  79. virtual SpeakerMode get_speaker_mode() const = 0;
  80. virtual Array get_device_list();
  81. virtual String get_device();
  82. virtual void set_device(String device) {}
  83. virtual void lock() = 0;
  84. virtual void unlock() = 0;
  85. virtual void finish() = 0;
  86. virtual Error capture_start() { return FAILED; }
  87. virtual Error capture_stop() { return FAILED; }
  88. virtual void capture_set_device(const String &p_name) {}
  89. virtual String capture_get_device() { return "Default"; }
  90. virtual Array capture_get_device_list(); // TODO: convert this and get_device_list to PoolStringArray
  91. virtual float get_latency() { return 0; }
  92. SpeakerMode get_speaker_mode_by_total_channels(int p_channels) const;
  93. int get_total_channels_by_speaker_mode(SpeakerMode) const;
  94. Vector<int32_t> get_input_buffer() { return input_buffer; }
  95. unsigned int get_input_position() { return input_position; }
  96. unsigned int get_input_size() { return input_size; }
  97. #ifdef DEBUG_ENABLED
  98. uint64_t get_profiling_time() const { return prof_time; }
  99. void reset_profiling_time() { prof_time = 0; }
  100. #endif
  101. AudioDriver();
  102. virtual ~AudioDriver() {}
  103. };
  104. class AudioDriverManager {
  105. enum {
  106. MAX_DRIVERS = 10
  107. };
  108. static AudioDriver *drivers[MAX_DRIVERS];
  109. static int driver_count;
  110. static AudioDriverDummy dummy_driver;
  111. public:
  112. static void add_driver(AudioDriver *p_driver);
  113. static void initialize(int p_driver);
  114. static int get_driver_count();
  115. static AudioDriver *get_driver(int p_driver);
  116. };
  117. class AudioBusLayout;
  118. class AudioServer : public Object {
  119. GDCLASS(AudioServer, Object)
  120. public:
  121. //re-expose this her, as AudioDriver is not exposed to script
  122. enum SpeakerMode {
  123. SPEAKER_MODE_STEREO,
  124. SPEAKER_SURROUND_31,
  125. SPEAKER_SURROUND_51,
  126. SPEAKER_SURROUND_71,
  127. };
  128. enum {
  129. AUDIO_DATA_INVALID_ID = -1
  130. };
  131. typedef void (*AudioCallback)(void *p_userdata);
  132. private:
  133. uint32_t buffer_size;
  134. uint64_t mix_count;
  135. uint64_t mix_frames;
  136. #ifdef DEBUG_ENABLED
  137. uint64_t prof_time;
  138. #endif
  139. float channel_disable_threshold_db;
  140. uint32_t channel_disable_frames;
  141. int channel_count;
  142. int to_mix;
  143. struct Bus {
  144. StringName name;
  145. bool solo;
  146. bool mute;
  147. bool bypass;
  148. bool soloed;
  149. //Each channel is a stereo pair.
  150. struct Channel {
  151. bool used;
  152. bool active;
  153. AudioFrame peak_volume;
  154. Vector<AudioFrame> buffer;
  155. Vector<Ref<AudioEffectInstance> > effect_instances;
  156. uint64_t last_mix_with_audio;
  157. Channel() {
  158. last_mix_with_audio = 0;
  159. used = false;
  160. active = false;
  161. peak_volume = AudioFrame(0, 0);
  162. }
  163. };
  164. Vector<Channel> channels;
  165. struct Effect {
  166. Ref<AudioEffect> effect;
  167. bool enabled;
  168. #ifdef DEBUG_ENABLED
  169. uint64_t prof_time;
  170. #endif
  171. };
  172. Vector<Effect> effects;
  173. float volume_db;
  174. StringName send;
  175. int index_cache;
  176. };
  177. Vector<Vector<AudioFrame> > temp_buffer; //temp_buffer for each level
  178. Vector<Bus *> buses;
  179. Map<StringName, Bus *> bus_map;
  180. void _update_bus_effects(int p_bus);
  181. static AudioServer *singleton;
  182. // TODO create an audiodata pool to optimize memory
  183. Map<void *, uint32_t> audio_data;
  184. size_t audio_data_total_mem;
  185. size_t audio_data_max_mem;
  186. Mutex *audio_data_lock;
  187. float output_latency;
  188. uint64_t output_latency_ticks;
  189. void init_channels_and_buffers();
  190. void _mix_step();
  191. #if 0
  192. struct AudioInBlock {
  193. Ref<AudioStreamSample> audio_stream;
  194. int current_position;
  195. bool loops;
  196. };
  197. Map<StringName, AudioInBlock *> audio_in_block_map;
  198. Vector<AudioInBlock *> audio_in_blocks;
  199. #endif
  200. struct CallbackItem {
  201. AudioCallback callback;
  202. void *userdata;
  203. bool operator<(const CallbackItem &p_item) const {
  204. return (callback == p_item.callback ? userdata < p_item.userdata : callback < p_item.callback);
  205. }
  206. };
  207. Set<CallbackItem> callbacks;
  208. Set<CallbackItem> update_callbacks;
  209. friend class AudioDriver;
  210. void _driver_process(int p_frames, int32_t *p_buffer);
  211. protected:
  212. static void _bind_methods();
  213. public:
  214. _FORCE_INLINE_ int get_channel_count() const {
  215. switch (get_speaker_mode()) {
  216. case SPEAKER_MODE_STEREO: return 1;
  217. case SPEAKER_SURROUND_31: return 2;
  218. case SPEAKER_SURROUND_51: return 3;
  219. case SPEAKER_SURROUND_71: return 4;
  220. }
  221. ERR_FAIL_V(1);
  222. }
  223. //do not use from outside audio thread
  224. bool thread_has_channel_mix_buffer(int p_bus, int p_buffer) const;
  225. AudioFrame *thread_get_channel_mix_buffer(int p_bus, int p_buffer);
  226. int thread_get_mix_buffer_size() const;
  227. int thread_find_bus_index(const StringName &p_name);
  228. void set_bus_count(int p_count);
  229. int get_bus_count() const;
  230. void remove_bus(int p_index);
  231. void add_bus(int p_at_pos = -1);
  232. void move_bus(int p_bus, int p_to_pos);
  233. void set_bus_name(int p_bus, const String &p_name);
  234. String get_bus_name(int p_bus) const;
  235. int get_bus_index(const StringName &p_bus_name) const;
  236. int get_bus_channels(int p_bus) const;
  237. void set_bus_volume_db(int p_bus, float p_volume_db);
  238. float get_bus_volume_db(int p_bus) const;
  239. void set_bus_send(int p_bus, const StringName &p_send);
  240. StringName get_bus_send(int p_bus) const;
  241. void set_bus_solo(int p_bus, bool p_enable);
  242. bool is_bus_solo(int p_bus) const;
  243. void set_bus_mute(int p_bus, bool p_enable);
  244. bool is_bus_mute(int p_bus) const;
  245. void set_bus_bypass_effects(int p_bus, bool p_enable);
  246. bool is_bus_bypassing_effects(int p_bus) const;
  247. void add_bus_effect(int p_bus, const Ref<AudioEffect> &p_effect, int p_at_pos = -1);
  248. void remove_bus_effect(int p_bus, int p_effect);
  249. int get_bus_effect_count(int p_bus);
  250. Ref<AudioEffect> get_bus_effect(int p_bus, int p_effect);
  251. void swap_bus_effects(int p_bus, int p_effect, int p_by_effect);
  252. void set_bus_effect_enabled(int p_bus, int p_effect, bool p_enabled);
  253. bool is_bus_effect_enabled(int p_bus, int p_effect) const;
  254. float get_bus_peak_volume_left_db(int p_bus, int p_channel) const;
  255. float get_bus_peak_volume_right_db(int p_bus, int p_channel) const;
  256. bool is_bus_channel_active(int p_bus, int p_channel) 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_mix_time() const; //useful for video -> audio sync
  269. virtual double get_output_delay() const;
  270. void *audio_data_alloc(uint32_t p_data_len, const uint8_t *p_from_data = NULL);
  271. void audio_data_free(void *p_data);
  272. size_t audio_data_get_total_memory_usage() const;
  273. size_t audio_data_get_max_memory_usage() const;
  274. void add_callback(AudioCallback p_callback, void *p_userdata);
  275. void remove_callback(AudioCallback p_callback, void *p_userdata);
  276. void add_update_callback(AudioCallback p_callback, void *p_userdata);
  277. void remove_update_callback(AudioCallback p_callback, void *p_userdata);
  278. void set_bus_layout(const Ref<AudioBusLayout> &p_bus_layout);
  279. Ref<AudioBusLayout> generate_bus_layout() const;
  280. Array get_device_list();
  281. String get_device();
  282. void set_device(String device);
  283. Array capture_get_device_list();
  284. String capture_get_device();
  285. void capture_set_device(const String &p_name);
  286. float get_output_latency() { return output_latency; }
  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