Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HTML5] Use browser mix rate by default on the Web. #52695

Merged
merged 1 commit into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/classes/ProjectSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@
<member name="audio/driver/mix_rate" type="int" setter="" getter="" default="44100">
The mixing rate used for audio (in Hz). In general, it's better to not touch this and leave it to the host operating system.
</member>
<member name="audio/driver/mix_rate.web" type="int" setter="" getter="" default="0">
Safer override for [member audio/driver/mix_rate] in the Web platform. Here [code]0[/code] means "let the browser choose" (since some browsers do not like forcing the mix rate).
</member>
<member name="audio/driver/output_latency" type="int" setter="" getter="" default="15">
Output latency in milliseconds for audio. Lower values will result in lower audio latency at the cost of increased CPU usage. Low values may result in audible cracking on slower hardware.
</member>
Expand Down
2 changes: 1 addition & 1 deletion platform/javascript/audio_driver_javascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Error AudioDriverJavaScript::init() {
mix_rate = GLOBAL_GET("audio/driver/mix_rate");
int latency = GLOBAL_GET("audio/driver/output_latency");

channel_count = godot_audio_init(mix_rate, latency, &_state_change_callback, &_latency_update_callback);
channel_count = godot_audio_init(&mix_rate, latency, &_state_change_callback, &_latency_update_callback);
buffer_length = closest_power_of_2((latency * mix_rate / 1000));
#ifndef NO_THREADS
node = memnew(WorkletNode);
Expand Down
2 changes: 1 addition & 1 deletion platform/javascript/godot_audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ extern "C" {
#include "stddef.h"

extern int godot_audio_is_available();
extern int godot_audio_init(int p_mix_rate, int p_latency, void (*_state_cb)(int), void (*_latency_cb)(float));
extern int godot_audio_init(int *p_mix_rate, int p_latency, void (*_state_cb)(int), void (*_latency_cb)(float));
extern void godot_audio_resume();

extern int godot_audio_capture_start();
Expand Down
17 changes: 12 additions & 5 deletions platform/javascript/js/libs/library_godot_audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ const GodotAudio = {
interval: 0,

init: function (mix_rate, latency, onstatechange, onlatencyupdate) {
const ctx = new (window.AudioContext || window.webkitAudioContext)({
sampleRate: mix_rate,
// latencyHint: latency / 1000 // Do not specify, leave 'interactive' for good performance.
});
const opts = {};
// If mix_rate is 0, let the browser choose.
if (mix_rate) {
opts['sampleRate'] = mix_rate;
}
// Do not specify, leave 'interactive' for good performance.
// opts['latencyHint'] = latency / 1000;
const ctx = new (window.AudioContext || window.webkitAudioContext)(opts);
GodotAudio.ctx = ctx;
ctx.onstatechange = function () {
let state = 0;
Expand Down Expand Up @@ -159,7 +163,10 @@ const GodotAudio = {
godot_audio_init: function (p_mix_rate, p_latency, p_state_change, p_latency_update) {
const statechange = GodotRuntime.get_func(p_state_change);
const latencyupdate = GodotRuntime.get_func(p_latency_update);
return GodotAudio.init(p_mix_rate, p_latency, statechange, latencyupdate);
const mix_rate = GodotRuntime.getHeapValue(p_mix_rate, 'i32');
const channels = GodotAudio.init(mix_rate, p_latency, statechange, latencyupdate);
GodotRuntime.setHeapValue(p_mix_rate, GodotAudio.ctx.sampleRate, 'i32');
return channels;
},

godot_audio_resume__sig: 'v',
Expand Down
1 change: 1 addition & 0 deletions servers/audio_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ int AudioDriverManager::get_driver_count() {
void AudioDriverManager::initialize(int p_driver) {
GLOBAL_DEF_RST("audio/driver/enable_input", false);
GLOBAL_DEF_RST("audio/driver/mix_rate", DEFAULT_MIX_RATE);
GLOBAL_DEF_RST("audio/driver/mix_rate.web", 0); // Safer default output_latency for web (use browser default).
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.

Expand Down