react-native-audio-api 0.4.0 → 0.4.2

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 (83) hide show
  1. package/android/src/main/cpp/core/AudioPlayer.cpp +38 -11
  2. package/android/src/main/cpp/core/AudioPlayer.h +6 -2
  3. package/common/cpp/HostObjects/AnalyserNodeHostObject.h +19 -6
  4. package/common/cpp/HostObjects/AudioAPIInstallerHostObject.h +11 -3
  5. package/common/cpp/HostObjects/AudioBufferHostObject.h +6 -5
  6. package/common/cpp/HostObjects/AudioNodeHostObject.h +5 -0
  7. package/common/cpp/HostObjects/AudioParamHostObject.h +2 -1
  8. package/common/cpp/HostObjects/BaseAudioContextHostObject.h +4 -3
  9. package/common/cpp/core/AnalyserNode.cpp +56 -34
  10. package/common/cpp/core/AnalyserNode.h +51 -19
  11. package/common/cpp/core/AudioArray.cpp +14 -14
  12. package/common/cpp/core/AudioArray.h +16 -15
  13. package/common/cpp/core/AudioBuffer.cpp +12 -9
  14. package/common/cpp/core/AudioBuffer.h +9 -8
  15. package/common/cpp/core/AudioBufferSourceNode.cpp +25 -20
  16. package/common/cpp/core/AudioBufferSourceNode.h +1 -0
  17. package/common/cpp/core/AudioBus.cpp +22 -26
  18. package/common/cpp/core/AudioBus.h +24 -24
  19. package/common/cpp/core/AudioContext.cpp +41 -1
  20. package/common/cpp/core/AudioContext.h +17 -0
  21. package/common/cpp/core/AudioDecoder.h +2 -2
  22. package/common/cpp/core/AudioDestinationNode.cpp +1 -1
  23. package/common/cpp/core/AudioDestinationNode.h +2 -1
  24. package/common/cpp/core/AudioNode.cpp +22 -8
  25. package/common/cpp/core/AudioNode.h +10 -10
  26. package/common/cpp/core/AudioNodeManager.cpp +1 -3
  27. package/common/cpp/core/AudioNodeManager.h +1 -1
  28. package/common/cpp/core/AudioParam.cpp +6 -3
  29. package/common/cpp/core/AudioParam.h +2 -1
  30. package/common/cpp/core/AudioScheduledSourceNode.cpp +1 -1
  31. package/common/cpp/core/AudioScheduledSourceNode.h +1 -0
  32. package/common/cpp/core/BaseAudioContext.cpp +7 -43
  33. package/common/cpp/core/BaseAudioContext.h +10 -21
  34. package/common/cpp/core/BiquadFilterNode.cpp +13 -14
  35. package/common/cpp/core/Constants.h +26 -12
  36. package/common/cpp/core/GainNode.cpp +1 -1
  37. package/common/cpp/core/OscillatorNode.cpp +4 -3
  38. package/common/cpp/core/PeriodicWave.cpp +7 -6
  39. package/common/cpp/core/PeriodicWave.h +4 -4
  40. package/common/cpp/core/StereoPannerNode.cpp +4 -4
  41. package/common/cpp/jsi/JsiHostObject.h +1 -1
  42. package/common/cpp/jsi/JsiPromise.h +1 -0
  43. package/common/cpp/utils/AudioUtils.cpp +2 -2
  44. package/common/cpp/utils/AudioUtils.h +2 -2
  45. package/common/cpp/utils/Locker.h +2 -2
  46. package/common/cpp/utils/VectorMath.cpp +1 -1
  47. package/ios/core/AudioPlayer.h +3 -2
  48. package/ios/core/AudioPlayer.m +49 -15
  49. package/ios/core/IOSAudioPlayer.h +4 -2
  50. package/ios/core/IOSAudioPlayer.mm +47 -11
  51. package/lib/module/core/AnalyserNode.js +6 -0
  52. package/lib/module/core/AnalyserNode.js.map +1 -1
  53. package/lib/module/core/AudioContext.js +2 -2
  54. package/lib/module/core/AudioContext.js.map +1 -1
  55. package/lib/module/core/AudioNode.js +5 -5
  56. package/lib/module/core/AudioNode.js.map +1 -1
  57. package/lib/module/index.js +16 -6
  58. package/lib/module/index.js.map +1 -1
  59. package/lib/module/index.native.js +1 -1
  60. package/lib/module/index.native.js.map +1 -1
  61. package/lib/typescript/core/AnalyserNode.d.ts +3 -0
  62. package/lib/typescript/core/AnalyserNode.d.ts.map +1 -1
  63. package/lib/typescript/core/AudioContext.d.ts +1 -1
  64. package/lib/typescript/core/AudioContext.d.ts.map +1 -1
  65. package/lib/typescript/core/AudioNode.d.ts +2 -2
  66. package/lib/typescript/core/AudioNode.d.ts.map +1 -1
  67. package/lib/typescript/core/types.d.ts +1 -0
  68. package/lib/typescript/core/types.d.ts.map +1 -1
  69. package/lib/typescript/index.d.ts +6 -4
  70. package/lib/typescript/index.d.ts.map +1 -1
  71. package/lib/typescript/index.native.d.ts +1 -1
  72. package/lib/typescript/index.native.d.ts.map +1 -1
  73. package/lib/typescript/interfaces.d.ts +3 -2
  74. package/lib/typescript/interfaces.d.ts.map +1 -1
  75. package/package.json +1 -1
  76. package/src/core/AnalyserNode.ts +9 -0
  77. package/src/core/AudioContext.ts +2 -2
  78. package/src/core/AudioNode.ts +5 -5
  79. package/src/core/types.ts +2 -0
  80. package/src/index.native.ts +1 -0
  81. package/src/index.ts +26 -7
  82. package/src/interfaces.ts +3 -1
  83. package/src/specs/global.d.ts +1 -1
