1 /*************************************************************************/
2 /*  audio_driver_dummy.cpp                                               */
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 #include "audio_driver_dummy.h"
32 
33 #include "core/os/os.h"
34 #include "core/project_settings.h"
35 
init()36 Error AudioDriverDummy::init() {
37 
38 	active = false;
39 	thread_exited = false;
40 	exit_thread = false;
41 	samples_in = NULL;
42 
43 	mix_rate = GLOBAL_GET("audio/mix_rate");
44 	speaker_mode = SPEAKER_MODE_STEREO;
45 	channels = 2;
46 
47 	int latency = GLOBAL_GET("audio/output_latency");
48 	buffer_frames = closest_power_of_2(latency * mix_rate / 1000);
49 
50 	samples_in = memnew_arr(int32_t, buffer_frames * channels);
51 
52 	mutex = Mutex::create();
53 	thread = Thread::create(AudioDriverDummy::thread_func, this);
54 
55 	return OK;
56 };
57 
thread_func(void * p_udata)58 void AudioDriverDummy::thread_func(void *p_udata) {
59 
60 	AudioDriverDummy *ad = (AudioDriverDummy *)p_udata;
61 
62 	uint64_t usdelay = (ad->buffer_frames / float(ad->mix_rate)) * 1000000;
63 
64 	while (!ad->exit_thread) {
65 
66 		if (ad->active) {
67 
68 			ad->lock();
69 
70 			ad->audio_server_process(ad->buffer_frames, ad->samples_in);
71 
72 			ad->unlock();
73 		};
74 
75 		OS::get_singleton()->delay_usec(usdelay);
76 	};
77 
78 	ad->thread_exited = true;
79 };
80 
start()81 void AudioDriverDummy::start() {
82 
83 	active = true;
84 };
85 
get_mix_rate() const86 int AudioDriverDummy::get_mix_rate() const {
87 
88 	return mix_rate;
89 };
90 
get_speaker_mode() const91 AudioDriver::SpeakerMode AudioDriverDummy::get_speaker_mode() const {
92 
93 	return speaker_mode;
94 };
95 
lock()96 void AudioDriverDummy::lock() {
97 
98 	if (!thread || !mutex)
99 		return;
100 	mutex->lock();
101 };
102 
unlock()103 void AudioDriverDummy::unlock() {
104 
105 	if (!thread || !mutex)
106 		return;
107 	mutex->unlock();
108 };
109 
finish()110 void AudioDriverDummy::finish() {
111 
112 	if (!thread)
113 		return;
114 
115 	exit_thread = true;
116 	Thread::wait_to_finish(thread);
117 
118 	if (samples_in) {
119 		memdelete_arr(samples_in);
120 	};
121 
122 	memdelete(thread);
123 	if (mutex)
124 		memdelete(mutex);
125 	thread = NULL;
126 };
127 
AudioDriverDummy()128 AudioDriverDummy::AudioDriverDummy() {
129 
130 	mutex = NULL;
131 	thread = NULL;
132 };
133 
~AudioDriverDummy()134 AudioDriverDummy::~AudioDriverDummy(){
135 
136 };
137