react-native-audio-api 0.1.0 → 0.3.0-rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (217) hide show
  1. package/README.md +10 -8
  2. package/RNAudioAPI.podspec +5 -0
  3. package/android/CMakeLists.txt +21 -10
  4. package/android/libs/fftw3/arm64-v8a/libfftw3.a +0 -0
  5. package/android/libs/fftw3/armeabi-v7a/libfftw3.a +0 -0
  6. package/android/libs/fftw3/x86/libfftw3.a +0 -0
  7. package/android/libs/fftw3/x86_64/libfftw3.a +0 -0
  8. package/android/libs/include/fftw3/fftw3.h +413 -0
  9. package/android/src/main/cpp/AudioPlayer/AudioPlayer.cpp +32 -2
  10. package/android/src/main/cpp/AudioPlayer/AudioPlayer.h +6 -2
  11. package/common/cpp/AudioAPIInstaller/AudioAPIInstallerHostObject.cpp +5 -3
  12. package/common/cpp/AudioAPIInstaller/AudioAPIInstallerHostObject.h +5 -1
  13. package/common/cpp/HostObjects/AudioBufferHostObject.cpp +6 -0
  14. package/common/cpp/HostObjects/AudioBufferSourceNodeHostObject.cpp +13 -1
  15. package/common/cpp/HostObjects/AudioContextHostObject.cpp +12 -149
  16. package/common/cpp/HostObjects/AudioContextHostObject.h +8 -14
  17. package/common/cpp/HostObjects/AudioNodeHostObject.cpp +0 -6
  18. package/common/cpp/HostObjects/BaseAudioContextHostObject.cpp +240 -0
  19. package/common/cpp/HostObjects/BaseAudioContextHostObject.h +41 -0
  20. package/common/cpp/HostObjects/BiquadFilterNodeHostObject.cpp +44 -0
  21. package/common/cpp/HostObjects/OscillatorNodeHostObject.cpp +21 -0
  22. package/common/cpp/HostObjects/OscillatorNodeHostObject.h +1 -0
  23. package/common/cpp/HostObjects/PeriodicWaveHostObject.cpp +33 -0
  24. package/common/cpp/HostObjects/PeriodicWaveHostObject.h +33 -0
  25. package/common/cpp/core/AudioArray.cpp +117 -0
  26. package/common/cpp/core/AudioArray.h +48 -0
  27. package/common/cpp/core/AudioBuffer.cpp +23 -80
  28. package/common/cpp/core/AudioBuffer.h +12 -13
  29. package/common/cpp/core/AudioBufferSourceNode.cpp +112 -25
  30. package/common/cpp/core/AudioBufferSourceNode.h +10 -6
  31. package/common/cpp/core/AudioBus.cpp +518 -0
  32. package/common/cpp/core/AudioBus.h +83 -0
  33. package/common/cpp/core/AudioContext.cpp +7 -77
  34. package/common/cpp/core/AudioContext.h +3 -60
  35. package/common/cpp/core/AudioDestinationNode.cpp +27 -15
  36. package/common/cpp/core/AudioDestinationNode.h +13 -5
  37. package/common/cpp/core/AudioNode.cpp +184 -22
  38. package/common/cpp/core/AudioNode.h +37 -37
  39. package/common/cpp/core/AudioNodeManager.cpp +75 -0
  40. package/common/cpp/core/AudioNodeManager.h +42 -0
  41. package/common/cpp/core/AudioParam.cpp +8 -11
  42. package/common/cpp/core/AudioParam.h +8 -8
  43. package/common/cpp/core/AudioScheduledSourceNode.cpp +19 -5
  44. package/common/cpp/core/AudioScheduledSourceNode.h +6 -2
  45. package/common/cpp/core/BaseAudioContext.cpp +191 -0
  46. package/common/cpp/core/BaseAudioContext.h +87 -0
  47. package/common/cpp/core/BiquadFilterNode.cpp +92 -69
  48. package/common/cpp/core/BiquadFilterNode.h +53 -57
  49. package/common/cpp/core/GainNode.cpp +12 -12
  50. package/common/cpp/core/GainNode.h +5 -3
  51. package/common/cpp/core/OscillatorNode.cpp +38 -29
  52. package/common/cpp/core/OscillatorNode.h +29 -69
  53. package/common/cpp/core/ParamChange.h +6 -6
  54. package/common/cpp/core/PeriodicWave.cpp +362 -0
  55. package/common/cpp/core/PeriodicWave.h +119 -0
  56. package/common/cpp/core/StereoPannerNode.cpp +31 -30
  57. package/common/cpp/core/StereoPannerNode.h +6 -6
  58. package/common/cpp/types/BiquadFilterType.h +19 -0
  59. package/common/cpp/types/ChannelCountMode.h +10 -0
  60. package/common/cpp/types/ChannelInterpretation.h +10 -0
  61. package/common/cpp/types/ContextState.h +10 -0
  62. package/common/cpp/types/OscillatorType.h +11 -0
  63. package/common/cpp/utils/FFTFrame.h +67 -0
  64. package/common/cpp/utils/JsiPromise.cpp +59 -0
  65. package/common/cpp/utils/JsiPromise.h +42 -0
  66. package/common/cpp/utils/Locker.h +49 -0
  67. package/common/cpp/utils/VectorMath.cpp +88 -3
  68. package/common/cpp/utils/VectorMath.h +7 -1
  69. package/common/cpp/utils/android/FFTFrame.cpp +23 -0
  70. package/common/cpp/utils/ios/FFTFrame.cpp +29 -0
  71. package/common/cpp/wrappers/AudioBufferSourceNodeWrapper.cpp +10 -0
  72. package/common/cpp/wrappers/AudioBufferSourceNodeWrapper.h +3 -2
  73. package/common/cpp/wrappers/AudioBufferWrapper.h +5 -5
  74. package/common/cpp/wrappers/AudioContextWrapper.cpp +7 -60
  75. package/common/cpp/wrappers/AudioContextWrapper.h +5 -26
  76. package/common/cpp/wrappers/AudioNodeWrapper.h +5 -5
  77. package/common/cpp/wrappers/AudioParamWrapper.h +4 -4
  78. package/common/cpp/wrappers/BaseAudioContextWrapper.cpp +83 -0
  79. package/common/cpp/wrappers/BaseAudioContextWrapper.h +50 -0
  80. package/common/cpp/wrappers/BiquadFilterNodeWrapper.cpp +9 -0
  81. package/common/cpp/wrappers/BiquadFilterNodeWrapper.h +9 -4
  82. package/common/cpp/wrappers/GainNodeWrapper.h +1 -1
  83. package/common/cpp/wrappers/OscillatorNodeWrapper.cpp +6 -0
  84. package/common/cpp/wrappers/OscillatorNodeWrapper.h +5 -2
  85. package/common/cpp/wrappers/PeriodicWaveWrapper.h +17 -0
  86. package/common/cpp/wrappers/StereoPannerNodeWrapper.h +1 -1
  87. package/ios/AudioAPIModule.h +20 -1
  88. package/ios/AudioAPIModule.mm +6 -4
  89. package/ios/AudioDecoder/AudioDecoder.h +17 -0
  90. package/ios/AudioDecoder/AudioDecoder.m +167 -0
  91. package/ios/AudioDecoder/IOSAudioDecoder.h +26 -0
  92. package/ios/AudioDecoder/IOSAudioDecoder.mm +40 -0
  93. package/ios/AudioPlayer/AudioPlayer.h +3 -2
  94. package/ios/AudioPlayer/AudioPlayer.m +14 -17
  95. package/ios/AudioPlayer/IOSAudioPlayer.h +7 -3
  96. package/ios/AudioPlayer/IOSAudioPlayer.mm +31 -7
  97. package/lib/module/core/AudioBuffer.js +37 -0
  98. package/lib/module/core/AudioBuffer.js.map +1 -0
  99. package/lib/module/core/AudioBufferSourceNode.js +28 -0
  100. package/lib/module/core/AudioBufferSourceNode.js.map +1 -0
  101. package/lib/module/core/AudioContext.js +10 -0
  102. package/lib/module/core/AudioContext.js.map +1 -0
  103. package/lib/module/core/AudioDestinationNode.js +7 -0
  104. package/lib/module/core/AudioDestinationNode.js.map +1 -0
  105. package/lib/module/core/AudioNode.js +22 -0
  106. package/lib/module/core/AudioNode.js.map +1 -0
  107. package/lib/module/core/AudioParam.js +35 -0
  108. package/lib/module/core/AudioParam.js.map +1 -0
  109. package/lib/module/core/AudioScheduledSourceNode.js +28 -0
  110. package/lib/module/core/AudioScheduledSourceNode.js.map +1 -0
  111. package/lib/module/core/BaseAudioContext.js +62 -0
  112. package/lib/module/core/BaseAudioContext.js.map +1 -0
  113. package/lib/module/core/BiquadFilterNode.js +25 -0
  114. package/lib/module/core/BiquadFilterNode.js.map +1 -0
  115. package/lib/module/core/GainNode.js +9 -0
  116. package/lib/module/core/GainNode.js.map +1 -0
  117. package/lib/module/core/OscillatorNode.js +24 -0
  118. package/lib/module/core/OscillatorNode.js.map +1 -0
  119. package/lib/module/core/PeriodicWave.js +8 -0
  120. package/lib/module/core/PeriodicWave.js.map +1 -0
  121. package/lib/module/core/StereoPannerNode.js +9 -0
  122. package/lib/module/core/StereoPannerNode.js.map +1 -0
  123. package/lib/module/core/types.js.map +1 -0
  124. package/lib/module/errors/IndexSizeError.js +8 -0
  125. package/lib/module/errors/IndexSizeError.js.map +1 -0
  126. package/lib/module/errors/InvalidAccessError.js +8 -0
  127. package/lib/module/errors/InvalidAccessError.js.map +1 -0
  128. package/lib/module/errors/InvalidStateError.js +8 -0
  129. package/lib/module/errors/InvalidStateError.js.map +1 -0
  130. package/lib/module/errors/RangeError.js +8 -0
  131. package/lib/module/errors/RangeError.js.map +1 -0
  132. package/lib/module/errors/index.js +5 -0
  133. package/lib/module/errors/index.js.map +1 -0
  134. package/lib/module/index.js +212 -15
  135. package/lib/module/index.js.map +1 -1
  136. package/lib/module/index.native.js +18 -0
  137. package/lib/module/index.native.js.map +1 -0
  138. package/lib/module/interfaces.js +2 -0
  139. package/lib/module/interfaces.js.map +1 -0
  140. package/lib/module/utils/resolveAudioSource.js +10 -0
  141. package/lib/module/utils/resolveAudioSource.js.map +1 -0
  142. package/lib/typescript/core/AudioBuffer.d.ts +12 -0
  143. package/lib/typescript/core/AudioBuffer.d.ts.map +1 -0
  144. package/lib/typescript/core/AudioBufferSourceNode.d.ts +12 -0
  145. package/lib/typescript/core/AudioBufferSourceNode.d.ts.map +1 -0
  146. package/lib/typescript/core/AudioContext.d.ts +6 -0
  147. package/lib/typescript/core/AudioContext.d.ts.map +1 -0
  148. package/lib/typescript/core/AudioDestinationNode.d.ts +7 -0
  149. package/lib/typescript/core/AudioDestinationNode.d.ts.map +1 -0
  150. package/lib/typescript/core/AudioNode.d.ts +16 -0
  151. package/lib/typescript/core/AudioNode.d.ts.map +1 -0
  152. package/lib/typescript/core/AudioParam.d.ts +14 -0
  153. package/lib/typescript/core/AudioParam.d.ts.map +1 -0
  154. package/lib/typescript/core/AudioScheduledSourceNode.d.ts +10 -0
  155. package/lib/typescript/core/AudioScheduledSourceNode.d.ts.map +1 -0
  156. package/lib/typescript/core/BaseAudioContext.d.ts +27 -0
  157. package/lib/typescript/core/BaseAudioContext.d.ts.map +1 -0
  158. package/lib/typescript/core/BiquadFilterNode.d.ts +16 -0
  159. package/lib/typescript/core/BiquadFilterNode.d.ts.map +1 -0
  160. package/lib/typescript/core/GainNode.d.ts +9 -0
  161. package/lib/typescript/core/GainNode.d.ts.map +1 -0
  162. package/lib/typescript/core/OscillatorNode.d.ts +15 -0
  163. package/lib/typescript/core/OscillatorNode.d.ts.map +1 -0
  164. package/lib/typescript/core/PeriodicWave.d.ts +5 -0
  165. package/lib/typescript/core/PeriodicWave.d.ts.map +1 -0
  166. package/lib/typescript/core/StereoPannerNode.d.ts +9 -0
  167. package/lib/typescript/core/StereoPannerNode.d.ts.map +1 -0
  168. package/lib/typescript/core/types.d.ts +15 -0
  169. package/lib/typescript/core/types.d.ts.map +1 -0
  170. package/lib/typescript/errors/IndexSizeError.d.ts +5 -0
  171. package/lib/typescript/errors/IndexSizeError.d.ts.map +1 -0
  172. package/lib/typescript/errors/InvalidAccessError.d.ts +5 -0
  173. package/lib/typescript/errors/InvalidAccessError.d.ts.map +1 -0
  174. package/lib/typescript/errors/InvalidStateError.d.ts +5 -0
  175. package/lib/typescript/errors/InvalidStateError.d.ts.map +1 -0
  176. package/lib/typescript/errors/RangeError.d.ts +5 -0
  177. package/lib/typescript/errors/RangeError.d.ts.map +1 -0
  178. package/lib/typescript/errors/index.d.ts +5 -0
  179. package/lib/typescript/errors/index.d.ts.map +1 -0
  180. package/lib/typescript/index.d.ts +88 -5
  181. package/lib/typescript/index.d.ts.map +1 -1
  182. package/lib/typescript/index.native.d.ts +14 -0
  183. package/lib/typescript/index.native.d.ts.map +1 -0
  184. package/lib/typescript/interfaces.d.ts +79 -0
  185. package/lib/typescript/interfaces.d.ts.map +1 -0
  186. package/lib/typescript/utils/resolveAudioSource.d.ts +3 -0
  187. package/lib/typescript/utils/resolveAudioSource.d.ts.map +1 -0
  188. package/package.json +4 -2
  189. package/src/core/AudioBuffer.ts +68 -0
  190. package/src/core/AudioBufferSourceNode.ts +35 -0
  191. package/src/core/AudioContext.ts +12 -0
  192. package/src/core/AudioDestinationNode.ts +9 -0
  193. package/src/core/AudioNode.ts +38 -0
  194. package/src/core/AudioParam.ts +55 -0
  195. package/src/core/AudioScheduledSourceNode.ts +43 -0
  196. package/src/core/BaseAudioContext.ts +108 -0
  197. package/src/core/BiquadFilterNode.ts +49 -0
  198. package/src/core/GainNode.ts +13 -0
  199. package/src/core/OscillatorNode.ts +37 -0
  200. package/src/core/PeriodicWave.ts +10 -0
  201. package/src/core/StereoPannerNode.ts +13 -0
  202. package/src/core/types.ts +33 -0
  203. package/src/errors/IndexSizeError.ts +8 -0
  204. package/src/errors/InvalidAccessError.ts +8 -0
  205. package/src/errors/InvalidStateError.ts +8 -0
  206. package/src/errors/RangeError.ts +8 -0
  207. package/src/errors/index.ts +4 -0
  208. package/src/index.native.ts +25 -0
  209. package/src/index.ts +380 -40
  210. package/src/interfaces.ts +121 -0
  211. package/src/modules/global.d.ts +3 -3
  212. package/src/utils/resolveAudioSource.ts +14 -0
  213. package/lib/module/types.js.map +0 -1
  214. package/lib/typescript/types.d.ts +0 -76
  215. package/lib/typescript/types.d.ts.map +0 -1
  216. package/src/types.ts +0 -108
  217. /package/lib/module/{types.js → core/types.js} +0 -0
