2023-01-05 12:25:55 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* audio_stream_generator.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2019-06-11 13:31:23 +00:00
|
|
|
|
2019-04-10 15:57:03 +00:00
|
|
|
#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;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2019-04-10 15:57:03 +00:00
|
|
|
float AudioStreamGenerator::get_buffer_length() const {
|
|
|
|
return buffer_len;
|
|
|
|
}
|
|
|
|
|
2022-07-20 23:00:58 +00:00
|
|
|
Ref<AudioStreamPlayback> AudioStreamGenerator::instantiate_playback() {
|
2019-04-10 15:57:03 +00:00
|
|
|
Ref<AudioStreamGeneratorPlayback> playback;
|
2021-06-17 22:03:09 +00:00
|
|
|
playback.instantiate();
|
2019-04-10 15:57:03 +00:00
|
|
|
playback->generator = this;
|
|
|
|
int target_buffer_size = mix_rate * buffer_len;
|
|
|
|
playback->buffer.resize(nearest_shift(target_buffer_size));
|
|
|
|
playback->buffer.clear();
|
|
|
|
return playback;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2019-04-10 15:57:03 +00:00
|
|
|
String AudioStreamGenerator::get_stream_name() const {
|
|
|
|
return "UserFeed";
|
|
|
|
}
|
|
|
|
|
2022-09-22 13:54:15 +00:00
|
|
|
double AudioStreamGenerator::get_length() const {
|
2019-04-10 15:57:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-28 04:51:03 +00:00
|
|
|
bool AudioStreamGenerator::is_monophonic() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-10 15:57:03 +00:00
|
|
|
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);
|
|
|
|
|
2022-05-20 05:24:41 +00:00
|
|
|
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");
|
2019-04-10 15:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
bool AudioStreamGeneratorPlayback::push_buffer(const PackedVector2Array &p_frames) {
|
2019-04-10 15:57:03 +00:00
|
|
|
int to_write = p_frames.size();
|
|
|
|
if (buffer.space_left() < to_write) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
const Vector2 *r = p_frames.ptr();
|
2022-09-29 06:18:07 +00:00
|
|
|
if constexpr (sizeof(real_t) == 4) {
|
2019-04-10 15:57:03 +00:00
|
|
|
//write directly
|
2020-02-17 21:06:54 +00:00
|
|
|
buffer.write((const AudioFrame *)r, to_write);
|
2019-04-10 15:57:03 +00:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2021-08-27 17:27:58 +00:00
|
|
|
int AudioStreamGeneratorPlayback::_mix_internal(AudioFrame *p_buffer, int p_frames) {
|
2023-09-10 07:22:21 +00:00
|
|
|
if (!active) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-10 15:57:03 +00:00
|
|
|
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) {
|
2023-09-10 07:22:21 +00:00
|
|
|
// Fill with zeros as fallback in case of buffer underrun.
|
2019-04-10 15:57:03 +00:00
|
|
|
for (int i = read_amount; i < p_frames; i++) {
|
|
|
|
p_buffer[i] = AudioFrame(0, 0);
|
|
|
|
}
|
|
|
|
skips++;
|
|
|
|
}
|
|
|
|
|
|
|
|
mixed += p_frames / generator->get_mix_rate();
|
2023-09-10 07:22:21 +00:00
|
|
|
return p_frames;
|
2019-04-10 15:57:03 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2019-04-10 15:57:03 +00:00
|
|
|
float AudioStreamGeneratorPlayback::get_stream_sampling_rate() {
|
|
|
|
return generator->get_mix_rate();
|
|
|
|
}
|
|
|
|
|
2022-09-22 13:54:15 +00:00
|
|
|
void AudioStreamGeneratorPlayback::start(double p_from_pos) {
|
2019-04-10 15:57:03 +00:00
|
|
|
if (mixed == 0.0) {
|
2022-03-10 07:17:38 +00:00
|
|
|
begin_resample();
|
2019-04-10 15:57:03 +00:00
|
|
|
}
|
|
|
|
skips = 0;
|
|
|
|
active = true;
|
|
|
|
mixed = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamGeneratorPlayback::stop() {
|
|
|
|
active = false;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2019-04-10 15:57:03 +00:00
|
|
|
bool AudioStreamGeneratorPlayback::is_playing() const {
|
2023-09-10 07:22:21 +00:00
|
|
|
return active;
|
2019-04-10 15:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int AudioStreamGeneratorPlayback::get_loop_count() const {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-22 13:54:15 +00:00
|
|
|
double AudioStreamGeneratorPlayback::get_playback_position() const {
|
2019-04-10 15:57:03 +00:00
|
|
|
return mixed;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-09-22 13:54:15 +00:00
|
|
|
void AudioStreamGeneratorPlayback::seek(double p_time) {
|
2019-04-10 15:57:03 +00:00
|
|
|
//no seek possible
|
|
|
|
}
|
|
|
|
|
2022-07-20 23:00:58 +00:00
|
|
|
void AudioStreamGeneratorPlayback::tag_used_streams() {
|
|
|
|
generator->tag_used(0);
|
|
|
|
}
|
|
|
|
|
2019-04-10 15:57:03 +00:00
|
|
|
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() {
|
2020-04-01 23:20:12 +00:00
|
|
|
generator = nullptr;
|
2019-04-10 15:57:03 +00:00
|
|
|
skips = 0;
|
|
|
|
active = false;
|
|
|
|
mixed = 0;
|
|
|
|
}
|