2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* audio_stream.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2022-01-03 20:27:34 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "audio_stream.h"
|
2020-01-26 10:24:14 +00:00
|
|
|
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/config/project_settings.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/os.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-08-23 17:53:27 +00:00
|
|
|
void AudioStreamPlayback::start(float p_from_pos) {
|
|
|
|
if (GDVIRTUAL_CALL(_start, p_from_pos)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ERR_FAIL_MSG("AudioStreamPlayback::start unimplemented!");
|
|
|
|
}
|
|
|
|
void AudioStreamPlayback::stop() {
|
|
|
|
if (GDVIRTUAL_CALL(_stop)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ERR_FAIL_MSG("AudioStreamPlayback::stop unimplemented!");
|
|
|
|
}
|
|
|
|
bool AudioStreamPlayback::is_playing() const {
|
|
|
|
bool ret;
|
|
|
|
if (GDVIRTUAL_CALL(_is_playing, ret)) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
ERR_FAIL_V_MSG(false, "AudioStreamPlayback::is_playing unimplemented!");
|
|
|
|
}
|
|
|
|
|
|
|
|
int AudioStreamPlayback::get_loop_count() const {
|
|
|
|
int ret;
|
|
|
|
if (GDVIRTUAL_CALL(_get_loop_count, ret)) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float AudioStreamPlayback::get_playback_position() const {
|
|
|
|
float ret;
|
|
|
|
if (GDVIRTUAL_CALL(_get_playback_position, ret)) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
ERR_FAIL_V_MSG(0, "AudioStreamPlayback::get_playback_position unimplemented!");
|
|
|
|
}
|
|
|
|
void AudioStreamPlayback::seek(float p_time) {
|
|
|
|
if (GDVIRTUAL_CALL(_seek, p_time)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 17:27:58 +00:00
|
|
|
int AudioStreamPlayback::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
|
|
|
|
int ret;
|
2022-03-10 07:17:38 +00:00
|
|
|
if (GDVIRTUAL_REQUIRED_CALL(_mix, p_buffer, p_rate_scale, p_frames, ret)) {
|
2021-08-27 17:27:58 +00:00
|
|
|
return ret;
|
2021-08-23 17:53:27 +00:00
|
|
|
}
|
2022-03-10 07:17:38 +00:00
|
|
|
|
2021-08-27 17:27:58 +00:00
|
|
|
return 0;
|
2021-08-23 17:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlayback::_bind_methods() {
|
|
|
|
GDVIRTUAL_BIND(_start, "from_pos")
|
|
|
|
GDVIRTUAL_BIND(_stop)
|
|
|
|
GDVIRTUAL_BIND(_is_playing)
|
|
|
|
GDVIRTUAL_BIND(_get_loop_count)
|
|
|
|
GDVIRTUAL_BIND(_get_playback_position)
|
|
|
|
GDVIRTUAL_BIND(_seek, "position")
|
|
|
|
GDVIRTUAL_BIND(_mix, "buffer", "rate_scale", "frames");
|
|
|
|
}
|
2015-09-09 21:50:52 +00:00
|
|
|
//////////////////////////////
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2022-03-10 07:17:38 +00:00
|
|
|
void AudioStreamPlaybackResampled::begin_resample() {
|
2017-01-21 22:00:25 +00:00
|
|
|
//clear cubic interpolation history
|
2017-03-05 15:44:50 +00:00
|
|
|
internal_buffer[0] = AudioFrame(0.0, 0.0);
|
|
|
|
internal_buffer[1] = AudioFrame(0.0, 0.0);
|
|
|
|
internal_buffer[2] = AudioFrame(0.0, 0.0);
|
|
|
|
internal_buffer[3] = AudioFrame(0.0, 0.0);
|
2017-01-21 22:00:25 +00:00
|
|
|
//mix buffer
|
2017-03-05 15:44:50 +00:00
|
|
|
_mix_internal(internal_buffer + 4, INTERNAL_BUFFER_LEN);
|
|
|
|
mix_offset = 0;
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2022-03-10 07:17:38 +00:00
|
|
|
int AudioStreamPlaybackResampled::_mix_internal(AudioFrame *p_buffer, int p_frames) {
|
|
|
|
int ret;
|
|
|
|
if (GDVIRTUAL_REQUIRED_CALL(_mix_resampled, p_buffer, p_frames, ret)) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
float AudioStreamPlaybackResampled::get_stream_sampling_rate() {
|
|
|
|
float ret;
|
|
|
|
if (GDVIRTUAL_REQUIRED_CALL(_get_stream_sampling_rate, ret)) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlaybackResampled::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("begin_resample"), &AudioStreamPlaybackResampled::begin_resample);
|
|
|
|
|
|
|
|
GDVIRTUAL_BIND(_mix_resampled, "dst_buffer", "frame_count");
|
|
|
|
GDVIRTUAL_BIND(_get_stream_sampling_rate);
|
|
|
|
}
|
|
|
|
|
2021-08-27 17:27:58 +00:00
|
|
|
int AudioStreamPlaybackResampled::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
|
2018-01-01 21:23:16 +00:00
|
|
|
float target_rate = AudioServer::get_singleton()->get_mix_rate();
|
2020-05-07 16:46:33 +00:00
|
|
|
float playback_speed_scale = AudioServer::get_singleton()->get_playback_speed_scale();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-07 16:46:33 +00:00
|
|
|
uint64_t mix_increment = uint64_t(((get_stream_sampling_rate() * p_rate_scale * playback_speed_scale) / double(target_rate)) * double(FP_LEN));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-08-27 17:27:58 +00:00
|
|
|
int mixed_frames_total = p_frames;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < p_frames; i++) {
|
2017-01-21 22:00:25 +00:00
|
|
|
uint32_t idx = CUBIC_INTERP_HISTORY + uint32_t(mix_offset >> FP_BITS);
|
2021-07-30 15:07:48 +00:00
|
|
|
//standard cubic interpolation (great quality/performance ratio)
|
|
|
|
//this used to be moved to a LUT for greater performance, but nowadays CPU speed is generally faster than memory.
|
2017-03-05 15:44:50 +00:00
|
|
|
float mu = (mix_offset & FP_MASK) / float(FP_LEN);
|
|
|
|
AudioFrame y0 = internal_buffer[idx - 3];
|
|
|
|
AudioFrame y1 = internal_buffer[idx - 2];
|
|
|
|
AudioFrame y2 = internal_buffer[idx - 1];
|
|
|
|
AudioFrame y3 = internal_buffer[idx - 0];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-08-27 17:27:58 +00:00
|
|
|
if (idx <= internal_buffer_end && idx >= internal_buffer_end && mixed_frames_total == p_frames) {
|
|
|
|
// The internal buffer ends somewhere in this range, and we haven't yet recorded the number of good frames we have.
|
|
|
|
mixed_frames_total = i;
|
|
|
|
}
|
|
|
|
|
2021-07-30 15:07:48 +00:00
|
|
|
float mu2 = mu * mu;
|
2021-07-30 20:20:25 +00:00
|
|
|
AudioFrame a0 = 3 * y1 - 3 * y2 + y3 - y0;
|
|
|
|
AudioFrame a1 = 2 * y0 - 5 * y1 + 4 * y2 - y3;
|
2021-07-30 15:07:48 +00:00
|
|
|
AudioFrame a2 = y2 - y0;
|
2021-07-30 20:20:25 +00:00
|
|
|
AudioFrame a3 = 2 * y1;
|
2021-07-30 15:07:48 +00:00
|
|
|
|
2021-07-30 20:20:25 +00:00
|
|
|
p_buffer[i] = (a0 * mu * mu2 + a1 * mu2 + a2 * mu + a3) / 2;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
mix_offset += mix_increment;
|
2017-01-21 22:00:25 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while ((mix_offset >> FP_BITS) >= INTERNAL_BUFFER_LEN) {
|
|
|
|
internal_buffer[0] = internal_buffer[INTERNAL_BUFFER_LEN + 0];
|
|
|
|
internal_buffer[1] = internal_buffer[INTERNAL_BUFFER_LEN + 1];
|
|
|
|
internal_buffer[2] = internal_buffer[INTERNAL_BUFFER_LEN + 2];
|
|
|
|
internal_buffer[3] = internal_buffer[INTERNAL_BUFFER_LEN + 3];
|
2017-12-20 16:55:30 +00:00
|
|
|
if (is_playing()) {
|
2021-08-27 17:27:58 +00:00
|
|
|
int mixed_frames = _mix_internal(internal_buffer + 4, INTERNAL_BUFFER_LEN);
|
|
|
|
if (mixed_frames != INTERNAL_BUFFER_LEN) {
|
|
|
|
// internal_buffer[mixed_frames] is the first frame of silence.
|
|
|
|
internal_buffer_end = mixed_frames;
|
|
|
|
} else {
|
|
|
|
// The internal buffer does not contain the first frame of silence.
|
|
|
|
internal_buffer_end = -1;
|
|
|
|
}
|
2017-12-20 16:55:30 +00:00
|
|
|
} else {
|
|
|
|
//fill with silence, not playing
|
2019-02-12 20:10:08 +00:00
|
|
|
for (int j = 0; j < INTERNAL_BUFFER_LEN; ++j) {
|
|
|
|
internal_buffer[j + 4] = AudioFrame(0, 0);
|
2017-12-17 21:31:35 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
mix_offset -= (INTERNAL_BUFFER_LEN << FP_BITS);
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-27 17:27:58 +00:00
|
|
|
return mixed_frames_total;
|
2017-01-21 22:00:25 +00:00
|
|
|
}
|
2018-06-07 15:46:14 +00:00
|
|
|
|
2017-07-15 04:23:10 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
2021-08-23 17:53:27 +00:00
|
|
|
Ref<AudioStreamPlayback> AudioStream::instance_playback() {
|
|
|
|
Ref<AudioStreamPlayback> ret;
|
|
|
|
if (GDVIRTUAL_CALL(_instance_playback, ret)) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
ERR_FAIL_V_MSG(Ref<AudioStreamPlayback>(), "Method must be implemented!");
|
|
|
|
}
|
|
|
|
String AudioStream::get_stream_name() const {
|
|
|
|
String ret;
|
|
|
|
if (GDVIRTUAL_CALL(_get_stream_name, ret)) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
|
|
|
|
float AudioStream::get_length() const {
|
|
|
|
float ret;
|
|
|
|
if (GDVIRTUAL_CALL(_get_length, ret)) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-28 04:51:03 +00:00
|
|
|
bool AudioStream::is_monophonic() const {
|
|
|
|
bool ret;
|
|
|
|
if (GDVIRTUAL_CALL(_is_monophonic, ret)) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-22 19:35:33 +00:00
|
|
|
void AudioStream::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_length"), &AudioStream::get_length);
|
2021-08-28 04:51:03 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("is_monophonic"), &AudioStream::is_monophonic);
|
2022-02-11 05:01:04 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("instance_playback"), &AudioStream::instance_playback);
|
2021-08-23 17:53:27 +00:00
|
|
|
GDVIRTUAL_BIND(_instance_playback);
|
|
|
|
GDVIRTUAL_BIND(_get_stream_name);
|
|
|
|
GDVIRTUAL_BIND(_get_length);
|
2021-08-28 04:51:03 +00:00
|
|
|
GDVIRTUAL_BIND(_is_monophonic);
|
2018-01-22 19:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
|
2018-02-27 07:54:56 +00:00
|
|
|
Ref<AudioStreamPlayback> AudioStreamMicrophone::instance_playback() {
|
|
|
|
Ref<AudioStreamPlaybackMicrophone> playback;
|
2021-06-17 22:03:09 +00:00
|
|
|
playback.instantiate();
|
2018-02-27 07:54:56 +00:00
|
|
|
|
|
|
|
playbacks.insert(playback.ptr());
|
|
|
|
|
|
|
|
playback->microphone = Ref<AudioStreamMicrophone>((AudioStreamMicrophone *)this);
|
|
|
|
playback->active = false;
|
|
|
|
|
|
|
|
return playback;
|
|
|
|
}
|
|
|
|
|
|
|
|
String AudioStreamMicrophone::get_stream_name() const {
|
|
|
|
//if (audio_stream.is_valid()) {
|
|
|
|
//return "Random: " + audio_stream->get_name();
|
|
|
|
//}
|
|
|
|
return "Microphone";
|
|
|
|
}
|
|
|
|
|
|
|
|
float AudioStreamMicrophone::get_length() const {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-28 04:51:03 +00:00
|
|
|
bool AudioStreamMicrophone::is_monophonic() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-27 07:54:56 +00:00
|
|
|
void AudioStreamMicrophone::_bind_methods() {
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioStreamMicrophone::AudioStreamMicrophone() {
|
|
|
|
}
|
|
|
|
|
2021-08-27 17:27:58 +00:00
|
|
|
int AudioStreamPlaybackMicrophone::_mix_internal(AudioFrame *p_buffer, int p_frames) {
|
2020-01-20 12:11:47 +00:00
|
|
|
AudioDriver::get_singleton()->lock();
|
2018-07-04 01:08:43 +00:00
|
|
|
|
2020-01-20 12:11:47 +00:00
|
|
|
Vector<int32_t> buf = AudioDriver::get_singleton()->get_input_buffer();
|
|
|
|
unsigned int input_size = AudioDriver::get_singleton()->get_input_size();
|
|
|
|
int mix_rate = AudioDriver::get_singleton()->get_mix_rate();
|
|
|
|
unsigned int playback_delay = MIN(((50 * mix_rate) / 1000) * 2, buf.size() >> 1);
|
2018-10-19 19:32:03 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
2020-01-20 12:11:47 +00:00
|
|
|
unsigned int input_position = AudioDriver::get_singleton()->get_input_position();
|
2018-10-19 19:32:03 +00:00
|
|
|
#endif
|
2018-02-27 07:54:56 +00:00
|
|
|
|
2021-08-27 17:27:58 +00:00
|
|
|
int mixed_frames = p_frames;
|
|
|
|
|
2020-01-20 12:11:47 +00:00
|
|
|
if (playback_delay > input_size) {
|
2018-07-27 02:47:22 +00:00
|
|
|
for (int i = 0; i < p_frames; i++) {
|
|
|
|
p_buffer[i] = AudioFrame(0.0f, 0.0f);
|
2018-02-27 07:54:56 +00:00
|
|
|
}
|
2020-01-20 12:11:47 +00:00
|
|
|
input_ofs = 0;
|
2018-07-27 02:47:22 +00:00
|
|
|
} else {
|
|
|
|
for (int i = 0; i < p_frames; i++) {
|
2020-01-20 12:11:47 +00:00
|
|
|
if (input_size > input_ofs && (int)input_ofs < buf.size()) {
|
|
|
|
float l = (buf[input_ofs++] >> 16) / 32768.f;
|
|
|
|
if ((int)input_ofs >= buf.size()) {
|
|
|
|
input_ofs = 0;
|
2018-07-27 02:47:22 +00:00
|
|
|
}
|
2020-01-20 12:11:47 +00:00
|
|
|
float r = (buf[input_ofs++] >> 16) / 32768.f;
|
|
|
|
if ((int)input_ofs >= buf.size()) {
|
|
|
|
input_ofs = 0;
|
2018-07-27 02:47:22 +00:00
|
|
|
}
|
2018-07-04 01:08:43 +00:00
|
|
|
|
2018-07-27 02:47:22 +00:00
|
|
|
p_buffer[i] = AudioFrame(l, r);
|
|
|
|
} else {
|
2021-08-27 17:27:58 +00:00
|
|
|
if (mixed_frames == p_frames) {
|
|
|
|
mixed_frames = i;
|
|
|
|
}
|
2018-07-27 02:47:22 +00:00
|
|
|
p_buffer[i] = AudioFrame(0.0f, 0.0f);
|
|
|
|
}
|
|
|
|
}
|
2018-02-27 07:54:56 +00:00
|
|
|
}
|
2018-07-04 01:08:43 +00:00
|
|
|
|
2018-10-19 19:32:03 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
2020-01-20 12:11:47 +00:00
|
|
|
if (input_ofs > input_position && (int)(input_ofs - input_position) < (p_frames * 2)) {
|
|
|
|
print_verbose(String(get_class_name()) + " buffer underrun: input_position=" + itos(input_position) + " input_ofs=" + itos(input_ofs) + " input_size=" + itos(input_size));
|
2018-10-19 19:32:03 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-01-20 12:11:47 +00:00
|
|
|
AudioDriver::get_singleton()->unlock();
|
2021-08-27 17:27:58 +00:00
|
|
|
|
|
|
|
return mixed_frames;
|
2018-02-27 07:54:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-27 17:27:58 +00:00
|
|
|
int AudioStreamPlaybackMicrophone::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
|
|
|
|
return AudioStreamPlaybackResampled::mix(p_buffer, p_rate_scale, p_frames);
|
2018-02-27 07:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float AudioStreamPlaybackMicrophone::get_stream_sampling_rate() {
|
2018-07-04 01:08:43 +00:00
|
|
|
return AudioDriver::get_singleton()->get_mix_rate();
|
2018-02-27 07:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlaybackMicrophone::start(float p_from_pos) {
|
2019-03-01 12:33:15 +00:00
|
|
|
if (active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:44:49 +00:00
|
|
|
if (!GLOBAL_GET("audio/driver/enable_input")) {
|
2019-11-07 08:44:15 +00:00
|
|
|
WARN_PRINT("Need to enable Project settings > Audio > Enable Audio Input option to use capturing.");
|
2018-12-13 15:35:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-20 12:11:47 +00:00
|
|
|
input_ofs = 0;
|
2018-02-27 07:54:56 +00:00
|
|
|
|
2020-01-20 12:11:47 +00:00
|
|
|
if (AudioDriver::get_singleton()->capture_start() == OK) {
|
2019-03-01 12:33:15 +00:00
|
|
|
active = true;
|
2022-03-10 07:17:38 +00:00
|
|
|
begin_resample();
|
2019-03-01 12:33:15 +00:00
|
|
|
}
|
2018-02-27 07:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlaybackMicrophone::stop() {
|
2019-03-01 12:33:15 +00:00
|
|
|
if (active) {
|
2020-01-20 12:11:47 +00:00
|
|
|
AudioDriver::get_singleton()->capture_stop();
|
2019-03-01 12:33:15 +00:00
|
|
|
active = false;
|
|
|
|
}
|
2018-02-27 07:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioStreamPlaybackMicrophone::is_playing() const {
|
|
|
|
return active;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AudioStreamPlaybackMicrophone::get_loop_count() const {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float AudioStreamPlaybackMicrophone::get_playback_position() const {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamPlaybackMicrophone::seek(float p_time) {
|
2019-06-26 13:08:25 +00:00
|
|
|
// Can't seek a microphone input
|
2018-02-27 07:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioStreamPlaybackMicrophone::~AudioStreamPlaybackMicrophone() {
|
|
|
|
microphone->playbacks.erase(this);
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioStreamPlaybackMicrophone::AudioStreamPlaybackMicrophone() {
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
void AudioStreamRandomizer::add_stream(int p_index) {
|
|
|
|
if (p_index < 0) {
|
|
|
|
p_index = audio_stream_pool.size();
|
|
|
|
}
|
|
|
|
ERR_FAIL_COND(p_index > audio_stream_pool.size());
|
|
|
|
PoolEntry entry{ nullptr, 1.0f };
|
|
|
|
audio_stream_pool.insert(p_index, entry);
|
|
|
|
emit_signal(SNAME("changed"));
|
|
|
|
notify_property_list_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamRandomizer::move_stream(int p_index_from, int p_index_to) {
|
|
|
|
ERR_FAIL_COND(p_index_from < 0);
|
|
|
|
ERR_FAIL_COND(p_index_from >= audio_stream_pool.size());
|
|
|
|
ERR_FAIL_COND(p_index_to < 0);
|
|
|
|
ERR_FAIL_COND(p_index_to > audio_stream_pool.size());
|
|
|
|
audio_stream_pool.insert(p_index_to, audio_stream_pool[p_index_from]);
|
|
|
|
// If 'from' is strictly after 'to' we need to increment the index by one because of the insertion.
|
|
|
|
if (p_index_from > p_index_to) {
|
|
|
|
p_index_from++;
|
2017-07-15 04:23:10 +00:00
|
|
|
}
|
2021-09-12 06:57:09 +00:00
|
|
|
audio_stream_pool.remove_at(p_index_from);
|
|
|
|
emit_signal(SNAME("changed"));
|
|
|
|
notify_property_list_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamRandomizer::remove_stream(int p_index) {
|
|
|
|
ERR_FAIL_COND(p_index < 0);
|
|
|
|
ERR_FAIL_COND(p_index >= audio_stream_pool.size());
|
|
|
|
audio_stream_pool.remove_at(p_index);
|
|
|
|
emit_signal(SNAME("changed"));
|
|
|
|
notify_property_list_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamRandomizer::set_stream(int p_index, Ref<AudioStream> p_stream) {
|
|
|
|
ERR_FAIL_COND(p_index < 0);
|
|
|
|
ERR_FAIL_COND(p_index >= audio_stream_pool.size());
|
|
|
|
audio_stream_pool.write[p_index].stream = p_stream;
|
|
|
|
emit_signal(SNAME("changed"));
|
2017-07-15 04:23:10 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
Ref<AudioStream> AudioStreamRandomizer::get_stream(int p_index) const {
|
|
|
|
ERR_FAIL_COND_V(p_index < 0, nullptr);
|
|
|
|
ERR_FAIL_COND_V(p_index >= audio_stream_pool.size(), nullptr);
|
|
|
|
return audio_stream_pool[p_index].stream;
|
2017-07-15 04:23:10 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
void AudioStreamRandomizer::set_stream_probability_weight(int p_index, float p_weight) {
|
|
|
|
ERR_FAIL_COND(p_index < 0);
|
|
|
|
ERR_FAIL_COND(p_index >= audio_stream_pool.size());
|
|
|
|
audio_stream_pool.write[p_index].weight = p_weight;
|
|
|
|
emit_signal(SNAME("changed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
float AudioStreamRandomizer::get_stream_probability_weight(int p_index) const {
|
|
|
|
ERR_FAIL_COND_V(p_index < 0, 0);
|
|
|
|
ERR_FAIL_COND_V(p_index >= audio_stream_pool.size(), 0);
|
|
|
|
return audio_stream_pool[p_index].weight;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamRandomizer::set_streams_count(int p_count) {
|
|
|
|
audio_stream_pool.resize(p_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
int AudioStreamRandomizer::get_streams_count() const {
|
|
|
|
return audio_stream_pool.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamRandomizer::set_random_pitch(float p_pitch) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_pitch < 1) {
|
2017-07-15 04:23:10 +00:00
|
|
|
p_pitch = 1;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-09-12 06:57:09 +00:00
|
|
|
random_pitch_scale = p_pitch;
|
|
|
|
}
|
|
|
|
|
|
|
|
float AudioStreamRandomizer::get_random_pitch() const {
|
|
|
|
return random_pitch_scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamRandomizer::set_random_volume_offset_db(float p_volume_offset_db) {
|
|
|
|
if (p_volume_offset_db < 0) {
|
|
|
|
p_volume_offset_db = 0;
|
|
|
|
}
|
|
|
|
random_volume_offset_db = p_volume_offset_db;
|
|
|
|
}
|
|
|
|
|
|
|
|
float AudioStreamRandomizer::get_random_volume_offset_db() const {
|
|
|
|
return random_volume_offset_db;
|
2017-07-15 04:23:10 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
void AudioStreamRandomizer::set_playback_mode(PlaybackMode p_playback_mode) {
|
|
|
|
playback_mode = p_playback_mode;
|
2017-07-15 04:23:10 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
AudioStreamRandomizer::PlaybackMode AudioStreamRandomizer::get_playback_mode() const {
|
|
|
|
return playback_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<AudioStreamPlayback> AudioStreamRandomizer::instance_playback_random() {
|
|
|
|
Ref<AudioStreamPlaybackRandomizer> playback;
|
2021-06-17 22:03:09 +00:00
|
|
|
playback.instantiate();
|
2021-09-12 06:57:09 +00:00
|
|
|
playbacks.insert(playback.ptr());
|
|
|
|
playback->randomizer = Ref<AudioStreamRandomizer>((AudioStreamRandomizer *)this);
|
|
|
|
|
|
|
|
double total_weight = 0;
|
|
|
|
Vector<PoolEntry> local_pool;
|
|
|
|
for (const PoolEntry &entry : audio_stream_pool) {
|
|
|
|
if (entry.stream.is_valid() && entry.weight > 0) {
|
|
|
|
local_pool.push_back(entry);
|
|
|
|
total_weight += entry.weight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (local_pool.is_empty()) {
|
|
|
|
return playback;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-09-12 06:57:09 +00:00
|
|
|
double chosen_cumulative_weight = Math::random(0.0, total_weight);
|
|
|
|
double cumulative_weight = 0;
|
|
|
|
for (PoolEntry &entry : local_pool) {
|
|
|
|
cumulative_weight += entry.weight;
|
|
|
|
if (cumulative_weight > chosen_cumulative_weight) {
|
|
|
|
playback->playback = entry.stream->instance_playback();
|
|
|
|
last_playback = entry.stream;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (playback->playback.is_null()) {
|
|
|
|
// This indicates a floating point error. Take the last element.
|
|
|
|
last_playback = local_pool[local_pool.size() - 1].stream;
|
|
|
|
playback->playback = local_pool.write[local_pool.size() - 1].stream->instance_playback();
|
|
|
|
}
|
|
|
|
return playback;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<AudioStreamPlayback> AudioStreamRandomizer::instance_playback_no_repeats() {
|
|
|
|
Ref<AudioStreamPlaybackRandomizer> playback;
|
2017-07-15 04:23:10 +00:00
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
double total_weight = 0;
|
|
|
|
Vector<PoolEntry> local_pool;
|
|
|
|
for (const PoolEntry &entry : audio_stream_pool) {
|
|
|
|
if (entry.stream == last_playback) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (entry.stream.is_valid() && entry.weight > 0) {
|
|
|
|
local_pool.push_back(entry);
|
|
|
|
total_weight += entry.weight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (local_pool.is_empty()) {
|
|
|
|
playback = instance_playback_random();
|
|
|
|
WARN_PRINT("Playback stream pool is too small to prevent repeats.");
|
|
|
|
return playback;
|
|
|
|
}
|
|
|
|
|
|
|
|
playback.instantiate();
|
2017-07-15 04:23:10 +00:00
|
|
|
playbacks.insert(playback.ptr());
|
2021-09-12 06:57:09 +00:00
|
|
|
playback->randomizer = Ref<AudioStreamRandomizer>((AudioStreamRandomizer *)this);
|
|
|
|
double chosen_cumulative_weight = Math::random(0.0, total_weight);
|
|
|
|
double cumulative_weight = 0;
|
|
|
|
for (PoolEntry &entry : local_pool) {
|
|
|
|
cumulative_weight += entry.weight;
|
|
|
|
if (cumulative_weight > chosen_cumulative_weight) {
|
|
|
|
last_playback = entry.stream;
|
|
|
|
playback->playback = entry.stream->instance_playback();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (playback->playback.is_null()) {
|
|
|
|
// This indicates a floating point error. Take the last element.
|
|
|
|
last_playback = local_pool[local_pool.size() - 1].stream;
|
|
|
|
playback->playback = local_pool.write[local_pool.size() - 1].stream->instance_playback();
|
|
|
|
}
|
2017-07-15 04:23:10 +00:00
|
|
|
return playback;
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
Ref<AudioStreamPlayback> AudioStreamRandomizer::instance_playback_sequential() {
|
|
|
|
Ref<AudioStreamPlaybackRandomizer> playback;
|
|
|
|
playback.instantiate();
|
|
|
|
playbacks.insert(playback.ptr());
|
|
|
|
playback->randomizer = Ref<AudioStreamRandomizer>((AudioStreamRandomizer *)this);
|
|
|
|
|
|
|
|
Vector<Ref<AudioStream>> local_pool;
|
|
|
|
for (const PoolEntry &entry : audio_stream_pool) {
|
|
|
|
if (entry.stream.is_null()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (local_pool.find(entry.stream) != -1) {
|
|
|
|
WARN_PRINT("Duplicate stream in sequential playback pool");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
local_pool.push_back(entry.stream);
|
|
|
|
}
|
|
|
|
if (local_pool.is_empty()) {
|
|
|
|
return playback;
|
2017-07-15 04:23:10 +00:00
|
|
|
}
|
2021-09-12 06:57:09 +00:00
|
|
|
bool found_last_stream = false;
|
|
|
|
for (Ref<AudioStream> &entry : local_pool) {
|
|
|
|
if (found_last_stream) {
|
|
|
|
last_playback = entry;
|
|
|
|
playback->playback = entry->instance_playback();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (entry == last_playback) {
|
|
|
|
found_last_stream = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (playback->playback.is_null()) {
|
|
|
|
// Wrap around
|
|
|
|
last_playback = local_pool[0];
|
|
|
|
playback->playback = local_pool.write[0]->instance_playback();
|
|
|
|
}
|
|
|
|
return playback;
|
2017-07-15 04:23:10 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
Ref<AudioStreamPlayback> AudioStreamRandomizer::instance_playback() {
|
|
|
|
switch (playback_mode) {
|
|
|
|
case PLAYBACK_RANDOM:
|
|
|
|
return instance_playback_random();
|
|
|
|
case PLAYBACK_RANDOM_NO_REPEATS:
|
|
|
|
return instance_playback_no_repeats();
|
|
|
|
case PLAYBACK_SEQUENTIAL:
|
|
|
|
return instance_playback_sequential();
|
|
|
|
default:
|
|
|
|
ERR_FAIL_V_MSG(nullptr, "Unhandled playback mode.");
|
2018-01-22 19:35:33 +00:00
|
|
|
}
|
2021-09-12 06:57:09 +00:00
|
|
|
}
|
2018-01-22 19:35:33 +00:00
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
String AudioStreamRandomizer::get_stream_name() const {
|
|
|
|
return "Randomizer";
|
|
|
|
}
|
|
|
|
|
|
|
|
float AudioStreamRandomizer::get_length() const {
|
2018-01-22 19:35:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
bool AudioStreamRandomizer::is_monophonic() const {
|
|
|
|
for (const PoolEntry &entry : audio_stream_pool) {
|
|
|
|
if (entry.stream.is_valid() && entry.stream->is_monophonic()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioStreamRandomizer::_get(const StringName &p_name, Variant &r_ret) const {
|
|
|
|
if (AudioStream::_get(p_name, r_ret)) {
|
|
|
|
return true;
|
2021-08-28 04:51:03 +00:00
|
|
|
}
|
2021-09-12 06:57:09 +00:00
|
|
|
Vector<String> components = String(p_name).split("/", true, 2);
|
|
|
|
if (components.size() == 2 && components[0].begins_with("stream_") && components[0].trim_prefix("stream_").is_valid_int()) {
|
|
|
|
int index = components[0].trim_prefix("stream_").to_int();
|
|
|
|
if (index < 0 || index >= (int)audio_stream_pool.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-08-28 04:51:03 +00:00
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
if (components[1] == "stream") {
|
|
|
|
r_ret = get_stream(index);
|
|
|
|
return true;
|
|
|
|
} else if (components[1] == "weight") {
|
|
|
|
r_ret = get_stream_probability_weight(index);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioStreamRandomizer::_set(const StringName &p_name, const Variant &p_value) {
|
|
|
|
if (AudioStream::_set(p_name, p_value)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Vector<String> components = String(p_name).split("/", true, 2);
|
|
|
|
if (components.size() == 2 && components[0].begins_with("stream_") && components[0].trim_prefix("stream_").is_valid_int()) {
|
|
|
|
int index = components[0].trim_prefix("stream_").to_int();
|
|
|
|
if (index < 0 || index >= (int)audio_stream_pool.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (components[1] == "stream") {
|
|
|
|
set_stream(index, p_value);
|
|
|
|
return true;
|
|
|
|
} else if (components[1] == "weight") {
|
|
|
|
set_stream_probability_weight(index, p_value);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamRandomizer::_get_property_list(List<PropertyInfo> *p_list) const {
|
|
|
|
AudioStream::_get_property_list(p_list); // Define the trivial scalar properties.
|
|
|
|
p_list->push_back(PropertyInfo(Variant::NIL, "Streams", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
|
|
|
|
for (int i = 0; i < audio_stream_pool.size(); i++) {
|
|
|
|
p_list->push_back(PropertyInfo(Variant::OBJECT, vformat("stream_%d/stream", i), PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::FLOAT, vformat("stream_%d/weight", i), PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"));
|
|
|
|
}
|
2021-08-28 04:51:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
void AudioStreamRandomizer::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("add_stream", "index"), &AudioStreamRandomizer::add_stream);
|
|
|
|
ClassDB::bind_method(D_METHOD("move_stream", "index_from", "index_to"), &AudioStreamRandomizer::move_stream);
|
|
|
|
ClassDB::bind_method(D_METHOD("remove_stream", "index"), &AudioStreamRandomizer::remove_stream);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_stream", "index", "stream"), &AudioStreamRandomizer::set_stream);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_stream", "index"), &AudioStreamRandomizer::get_stream);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_stream_probability_weight", "index", "weight"), &AudioStreamRandomizer::set_stream_probability_weight);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_stream_probability_weight", "index"), &AudioStreamRandomizer::get_stream_probability_weight);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_streams_count", "count"), &AudioStreamRandomizer::set_streams_count);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_streams_count"), &AudioStreamRandomizer::get_streams_count);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_random_pitch", "scale"), &AudioStreamRandomizer::set_random_pitch);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_random_pitch"), &AudioStreamRandomizer::get_random_pitch);
|
2017-07-15 04:23:10 +00:00
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_random_volume_offset_db", "db_offset"), &AudioStreamRandomizer::set_random_volume_offset_db);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_random_volume_offset_db"), &AudioStreamRandomizer::get_random_volume_offset_db);
|
2017-07-15 04:23:10 +00:00
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_playback_mode", "mode"), &AudioStreamRandomizer::set_playback_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_playback_mode"), &AudioStreamRandomizer::get_playback_mode);
|
|
|
|
|
|
|
|
ADD_ARRAY("streams", "stream_");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "streams_count", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_streams_count", "get_streams_count");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_mode", PROPERTY_HINT_ENUM, "Random (Avoid Repeats),Random,Sequential"), "set_playback_mode", "get_playback_mode");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "random_pitch", PROPERTY_HINT_RANGE, "1,16,0.01"), "set_random_pitch", "get_random_pitch");
|
2021-09-12 06:57:09 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "random_volume_offset_db", PROPERTY_HINT_RANGE, "0,40,0"), "set_random_volume_offset_db", "get_random_volume_offset_db");
|
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(PLAYBACK_RANDOM_NO_REPEATS);
|
|
|
|
BIND_ENUM_CONSTANT(PLAYBACK_RANDOM);
|
|
|
|
BIND_ENUM_CONSTANT(PLAYBACK_SEQUENTIAL);
|
2017-07-15 04:23:10 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
AudioStreamRandomizer::AudioStreamRandomizer() {
|
|
|
|
random_pitch_scale = 1.1;
|
|
|
|
random_volume_offset_db = 5;
|
2017-07-15 04:23:10 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
void AudioStreamPlaybackRandomizer::start(float p_from_pos) {
|
2017-07-15 04:23:10 +00:00
|
|
|
playing = playback;
|
2021-09-12 06:57:09 +00:00
|
|
|
{
|
|
|
|
float range_from = 1.0 / randomizer->random_pitch_scale;
|
|
|
|
float range_to = randomizer->random_pitch_scale;
|
2017-07-15 04:23:10 +00:00
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
pitch_scale = range_from + Math::randf() * (range_to - range_from);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
float range_from = -randomizer->random_volume_offset_db;
|
|
|
|
float range_to = randomizer->random_volume_offset_db;
|
|
|
|
|
|
|
|
float volume_offset_db = range_from + Math::randf() * (range_to - range_from);
|
|
|
|
volume_scale = Math::db2linear(volume_offset_db);
|
|
|
|
}
|
2017-07-15 04:23:10 +00:00
|
|
|
|
|
|
|
if (playing.is_valid()) {
|
|
|
|
playing->start(p_from_pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
void AudioStreamPlaybackRandomizer::stop() {
|
2017-07-15 04:23:10 +00:00
|
|
|
if (playing.is_valid()) {
|
|
|
|
playing->stop();
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
bool AudioStreamPlaybackRandomizer::is_playing() const {
|
2017-07-15 04:23:10 +00:00
|
|
|
if (playing.is_valid()) {
|
|
|
|
return playing->is_playing();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
int AudioStreamPlaybackRandomizer::get_loop_count() const {
|
2017-07-15 04:23:10 +00:00
|
|
|
if (playing.is_valid()) {
|
|
|
|
return playing->get_loop_count();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
float AudioStreamPlaybackRandomizer::get_playback_position() const {
|
2017-07-15 04:23:10 +00:00
|
|
|
if (playing.is_valid()) {
|
2017-09-21 03:31:36 +00:00
|
|
|
return playing->get_playback_position();
|
2017-07-15 04:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
void AudioStreamPlaybackRandomizer::seek(float p_time) {
|
2017-07-15 04:23:10 +00:00
|
|
|
if (playing.is_valid()) {
|
2017-09-21 03:31:36 +00:00
|
|
|
playing->seek(p_time);
|
2017-07-15 04:23:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
int AudioStreamPlaybackRandomizer::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
|
2017-07-15 04:23:10 +00:00
|
|
|
if (playing.is_valid()) {
|
2021-08-27 17:27:58 +00:00
|
|
|
return playing->mix(p_buffer, p_rate_scale * pitch_scale, p_frames);
|
2017-07-15 04:23:10 +00:00
|
|
|
} else {
|
|
|
|
for (int i = 0; i < p_frames; i++) {
|
2017-10-28 12:04:25 +00:00
|
|
|
p_buffer[i] = AudioFrame(0, 0);
|
2017-07-15 04:23:10 +00:00
|
|
|
}
|
2021-08-27 17:27:58 +00:00
|
|
|
return p_frames;
|
2017-07-15 04:23:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:57:09 +00:00
|
|
|
AudioStreamPlaybackRandomizer::~AudioStreamPlaybackRandomizer() {
|
|
|
|
randomizer->playbacks.erase(this);
|
2017-07-15 04:23:10 +00:00
|
|
|
}
|
2021-08-23 17:53:27 +00:00
|
|
|
/////////////////////////////////////////////
|