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
4#ifndef QAUDIOENGINE_P_H
5#define QAUDIOENGINE_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of other Qt classes. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <qtspatialaudioglobal_p.h>
19#include <qaudioengine.h>
20#include <qaudiodevice.h>
21#include <qaudiodecoder.h>
22#include <qthread.h>
23#include <qmutex.h>
24#include <qurl.h>
25#include <qaudiobuffer.h>
26#include <qvector3d.h>
27#include <qfile.h>
28
29namespace vraudio {
30class ResonanceAudio;
31}
32
33QT_BEGIN_NAMESPACE
34
35class QSpatialSound;
36class QAmbientSound;
37class QAudioSink;
38class QAudioOutputStream;
39class QAmbisonicDecoder;
40class QAudioDecoder;
41class QAudioRoom;
42class QAudioListener;
43
44class QAudioEnginePrivate
45{
46public:
47 static QAudioEnginePrivate *get(QAudioEngine *engine) { return engine ? engine->d : nullptr; }
48
49 static constexpr int bufferSize = 128;
50
51 QAudioEnginePrivate();
52 ~QAudioEnginePrivate();
53 vraudio::ResonanceAudio *resonanceAudio = nullptr;
54 int sampleRate = 44100;
55 float masterVolume = 1.;
56 QAudioEngine::OutputMode outputMode = QAudioEngine::Surround;
57 bool roomEffectsEnabled = true;
58
59 // Resonance Audio uses meters internally, while Qt Quick 3D and our API uses cm by default.
60 // To make things independent from the scale setting, we store all distances in meters internally
61 // and convert in the setters and getters.
62 float distanceScale = 0.01f;
63
64 QMutex mutex;
65 QAudioDevice device;
66 QAtomicInteger<bool> paused = false;
67
68 QThread audioThread;
69 std::unique_ptr<QAudioOutputStream> outputStream;
70 std::unique_ptr<QAmbisonicDecoder> ambisonicDecoder;
71
72 QAudioListener *listener = nullptr;
73 QList<QSpatialSound *> sources;
74 QList<QAmbientSound *> stereoSources;
75 QList<QAudioRoom *> rooms;
76 mutable bool listenerPositionDirty = true;
77 QAudioRoom *currentRoom = nullptr;
78
79 void addSpatialSound(QSpatialSound *sound);
80 void removeSpatialSound(QSpatialSound *sound);
81 void addStereoSound(QAmbientSound *sound);
82 void removeStereoSound(QAmbientSound *sound);
83
84 void addRoom(QAudioRoom *room);
85 void removeRoom(QAudioRoom *room);
86 void updateRooms();
87
88 QVector3D listenerPosition() const;
89};
90
91class QAmbientSoundPrivate : public QObject
92{
93public:
94 QAmbientSoundPrivate(QObject *parent, int nchannels = 2)
95 : QObject(parent)
96 , nchannels(nchannels)
97 {}
98
99 template<typename T>
100 static QAmbientSoundPrivate *get(T *soundSource) { return soundSource ? soundSource->d : nullptr; }
101
102 QUrl url;
103 float volume = 1.;
104 int nchannels = 2;
105 std::unique_ptr<QAudioDecoder> decoder;
106 std::unique_ptr<QFile> sourceDeviceFile;
107 QAudioEngine *engine = nullptr;
108
109 QMutex mutex;
110 int currentBuffer = 0;
111 int bufPos = 0;
112 int m_currentLoop = 0;
113 QList<QAudioBuffer> buffers;
114 int sourceId = -1; // kInvalidSourceId
115
116 QAtomicInteger<bool> m_autoPlay = true;
117 QAtomicInteger<bool> m_playing = false;
118 QAtomicInt m_loops = 1;
119 bool m_loading = false;
120
121 void play() {
122 m_playing = true;
123 }
124 void pause() {
125 m_playing = false;
126 }
127 void stop() {
128 QMutexLocker locker(&mutex);
129 m_playing = false;
130 currentBuffer = 0;
131 bufPos = 0;
132 m_currentLoop = 0;
133 }
134
135 void load();
136 void getBuffer(float *buf, int frames, int channels);
137
138private Q_SLOTS:
139 void bufferReady();
140 void finished();
141
142};
143
144QT_END_NAMESPACE
145
146#endif
147

source code of qtmultimedia/src/spatialaudio/qaudioengine_p.h