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