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
9#include <AL/al.h>
10#include <AL/alc.h>
11
12QT_BEGIN_NAMESPACE
13
14namespace {
15
17{
18 QAudioDevicePrivate::AudioDeviceFormat format;
19
20 format.minimumChannelCount = 1;
21 format.maximumChannelCount = 2;
22 format.minimumSampleRate = 8000;
23 format.maximumSampleRate = 96000; // js AudioContext max according to docs
24
25 // native openAL formats
26 format.supportedSampleFormats.append(QAudioFormat::UInt8);
27 format.supportedSampleFormats.append(QAudioFormat::Int16);
28
29 // Browsers use 32bit floats as native, but emscripten reccomends checking for the exension.
30 if (alIsExtensionPresent("AL_EXT_float32"))
31 format.supportedSampleFormats.append(QAudioFormat::Float);
32
33 format.preferredFormat.setChannelCount(2);
34
35 // FIXME: firefox
36 // An AudioContext was prevented from starting automatically.
37 // It must be created or resumed after a user gesture on the page.
38 emscripten::val audioContext = emscripten::val::global("window")["AudioContext"].new_();
39 if (audioContext == emscripten::val::undefined())
40 audioContext = emscripten::val::global("window")["webkitAudioContext"].new_();
41
42 if (audioContext != emscripten::val::undefined()) {
43 audioContext.call<void>("resume");
44 int sRate = audioContext["sampleRate"].as<int>();
45 audioContext.call<void>("close");
46 format.preferredFormat.setSampleRate(sRate);
47 }
48
49 auto f = QAudioFormat::Float;
50
51 if (!format.supportedSampleFormats.contains(f))
52 f = QAudioFormat::Int16;
53 format.preferredFormat.setSampleFormat(f);
54
55 return format;
56}
57
58} // namespace
59
60QWasmAudioDevice::QWasmAudioDevice(const char *device, const char *desc, bool isDef,
61 QAudioDevice::Mode mode)
62 : QAudioDevicePrivate(device, mode, QString::fromUtf8(desc), isDef,
63 createDefaultWasmAudioDeviceFormat())
64{
65}
66
67QT_END_NAMESPACE
QAudioDevicePrivate::AudioDeviceFormat createDefaultWasmAudioDeviceFormat()