Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwasmaudiodevice.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include <emscripten.h>
6#include <emscripten/val.h>
7#include <emscripten/bind.h>
8
10
11namespace {
12
13// Probe the hardware sample rate once and cache it. The rate is the same for
14// all devices, so creating a temporary AudioContext per enumerated device is
15// wasteful and can cause browser device errors from overlapping contexts.
17{
18 static int cachedRate = 0;
19 if (cachedRate > 0)
20 return cachedRate;
21
22 // FIXME: firefox
23 // An AudioContext was prevented from starting automatically.
24 // It must be created or resumed after a user gesture on the page.
25 emscripten::val ctx = emscripten::val::global("window")["AudioContext"].new_();
26 if (ctx == emscripten::val::undefined())
27 ctx = emscripten::val::global("window")["webkitAudioContext"].new_();
28
29 if (ctx != emscripten::val::undefined()) {
30 cachedRate = ctx["sampleRate"].as<int>();
31 ctx.call<void>("close");
32 }
33
34 return cachedRate;
35}
36
38{
39 QAudioDevicePrivate::AudioDeviceFormat format;
40
41 format.minimumChannelCount = 1;
42 format.maximumChannelCount = 2;
43 format.minimumSampleRate = 8000;
44 format.maximumSampleRate = 96000; // js AudioContext max according to docs
45
46 // WebAudio natively supports all these formats via AudioWorklet.
47 format.supportedSampleFormats.append(QAudioFormat::UInt8);
48 format.supportedSampleFormats.append(QAudioFormat::Int16);
49 format.supportedSampleFormats.append(QAudioFormat::Int32);
50 format.supportedSampleFormats.append(QAudioFormat::Float);
51
52 format.preferredFormat.setChannelCount(2);
53
54 if (int sRate = probeHardwareSampleRate(); sRate > 0)
55 format.preferredFormat.setSampleRate(sRate);
56
57 format.preferredFormat.setSampleFormat(QAudioFormat::Float);
58
59 return format;
60}
61
62} // namespace
63
64QWasmAudioDevice::QWasmAudioDevice(const char *device, const char *desc, bool isDef,
65 QAudioDevice::Mode mode)
66 : QAudioDevicePrivate(device, mode, QString::fromUtf8(desc), isDef,
67 createDefaultWasmAudioDeviceFormat())
68{
69}
70
71QT_END_NAMESPACE
Combined button and popup list for selecting options.
QAudioDevicePrivate::AudioDeviceFormat createDefaultWasmAudioDeviceFormat()