@@ -2,47 +2,48 @@
2
2
 
3
3
  #include <algorithm>
4
4
  #include <memory>
5
+ #include <cstddef>
5
6
 
6
7
  namespace audioapi {
7
8
 
8
9
  class AudioArray {
9
10
  public:
10
- explicit AudioArray(int size);
11
+ explicit AudioArray(size_t size);
11
12
  ~AudioArray();
12
13
 
13
- [[nodiscard]] int getSize() const;
14
+ [[nodiscard]] size_t getSize() const;
14
15
  [[nodiscard]] float *getData() const;
15
16
 
16
- float &operator[](int index);
17
- const float &operator[](int index) const;
17
+ float &operator[](size_t index);
18
+ const float &operator[](size_t index) const;
18
19
 
19
20
  void normalize();
20
- void resize(int size);
21
+ void resize(size_t size);
21
22
  void scale(float value);
22
23
  [[nodiscard]] float getMaxAbsValue() const;
23
24
 
24
25
  void zero();
25
- void zero(int start, int length);
26
+ void zero(size_t start, size_t length);
26
27
 
27
28
  void sum(const AudioArray *source);
28
- void sum(const AudioArray *source, int start, int length);
29
+ void sum(const AudioArray *source, size_t start, size_t length);
29
30
  void sum(
30
31
  const AudioArray *source,
31
- int sourceStart,
32
- int destinationStart,
33
- int length);
32
+ size_t sourceStart,
33
+ size_t destinationStart,
34
+ size_t length);
34
35
 
35
36
  void copy(const AudioArray *source);
36
- void copy(const AudioArray *source, int start, int length);
37
+ void copy(const AudioArray *source, size_t start, size_t length);
37
38
  void copy(
38
39
  const AudioArray *source,
39
- int sourceStart,
40
- int destinationStart,
41
- int length);
40
+ size_t sourceStart,
41
+ size_t destinationStart,
42
+ size_t length);
42
43
 
43
44
  private:
44
45
  float *data_;
45
- int size_;
46
+ size_t size_;
46
47
  };
47
48
 
48
49
  } // namespace audioapi
@@ -4,7 +4,10 @@
4
4
 
