Debian Codegraph @

servers/audio/audio_driver_dummy.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* audio_driver_dummy.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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
35 AudioDriverDummy *AudioDriverDummy::singleton = nullptr;
36
37 Error AudioDriverDummy::init() {
38 active.clear();
39 exit_thread.clear();
40 samples_in = nullptr;
41
42 if (mix_rate == -1) {
43 mix_rate = _get_configured_mix_rate();
44 }
45
46 channels = get_channels();
47 samples_in = memnew_arr(int32_t, size_t(buffer_frames) * channels);
48
49 if (use_threads) {
50 thread.start(AudioDriverDummy::thread_func, this);
51 }
52
53 return OK;
54 }
55
56 void AudioDriverDummy::thread_func(void *p_udata) {
57 AudioDriverDummy *ad = static_cast<AudioDriverDummy *>(p_udata);
58
59 uint64_t usdelay = (ad->buffer_frames / float(ad->mix_rate)) * 1000000;
60
61 while (!ad->exit_thread.is_set()) {
62 if (ad->active.is_set()) {
63 ad->lock();
64 ad->start_counting_ticks();
65
66 ad->audio_server_process(ad->buffer_frames, ad->samples_in);
67
68 ad->stop_counting_ticks();
69 ad->unlock();
70 }
71
72 OS::get_singleton()->delay_usec(usdelay);
73 }
74 }
75
76 void AudioDriverDummy::start() {
77 active.set();
78 }
79
80 int AudioDriverDummy::get_mix_rate() const {
81 return mix_rate;
82 }
83
84 AudioDriver::SpeakerMode AudioDriverDummy::get_speaker_mode() const {
85 return speaker_mode;
86 }
87
88 void AudioDriverDummy::lock() {
89 mutex.lock();
90 }
91
92 void AudioDriverDummy::unlock() {
93 mutex.unlock();
94 }
95
96 void AudioDriverDummy::set_use_threads(bool p_use_threads) {
97 use_threads = p_use_threads;
98 }
99
100 void AudioDriverDummy::set_speaker_mode(SpeakerMode p_mode) {
101 speaker_mode = p_mode;
102 }
103
104 void AudioDriverDummy::set_mix_rate(int p_rate) {
105 mix_rate = p_rate;
106 }
107
108 uint32_t AudioDriverDummy::get_channels() const {
109 static const int channels_for_mode[4] = { 2, 4, 8, 16 };
110 return channels_for_mode[speaker_mode];
111 }
112
113 void AudioDriverDummy::mix_audio(int p_frames, int32_t *p_buffer) {
114 ERR_FAIL_COND(!active.is_set()); // If not active, should not mix.
115 ERR_FAIL_COND(use_threads == true); // If using threads, this will not work well.
116
117 uint32_t todo = p_frames;
118 while (todo) {
119 uint32_t to_mix = MIN(buffer_frames, todo);
120 lock();
121 audio_server_process(to_mix, samples_in);
122 unlock();
123
124 uint32_t total_samples = to_mix * channels;
125
126 for (uint32_t i = 0; i < total_samples; i++) {
127 p_buffer[i] = samples_in[i];
128 }
129
130 todo -= to_mix;
131 p_buffer += total_samples;
132 }
133 }
134
135 void AudioDriverDummy::finish() {
136 if (use_threads) {
137 exit_thread.set();
138 if (thread.is_started()) {
139 thread.wait_to_finish();
140 }
141 }
142
143 if (samples_in) {
144 memdelete_arr(samples_in);
145 }
146 }
147
148 AudioDriverDummy::AudioDriverDummy() {
149 singleton = this;
150 }