summaryrefslogtreecommitdiff
path: root/servers/audio/effects/audio_stream_generator.cpp
blob: 46de1692e4230340c526918e49369c6f89cf2fbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*************************************************************************/
/*  audio_stream_generator.cpp                                           */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#include "audio_stream_generator.h"

void AudioStreamGenerator::set_mix_rate(float p_mix_rate) {
	mix_rate = p_mix_rate;
}

float AudioStreamGenerator::get_mix_rate() const {
	return mix_rate;
}

void AudioStreamGenerator::set_buffer_length(float p_seconds) {
	buffer_len = p_seconds;
}

float AudioStreamGenerator::get_buffer_length() const {
	return buffer_len;
}

Ref<AudioStreamPlayback> AudioStreamGenerator::instance_playback() {
	Ref<AudioStreamGeneratorPlayback> playback;
	playback.instantiate();
	playback->generator = this;
	int target_buffer_size = mix_rate * buffer_len;
	playback->buffer.resize(nearest_shift(target_buffer_size));
	playback->buffer.clear();
	return playback;
}

String AudioStreamGenerator::get_stream_name() const {
	return "UserFeed";
}

float AudioStreamGenerator::get_length() const {
	return 0;
}

bool AudioStreamGenerator::is_monophonic() const {
	return true;
}

void AudioStreamGenerator::_bind_methods() {
	ClassDB::bind_method(D_METHOD("set_mix_rate", "hz"), &AudioStreamGenerator::set_mix_rate);
	ClassDB::bind_method(D_METHOD("get_mix_rate"), &AudioStreamGenerator::get_mix_rate);

	ClassDB::bind_method(D_METHOD("set_buffer_length", "seconds"), &AudioStreamGenerator::set_buffer_length);
	ClassDB::bind_method(D_METHOD("get_buffer_length"), &AudioStreamGenerator::get_buffer_length);

	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mix_rate", PROPERTY_HINT_RANGE, "20,192000,1,suffix:Hz"), "set_mix_rate", "get_mix_rate");
	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "buffer_length", PROPERTY_HINT_RANGE, "0.01,10,0.01,suffix:s"), "set_buffer_length", "get_buffer_length");
}

AudioStreamGenerator::AudioStreamGenerator() {
	mix_rate = 44100;
	buffer_len = 0.5;
}

////////////////

bool AudioStreamGeneratorPlayback::push_frame(const Vector2 &p_frame) {
	if (buffer.space_left() < 1) {
		return false;
	}

	AudioFrame f = p_frame;

	buffer.write(&f, 1);
	return true;
}

bool AudioStreamGeneratorPlayback::can_push_buffer(int p_frames) const {
	return buffer.space_left() >= p_frames;
}

bool AudioStreamGeneratorPlayback::push_buffer(const PackedVector2Array &p_frames) {
	int to_write = p_frames.size();
	if (buffer.space_left() < to_write) {
		return false;
	}

	const Vector2 *r = p_frames.ptr();
	if (sizeof(real_t) == 4) {
		//write directly
		buffer.write((const AudioFrame *)r, to_write);
	} else {
		//convert from double
		AudioFrame buf[2048];
		int ofs = 0;
		while (to_write) {
			int w = MIN(to_write, 2048);
			for (int i = 0; i < w; i++) {
				buf[i] = r[i + ofs];
			}
			buffer.write(buf, w);
			ofs += w;
			to_write -= w;
		}
	}
	return true;
}

int AudioStreamGeneratorPlayback::get_frames_available() const {
	return buffer.space_left();
}

int AudioStreamGeneratorPlayback::get_skips() const {
	return skips;
}

void AudioStreamGeneratorPlayback::clear_buffer() {
	ERR_FAIL_COND(active);
	buffer.clear();
	mixed = 0;
}

int AudioStreamGeneratorPlayback::_mix_internal(AudioFrame *p_buffer, int p_frames) {
	int read_amount = buffer.data_left();
	if (p_frames < read_amount) {
		read_amount = p_frames;
	}

	buffer.read(p_buffer, read_amount);

	if (read_amount < p_frames) {
		//skipped, not ideal
		for (int i = read_amount; i < p_frames; i++) {
			p_buffer[i] = AudioFrame(0, 0);
		}

		skips++;
	}

	mixed += p_frames / generator->get_mix_rate();
	return read_amount < p_frames ? read_amount : p_frames;
}

float AudioStreamGeneratorPlayback::get_stream_sampling_rate() {
	return generator->get_mix_rate();
}

void AudioStreamGeneratorPlayback::start(float p_from_pos) {
	if (mixed == 0.0) {
		begin_resample();
	}
	skips = 0;
	active = true;
	mixed = 0.0;
}

void AudioStreamGeneratorPlayback::stop() {
	active = false;
}

bool AudioStreamGeneratorPlayback::is_playing() const {
	return active; //always playing, can't be stopped
}

int AudioStreamGeneratorPlayback::get_loop_count() const {
	return 0;
}

float AudioStreamGeneratorPlayback::get_playback_position() const {
	return mixed;
}

void AudioStreamGeneratorPlayback::seek(float p_time) {
	//no seek possible
}

void AudioStreamGeneratorPlayback::_bind_methods() {
	ClassDB::bind_method(D_METHOD("push_frame", "frame"), &AudioStreamGeneratorPlayback::push_frame);
	ClassDB::bind_method(D_METHOD("can_push_buffer", "amount"), &AudioStreamGeneratorPlayback::can_push_buffer);
	ClassDB::bind_method(D_METHOD("push_buffer", "frames"), &AudioStreamGeneratorPlayback::push_buffer);
	ClassDB::bind_method(D_METHOD("get_frames_available"), &AudioStreamGeneratorPlayback::get_frames_available);
	ClassDB::bind_method(D_METHOD("get_skips"), &AudioStreamGeneratorPlayback::get_skips);
	ClassDB::bind_method(D_METHOD("clear_buffer"), &AudioStreamGeneratorPlayback::clear_buffer);
}

AudioStreamGeneratorPlayback::AudioStreamGeneratorPlayback() {
	generator = nullptr;
	skips = 0;
	active = false;
	mixed = 0;
}