Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qaudioroom.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-3.0-only
3#include <qaudioroom_p.h>
4
6
7namespace {
8inline QVector3D toVector(const float *f)
9{
10 return QVector3D(f[0], f[1], f[2]);
11}
12
13inline void toFloats(const QVector3D &v, float *f)
14{
15 f[0] = v.x();
16 f[1] = v.y();
17 f[2] = v.z();
18}
19
20inline QQuaternion toQuaternion(const float *f)
21{
22 // resonance audio puts the scalar component last
23 return QQuaternion(f[3], f[0], f[1], f[2]);
24}
25
26inline void toFloats(const QQuaternion &q, float *f)
27{
28 f[0] = q.x();
29 f[1] = q.y();
30 f[2] = q.z();
31 f[3] = q.scalar();
32}
33
34// Default values for occlusion and dampening of different wall materials.
35// These values are used as defaults if a wall is only defined by a material
36// and define how sound passes through the wall.
37// We define both occlusion and dampening constants to be able to tune the
38// sound. Dampening only reduces the level of the sound without affecting its
39// tone, while occlusion will dampen higher frequencies more than lower ones
40struct {
41 float occlusion;
42 float dampening;
43} occlusionAndDampening[] = {
44 { 0.f, 1.f }, // Transparent,
45 { 0.f, .1f }, // AcousticCeilingTiles,
46 { 2.f, .4f }, // BrickBare,
47 { 2.f, .4f }, // BrickPainted,
48 { 4.f, 1.f }, // ConcreteBlockCoarse,
49 { 4.f, 1.f }, // ConcreteBlockPainted,
50 { .7f, .7f }, // CurtainHeavy,
51 { .5f, .5f }, // FiberGlassInsulation,
52 { .2f, .3f }, // GlassThin,
53 { .5f, .2f }, // GlassThick,
54 { 7.f, 1.f }, // Grass,
55 { 4.f, 1.f }, // LinoleumOnConcrete,
56 { 4.f, 1.f }, // Marble,
57 { 0.f, .2f }, // Metal,
58 { 4.f, 1.f }, // ParquetOnConcrete,
59 { 2.f, .4f }, // PlasterRough,
60 { 2.f, .4f }, // PlasterSmooth,
61 { 1.5f, .2f }, // PlywoodPanel,
62 { 4.f, 1.f }, // PolishedConcreteOrTile,
63 { 4.f, 1.f }, // Sheetrock,
64 { 4.f, 1.f }, // WaterOrIceSurface,
65 { 1.f, .3f }, // WoodCeiling,
66 { 1.f, .3f }, // WoodPanel,
67 { 0.f, .0f }, // UniformMaterial,
68};
69
70}
71
72// make sure the wall definitions agree with resonance audio
73
74static_assert(QAudioRoom::LeftWall == 0);
75static_assert(QAudioRoom::RightWall == 1);
76static_assert(QAudioRoom::Floor == 2);
77static_assert(QAudioRoom::Ceiling == 3);
78static_assert(QAudioRoom::FrontWall == 4);
79static_assert(QAudioRoom::BackWall == 5);
80
82{
83 return m_wallOcclusion[wall] < 0 ? occlusionAndDampening[roomProperties.material_names[wall]].occlusion : m_wallOcclusion[wall];
84}
85
87{
88 return m_wallDampening[wall] < 0 ? occlusionAndDampening[roomProperties.material_names[wall]].dampening : m_wallDampening[wall];
89}
90
92{
93 if (!dirty)
94 return;
95 reflections = vraudio::ComputeReflectionProperties(roomProperties);
96 reverb = vraudio::ComputeReverbProperties(roomProperties);
97 dirty = false;
98}
99
100
125 : d(new QAudioRoomPrivate)
126{
128 d->engine = engine;
130 ep->addRoom(this);
131}
132
137{
138 auto *ep = QAudioEnginePrivate::get(d->engine);
139 if (ep)
140 ep->removeRoom(this);
141 delete d;
142}
143
198{
199 auto *ep = QAudioEnginePrivate::get(d->engine);
200 pos *= ep->distanceScale;
201 if (toVector(d->roomProperties.position) == pos)
202 return;
203 toFloats(pos, d->roomProperties.position);
204 d->dirty = true;
206}
207
209{
210 auto *ep = QAudioEnginePrivate::get(d->engine);
211 auto pos = toVector(d->roomProperties.position);
212 pos /= ep->distanceScale;
213 return pos;
214}
215
225{
226 auto *ep = QAudioEnginePrivate::get(d->engine);
227 dim *= ep->distanceScale;
228 if (toVector(d->roomProperties.dimensions) == dim)
229 return;
230 toFloats(dim, d->roomProperties.dimensions);
231 d->dirty = true;
233}
234
236{
237 auto *ep = QAudioEnginePrivate::get(d->engine);
238 auto dim = toVector(d->roomProperties.dimensions);
239 dim /= ep->distanceScale;
240 return dim;
241}
242
249{
250 if (toQuaternion(d->roomProperties.rotation) == q)
251 return;
252 toFloats(q, d->roomProperties.rotation);
253 d->dirty = true;
255}
256
258{
259 return toQuaternion(d->roomProperties.rotation);
260}
261
276{
277 static_assert(vraudio::kUniform == int(UniformMaterial));
278 static_assert(vraudio::kTransparent == int(Transparent));
279
280 if (d->roomProperties.material_names[int(wall)] == int(material))
281 return;
282 d->roomProperties.material_names[int(wall)] = vraudio::MaterialName(int(material));
283 d->dirty = true;
285}
286
293{
294 return Material(d->roomProperties.material_names[int(wall)]);
295}
296
308{
309 if (factor < 0.)
310 factor = 0.;
311 if (d->roomProperties.reflection_scalar == factor)
312 return;
313 d->roomProperties.reflection_scalar = factor;
314 d->dirty = true;
316}
317
319{
320 return d->roomProperties.reflection_scalar;
321}
322
334{
335 if (factor < 0)
336 factor = 0;
337 if (d->roomProperties.reverb_gain == factor)
338 return;
339 d->roomProperties.reverb_gain = factor;
340 d->dirty = true;
342}
343
345{
346 return d->roomProperties.reverb_gain;
347}
348
359{
360 if (factor < 0)
361 factor = 0;
362 if (d->roomProperties.reverb_time == factor)
363 return;
364 d->roomProperties.reverb_time = factor;
365 d->dirty = true;
367}
368
370{
371 return d->roomProperties.reverb_time;
372}
373
384{
385 if (d->roomProperties.reverb_brightness == factor)
386 return;
387 d->roomProperties.reverb_brightness = factor;
388 d->dirty = true;
390}
391
393{
394 return d->roomProperties.reverb_brightness;
395}
396
398
399#include "moc_qaudioroom.cpp"
static QAudioEnginePrivate * get(QAudioEngine *engine)
\inmodule QtSpatialAudio
float m_wallDampening[6]
vraudio::ReflectionProperties reflections
vraudio::RoomProperties roomProperties
float wallDampening(QAudioRoom::Wall wall) const
float wallOcclusion(QAudioRoom::Wall wall) const
float m_wallOcclusion[6]
QAudioEngine * engine
vraudio::ReverbProperties reverb
void reverbTimeChanged()
float reverbGain
A gain factor for reverb generated in this room.
Definition qaudioroom.h:23
float reverbBrightness
A brightness factor to be applied to the generated reverb.
Definition qaudioroom.h:25
QAudioRoom(QAudioEngine *engine)
Constructs a QAudioRoom for engine.
QQuaternion rotation
Defines the orientation of the room in 3D space.
Definition qaudioroom.h:21
void positionChanged()
void setReverbGain(float factor)
void setRotation(const QQuaternion &q)
void reverbGainChanged()
void wallsChanged()
Signals when the wall material changes.
void setDimensions(QVector3D dim)
void rotationChanged()
void reverbBrightnessChanged()
void reflectionGainChanged()
Material
Defines different materials that can be applied to the different walls of the room.
Definition qaudioroom.h:30
@ UniformMaterial
Definition qaudioroom.h:54
void setWallMaterial(Wall wall, Material material)
Sets wall to material.
void setReverbTime(float factor)
QVector3D position
Defines the position of the center of the room in 3D space.
Definition qaudioroom.h:19
float reverbTime
A factor to be applies to all reverb timings generated for this room.
Definition qaudioroom.h:24
Wall
An enum defining the 6 walls of the room.
Definition qaudioroom.h:57
void setPosition(QVector3D pos)
void setReverbBrightness(float factor)
~QAudioRoom()
Destroys the room.
void dimensionsChanged()
void setReflectionGain(float factor)
QVector3D dimensions
Defines the dimensions of the room in 3D space.
Definition qaudioroom.h:20
float reflectionGain
A gain factor for reflections generated in this room.
Definition qaudioroom.h:22
Material wallMaterial(Wall wall) const
returns the material being used for wall.
The QQuaternion class represents a quaternion consisting of a vector and scalar.
The QVector3D class represents a vector or vertex in 3D space.
Definition qvectornd.h:171
Combined button and popup list for selecting options.
QVector3D toVector(const float *f)
Definition qaudioroom.cpp:8
void toFloats(const QVector3D &v, float *f)
QQuaternion toQuaternion(const float *f)
GLsizei const GLfloat * v
[13]
GLfloat GLfloat f
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define emit
QJSEngine engine
[0]