@@ -0,0 +1,518 @@
1
+ #include <algorithm>
2
+
3
+ #include "AudioArray.h"
4
+ #include "AudioBus.h"
5
+ #include "BaseAudioContext.h"
6
+ #include "Constants.h"
7
+ #include "VectorMath.h"
8
+
9
+ // Implementation of channel summing/mixing is based on the WebKit approach,
10
+ // source:
11
+ // https://github.com/WebKit/WebKit/blob/main/Source/WebCore/platform/audio/AudioBus.cpp
12
+
13
+ const float SQRT_HALF = sqrtf(0.5f);
14
+
15
+ namespace audioapi {
16
+
17
+ /**
18
+ * Public interfaces - memory management
19
+ */
20
+ AudioBus::AudioBus(int sampleRate, int size)
21
+ : numberOfChannels_(CHANNEL_COUNT), sampleRate_(sampleRate), size_(size) {
22
+ createChannels();
23
+ }
24
+
25
+ AudioBus::AudioBus(int sampleRate, int size, int numberOfChannels)
26
+ : numberOfChannels_(numberOfChannels),
27
+ sampleRate_(sampleRate),
28
+ size_(size) {
29
+ createChannels();
30
+ }
31
+
32
+ AudioBus::~AudioBus() {
33
+ channels_.clear();
34
+ }
35
+
36
+ /**
37
+ * Public interfaces - getters
38
+ */
39
+
40
+ int AudioBus::getNumberOfChannels() const {
41
+ return numberOfChannels_;
42
+ }
43
+
44
+ int AudioBus::getSampleRate() const {
45
+ return sampleRate_;
46
+ }
47
+
48
+ int AudioBus::getSize() const {
49
+ return size_;
50
+ }
51
+
52
+ AudioArray *AudioBus::getChannel(int index) const {
53
+ return channels_[index].get();
54
+ }
55
+
56
+ AudioArray *AudioBus::getChannelByType(int channelType) const {
57
+ switch (getNumberOfChannels()) {
58
+ case 1: // mono
59
+ if (channelType == ChannelMono || channelType == ChannelLeft) {
60
+ return getChannel(0);
61
+ }
62
+ return nullptr;
63
+
64
+ case 2: // stereo
65
+ switch (channelType) {
66
+ case ChannelLeft:
67
+ return getChannel(0);
68
+ case ChannelRight:
69
+ return getChannel(1);
70
+ default:
71
+ return nullptr;
72
+ }
73
+
74
+ case 4: // quad
75
+ switch (channelType) {
76
+ case ChannelLeft:
77
+ return getChannel(0);
78
+ case ChannelRight:
79
+ return getChannel(1);
80
+ case ChannelSurroundLeft:
81
+ return getChannel(2);
82
+ case ChannelSurroundRight:
83
+ return getChannel(3);
84
+ default:
85
+ return nullptr;
86
+ }
87
+
88
+ case 5: // 5.0
89
+ switch (channelType) {
90
+ case ChannelLeft:
91
+ return getChannel(0);
92
+ case ChannelRight:
93
+ return getChannel(1);
94
+ case ChannelCenter:
95
+ return getChannel(2);
96
+ case ChannelSurroundLeft:
97
+ return getChannel(3);
98
+ case ChannelSurroundRight:
99
+ return getChannel(4);
100
+ default:
101
+ return nullptr;
102
+ }
103
+
104
+ case 6: // 5.1
105
+ switch (channelType) {
106
+ case ChannelLeft:
107
+ return getChannel(0);
108
+ case ChannelRight:
109
+ return getChannel(1);
110
+ case ChannelCenter:
111
+ return getChannel(2);
112
+ case ChannelLFE:
113
+ return getChannel(3);
114
+ case ChannelSurroundLeft:
115
+ return getChannel(4);
116
+ case ChannelSurroundRight:
117
+ return getChannel(5);
118
+ default:
119
+ return nullptr;
120
+ }
121
+ default:
122
+ return nullptr;
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Public interfaces - audio processing and setters
128
+ */
129
+
130
+ void AudioBus::zero() {
131
+ zero(0, getSize());
132
+ }
133
+
134
+ void AudioBus::zero(int start, int length) {
135
+ for (auto it = channels_.begin(); it != channels_.end(); it += 1) {
136
+ it->get()->zero(start, length);
137
+ }
138
+ }
139
+
140
+ void AudioBus::normalize() {
141
+ float maxAbsValue = this->maxAbsValue();
142
+
143
+ if (maxAbsValue == 0.0f || maxAbsValue == 1.0f) {
144
+ return;
145
+ }
146
+
147
+ float scale = 1.0f / maxAbsValue;
148
+ this->scale(scale);
149
+ }
150
+
151
+ void AudioBus::scale(float value) {
152
+ for (auto &channel : channels_) {
153
+ channel->scale(value);
154
+ }
155
+ }
156
+
157
+ float AudioBus::maxAbsValue() const {
158
+ float maxAbsValue = 1.0f;
159
+
160
+ for (const auto &channel : channels_) {
161
+ float channelMaxAbsValue = channel->getMaxAbsValue();
162
+ maxAbsValue = std::max(maxAbsValue, channelMaxAbsValue);
163
+ }
164
+
165
+ return maxAbsValue;
166
+ }
167
+
168
+ void AudioBus::sum(const AudioBus *source) {
169
+ sum(source, 0, 0, getSize());
170
+ }
171
+
172
+ void AudioBus::sum(const AudioBus *source, int start, int length) {
173
+ sum(source, start, start, length);
174
+ }
175
+
176
+ void AudioBus::sum(
177
+ const AudioBus *source,
178
+ int sourceStart,
179
+ int destinationStart,
180
+ int length) {
181
+ if (source == this) {
182
+ return;
183
+ }
184
+
185
+ int numberOfSourceChannels = source->getNumberOfChannels();
186
+ int numberOfChannels = getNumberOfChannels();
187
+
188
+ // TODO: consider adding ability to enforce discrete summing (if/when it will
189
+ // be useful). Source channel count is smaller than current bus, we need to
190
+ // up-mix.
191
+ if (numberOfSourceChannels < numberOfChannels) {
192
+ sumByUpMixing(source, sourceStart, destinationStart, length);
193
+ return;
194
+ }
195
+
196
+ // Source channel count is larger than current bus, we need to down-mix.
197
+ if (numberOfSourceChannels > numberOfChannels) {
198
+ sumByDownMixing(source, sourceStart, destinationStart, length);
199
+ return;
200
+ }
201
+
202
+ // Source and destination channel counts are the same. Just sum the channels.
203
+ for (int i = 0; i < numberOfChannels_; i += 1) {
204
+ getChannel(i)->sum(
205
+ source->getChannel(i), sourceStart, destinationStart, length);
206
+ }
207
+ }
208
+
209
+ void AudioBus::copy(const AudioBus *source) {
210
+ copy(source, 0, 0, getSize());
211
+ }
212
+
213
+ void AudioBus::copy(const AudioBus *source, int start, int length) {
214
+ copy(source, start, start, length);
215
+ }
216
+
217
+ void AudioBus::copy(
218
+ const AudioBus *source,
219
+ int sourceStart,
220
+ int destinationStart,
221
+ int length) {
222
+ if (source == this) {
223
+ return;
224
+ }
225
+
226
+ if (source->getNumberOfChannels() == getNumberOfChannels()) {
227
+ for (int i = 0; i < getNumberOfChannels(); i += 1) {
228
+ getChannel(i)->copy(
229
+ source->getChannel(i), sourceStart, destinationStart, length);
230
+ }
231
+
232
+ return;
233
+ }
234
+
235
+ // zero + sum is equivalent to copy, but takes care of up/down-mixing.
236
+ zero(destinationStart, length);
237
+ sum(source, sourceStart, destinationStart, length);
238
+ }
239
+
240
+ /**
241
+ * Internal tooling - channel initialization
242
+ */
243
+
244
+ void AudioBus::createChannels() {
245
+ channels_ = std::vector<std::shared_ptr<AudioArray>>(numberOfChannels_);
246
+
247
+ for (int i = 0; i < numberOfChannels_; i += 1) {
248
+ channels_[i] = std::make_shared<AudioArray>(size_);
249
+ }
250
+ }
251
+
252
+ /**
253
+ * Internal tooling - channel summing
254
+ */
255
+
256
+ void AudioBus::discreteSum(
257
+ const AudioBus *source,
258
+ int sourceStart,
259
+ int destinationStart,
260
+ int length) const {
261
+ int numberOfChannels =
262
+ std::min(getNumberOfChannels(), source->getNumberOfChannels());
263
+
264
+ // In case of source > destination, we "down-mix" and drop the extra channels.
265
+ // In case of source < destination, we "up-mix" as many channels as we have,
266
+ // leaving the remaining channels untouched.
267
+ for (int i = 0; i < numberOfChannels; i++) {
268
+ getChannel(i)->sum(
269
+ source->getChannel(i), sourceStart, destinationStart, length);
270
+ }
271
+ }
272
+
273
+ void AudioBus::sumByUpMixing(
274
+ const AudioBus *source,
275
+ int sourceStart,
276
+ int destinationStart,
277
+ int length) {
278
+ int numberOfSourceChannels = source->getNumberOfChannels();
279
+ int numberOfChannels = getNumberOfChannels();
280
+
281
+ // Mono to stereo (1 -> 2, 4)
282
+ if (numberOfSourceChannels == 1 &&
283
+ (numberOfChannels == 2 || numberOfChannels == 4)) {
284
+ AudioArray *sourceChannel = source->getChannelByType(ChannelMono);
285
+
286
+ getChannelByType(ChannelLeft)
287
+ ->sum(sourceChannel, sourceStart, destinationStart, length);
288
+ getChannelByType(ChannelRight)
289
+ ->sum(sourceChannel, sourceStart, destinationStart, length);
290
+ return;
291
+ }
292
+
293
+ // Mono to 5.1 (1 -> 6)
294
+ if (numberOfSourceChannels == 1 && numberOfChannels == 6) {
295
+ AudioArray *sourceChannel = source->getChannel(0);
296
+
297
+ getChannelByType(ChannelCenter)
298
+ ->sum(sourceChannel, sourceStart, destinationStart, length);
299
+ return;
300
+ }
301
+
302
+ // Stereo 2 to stereo 4 or 5.1 (2 -> 4, 6)
303
+ if (numberOfSourceChannels == 2 &&
304
+ (numberOfChannels == 4 || numberOfChannels == 6)) {
305
+ getChannelByType(ChannelLeft)
306
+ ->sum(
307
+ source->getChannelByType(ChannelLeft),
308
+ sourceStart,
309
+ destinationStart,
310
+ length);
311
+ getChannelByType(ChannelRight)
312
+ ->sum(
313
+ source->getChannelByType(ChannelRight),
314
+ sourceStart,
315
+ destinationStart,
316
+ length);
317
+ return;
318
+ }
319
+
320
+ // Stereo 4 to 5.1 (4 -> 6)
321
+ if (numberOfSourceChannels == 4 && numberOfChannels == 6) {
322
+ getChannelByType(ChannelLeft)
323
+ ->sum(
324
+ source->getChannelByType(ChannelLeft),
325
+ sourceStart,
326
+ destinationStart,
327
+ length);
328
+ getChannelByType(ChannelRight)
329
+ ->sum(
330
+ source->getChannelByType(ChannelRight),
331
+ sourceStart,
332
+ destinationStart,
333
+ length);
334
+ getChannelByType(ChannelSurroundLeft)
335
+ ->sum(
336
+ source->getChannelByType(ChannelSurroundLeft),
337
+ sourceStart,
338
+ destinationStart,
339
+ length);
340
+ getChannelByType(ChannelSurroundRight)
341
+ ->sum(
342
+ source->getChannelByType(ChannelSurroundRight),
343
+ sourceStart,
344
+ destinationStart,
345
+ length);
346
+ return;
347
+ }
348
+
349
+ discreteSum(source, sourceStart, destinationStart, length);
350
+ }
351
+
352
+ void AudioBus::sumByDownMixing(
353
+ const AudioBus *source,
354
+ int sourceStart,
355
+ int destinationStart,
356
+ int length) {
357
+ int numberOfSourceChannels = source->getNumberOfChannels();
358
+ int numberOfChannels = getNumberOfChannels();
359
+
360
+ // Stereo to mono (2 -> 1): output += 0.5 * (input.left + input.right).
361
+ if (numberOfSourceChannels == 2 && numberOfChannels == 1) {
362
+ float *sourceLeft = source->getChannelByType(ChannelLeft)->getData();
363
+ float *sourceRight = source->getChannelByType(ChannelRight)->getData();
364
+
365
+ float *destinationData = getChannelByType(ChannelMono)->getData();
366
+
367
+ VectorMath::multiplyByScalarThenAddToOutput(
368
+ sourceLeft + sourceStart,
369
+ 0.5f,
370
+ destinationData + destinationStart,
371
+ length);
372
+ VectorMath::multiplyByScalarThenAddToOutput(
373
+ sourceRight + sourceStart,
374
+ 0.5f,
375
+ destinationData + destinationStart,
376
+ length);
377
+ return;
378
+ }
379
+
380
+ // Stereo 4 to mono: output += 0.25 * (input.left + input.right +
381
+ // input.surroundLeft + input.surroundRight)
382
+ if (numberOfSourceChannels == 4 && numberOfChannels == 1) {
383
+ float *sourceLeft = source->getChannelByType(ChannelLeft)->getData();
384
+ float *sourceRight = source->getChannelByType(ChannelRight)->getData();
385
+ float *sourceSurroundLeft =
386
+ source->getChannelByType(ChannelSurroundLeft)->getData();
387
+ float *sourceSurroundRight =
388
+ source->getChannelByType(ChannelSurroundRight)->getData();
389
+
390
+ float *destinationData = getChannelByType(ChannelMono)->getData();
391
+
392
+ VectorMath::multiplyByScalarThenAddToOutput(
393
+ sourceLeft + sourceStart,
394
+ 0.25f,
395
+ destinationData + destinationStart,
396
+ length);
397
+ VectorMath::multiplyByScalarThenAddToOutput(
398
+ sourceRight + sourceStart,
399
+ 0.25f,
400
+ destinationData + destinationStart,
401
+ length);
402
+ VectorMath::multiplyByScalarThenAddToOutput(
403
+ sourceSurroundLeft + sourceStart,
404
+ 0.25f,
405
+ destinationData + destinationStart,
406
+ length);
407
+ VectorMath::multiplyByScalarThenAddToOutput(
408
+ sourceSurroundRight + sourceStart,
409
+ 0.25f,
410
+ destinationData + destinationStart,
411
+ length);
412
+ return;
413
+ }
414
+
415
+ // 5.1 to stereo:
416
+ // output.left += input.left + sqrt(1/2) * (input.center + input.surroundLeft)
417
+ // output.right += input.right + sqrt(1/2) * (input.center +
418
+ // input.surroundRight)
419
+ if (numberOfSourceChannels == 6 && numberOfChannels == 2) {
420
+ float *sourceLeft = source->getChannelByType(ChannelLeft)->getData();
421
+ float *sourceRight = source->getChannelByType(ChannelRight)->getData();
422
+ float *sourceCenter = source->getChannelByType(ChannelCenter)->getData();
423
+ float *sourceSurroundLeft =
424
+ source->getChannelByType(ChannelSurroundLeft)->getData();
425
+ float *sourceSurroundRight =
426
+ source->getChannelByType(ChannelSurroundRight)->getData();
427
+
428
+ float *destinationLeft = getChannelByType(ChannelLeft)->getData();
429
+ float *destinationRight = getChannelByType(ChannelRight)->getData();
430
+
431
+ VectorMath::add(
432
+ sourceLeft + sourceStart,
433
+ destinationLeft + destinationStart,
434
+ destinationLeft + destinationStart,
435
+ length);
436
+ VectorMath::multiplyByScalarThenAddToOutput(
437
+ sourceCenter + sourceStart,
438
+ SQRT_HALF,
439
+ destinationLeft + destinationStart,
440
+ length);
441
+ VectorMath::multiplyByScalarThenAddToOutput(
442
+ sourceSurroundLeft + sourceStart,
443
+ SQRT_HALF,
444
+ destinationLeft + destinationStart,
445
+ length);
446
+
447
+ VectorMath::add(
448
+ sourceRight + sourceStart,
449
+ destinationRight + destinationStart,
450
+ destinationRight + destinationStart,
451
+ length);
452
+ VectorMath::multiplyByScalarThenAddToOutput(
453
+ sourceCenter + sourceStart,
454
+ SQRT_HALF,
455
+ destinationRight + destinationStart,
456
+ length);
457
+ VectorMath::multiplyByScalarThenAddToOutput(
458
+ sourceSurroundRight + sourceStart,
459
+ SQRT_HALF,
460
+ destinationRight + destinationStart,
461
+ length);
462
+ return;
463
+ }
464
+
465
+ // 5.1 to stereo 4:
466
+ // output.left += input.left + sqrt(1/2) * input.center
467
+ // output.right += input.right + sqrt(1/2) * input.center
468
+ // output.surroundLeft += input.surroundLeft
469
+ // output.surroundRight += input.surroundRight
470
+ if (numberOfSourceChannels == 6 && numberOfChannels == 4) {
471
+ float *sourceLeft = source->getChannelByType(ChannelLeft)->getData();
472
+ float *sourceRight = source->getChannelByType(ChannelRight)->getData();
473
+ float *sourceCenter = source->getChannelByType(ChannelCenter)->getData();
474
+ float *sourceSurroundLeft =
475
+ source->getChannelByType(ChannelSurroundLeft)->getData();
476
+ float *sourceSurroundRight =
477
+ source->getChannelByType(ChannelSurroundRight)->getData();
478
+
479
+ float *destinationLeft = getChannelByType(ChannelLeft)->getData();
480
+ float *destinationRight = getChannelByType(ChannelRight)->getData();
481
+ float *destinationSurroundLeft =
482
+ getChannelByType(ChannelSurroundLeft)->getData();
483
+ float *destinationSurroundRight =
484
+ getChannelByType(ChannelSurroundRight)->getData();
485
+
486
+ VectorMath::add(
487
+ sourceLeft + sourceStart,
488
+ destinationLeft + destinationStart,
489
+ destinationLeft + destinationStart,
490
+ length);
491
+ VectorMath::multiplyByScalarThenAddToOutput(
492
+ sourceCenter, SQRT_HALF, destinationLeft + destinationStart, length);
493
+
494
+ VectorMath::add(
495
+ sourceRight + sourceStart,
496
+ destinationRight + destinationStart,
497
+ destinationRight + destinationStart,
498
+ length);
499
+ VectorMath::multiplyByScalarThenAddToOutput(
500
+ sourceCenter, SQRT_HALF, destinationRight + destinationStart, length);
501
+
502
+ VectorMath::add(
503
+ sourceSurroundLeft + sourceStart,
504
+ destinationSurroundLeft + destinationStart,
505
+ destinationSurroundLeft + destinationStart,
506
+ length);
507
+ VectorMath::add(
508
+ sourceSurroundRight + sourceStart,
509
+ destinationSurroundRight + destinationStart,
510
+ destinationSurroundRight + destinationStart,
511
+ length);
512
+ return;
513
+ }
514
+
515
+ discreteSum(source, sourceStart, destinationStart, length);
516
+ }
517
+
518
+ } // namespace audioapi
@@ -0,0 +1,83 @@
1
+ #pragma once
2
+
3
+ #include <algorithm>
4
+ #include <memory>
5
+ #include <vector>
6
+
7
+ namespace audioapi {
8
+
9
+ class BaseAudioContext;
10
+ class AudioArray;
11
+
12
+ class AudioBus {
13
+ public:
14
+ enum {
15
+ ChannelMono = 0,
16
+ ChannelLeft = 0,
17
+ ChannelRight = 1,
18
+ ChannelCenter = 2,
19
+ ChannelLFE = 3,
20
+ ChannelSurroundLeft = 4,
21
+ ChannelSurroundRight = 5,
22
+ };
23
+
24
+ explicit AudioBus(int sampleRate, int size);
25
+ explicit AudioBus(int sampleRate, int size, int numberOfChannels);
26
+
27
+ ~AudioBus();
28
+
29
+ [[nodiscard]] int getNumberOfChannels() const;
30
+ [[nodiscard]] int getSampleRate() const;
31
+ [[nodiscard]] int getSize() const;
32
+ [[nodiscard]] AudioArray *getChannel(int index) const;
33
+ [[nodiscard]] AudioArray *getChannelByType(int channelType) const;
34
+
35
+ void normalize();
36
+ void scale(float value);
37
+ [[nodiscard]] float maxAbsValue() const;
38
+
39
+ void zero();
40
+ void zero(int start, int length);
41
+
42
+ void sum(const AudioBus *source);
43
+ void sum(const AudioBus *source, int start, int length);
44
+ void sum(
45
+ const AudioBus *source,
46
+ int sourceStart,
47
+ int destinationStart,
48
+ int length);
49
+
50
+ void copy(const AudioBus *source);
51
+ void copy(const AudioBus *source, int start, int length);
52
+ void copy(
53
+ const AudioBus *source,
54
+ int sourceStart,
55
+ int destinationStart,
56
+ int length);
57
+
58
+ private:
59
+ std::vector<std::shared_ptr<AudioArray>> channels_;
60
+
61
+ int numberOfChannels_;
62
+ int sampleRate_;
63
+ int size_;
64
+
65
+ void createChannels();
66
+ void discreteSum(
67
+ const AudioBus *source,
68
+ int sourceStart,
69
+ int destinationStart,
70
+ int length) const;
71
+ void sumByUpMixing(
72
+ const AudioBus *source,
73
+ int sourceStart,
74
+ int destinationStart,
75
+ int length);
76
+ void sumByDownMixing(
77
+ const AudioBus *source,
78
+ int sourceStart,
79
+ int destinationStart,
80
+ int length);
81
+ };
82
+
83
+ } // namespace audioapi
@@ -1,90 +1,20 @@
1
- #include "AudioContext.h"
2
-
3
- namespace audioapi {
4
-
5
- AudioContext::AudioContext() {
6
1
  #ifdef ANDROID
7
- audioPlayer_ = std::make_shared<AudioPlayer>(this->renderAudio());
2
+ #include "AudioPlayer.h"
8
3
  #else
9
- audioPlayer_ = std::make_shared<IOSAudioPlayer>(this->renderAudio());
4
+ #include "IOSAudioPlayer.h"
10
5
  #endif
11
- destination_ = std::make_shared<AudioDestinationNode>(this);
12
6
 
13
- sampleRate_ = audioPlayer_->getSampleRate();
7
+ #include "AudioContext.h"
14
8
 
15
- auto now = std::chrono::high_resolution_clock ::now();
16
- contextStartTime_ =
17
- static_cast<double>(std::chrono::duration_cast<std::chrono::nanoseconds>(
18
- now.time_since_epoch())
19
- .count());
9
+ namespace audioapi {
20
10
 
11
+ AudioContext::AudioContext() : BaseAudioContext() {
21
12
  audioPlayer_->start();
22
13
  }
23
14
 
24
- std::string AudioContext::getState() {
25
- return AudioContext::toString(state_);
26
- }
27
-
28
- int AudioContext::getSampleRate() const {
29
- return sampleRate_;
30
- }
31
-
32
- double AudioContext::getCurrentTime() const {
33
- auto now = std::chrono::high_resolution_clock ::now();
34
- auto currentTime =
35
- static_cast<double>(std::chrono::duration_cast<std::chrono::nanoseconds>(
36
- now.time_since_epoch())
37
- .count());
38
- return (currentTime - contextStartTime_) / 1e9;
39
- }
40
-
41
15
  void AudioContext::close() {
42
- state_ = State::CLOSED;
43
-
44
- if (audioPlayer_) {
45
- audioPlayer_->stop();
46
- }
47
-
48
- audioPlayer_.reset();
49
- destination_.reset();
50
- }
51
-
52
- std::shared_ptr<AudioDestinationNode> AudioContext::getDestination() {
53
- return destination_;
54
- }
55
-
56
- std::shared_ptr<OscillatorNode> AudioContext::createOscillator() {
57
- return std::make_shared<OscillatorNode>(this);
58
- }
59
-
60
- std::shared_ptr<GainNode> AudioContext::createGain() {
61
- return std::make_shared<GainNode>(this);
62
- }
63
-
64
- std::shared_ptr<StereoPannerNode> AudioContext::createStereoPanner() {
65
- return std::make_shared<StereoPannerNode>(this);
16
+ state_ = ContextState::CLOSED;
17
+ audioPlayer_->stop();
66
18
  }
67
19
 
68
- std::shared_ptr<BiquadFilterNode> AudioContext::createBiquadFilter() {
69
- return std::make_shared<BiquadFilterNode>(this);
70
- }
71
-
72
- std::shared_ptr<AudioBufferSourceNode> AudioContext::createBufferSource() {
73
- return std::make_shared<AudioBufferSourceNode>(this);
74
- }
75
-
76
- std::shared_ptr<AudioBuffer>
77
- AudioContext::createBuffer(int numberOfChannels, int length, int sampleRate) {
78
- return std::make_shared<AudioBuffer>(numberOfChannels, length, sampleRate);
79
- }
80
-
81
- std::function<void(float *, int)> AudioContext::renderAudio() {
82
- if (state_ == State::CLOSED) {
83
- return [](float *, int) {};
84
- }
85
-
86
- return [this](float *data, int frames) {
87
- destination_->renderAudio(data, frames);
88
- };
89
- }
90
20
  } // namespace audioapi