5
5
  namespace audioapi {
6
6
 
7
- AudioBuffer::AudioBuffer(int numberOfChannels, int length, int sampleRate) {
7
+ AudioBuffer::AudioBuffer(
8
+ int numberOfChannels,
9
+ size_t length,
10
+ float sampleRate) {
8
11
  bus_ = std::make_shared<AudioBus>(sampleRate, length, numberOfChannels);
9
12
  }
10
13
 
@@ -12,7 +15,7 @@ AudioBuffer::AudioBuffer(AudioBus *bus) {
12
15
  bus_ = std::shared_ptr<AudioBus>(bus);
13
16
  }
14
17
 
15
- int AudioBuffer::getLength() const {
18
+ size_t AudioBuffer::getLength() const {
16
19
  return bus_->getSize();
17
20
  }
18
21
 
@@ -20,12 +23,12 @@ int AudioBuffer::getNumberOfChannels() const {
20
23
  return bus_->getNumberOfChannels();
21
24
  }
22
25
 
23
- int AudioBuffer::getSampleRate() const {
26
+ float AudioBuffer::getSampleRate() const {
24
27
  return bus_->getSampleRate();
25
28
  }
26
29
 
27
- double AudioBuffer::getDuration() const {
28
- return static_cast<double>(getLength()) / getSampleRate();
30
+ float AudioBuffer::getDuration() const {
31
+ return static_cast<float>(getLength()) / getSampleRate();
29
32
  }
30
33
 
31
34
  float *AudioBuffer::getChannelData(int channel) const {
@@ -34,9 +37,9 @@ float *AudioBuffer::getChannelData(int channel) const {
34
37
 
35
38
  void AudioBuffer::copyFromChannel(
36
39
  float *destination,
37
- int destinationLength,
40
+ size_t destinationLength,
38
41
  int channelNumber,
39
- int startInChannel) const {
42
+ size_t startInChannel) const {
40
43
  memcpy(
41
44
  destination,
42
45
  bus_->getChannel(channelNumber)->getData() + startInChannel,
@@ -46,9 +49,9 @@ void AudioBuffer::copyFromChannel(
46
49
 
47
50
  void AudioBuffer::copyToChannel(
48
51
  const float *source,
49
- int sourceLength,
52
+ size_t sourceLength,
50
53
  int channelNumber,
51
- int startInChannel) {
54
+ size_t startInChannel) {
52
55
  memcpy(
53
56
  bus_->getChannel(channelNumber)->getData() + startInChannel,
54
57
  source,
@@ -4,6 +4,7 @@
4
4
  #include <memory>
5
5
  #include <string>
6
6
  #include <vector>
7
+ #include <cstddef>
7
8
 
8
9
  namespace audioapi {
9
10
 
@@ -11,26 +12,26 @@ class AudioBus;
11
12
 
12
13
  class AudioBuffer : public std::enable_shared_from_this<AudioBuffer> {
13
14
  public:
14
- explicit AudioBuffer(int numberOfChannels, int length, int sampleRate);
15
+ explicit AudioBuffer(int numberOfChannels, size_t length, float sampleRate);
15
16
  explicit AudioBuffer(AudioBus *bus);
16
17
 
17
- [[nodiscard]] int getLength() const;
18
- [[nodiscard]] int getSampleRate() const;
19
- [[nodiscard]] double getDuration() const;
18
+ [[nodiscard]] size_t getLength() const;
19
+ [[nodiscard]] float getSampleRate() const;
20
+ [[nodiscard]] float getDuration() const;
20
21
 
21
22
  [[nodiscard]] int getNumberOfChannels() const;
22
23
  [[nodiscard]] float *getChannelData(int channel) const;
23
24
 
24
25
  void copyFromChannel(
25
26
  float *destination,
26
- int destinationLength,
27
+ size_t destinationLength,
27
28
  int channelNumber,
28
- int startInChannel) const;
29
+ size_t startInChannel) const;
29
30
  void copyToChannel(
30
31
  const float *source,
31
- int sourceLength,
32
+ size_t sourceLength,
32
33
  int channelNumber,
33
- int startInChannel);
34
+ size_t startInChannel);
34
35
 
35
36
  private:
36
37
  std::shared_ptr<AudioBus> bus_;
@@ -16,10 +16,9 @@ AudioBufferSourceNode::AudioBufferSourceNode(BaseAudioContext *context)
16
16
  loopStart_(0),
17
17
  loopEnd_(0),
18
18
  vReadIndex_(0.0) {
19
- numberOfInputs_ = 0;
20
19
  buffer_ = std::shared_ptr<AudioBuffer>(nullptr);
21
20
 
22
- detuneParam_ = std::make_shared<AudioParam>(0.0, -MAX_DETUNE, MAX_DETUNE);
21
+ detuneParam_ = std::make_shared<AudioParam>(0.0, MIN_DETUNE, MAX_DETUNE);
23
22
  playbackRateParam_ = std::make_shared<AudioParam>(
24
23
  1.0, MOST_NEGATIVE_SINGLE_FLOAT, MOST_POSITIVE_SINGLE_FLOAT);
25
24
 
@@ -73,7 +72,9 @@ void AudioBufferSourceNode::setBuffer(
73
72
 
74
73
  buffer_ = buffer;
75
74
  alignedBus_ = std::make_shared<AudioBus>(
76
- context_->getSampleRate(), buffer_->getLength());
75
+ context_->getSampleRate(),
76
+ buffer_->getLength(),
77
+ buffer_->getNumberOfChannels());
77
78
 
78
79
  alignedBus_->zero();
79
80
  alignedBus_->sum(buffer_->bus_.get());
@@ -90,7 +91,7 @@ void AudioBufferSourceNode::processNode(
90
91
 
91
92
  // No audio data to fill, zero the output and return.
92
93
  if (!isPlaying() || !alignedBus_ || alignedBus_->getSize() == 0 ||
93
- !playbackRate) {
94
+ playbackRate == 0.0f) {
94
95
  processingBus->zero();
95
96
  return;
96
97
  }
@@ -115,7 +116,7 @@ void AudioBufferSourceNode::processWithoutInterpolation(
115
116
  size_t startOffset,
116
117
  size_t offsetLength,
117
118
  float playbackRate) {
118
- size_t direction = playbackRate < 0 ? -1 : 1;
119
+ size_t direction = playbackRate < 0.0f ? -1 : 1;
119
120
 
120
121
  auto readIndex = static_cast<size_t>(vReadIndex_);
121
122
  size_t writeIndex = startOffset;
@@ -165,7 +166,7 @@ void AudioBufferSourceNode::processWithoutInterpolation(
165
166
  }
166
167
 
167
168
  // update reading index for next render quantum
168
- vReadIndex_ = readIndex;
169
+ vReadIndex_ = static_cast<double>(readIndex);
169
170
  }
170
171
 
171
172
  void AudioBufferSourceNode::processWithInterpolation(
@@ -173,13 +174,13 @@ void AudioBufferSourceNode::processWithInterpolation(
173
174
  size_t startOffset,
174
175
  size_t offsetLength,
175
176
  float playbackRate) {
176
- size_t direction = playbackRate < 0 ? -1 : 1;
177
+ size_t direction = playbackRate < 0.0f ? -1 : 1;
177
178
 
178
179
  size_t writeIndex = startOffset;
179
180
 
180
- double vFrameStart = getVirtualStartFrame();
181
- double vFrameEnd = getVirtualEndFrame();
182
- double vFrameDelta = vFrameEnd - vFrameStart;
181
+ auto vFrameStart = getVirtualStartFrame();
182
+ auto vFrameEnd = getVirtualEndFrame();
183
+ auto vFrameDelta = vFrameEnd - vFrameStart;
183
184
 
184
185
  auto frameStart = static_cast<size_t>(vFrameStart);
185
186
  auto frameEnd = static_cast<size_t>(vFrameEnd);
@@ -195,7 +196,8 @@ void AudioBufferSourceNode::processWithInterpolation(
195
196
  while (framesLeft > 0) {
196
197
  auto readIndex = static_cast<size_t>(vReadIndex_);
197
198
  size_t nextReadIndex = readIndex + 1;
198
- float factor = vReadIndex_ - readIndex;
199
+ auto factor =
200
+ static_cast<float>(vReadIndex_ - static_cast<double>(readIndex));
199
201
 
200
202
  if (nextReadIndex >= frameEnd) {
201
203
  nextReadIndex = loop_ ? frameStart : readIndex;
@@ -210,11 +212,11 @@ void AudioBufferSourceNode::processWithInterpolation(
210
212
  }
211
213
 
212
214
  writeIndex += 1;
213
- vReadIndex_ += playbackRate * direction;
215
+ vReadIndex_ += playbackRate * static_cast<double>(direction);
214
216
  framesLeft -= 1;
215
217
 
216
218
  if (vReadIndex_ < vFrameStart || vReadIndex_ >= vFrameEnd) {
217
- vReadIndex_ -= direction * vFrameDelta;
219
+ vReadIndex_ -= static_cast<double>(direction) * vFrameDelta;
218
220
 
219
221
  if (!loop_) {
220
222
  processingBus->zero(writeIndex, framesLeft);
@@ -227,23 +229,26 @@ void AudioBufferSourceNode::processWithInterpolation(
227
229
  }
228
230
 
229
231
  float AudioBufferSourceNode::getPlaybackRateValue(size_t &startOffset) {
230
- double time =
231
- context_->getCurrentTime() + startOffset / context_->getSampleRate();
232
+ auto time = context_->getCurrentTime() +
233
+ static_cast<double>(startOffset) / context_->getSampleRate();
232
234
 
233
- return playbackRateParam_->getValueAtTime(time) *
234
- std::pow(2.0f, detuneParam_->getValueAtTime(time) / 1200.0f);
235
+ auto sampleRateFactor = buffer_->getSampleRate() / context_->getSampleRate();
236
+ auto detune = std::pow(2.0f, detuneParam_->getValueAtTime(time) / 1200.0f);
237
+ auto playbackRate = playbackRateParam_->getValueAtTime(time);
238
+
239
+ return playbackRate * sampleRateFactor * detune;
235
240
  }
236
241
 
237
242
  double AudioBufferSourceNode::getVirtualStartFrame() {
238
- double loopStartFrame = loopStart_ * context_->getSampleRate();
243
+ auto loopStartFrame = loopStart_ * context_->getSampleRate();
239
244
 
240
245
  return loop_ && loopStartFrame >= 0 && loopStart_ < loopEnd_ ? loopStartFrame
241
246
  : 0.0;
242
247
  }
243
248
 
244
249
  double AudioBufferSourceNode::getVirtualEndFrame() {
245
- double inputBufferLength = alignedBus_->getSize();
246
- double loopEndFrame = loopEnd_ * context_->getSampleRate();
250
+ auto inputBufferLength = static_cast<double>(alignedBus_->getSize());
251
+ auto loopEndFrame = loopEnd_ * context_->getSampleRate();
247
252
 
248
253
  return loop_ && loopEndFrame > 0 && loopStart_ < loopEnd_
249
254
  ? std::min(loopEndFrame, inputBufferLength)
@@ -1,6 +1,7 @@
1
1
  #pragma once
2
2
 
3
3
  #include <memory>
4
+ #include <cstddef>
4
5
 
5
6
  #include "AudioBuffer.h"
6
7
  #include "AudioScheduledSourceNode.h"
@@ -17,12 +17,8 @@ namespace audioapi {
17
17
  /**
18
18
  * Public interfaces - memory management
19
19
  */
20
- AudioBus::AudioBus(int sampleRate, int size)
21
- : numberOfChannels_(CHANNEL_COUNT), sampleRate_(sampleRate), size_(size) {
22
- createChannels();
23
- }
24
20
 
25
- AudioBus::AudioBus(int sampleRate, int size, int numberOfChannels)
21
+ AudioBus::AudioBus(float sampleRate, size_t size, int numberOfChannels)
26
22
  : numberOfChannels_(numberOfChannels),
27
23
  sampleRate_(sampleRate),
28
24
  size_(size) {
@@ -41,11 +37,11 @@ int AudioBus::getNumberOfChannels() const {
41
37
  return numberOfChannels_;
42
38
  }
43
39
 
44
- int AudioBus::getSampleRate() const {
40
+ float AudioBus::getSampleRate() const {
45
41
  return sampleRate_;
46
42
  }
47
43
 
48
- int AudioBus::getSize() const {
44
+ size_t AudioBus::getSize() const {
49
45
  return size_;
50
46
  }
51
47
 
@@ -56,7 +52,7 @@ AudioArray *AudioBus::getChannel(int index) const {
56
52
  AudioArray *AudioBus::getChannelByType(int channelType) const {
57
53
  switch (getNumberOfChannels()) {
58
54
  case 1: // mono
59
- if (channelType == ChannelMono || channelType == ChannelLeft) {
55
+ if (channelType == ChannelMono) {
60
56
  return getChannel(0);
61
57
  }
62
58
  return nullptr;
@@ -131,7 +127,7 @@ void AudioBus::zero() {
131
127
  zero(0, getSize());
132
128
  }
133
129
 
134
- void AudioBus::zero(int start, int length) {
130
+ void AudioBus::zero(size_t start, size_t length) {
135
131
  for (auto it = channels_.begin(); it != channels_.end(); it += 1) {
136
132
  it->get()->zero(start, length);
137
133
  }
@@ -169,15 +165,15 @@ void AudioBus::sum(const AudioBus *source) {
169
165
  sum(source, 0, 0, getSize());
170
166
  }
171
167
 
172
- void AudioBus::sum(const AudioBus *source, int start, int length) {
168
+ void AudioBus::sum(const AudioBus *source, size_t start, size_t length) {
173
169
  sum(source, start, start, length);
174
170
  }
175
171
 
176
172
  void AudioBus::sum(
177
173
  const AudioBus *source,
178
- int sourceStart,
179
- int destinationStart,
180
- int length) {
174
+ size_t sourceStart,
175
+ size_t destinationStart,
176
+ size_t length) {
181
177
  if (source == this) {
182
178
  return;
183
179
  }
@@ -210,15 +206,15 @@ void AudioBus::copy(const AudioBus *source) {
210
206
  copy(source, 0, 0, getSize());
211
207
  }
212
208
 
213
- void AudioBus::copy(const AudioBus *source, int start, int length) {
209
+ void AudioBus::copy(const AudioBus *source, size_t start, size_t length) {
214
210
  copy(source, start, start, length);
215
211
  }
216
212
 
217
213
  void AudioBus::copy(
218
214
  const AudioBus *source,
219
- int sourceStart,
220
- int destinationStart,
221
- int length) {
215
+ size_t sourceStart,
216
+ size_t destinationStart,
217
+ size_t length) {
222
218
  if (source == this) {
223
219
  return;
224
220
  }
@@ -255,9 +251,9 @@ void AudioBus::createChannels() {
255
251
 
256
252
  void AudioBus::discreteSum(
257
253
  const AudioBus *source,
258
- int sourceStart,
259
- int destinationStart,
260
- int length) const {
254
+ size_t sourceStart,
255
+ size_t destinationStart,
256
+ size_t length) const {
261
257
  int numberOfChannels =
262
258
  std::min(getNumberOfChannels(), source->getNumberOfChannels());
263
259
 
@@ -272,9 +268,9 @@ void AudioBus::discreteSum(
272
268
 
273
269
  void AudioBus::sumByUpMixing(
274
270
  const AudioBus *source,
275
- int sourceStart,
276
- int destinationStart,
277
- int length) {
271
+ size_t sourceStart,
272
+ size_t destinationStart,
273
+ size_t length) {
278
274
  int numberOfSourceChannels = source->getNumberOfChannels();
279
275
  int numberOfChannels = getNumberOfChannels();
280
276
 
@@ -351,9 +347,9 @@ void AudioBus::sumByUpMixing(
351
347
 
352
348
  void AudioBus::sumByDownMixing(
353
349
  const AudioBus *source,
354
- int sourceStart,
355
- int destinationStart,
356
- int length) {
350
+ size_t sourceStart,
351
+ size_t destinationStart,
352
+ size_t length) {
357
353
  int numberOfSourceChannels = source->getNumberOfChannels();
358
354
  int numberOfChannels = getNumberOfChannels();
359
355
 
@@ -3,6 +3,7 @@
3
3
  #include <algorithm>
4
4
  #include <memory>
5
5
  #include <vector>
6
+ #include <cstddef>
6
7
 
7
8
  namespace audioapi {
8
9
 
@@ -21,14 +22,13 @@ class AudioBus {
21
22
  ChannelSurroundRight = 5,
22
23
  };
23
24
 
24
- explicit AudioBus(int sampleRate, int size);
25
- explicit AudioBus(int sampleRate, int size, int numberOfChannels);
25
+ explicit AudioBus(float sampleRate, size_t size, int numberOfChannels);
26
26
 
27
27
  ~AudioBus();
28
28
 
29
29
  [[nodiscard]] int getNumberOfChannels() const;
30
- [[nodiscard]] int getSampleRate() const;
31
- [[nodiscard]] int getSize() const;
30
+ [[nodiscard]] float getSampleRate() const;
31
+ [[nodiscard]] size_t getSize() const;
32
32
  [[nodiscard]] AudioArray *getChannel(int index) const;
33
33
  [[nodiscard]] AudioArray *getChannelByType(int channelType) const;
34
34
 
@@ -37,47 +37,47 @@ class AudioBus {
37
37
  [[nodiscard]] float maxAbsValue() const;
38
38
 
39
39
  void zero();
40
- void zero(int start, int length);
40
+ void zero(size_t start, size_t length);
41
41
 
42
42
  void sum(const AudioBus *source);
43
- void sum(const AudioBus *source, int start, int length);
43
+ void sum(const AudioBus *source, size_t start, size_t length);
44
44
  void sum(
45
45
  const AudioBus *source,
46
- int sourceStart,
47
- int destinationStart,
48
- int length);
46
+ size_t sourceStart,
47
+ size_t destinationStart,
48
+ size_t length);
49
49
 
50
50
  void copy(const AudioBus *source);
51
- void copy(const AudioBus *source, int start, int length);
51
+ void copy(const AudioBus *source, size_t start, size_t length);
52
52
  void copy(
53
53
  const AudioBus *source,
54
- int sourceStart,
55
- int destinationStart,
56
- int length);
54
+ size_t sourceStart,
55
+ size_t destinationStart,
56
+ size_t length);
57
57
 
58
58
  private:
59
59
  std::vector<std::shared_ptr<AudioArray>> channels_;
60
60
 
61
61
  int numberOfChannels_;
62
- int sampleRate_;
63
- int size_;
62
+ float sampleRate_;
63
+ size_t size_;
64
64
 
65
65
  void createChannels();
66
66
  void discreteSum(
67
67
  const AudioBus *source,
68
- int sourceStart,
69
- int destinationStart,
70
- int length) const;
68
+ size_t sourceStart,
69
+ size_t destinationStart,
70
+ size_t length) const;
71
71
  void sumByUpMixing(
72
72
  const AudioBus *source,
73
- int sourceStart,
74
- int destinationStart,
75
- int length);
73
+ size_t sourceStart,
74
+ size_t destinationStart,
75
+ size_t length);
76
76
  void sumByDownMixing(
77
77
  const AudioBus *source,
78
- int sourceStart,
79
- int destinationStart,
80
- int length);
78
+ size_t sourceStart,
79
+ size_t destinationStart,
80
+ size_t length);
81
81
  };
82
82
 
83
83
  } // namespace audioapi
@@ -5,16 +5,56 @@
5
5
  #endif
6
6
 
7
7
  #include "AudioContext.h"
8
+ #include "AudioDecoder.h"
9
+ #include "AudioDestinationNode.h"
8
10
 
9
11
  namespace audioapi {
10
-
11
12
  AudioContext::AudioContext() : BaseAudioContext() {
13
+ #ifdef ANDROID
14
+ audioPlayer_ = std::make_shared<AudioPlayer>(this->renderAudio());
15
+ #else
16
+ audioPlayer_ = std::make_shared<IOSAudioPlayer>(this->renderAudio());
17
+ #endif
18
+ sampleRate_ = audioPlayer_->getSampleRate();
19
+ audioDecoder_ = std::make_shared<AudioDecoder>(sampleRate_);
20
+
21
+ audioPlayer_->start();
22
+ }
23
+
24
+ AudioContext::AudioContext(float sampleRate) : BaseAudioContext() {
25
+ #ifdef ANDROID
26
+ audioPlayer_ = std::make_shared<AudioPlayer>(this->renderAudio(), sampleRate);
27
+ #else
28
+ audioPlayer_ =
29
+ std::make_shared<IOSAudioPlayer>(this->renderAudio(), sampleRate);
30
+ #endif
31
+ sampleRate_ = audioPlayer_->getSampleRate();
32
+ audioDecoder_ = std::make_shared<AudioDecoder>(sampleRate_);
33
+
12
34
  audioPlayer_->start();
13
35
  }
14
36
 
37
+ AudioContext::~AudioContext() {
38
+ if (isRunning()) {
39
+ return;
40
+ }
41
+
42
+ close();
43
+ }
44
+
15
45
  void AudioContext::close() {
16
46
  state_ = ContextState::CLOSED;
17
47
  audioPlayer_->stop();
18
48
  }
19
49
 
50
+ std::function<void(AudioBus *, int)> AudioContext::renderAudio() {
51
+ if (!isRunning()) {
52
+ return [](AudioBus *, int) {};
53
+ }
54
+
55
+ return [this](AudioBus *data, int frames) {
56
+ destination_->renderAudio(data, frames);
57
+ };
58
+ }
59
+
20
60
  } // namespace audioapi
@@ -1,16 +1,33 @@
1
1
  #pragma once
2
2
 
3
3
  #include <memory>
4
+ #include <functional>
4
5
 
5
6
  #include "BaseAudioContext.h"
6
7
 
7
8
  namespace audioapi {
9
+ #ifdef ANDROID
10
+ class AudioPlayer;
11
+ #else
12
+ class IOSAudioPlayer;
13
+ #endif
8
14
 
9
15
  class AudioContext : public BaseAudioContext {
10
16
  public:
11
17
  AudioContext();
18
+ explicit AudioContext(float sampleRate);
19
+ ~AudioContext() override;
12
20
 
13
21
  void close();
22
+
23
+ std::function<void(AudioBus *, int)> renderAudio();
24
+
25
+ private:
26
+ #ifdef ANDROID
27
+ std::shared_ptr<AudioPlayer> audioPlayer_;
28
+ #else
29
+ std::shared_ptr<IOSAudioPlayer> audioPlayer_;
30
+ #endif
14
31
  };
15
32
 
16
33
  } // namespace audioapi
@@ -8,12 +8,12 @@ class AudioBus;
8
8
 
9
9
  class AudioDecoder {
10
10
  public:
11
- explicit AudioDecoder(int sampleRate) : sampleRate_(sampleRate) {}
11
+ explicit AudioDecoder(float sampleRate) : sampleRate_(sampleRate) {}
12
12
 
13
13
  [[nodiscard]] AudioBus *decodeWithFilePath(const std::string &path) const;
14
14
 
15
15
  private:
16
- int sampleRate_;
16
+ float sampleRate_;
17
17
  };
18
18
 
19
19
  } // namespace audioapi
@@ -25,7 +25,7 @@ double AudioDestinationNode::getCurrentTime() const {
25
25
 
26
26
  void AudioDestinationNode::renderAudio(
27
27
  AudioBus *destinationBus,
28
- int32_t numFrames) {
28
+ int numFrames) {
29
29
  if (!numFrames || !destinationBus || !isInitialized_) {
30
30
  return;
31
31
  }
@@ -3,6 +3,7 @@
3
3
  #include <algorithm>
4
4
  #include <memory>
5
5
  #include <vector>
6
+ #include <cstddef>
6
7
 
7
8
  #include "AudioNode.h"
8
9
 
@@ -15,7 +16,7 @@ class AudioDestinationNode : public AudioNode {
15
16
  public:
16
17
  explicit AudioDestinationNode(BaseAudioContext *context);
17
18
 
18
- void renderAudio(AudioBus *audioData, int32_t numFrames);
19
+ void renderAudio(AudioBus *audioData, int numFrames);
19
20
 
20
21
  std::size_t getCurrentSampleFrame() const;
21
22
  double getCurrentTime() const;