1/**************************************************************************/
2/* audio_stream_generator.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_stream_generator.h"
32
33void AudioStreamGenerator::set_mix_rate(float p_mix_rate) {
34 mix_rate = p_mix_rate;
35}
36
37float AudioStreamGenerator::get_mix_rate() const {
38 return mix_rate;
39}
40
41void AudioStreamGenerator::set_buffer_length(float p_seconds) {
42 buffer_len = p_seconds;
43}
44
45float AudioStreamGenerator::get_buffer_length() const {
46 return buffer_len;
47}
48
49Ref<AudioStreamPlayback> AudioStreamGenerator::instantiate_playback() {
50 Ref<AudioStreamGeneratorPlayback> playback;
51 playback.instantiate();
52 playback->generator = this;
53 int target_buffer_size = mix_rate * buffer_len;
54 playback->buffer.resize(nearest_shift(target_buffer_size));
55 playback->buffer.clear();
56 return playback;
57}
58
59String AudioStreamGenerator::get_stream_name() const {
60 return "UserFeed";
61}
62
63double AudioStreamGenerator::get_length() const {
64 return 0;
65}
66
67bool AudioStreamGenerator::is_monophonic() const {
68 return true;
69}
70
71void AudioStreamGenerator::_bind_methods() {
72 ClassDB::bind_method(D_METHOD("set_mix_rate", "hz"), &AudioStreamGenerator::set_mix_rate);
73 ClassDB::bind_method(D_METHOD("get_mix_rate"), &AudioStreamGenerator::get_mix_rate);
74
75 ClassDB::bind_method(D_METHOD("set_buffer_length", "seconds"), &AudioStreamGenerator::set_buffer_length);
76 ClassDB::bind_method(D_METHOD("get_buffer_length"), &AudioStreamGenerator::get_buffer_length);
77
78 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mix_rate", PROPERTY_HINT_RANGE, "20,192000,1,suffix:Hz"), "set_mix_rate", "get_mix_rate");
79 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "buffer_length", PROPERTY_HINT_RANGE, "0.01,10,0.01,suffix:s"), "set_buffer_length", "get_buffer_length");
80}
81
82AudioStreamGenerator::AudioStreamGenerator() {
83 mix_rate = 44100;
84 buffer_len = 0.5;
85}
86
87////////////////
88
89bool AudioStreamGeneratorPlayback::push_frame(const Vector2 &p_frame) {
90 if (buffer.space_left() < 1) {
91 return false;
92 }
93
94 AudioFrame f = p_frame;
95
96 buffer.write(&f, 1);
97 return true;
98}
99
100bool AudioStreamGeneratorPlayback::can_push_buffer(int p_frames) const {
101 return buffer.space_left() >= p_frames;
102}
103
104bool AudioStreamGeneratorPlayback::push_buffer(const PackedVector2Array &p_frames) {
105 int to_write = p_frames.size();
106 if (buffer.space_left() < to_write) {
107 return false;
108 }
109
110 const Vector2 *r = p_frames.ptr();
111 if constexpr (sizeof(real_t) == 4) {
112 //write directly
113 buffer.write((const AudioFrame *)r, to_write);
114 } else {
115 //convert from double
116 AudioFrame buf[2048];
117 int ofs = 0;
118 while (to_write) {
119 int w = MIN(to_write, 2048);
120 for (int i = 0; i < w; i++) {
121 buf[i] = r[i + ofs];
122 }
123 buffer.write(buf, w);
124 ofs += w;
125 to_write -= w;
126 }
127 }
128 return true;
129}
130
131int AudioStreamGeneratorPlayback::get_frames_available() const {
132 return buffer.space_left();
133}
134
135int AudioStreamGeneratorPlayback::get_skips() const {
136 return skips;
137}
138
139void AudioStreamGeneratorPlayback::clear_buffer() {
140 ERR_FAIL_COND(active);
141 buffer.clear();
142 mixed = 0;
143}
144
145int AudioStreamGeneratorPlayback::_mix_internal(AudioFrame *p_buffer, int p_frames) {
146 int read_amount = buffer.data_left();
147 if (p_frames < read_amount) {
148 read_amount = p_frames;
149 }
150
151 buffer.read(p_buffer, read_amount);
152
153 if (read_amount < p_frames) {
154 //skipped, not ideal
155 for (int i = read_amount; i < p_frames; i++) {
156 p_buffer[i] = AudioFrame(0, 0);
157 }
158
159 skips++;
160 }
161
162 mixed += p_frames / generator->get_mix_rate();
163 return read_amount < p_frames ? read_amount : p_frames;
164}
165
166float AudioStreamGeneratorPlayback::get_stream_sampling_rate() {
167 return generator->get_mix_rate();
168}
169
170void AudioStreamGeneratorPlayback::start(double p_from_pos) {
171 if (mixed == 0.0) {
172 begin_resample();
173 }
174 skips = 0;
175 active = true;
176 mixed = 0.0;
177}
178
179void AudioStreamGeneratorPlayback::stop() {
180 active = false;
181}
182
183bool AudioStreamGeneratorPlayback::is_playing() const {
184 return active; //always playing, can't be stopped
185}
186
187int AudioStreamGeneratorPlayback::get_loop_count() const {
188 return 0;
189}
190
191double AudioStreamGeneratorPlayback::get_playback_position() const {
192 return mixed;
193}
194
195void AudioStreamGeneratorPlayback::seek(double p_time) {
196 //no seek possible
197}
198
199void AudioStreamGeneratorPlayback::tag_used_streams() {
200 generator->tag_used(0);
201}
202
203void AudioStreamGeneratorPlayback::_bind_methods() {
204 ClassDB::bind_method(D_METHOD("push_frame", "frame"), &AudioStreamGeneratorPlayback::push_frame);
205 ClassDB::bind_method(D_METHOD("can_push_buffer", "amount"), &AudioStreamGeneratorPlayback::can_push_buffer);
206 ClassDB::bind_method(D_METHOD("push_buffer", "frames"), &AudioStreamGeneratorPlayback::push_buffer);
207 ClassDB::bind_method(D_METHOD("get_frames_available"), &AudioStreamGeneratorPlayback::get_frames_available);
208 ClassDB::bind_method(D_METHOD("get_skips"), &AudioStreamGeneratorPlayback::get_skips);
209 ClassDB::bind_method(D_METHOD("clear_buffer"), &AudioStreamGeneratorPlayback::clear_buffer);
210}
211
212AudioStreamGeneratorPlayback::AudioStreamGeneratorPlayback() {
213 generator = nullptr;
214 skips = 0;
215 active = false;
216 mixed = 0;
217}
218