Improved stream paused fade code
This commit is contained in:
parent
e2b7a68db4
commit
087329074d
6 changed files with 52 additions and 48 deletions
|
@ -37,7 +37,7 @@
|
|||
void AudioStreamPlayer2D::_mix_audio() {
|
||||
|
||||
if (!stream_playback.is_valid() || !active ||
|
||||
(stream_paused && stream_paused_fade <= 0.f)) {
|
||||
(stream_paused && !stream_paused_fade_out)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -50,11 +50,13 @@ void AudioStreamPlayer2D::_mix_audio() {
|
|||
AudioFrame *buffer = mix_buffer.ptrw();
|
||||
int buffer_size = mix_buffer.size();
|
||||
|
||||
// Mix if we're not paused or we're fading out
|
||||
if (!stream_paused || stream_paused_fade > 0.f) {
|
||||
stream_playback->mix(buffer, pitch_scale, buffer_size);
|
||||
if (stream_paused_fade_out) {
|
||||
// Short fadeout ramp
|
||||
buffer_size = MIN(buffer_size, 128);
|
||||
}
|
||||
|
||||
stream_playback->mix(buffer, pitch_scale, buffer_size);
|
||||
|
||||
//write all outputs
|
||||
for (int i = 0; i < output_count; i++) {
|
||||
|
||||
|
@ -82,15 +84,10 @@ void AudioStreamPlayer2D::_mix_audio() {
|
|||
}
|
||||
|
||||
//mix!
|
||||
AudioFrame vol_inc = (current.vol - prev_outputs[i].vol) / float(buffer_size);
|
||||
AudioFrame vol = current.vol;
|
||||
|
||||
if (stream_paused) {
|
||||
vol = vol * stream_paused_fade;
|
||||
if (stream_paused_fade > 0.f) {
|
||||
stream_paused_fade -= 0.1f;
|
||||
}
|
||||
}
|
||||
AudioFrame target_volume = stream_paused_fade_out ? AudioFrame(0.f, 0.f) : current.vol;
|
||||
AudioFrame vol_prev = stream_paused_fade_in ? AudioFrame(0.f, 0.f) : prev_outputs[i].vol;
|
||||
AudioFrame vol_inc = (target_volume - vol_prev) / float(buffer_size);
|
||||
AudioFrame vol = stream_paused_fade_in ? AudioFrame(0.f, 0.f) : current.vol;
|
||||
|
||||
int cc = AudioServer::get_singleton()->get_channel_count();
|
||||
|
||||
|
@ -131,6 +128,8 @@ void AudioStreamPlayer2D::_mix_audio() {
|
|||
}
|
||||
|
||||
output_ready = false;
|
||||
stream_paused_fade_in = false;
|
||||
stream_paused_fade_out = false;
|
||||
}
|
||||
|
||||
void AudioStreamPlayer2D::_notification(int p_what) {
|
||||
|
@ -439,7 +438,8 @@ void AudioStreamPlayer2D::set_stream_paused(bool p_pause) {
|
|||
|
||||
if (p_pause != stream_paused) {
|
||||
stream_paused = p_pause;
|
||||
stream_paused_fade = stream_paused ? 1.f : 0.f;
|
||||
stream_paused_fade_in = p_pause ? false : true;
|
||||
stream_paused_fade_out = p_pause ? true : false;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -518,7 +518,8 @@ AudioStreamPlayer2D::AudioStreamPlayer2D() {
|
|||
output_ready = false;
|
||||
area_mask = 1;
|
||||
stream_paused = false;
|
||||
stream_paused_fade = 0.f;
|
||||
stream_paused_fade_in = false;
|
||||
stream_paused_fade_out = false;
|
||||
AudioServer::get_singleton()->connect("bus_layout_changed", this, "_bus_layout_changed");
|
||||
}
|
||||
|
||||
|
|
|
@ -71,9 +71,10 @@ private:
|
|||
|
||||
float volume_db;
|
||||
float pitch_scale;
|
||||
float stream_paused_fade;
|
||||
bool autoplay;
|
||||
bool stream_paused;
|
||||
bool stream_paused_fade_in;
|
||||
bool stream_paused_fade_out;
|
||||
StringName bus;
|
||||
|
||||
void _mix_audio();
|
||||
|
|
|
@ -36,7 +36,7 @@
|
|||
void AudioStreamPlayer3D::_mix_audio() {
|
||||
|
||||
if (!stream_playback.is_valid() || !active ||
|
||||
(stream_paused && stream_paused_fade <= 0.f)) {
|
||||
(stream_paused && !stream_paused_fade_out)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -51,9 +51,13 @@ void AudioStreamPlayer3D::_mix_audio() {
|
|||
AudioFrame *buffer = mix_buffer.ptrw();
|
||||
int buffer_size = mix_buffer.size();
|
||||
|
||||
if (stream_paused_fade_out) {
|
||||
// Short fadeout ramp
|
||||
buffer_size = MIN(buffer_size, 128);
|
||||
}
|
||||
|
||||
// Mix if we're not paused or we're fading out
|
||||
if ((output_count > 0 || out_of_range_mode == OUT_OF_RANGE_MIX) &&
|
||||
(!stream_paused || stream_paused_fade > 0.f)) {
|
||||
if ((output_count > 0 || out_of_range_mode == OUT_OF_RANGE_MIX)) {
|
||||
|
||||
float output_pitch_scale = 0.0;
|
||||
if (output_count) {
|
||||
|
@ -103,15 +107,10 @@ void AudioStreamPlayer3D::_mix_audio() {
|
|||
int buffers = AudioServer::get_singleton()->get_channel_count();
|
||||
|
||||
for (int k = 0; k < buffers; k++) {
|
||||
AudioFrame vol_inc = (current.vol[k] - prev_outputs[i].vol[k]) / float(buffer_size);
|
||||
AudioFrame vol = current.vol[k];
|
||||
|
||||
if (stream_paused) {
|
||||
vol = vol * stream_paused_fade;
|
||||
if (stream_paused_fade > 0.f) {
|
||||
stream_paused_fade -= 0.1f;
|
||||
}
|
||||
}
|
||||
AudioFrame target_volume = stream_paused_fade_out ? AudioFrame(0.f, 0.f) : current.vol[k];
|
||||
AudioFrame vol_prev = stream_paused_fade_in ? AudioFrame(0.f, 0.f) : prev_outputs[i].vol[k];
|
||||
AudioFrame vol_inc = (target_volume - vol_prev) / float(buffer_size);
|
||||
AudioFrame vol = stream_paused_fade_in ? AudioFrame(0.f, 0.f) : current.vol[k];
|
||||
|
||||
AudioFrame *target = AudioServer::get_singleton()->thread_get_channel_mix_buffer(current.bus_index, k);
|
||||
|
||||
|
@ -193,6 +192,8 @@ void AudioStreamPlayer3D::_mix_audio() {
|
|||
}
|
||||
|
||||
output_ready = false;
|
||||
stream_paused_fade_in = false;
|
||||
stream_paused_fade_out = false;
|
||||
}
|
||||
|
||||
float AudioStreamPlayer3D::_get_attenuation_db(float p_distance) const {
|
||||
|
@ -846,7 +847,8 @@ void AudioStreamPlayer3D::set_stream_paused(bool p_pause) {
|
|||
|
||||
if (p_pause != stream_paused) {
|
||||
stream_paused = p_pause;
|
||||
stream_paused_fade = stream_paused ? 1.f : 0.f;
|
||||
stream_paused_fade_in = stream_paused ? false : true;
|
||||
stream_paused_fade_out = stream_paused ? true : false;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -984,7 +986,8 @@ AudioStreamPlayer3D::AudioStreamPlayer3D() {
|
|||
out_of_range_mode = OUT_OF_RANGE_MIX;
|
||||
doppler_tracking = DOPPLER_TRACKING_DISABLED;
|
||||
stream_paused = false;
|
||||
stream_paused_fade = 0.f;
|
||||
stream_paused_fade_in = false;
|
||||
stream_paused_fade_out = false;
|
||||
|
||||
velocity_tracker.instance();
|
||||
AudioServer::get_singleton()->connect("bus_layout_changed", this, "_bus_layout_changed");
|
||||
|
|
|
@ -107,9 +107,10 @@ private:
|
|||
float unit_size;
|
||||
float max_db;
|
||||
float pitch_scale;
|
||||
float stream_paused_fade;
|
||||
bool autoplay;
|
||||
bool stream_paused;
|
||||
bool stream_paused_fade_in;
|
||||
bool stream_paused_fade_out;
|
||||
StringName bus;
|
||||
|
||||
void _mix_audio();
|
||||
|
|
|
@ -41,30 +41,22 @@ void AudioStreamPlayer::_mix_internal(bool p_fadeout) {
|
|||
int buffer_size = mix_buffer.size();
|
||||
|
||||
if (p_fadeout) {
|
||||
buffer_size = MIN(buffer_size, 16); //short fadeout ramp
|
||||
// Short fadeout ramp
|
||||
buffer_size = MIN(buffer_size, 128);
|
||||
}
|
||||
|
||||
// Mix if we're not paused or we're fading out
|
||||
if (!stream_paused || stream_paused_fade > 0.f) {
|
||||
stream_playback->mix(buffer, pitch_scale, buffer_size);
|
||||
}
|
||||
stream_playback->mix(buffer, pitch_scale, buffer_size);
|
||||
|
||||
//multiply volume interpolating to avoid clicks if this changes
|
||||
float target_volume = p_fadeout ? -80.0 : volume_db;
|
||||
float vol = Math::db2linear(mix_volume_db);
|
||||
float vol_inc = (Math::db2linear(target_volume) - vol) / float(buffer_size);
|
||||
|
||||
if (stream_paused) {
|
||||
vol = vol * stream_paused_fade;
|
||||
if (stream_paused_fade > 0.f) {
|
||||
stream_paused_fade -= 0.1f;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < buffer_size; i++) {
|
||||
buffer[i] *= vol;
|
||||
vol += vol_inc;
|
||||
}
|
||||
|
||||
//set volume for next mix
|
||||
mix_volume_db = target_volume;
|
||||
|
||||
|
@ -99,8 +91,14 @@ void AudioStreamPlayer::_mix_internal(bool p_fadeout) {
|
|||
|
||||
void AudioStreamPlayer::_mix_audio() {
|
||||
|
||||
if (!stream_playback.is_valid() || !active ||
|
||||
(stream_paused && stream_paused_fade <= 0.f)) {
|
||||
if (!stream_playback.is_valid() || !active)
|
||||
return;
|
||||
|
||||
if (stream_paused) {
|
||||
if (stream_paused_fade) {
|
||||
_mix_internal(true);
|
||||
stream_paused_fade = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -295,7 +293,7 @@ void AudioStreamPlayer::set_stream_paused(bool p_pause) {
|
|||
|
||||
if (p_pause != stream_paused) {
|
||||
stream_paused = p_pause;
|
||||
stream_paused_fade = stream_paused ? 1.f : 0.f;
|
||||
stream_paused_fade = p_pause ? true : false;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -385,7 +383,7 @@ AudioStreamPlayer::AudioStreamPlayer() {
|
|||
setseek = -1;
|
||||
active = false;
|
||||
stream_paused = false;
|
||||
stream_paused_fade = 0.f;
|
||||
stream_paused_fade = false;
|
||||
mix_target = MIX_TARGET_STEREO;
|
||||
|
||||
AudioServer::get_singleton()->connect("bus_layout_changed", this, "_bus_layout_changed");
|
||||
|
|
|
@ -56,9 +56,9 @@ private:
|
|||
float mix_volume_db;
|
||||
float pitch_scale;
|
||||
float volume_db;
|
||||
float stream_paused_fade;
|
||||
bool autoplay;
|
||||
bool stream_paused;
|
||||
bool stream_paused_fade;
|
||||
StringName bus;
|
||||
|
||||
MixTarget mix_target;
|
||||
|
|
Loading…
Reference in a new issue