summaryrefslogtreecommitdiff
path: root/servers/audio_server.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'servers/audio_server.cpp')
-rw-r--r--servers/audio_server.cpp83
1 files changed, 49 insertions, 34 deletions
diff --git a/servers/audio_server.cpp b/servers/audio_server.cpp
index 09d2914e05..4c54188cb2 100644
--- a/servers/audio_server.cpp
+++ b/servers/audio_server.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -30,11 +30,11 @@
#include "audio_server.h"
+#include "core/config/project_settings.h"
#include "core/debugger/engine_debugger.h"
+#include "core/io/file_access.h"
#include "core/io/resource_loader.h"
-#include "core/os/file_access.h"
#include "core/os/os.h"
-#include "core/project_settings.h"
#include "scene/resources/audio_stream_sample.h"
#include "servers/audio/audio_driver_dummy.h"
#include "servers/audio/effects/audio_effect_compressor.h"
@@ -71,13 +71,20 @@ void AudioDriver::update_mix_time(int p_frames) {
}
}
-double AudioDriver::get_time_since_last_mix() const {
- return (OS::get_singleton()->get_ticks_usec() - _last_mix_time) / 1000000.0;
+double AudioDriver::get_time_since_last_mix() {
+ lock();
+ uint64_t last_mix_time = _last_mix_time;
+ unlock();
+ return (OS::get_singleton()->get_ticks_usec() - last_mix_time) / 1000000.0;
}
-double AudioDriver::get_time_to_next_mix() const {
- double total = (OS::get_singleton()->get_ticks_usec() - _last_mix_time) / 1000000.0;
- double mix_buffer = _last_mix_frames / (double)get_mix_rate();
+double AudioDriver::get_time_to_next_mix() {
+ lock();
+ uint64_t last_mix_time = _last_mix_time;
+ uint64_t last_mix_frames = _last_mix_frames;
+ unlock();
+ double total = (OS::get_singleton()->get_ticks_usec() - last_mix_time) / 1000000.0;
+ double mix_buffer = last_mix_frames / (double)get_mix_rate();
return mix_buffer - total;
}
@@ -181,9 +188,10 @@ int AudioDriverManager::get_driver_count() {
}
void AudioDriverManager::initialize(int p_driver) {
- GLOBAL_DEF_RST("audio/enable_audio_input", false);
- GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE);
- GLOBAL_DEF_RST("audio/output_latency", DEFAULT_OUTPUT_LATENCY);
+ GLOBAL_DEF_RST("audio/driver/enable_input", false);
+ GLOBAL_DEF_RST("audio/driver/mix_rate", DEFAULT_MIX_RATE);
+ GLOBAL_DEF_RST("audio/driver/output_latency", DEFAULT_OUTPUT_LATENCY);
+ GLOBAL_DEF_RST("audio/driver/output_latency.web", 50); // Safer default output_latency for web.
int failed_driver = -1;
@@ -238,6 +246,7 @@ void AudioServer::_driver_process(int p_frames, int32_t *p_buffer) {
init_channels_and_buffers();
}
+ ERR_FAIL_COND_MSG(buses.is_empty() && todo, "AudioServer bus count is less than 1.");
while (todo) {
if (to_mix == 0) {
_mix_step();
@@ -393,6 +402,7 @@ void AudioServer::_mix_step() {
for (int k = 0; k < bus->channels.size(); k++) {
if (!bus->channels[k].active) {
+ bus->channels.write[k].peak_volume = AudioFrame(AUDIO_MIN_PEAK_DB, AUDIO_MIN_PEAK_DB);
continue;
}
@@ -426,7 +436,7 @@ void AudioServer::_mix_step() {
}
}
- bus->channels.write[k].peak_volume = AudioFrame(Math::linear2db(peak.l + 0.0000000001), Math::linear2db(peak.r + 0.0000000001));
+ bus->channels.write[k].peak_volume = AudioFrame(Math::linear2db(peak.l + AUDIO_PEAK_OFFSET), Math::linear2db(peak.r + AUDIO_PEAK_OFFSET));
if (!bus->channels[k].used) {
//see if any audio is contained, because channel was not used
@@ -551,7 +561,7 @@ void AudioServer::set_bus_count(int p_count) {
unlock();
- emit_signal("bus_layout_changed");
+ emit_signal(SNAME("bus_layout_changed"));
}
void AudioServer::remove_bus(int p_index) {
@@ -566,7 +576,7 @@ void AudioServer::remove_bus(int p_index) {
buses.remove(p_index);
unlock();
- emit_signal("bus_layout_changed");
+ emit_signal(SNAME("bus_layout_changed"));
}
void AudioServer::add_bus(int p_at_pos) {
@@ -620,7 +630,7 @@ void AudioServer::add_bus(int p_at_pos) {
buses.insert(p_at_pos, bus);
}
- emit_signal("bus_layout_changed");
+ emit_signal(SNAME("bus_layout_changed"));
}
void AudioServer::move_bus(int p_bus, int p_to_pos) {
@@ -644,7 +654,7 @@ void AudioServer::move_bus(int p_bus, int p_to_pos) {
buses.insert(p_to_pos - 1, bus);
}
- emit_signal("bus_layout_changed");
+ emit_signal(SNAME("bus_layout_changed"));
}
int AudioServer::get_bus_count() const {
@@ -690,7 +700,7 @@ void AudioServer::set_bus_name(int p_bus, const String &p_name) {
bus_map[attempt] = buses[p_bus];
unlock();
- emit_signal("bus_layout_changed");
+ emit_signal(SNAME("bus_layout_changed"));
}
String AudioServer::get_bus_name(int p_bus) const {
@@ -784,7 +794,7 @@ void AudioServer::_update_bus_effects(int p_bus) {
for (int i = 0; i < buses[p_bus]->channels.size(); i++) {
buses.write[p_bus]->channels.write[i].effect_instances.resize(buses[p_bus]->effects.size());
for (int j = 0; j < buses[p_bus]->effects.size(); j++) {
- Ref<AudioEffectInstance> fx = buses.write[p_bus]->effects.write[j].effect->instance();
+ Ref<AudioEffectInstance> fx = buses.write[p_bus]->effects.write[j].effect->instantiate();
if (Object::cast_to<AudioEffectCompressorInstance>(*fx)) {
Object::cast_to<AudioEffectCompressorInstance>(*fx)->set_current_channel(i);
}
@@ -803,7 +813,7 @@ void AudioServer::add_bus_effect(int p_bus, const Ref<AudioEffect> &p_effect, in
Bus::Effect fx;
fx.effect = p_effect;
- //fx.instance=p_effect->instance();
+ //fx.instance=p_effect->instantiate();
fx.enabled = true;
#ifdef DEBUG_ENABLED
fx.prof_time = 0;
@@ -903,12 +913,14 @@ bool AudioServer::is_bus_channel_active(int p_bus, int p_channel) const {
return buses[p_bus]->channels[p_channel].active;
}
-void AudioServer::set_global_rate_scale(float p_scale) {
- global_rate_scale = p_scale;
+void AudioServer::set_playback_speed_scale(float p_scale) {
+ ERR_FAIL_COND(p_scale <= 0);
+
+ playback_speed_scale = p_scale;
}
-float AudioServer::get_global_rate_scale() const {
- return global_rate_scale;
+float AudioServer::get_playback_speed_scale() const {
+ return playback_speed_scale;
}
void AudioServer::init_channels_and_buffers() {
@@ -928,9 +940,9 @@ void AudioServer::init_channels_and_buffers() {
}
void AudioServer::init() {
- channel_disable_threshold_db = GLOBAL_DEF_RST("audio/channel_disable_threshold_db", -60.0);
- channel_disable_frames = float(GLOBAL_DEF_RST("audio/channel_disable_time", 2.0)) * get_mix_rate();
- ProjectSettings::get_singleton()->set_custom_property_info("audio/channel_disable_time", PropertyInfo(Variant::FLOAT, "audio/channel_disable_time", PROPERTY_HINT_RANGE, "0,5,0.01,or_greater"));
+ channel_disable_threshold_db = GLOBAL_DEF_RST("audio/buses/channel_disable_threshold_db", -60.0);
+ channel_disable_frames = float(GLOBAL_DEF_RST("audio/buses/channel_disable_time", 2.0)) * get_mix_rate();
+ ProjectSettings::get_singleton()->set_custom_property_info("audio/buses/channel_disable_time", PropertyInfo(Variant::FLOAT, "audio/buses/channel_disable_time", PROPERTY_HINT_RANGE, "0,5,0.01,or_greater"));
buffer_size = 1024; //hardcoded for now
init_channels_and_buffers();
@@ -947,7 +959,7 @@ void AudioServer::init() {
set_edited(false); //avoid editors from thinking this was edited
#endif
- GLOBAL_DEF_RST("audio/video_delay_compensation_ms", 0);
+ GLOBAL_DEF_RST("audio/video/video_delay_compensation_ms", 0);
}
void AudioServer::update() {
@@ -1024,7 +1036,7 @@ void AudioServer::update() {
}
void AudioServer::load_default_bus_layout() {
- String layout_path = ProjectSettings::get_singleton()->get("audio/default_bus_layout");
+ String layout_path = ProjectSettings::get_singleton()->get("audio/buses/default_bus_layout");
if (ResourceLoader::exists(layout_path)) {
Ref<AudioBusLayout> default_layout = ResourceLoader::load(layout_path);
@@ -1152,6 +1164,9 @@ void AudioServer::set_bus_layout(const Ref<AudioBusLayout> &p_bus_layout) {
Bus::Effect bfx;
bfx.effect = fx;
bfx.enabled = p_bus_layout->buses[i].effects[j].enabled;
+#if DEBUG_ENABLED
+ bfx.prof_time = 0;
+#endif
bus->effects.push_back(bfx);
}
}
@@ -1173,7 +1188,7 @@ void AudioServer::set_bus_layout(const Ref<AudioBusLayout> &p_bus_layout) {
Ref<AudioBusLayout> AudioServer::generate_bus_layout() const {
Ref<AudioBusLayout> state;
- state.instance();
+ state.instantiate();
state->buses.resize(buses.size());
@@ -1262,8 +1277,8 @@ void AudioServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_bus_peak_volume_left_db", "bus_idx", "channel"), &AudioServer::get_bus_peak_volume_left_db);
ClassDB::bind_method(D_METHOD("get_bus_peak_volume_right_db", "bus_idx", "channel"), &AudioServer::get_bus_peak_volume_right_db);
- ClassDB::bind_method(D_METHOD("set_global_rate_scale", "scale"), &AudioServer::set_global_rate_scale);
- ClassDB::bind_method(D_METHOD("get_global_rate_scale"), &AudioServer::get_global_rate_scale);
+ ClassDB::bind_method(D_METHOD("set_playback_speed_scale", "scale"), &AudioServer::set_playback_speed_scale);
+ ClassDB::bind_method(D_METHOD("get_playback_speed_scale"), &AudioServer::get_playback_speed_scale);
ClassDB::bind_method(D_METHOD("lock"), &AudioServer::lock);
ClassDB::bind_method(D_METHOD("unlock"), &AudioServer::unlock);
@@ -1287,7 +1302,7 @@ void AudioServer::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::INT, "bus_count"), "set_bus_count", "get_bus_count");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "device"), "set_device", "get_device");
- ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "global_rate_scale"), "set_global_rate_scale", "get_global_rate_scale");
+ ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "playback_speed_scale"), "set_playback_speed_scale", "get_playback_speed_scale");
ADD_SIGNAL(MethodInfo("bus_layout_changed"));
@@ -1307,7 +1322,7 @@ AudioServer::AudioServer() {
#endif
mix_time = 0;
mix_size = 0;
- global_rate_scale = 1;
+ playback_speed_scale = 1;
}
AudioServer::~AudioServer() {