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