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.cpp120
1 files changed, 97 insertions, 23 deletions
diff --git a/servers/audio_server.cpp b/servers/audio_server.cpp
index 8ee43ddc32..ceb843c031 100644
--- a/servers/audio_server.cpp
+++ b/servers/audio_server.cpp
@@ -117,6 +117,10 @@ AudioDriver::AudioDriver() {
_last_mix_time = 0;
_mix_amount = 0;
+
+#ifdef DEBUG_ENABLED
+ prof_time = 0;
+#endif
}
AudioDriver *AudioDriverManager::drivers[MAX_DRIVERS];
@@ -184,6 +188,10 @@ void AudioServer::_driver_process(int p_frames, int32_t *p_buffer) {
int todo = p_frames;
+#ifdef DEBUG_ENABLED
+ uint64_t prof_ticks = OS::get_singleton()->get_ticks_usec();
+#endif
+
if (channel_count != get_channel_count()) {
// Amount of channels changed due to a device change
// reinitialize the buses channels and buffers
@@ -235,27 +243,15 @@ void AudioServer::_driver_process(int p_frames, int32_t *p_buffer) {
to_mix -= to_copy;
}
-#ifdef DEBUG_ENABLED
- if (OS::get_singleton() && OS::get_singleton()->is_stdout_verbose()) {
- static uint64_t first_ticks = 0;
- static uint64_t last_ticks = 0;
- static uint64_t ticks = 0;
- static int count = 0;
- static int total = 0;
-
- ticks = OS::get_singleton()->get_ticks_msec();
- if ((ticks - first_ticks) > 10 * 1000 && count > 0) {
- print_line("Audio Driver " + String(AudioDriver::get_singleton()->get_name()) + " average latency: " + itos(total / count) + "ms (frame=" + itos(p_frames) + ")");
- first_ticks = ticks;
- total = 0;
- count = 0;
- }
-
- total += ticks - last_ticks;
- count++;
-
- last_ticks = ticks;
+ // Calculate latency for Performance.AUDIO_OUTPUT_LATENCY
+ if (OS::get_singleton()) {
+ uint64_t ticks = OS::get_singleton()->get_ticks_usec();
+ output_latency = (ticks - output_latency_ticks) / 1000000.f;
+ output_latency_ticks = ticks;
}
+
+#ifdef DEBUG_ENABLED
+ prof_time += OS::get_singleton()->get_ticks_usec() - prof_ticks;
#endif
}
@@ -330,6 +326,10 @@ void AudioServer::_mix_step() {
if (!bus->effects[j].enabled)
continue;
+#ifdef DEBUG_ENABLED
+ uint64_t ticks = OS::get_singleton()->get_ticks_usec();
+#endif
+
for (int k = 0; k < bus->channels.size(); k++) {
if (!bus->channels[k].active)
@@ -344,6 +344,10 @@ void AudioServer::_mix_step() {
continue;
SWAP(bus->channels[k].buffer, temp_buffer[k]);
}
+
+#ifdef DEBUG_ENABLED
+ bus->effects[j].prof_time += OS::get_singleton()->get_ticks_usec() - ticks;
+#endif
}
}
@@ -784,6 +788,9 @@ void AudioServer::add_bus_effect(int p_bus, const Ref<AudioEffect> &p_effect, in
fx.effect = p_effect;
//fx.instance=p_effect->instance();
fx.enabled = true;
+#ifdef DEBUG_ENABLED
+ fx.prof_time = 0;
+#endif
if (p_at_pos >= buses[p_bus]->effects.size() || p_at_pos < 0) {
buses[p_bus]->effects.push_back(fx);
@@ -896,8 +903,8 @@ void AudioServer::init_channels_and_buffers() {
void AudioServer::init() {
- channel_disable_threshold_db = GLOBAL_DEF("audio/channel_disable_threshold_db", -60.0);
- channel_disable_frames = float(GLOBAL_DEF("audio/channel_disable_time", 2.0)) * get_mix_rate();
+ 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();
buffer_size = 1024; //hardcoded for now
init_channels_and_buffers();
@@ -913,7 +920,69 @@ void AudioServer::init() {
set_edited(false); //avoid editors from thinking this was edited
#endif
- GLOBAL_DEF("audio/video_delay_compensation_ms", 0);
+ GLOBAL_DEF_RST("audio/video_delay_compensation_ms", 0);
+}
+
+void AudioServer::update() {
+#ifdef DEBUG_ENABLED
+ if (ScriptDebugger::get_singleton() && ScriptDebugger::get_singleton()->is_profiling()) {
+
+ // Driver time includes server time + effects times
+ // Server time includes effects times
+ uint64_t driver_time = AudioDriver::get_singleton()->get_profiling_time();
+ uint64_t server_time = prof_time;
+
+ // Substract the server time from the driver time
+ if (driver_time > server_time)
+ driver_time -= server_time;
+
+ Array values;
+
+ for (int i = buses.size() - 1; i >= 0; i--) {
+ Bus *bus = buses[i];
+ if (bus->bypass)
+ continue;
+
+ for (int j = 0; j < bus->effects.size(); j++) {
+ if (!bus->effects[j].enabled)
+ continue;
+
+ values.push_back(String(bus->name) + bus->effects[j].effect->get_name());
+ values.push_back(USEC_TO_SEC(bus->effects[j].prof_time));
+
+ // Substract the effect time from the driver and server times
+ if (driver_time > bus->effects[j].prof_time)
+ driver_time -= bus->effects[j].prof_time;
+ if (server_time > bus->effects[j].prof_time)
+ server_time -= bus->effects[j].prof_time;
+ }
+ }
+
+ values.push_back("audio_server");
+ values.push_back(USEC_TO_SEC(server_time));
+ values.push_back("audio_driver");
+ values.push_back(USEC_TO_SEC(driver_time));
+
+ ScriptDebugger::get_singleton()->add_profiling_frame_data("audio_thread", values);
+ }
+
+ // Reset profiling times
+ for (int i = buses.size() - 1; i >= 0; i--) {
+ Bus *bus = buses[i];
+ if (bus->bypass)
+ continue;
+
+ for (int j = 0; j < bus->effects.size(); j++) {
+ if (!bus->effects[j].enabled)
+ continue;
+
+ bus->effects[j].prof_time = 0;
+ }
+ }
+
+ AudioDriver::get_singleton()->reset_profiling_time();
+ prof_time = 0;
+#endif
}
void AudioServer::load_default_bus_layout() {
@@ -1201,6 +1270,11 @@ AudioServer::AudioServer() {
mix_frames = 0;
channel_count = 0;
to_mix = 0;
+ output_latency = 0;
+ output_latency_ticks = 0;
+#ifdef DEBUG_ENABLED
+ prof_time = 0;
+#endif
}
AudioServer::~AudioServer() {