From edfac528c8381f510ca83a8edb94db9a3e3e10f7 Mon Sep 17 00:00:00 2001 From: Lorn Potter Date: Fri, 10 Jun 2022 11:42:16 +1000 Subject: wasm: update and fix qwasmaudiodevice - set min and max sample rates according to AudioContext docs - remove use of EM_ASM to allow better use of multithreading - ensure feature thread is used Fixes: QTBUG-104045 Change-Id: Id0bf8905c68f4364abc6f3a9029f75cc5275ff0b Reviewed-by: Alexandru Croitor Reviewed-by: Lars Knoll (cherry picked from commit 731e6b6f510b01e496860a208d304789aa72fca3) Reviewed-by: Qt Cherry-pick Bot --- CMakeLists.txt | 5 +++++ src/multimedia/wasm/qwasmaudiodevice.cpp | 26 +++++++++++++++++--------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 033d02ec5..96b2f8cb3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,5 +21,10 @@ if(NOT TARGET Qt::Network) message(NOTICE "Skipping the build as the condition \"TARGET Qt::Network\" is not met.") return() endif() +if(NOT QT_FEATURE_thread) + message(NOTICE "Skipping the build as the QT_FEATURE_thread is not met.") + return() +endif() + qt_build_repo() diff --git a/src/multimedia/wasm/qwasmaudiodevice.cpp b/src/multimedia/wasm/qwasmaudiodevice.cpp index 18eb9e967..b8f3532b5 100644 --- a/src/multimedia/wasm/qwasmaudiodevice.cpp +++ b/src/multimedia/wasm/qwasmaudiodevice.cpp @@ -3,6 +3,9 @@ #include "qwasmaudiodevice_p.h" #include +#include +#include + #include #include @@ -16,8 +19,8 @@ QWasmAudioDevice::QWasmAudioDevice(const char *device, const char *desc, bool is minimumChannelCount = 1; maximumChannelCount = 2; - minimumSampleRate = 1; - maximumSampleRate = 192'000; + minimumSampleRate = 8000; + maximumSampleRate = 96000; // js AudioContext max according to docs // native openAL formats supportedSampleFormats.append(QAudioFormat::UInt8); @@ -29,13 +32,18 @@ QWasmAudioDevice::QWasmAudioDevice(const char *device, const char *desc, bool is preferredFormat.setChannelCount(2); - preferredFormat.setSampleRate(EM_ASM_INT({ - var AudioContext = window.AudioContext || window.webkitAudioContext; - var ctx = new AudioContext(); - var sr = ctx.sampleRate; - ctx.close(); - return sr; - })); + // FIXME: firefox + // An AudioContext was prevented from starting automatically. + // It must be created or resumed after a user gesture on the page. + emscripten::val audioContext = emscripten::val::global("window")["AudioContext"].new_(); + if (audioContext == emscripten::val::undefined()) + audioContext = emscripten::val::global("window")["webkitAudioContext"].new_(); + + if (audioContext != emscripten::val::undefined()) { + int sRate = audioContext["sampleRate"].as(); + audioContext.call("close"); + preferredFormat.setSampleRate(sRate); + } auto f = QAudioFormat::Float; -- cgit v1.2.3