react-native-audio-api 0.3.1 → 0.3.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 (134) hide show
  1. package/RNAudioAPI.podspec +40 -29
  2. package/android/CMakeLists.txt +17 -21
  3. package/android/build.gradle +26 -9
  4. package/android/src/main/cpp/OnLoad.cpp +1 -1
  5. package/android/src/main/cpp/core/AudioAPIInstaller.cpp +37 -0
  6. package/android/src/main/cpp/{AudioAPIInstaller → core}/AudioAPIInstaller.h +4 -13
  7. package/android/src/main/cpp/{AudioDecoder → core}/AudioDecoder.cpp +0 -8
  8. package/android/src/main/java/com/swmansion/audioapi/AudioAPIPackage.kt +2 -0
  9. package/android/src/main/java/com/swmansion/audioapi/nativemodules/AudioAPIModule.kt +6 -4
  10. package/common/cpp/HostObjects/AudioParamHostObject.h +39 -2
  11. package/common/cpp/core/AudioBufferSourceNode.cpp +4 -3
  12. package/{android/src/main/cpp/AudioDecoder → common/cpp/core}/AudioDecoder.h +1 -3
  13. package/common/cpp/core/AudioParam.cpp +218 -58
  14. package/common/cpp/core/AudioParam.h +19 -15
  15. package/common/cpp/core/AudioScheduledSourceNode.cpp +9 -1
  16. package/common/cpp/core/AudioScheduledSourceNode.h +4 -1
  17. package/common/cpp/core/BaseAudioContext.cpp +3 -13
  18. package/common/cpp/core/BaseAudioContext.h +2 -4
  19. package/common/cpp/core/BiquadFilterNode.cpp +5 -7
  20. package/common/cpp/{HostObjects → core}/Constants.h +2 -4
  21. package/common/cpp/core/GainNode.cpp +1 -1
  22. package/common/cpp/core/OscillatorNode.cpp +4 -3
  23. package/common/cpp/core/ParamChangeEvent.cpp +58 -0
  24. package/common/cpp/core/{ParamChange.h → ParamChangeEvent.h} +11 -5
  25. package/common/cpp/core/StereoPannerNode.cpp +1 -1
  26. package/common/cpp/jsi/JsiHostObject.cpp +35 -30
  27. package/common/cpp/jsi/JsiHostObject.h +12 -6
  28. package/common/cpp/jsi/RuntimeAwareCache.h +57 -0
  29. package/common/cpp/jsi/RuntimeLifecycleMonitor.cpp +61 -0
  30. package/common/cpp/jsi/RuntimeLifecycleMonitor.h +32 -0
  31. package/common/cpp/types/BiquadFilterType.h +0 -4
  32. package/common/cpp/types/ChannelCountMode.h +0 -3
  33. package/common/cpp/types/ChannelInterpretation.h +0 -3
  34. package/common/cpp/types/ContextState.h +0 -3
  35. package/common/cpp/types/OscillatorType.h +0 -4
  36. package/common/cpp/types/ParamChangeEventType.h +13 -0
  37. package/common/cpp/utils/AudioUtils.h +1 -5
  38. package/common/cpp/utils/{ios/FFTFrame.cpp → FFTFrame.cpp} +30 -3
  39. package/common/cpp/utils/VectorMath.h +7 -33
  40. package/ios/AudioAPIModule.h +4 -12
  41. package/ios/AudioAPIModule.mm +25 -20
  42. package/ios/core/AudioDecoder.mm +45 -0
  43. package/lib/module/core/AudioBuffer.js +3 -1
  44. package/lib/module/core/AudioBuffer.js.map +1 -1
  45. package/lib/module/core/AudioBufferSourceNode.js +5 -3
  46. package/lib/module/core/AudioBufferSourceNode.js.map +1 -1
  47. package/lib/module/core/AudioContext.js +3 -1
  48. package/lib/module/core/AudioContext.js.map +1 -1
  49. package/lib/module/core/AudioDestinationNode.js +4 -6
  50. package/lib/module/core/AudioDestinationNode.js.map +1 -1
  51. package/lib/module/core/AudioNode.js +3 -1
  52. package/lib/module/core/AudioNode.js.map +1 -1
  53. package/lib/module/core/AudioParam.js +27 -1
  54. package/lib/module/core/AudioParam.js.map +1 -1
  55. package/lib/module/core/AudioScheduledSourceNode.js +4 -5
  56. package/lib/module/core/AudioScheduledSourceNode.js.map +1 -1
  57. package/lib/module/core/BaseAudioContext.js +11 -9
  58. package/lib/module/core/BaseAudioContext.js.map +1 -1
  59. package/lib/module/core/BiquadFilterNode.js +5 -3
  60. package/lib/module/core/BiquadFilterNode.js.map +1 -1
  61. package/lib/module/core/GainNode.js +4 -2
  62. package/lib/module/core/GainNode.js.map +1 -1
  63. package/lib/module/core/OscillatorNode.js +5 -3
  64. package/lib/module/core/OscillatorNode.js.map +1 -1
  65. package/lib/module/core/PeriodicWave.js +2 -0
  66. package/lib/module/core/PeriodicWave.js.map +1 -1
  67. package/lib/module/core/StereoPannerNode.js +4 -2
  68. package/lib/module/core/StereoPannerNode.js.map +1 -1
  69. package/lib/module/core/types.js +1 -1
  70. package/lib/module/errors/IndexSizeError.js +2 -0
  71. package/lib/module/errors/IndexSizeError.js.map +1 -1
  72. package/lib/module/errors/InvalidAccessError.js +2 -0
  73. package/lib/module/errors/InvalidAccessError.js.map +1 -1
  74. package/lib/module/errors/InvalidStateError.js +2 -0
  75. package/lib/module/errors/InvalidStateError.js.map +1 -1
  76. package/lib/module/errors/RangeError.js +2 -0
  77. package/lib/module/errors/RangeError.js.map +1 -1
  78. package/lib/module/errors/index.js +6 -4
  79. package/lib/module/errors/index.js.map +1 -1
  80. package/lib/module/index.js +18 -11
  81. package/lib/module/index.js.map +1 -1
  82. package/lib/module/index.native.js +16 -14
  83. package/lib/module/index.native.js.map +1 -1
  84. package/lib/module/interfaces.js +2 -0
  85. package/lib/module/specs/global.d.js +4 -0
  86. package/lib/module/{modules → specs}/global.d.js.map +1 -1
  87. package/lib/module/{utils → specs}/install.js +5 -9
  88. package/lib/module/specs/install.js.map +1 -0
  89. package/lib/typescript/core/AudioBuffer.d.ts +2 -0
  90. package/lib/typescript/core/AudioBuffer.d.ts.map +1 -1
  91. package/lib/typescript/core/AudioDestinationNode.d.ts +0 -3
  92. package/lib/typescript/core/AudioDestinationNode.d.ts.map +1 -1
  93. package/lib/typescript/core/AudioParam.d.ts +4 -0
  94. package/lib/typescript/core/AudioParam.d.ts.map +1 -1
  95. package/lib/typescript/core/AudioScheduledSourceNode.d.ts +0 -3
  96. package/lib/typescript/core/AudioScheduledSourceNode.d.ts.map +1 -1
  97. package/lib/typescript/core/PeriodicWave.d.ts +2 -0
  98. package/lib/typescript/core/PeriodicWave.d.ts.map +1 -1
  99. package/lib/typescript/index.d.ts +9 -3
  100. package/lib/typescript/index.d.ts.map +1 -1
  101. package/lib/typescript/interfaces.d.ts +4 -0
  102. package/lib/typescript/interfaces.d.ts.map +1 -1
  103. package/lib/typescript/{utils → specs}/install.d.ts.map +1 -1
  104. package/metro-config/index.d.ts +5 -0
  105. package/metro-config/index.js +41 -0
  106. package/metro-config/tsconfig.json +3 -0
  107. package/package.json +50 -16
  108. package/scripts/audioapi_utils.rb +56 -0
  109. package/src/core/AudioDestinationNode.ts +1 -7
  110. package/src/core/AudioParam.ts +48 -0
  111. package/src/core/AudioScheduledSourceNode.ts +0 -5
  112. package/src/index.native.ts +1 -1
  113. package/src/index.ts +31 -14
  114. package/src/interfaces.ts +12 -0
  115. package/src/{modules → specs}/global.d.ts +2 -0
  116. package/src/{utils → specs}/install.ts +4 -11
  117. package/android/src/main/cpp/AudioAPIInstaller/AudioAPIInstaller.cpp +0 -20
  118. package/common/cpp/core/ParamChange.cpp +0 -46
  119. package/common/cpp/utils/android/FFTFrame.cpp +0 -23
  120. package/ios/AudioDecoder/AudioDecoder.h +0 -17
  121. package/ios/AudioDecoder/AudioDecoder.m +0 -79
  122. package/ios/AudioDecoder/IOSAudioDecoder.h +0 -28
  123. package/ios/AudioDecoder/IOSAudioDecoder.mm +0 -46
  124. package/lib/module/modules/global.d.js +0 -2
  125. package/lib/module/utils/install.js.map +0 -1
  126. /package/android/src/main/cpp/{AudioPlayer → core}/AudioPlayer.cpp +0 -0
  127. /package/android/src/main/cpp/{AudioPlayer → core}/AudioPlayer.h +0 -0
  128. /package/common/cpp/{AudioAPIInstaller → HostObjects}/AudioAPIInstallerHostObject.h +0 -0
  129. /package/{android/libs/include → common/cpp/libs}/miniaudio.h +0 -0
  130. /package/ios/{AudioPlayer → core}/AudioPlayer.h +0 -0
  131. /package/ios/{AudioPlayer → core}/AudioPlayer.m +0 -0
  132. /package/ios/{AudioPlayer → core}/IOSAudioPlayer.h +0 -0
  133. /package/ios/{AudioPlayer → core}/IOSAudioPlayer.mm +0 -0
  134. /package/lib/typescript/{utils → specs}/install.d.ts +0 -0
@@ -1,23 +1,19 @@
1
1
  #include "AudioParam.h"
2
+
3
+ #include "AudioUtils.h"
2
4
  #include "BaseAudioContext.h"
3
5
 
4
6
  namespace audioapi {
5
7
 
6
- AudioParam::AudioParam(
7
- BaseAudioContext *context,
8
- float defaultValue,
9
- float minValue,
10
- float maxValue)
8
+ AudioParam::AudioParam(float defaultValue, float minValue, float maxValue)
11
9
  : value_(defaultValue),
12
10
  defaultValue_(defaultValue),
13
11
  minValue_(minValue),
14
- maxValue_(maxValue),
15
- context_(context),
16
- changesQueue_() {
12
+ maxValue_(maxValue) {
17
13
  startTime_ = 0;
18
14
  endTime_ = 0;
19
- startValue_ = 0;
20
- endValue_ = 0;
15
+ startValue_ = value_;
16
+ endValue_ = value_;
21
17
  calculateValue_ = [this](double, double, float, float, double) {
22
18
  return value_;
23
19
  };
@@ -40,94 +36,258 @@ float AudioParam::getMaxValue() const {
40
36
  }
41
37
 
42
38
  void AudioParam::setValue(float value) {
43
- value_ = checkValue(value);
39
+ value_ = std::clamp(value, minValue_, maxValue_);
44
40
  }
45
41
 
46
42
  float AudioParam::getValueAtTime(double time) {
47
- if (!changesQueue_.empty()) {
48
- if (endTime_ < time) {
49
- auto change = *changesQueue_.begin();
50
- startTime_ = change.getStartTime();
51
- endTime_ = change.getEndTime();
52
- startValue_ = change.getStartValue();
53
- endValue_ = change.getEndValue();
54
- calculateValue_ = change.getCalculateValue();
55
- changesQueue_.erase(changesQueue_.begin());
56
- }
43
+ if (endTime_ < time && !eventsQueue_.empty()) {
44
+ auto event = eventsQueue_.front();
45
+ startTime_ = event.getStartTime();
46
+ endTime_ = event.getEndTime();
47
+ startValue_ = event.getStartValue();
48
+ endValue_ = event.getEndValue();
49
+ calculateValue_ = event.getCalculateValue();
50
+ eventsQueue_.pop_front();
57
51
  }
58
52
 
59
- if (startTime_ <= time) {
60
- value_ =
61
- calculateValue_(startTime_, endTime_, startValue_, endValue_, time);
62
- }
53
+ setValue(calculateValue_(startTime_, endTime_, startValue_, endValue_, time));
63
54
 
64
55
  return value_;
65
56
  }
66
57
 
67
- void AudioParam::setValueAtTime(float value, double time) {
68
- value = checkValue(value);
69
- auto calculateValue = [](double, double, float, float endValue, double) {
58
+ void AudioParam::setValueAtTime(float value, double startTime) {
59
+ if (startTime <= getQueueEndTime()) {
60
+ return;
61
+ }
62
+
63
+ auto calculateValue = [](double startTime,
64
+ double,
65
+ float startValue,
66
+ float endValue,
67
+ double time) {
68
+ if (time < startTime) {
69
+ return startValue;
70
+ }
71
+
70
72
  return endValue;
71
73
  };
72
74
 
73
- auto paramChange = ParamChange(time, time, value, value, calculateValue);
74
- changesQueue_.insert(paramChange);
75
+ auto event = ParamChangeEvent(
76
+ startTime,
77
+ startTime,
78
+ getQueueEndValue(),
79
+ value,
80
+ calculateValue,
81
+ ParamChangeEventType::SET_VALUE);
82
+ updateQueue(event);
75
83
  }
76
84
 
77
- void AudioParam::linearRampToValueAtTime(float value, double time) {
78
- value = checkValue(value);
85
+ void AudioParam::linearRampToValueAtTime(float value, double endTime) {
86
+ if (endTime <= getQueueEndTime()) {
87
+ return;
88
+ }
89
+
79
90
  auto calculateValue = [](double startTime,
80
91
  double endTime,
81
92
  float startValue,
82
93
  float endValue,
83
94
  double time) {
84
- return time >= endTime ? endValue
85
- : startValue +
86
- (endValue - startValue) * (time - startTime) /
87
- (endTime - startTime);
95
+ if (time < startTime) {
96
+ return startValue;
97
+ }
98
+
99
+ if (time < endTime) {
100
+ return static_cast<float>(
101
+ startValue +
102
+ (endValue - startValue) * (time - startTime) / (endTime - startTime));
103
+ }
104
+
105
+ return endValue;
88
106
  };
89
107
 
90
- auto paramChange =
91
- ParamChange(getStartTime(), time, getStartValue(), value, calculateValue);
92
- changesQueue_.emplace(paramChange);
108
+ auto event = ParamChangeEvent(
109
+ getQueueEndTime(),
110
+ endTime,
111
+ getQueueEndValue(),
112
+ value,
113
+ calculateValue,
114
+ ParamChangeEventType::LINEAR_RAMP);
115
+ updateQueue(event);
93
116
  }
94
117
 
95
- void AudioParam::exponentialRampToValueAtTime(float value, double time) {
96
- value = checkValue(value);
118
+ void AudioParam::exponentialRampToValueAtTime(float value, double endTime) {
119
+ if (endTime <= getQueueEndTime()) {
120
+ return;
121
+ }
122
+
97
123
  auto calculateValue = [](double startTime,
98
124
  double endTime,
99
125
  float startValue,
100
126
  float endValue,
101
127
  double time) {
102
- return time >= endTime ? endValue
103
- : startValue *
104
- pow(endValue / startValue,
105
- (time - startTime) / (endTime - startTime));
128
+ if (time < startTime) {
129
+ return startValue;
130
+ }
131
+
132
+ if (time < endTime) {
133
+ return static_cast<float>(
134
+ startValue *
135
+ pow(endValue / startValue,
136
+ (time - startTime) / (endTime - startTime)));
137
+ }
138
+
139
+ return endValue;
106
140
  };
107
141
 
108
- auto paramChange =
109
- ParamChange(getStartTime(), time, getStartValue(), value, calculateValue);
110
- changesQueue_.emplace(paramChange);
142
+ auto event = ParamChangeEvent(
143
+ getQueueEndTime(),
144
+ endTime,
145
+ getQueueEndValue(),
146
+ value,
147
+ calculateValue,
148
+ ParamChangeEventType::EXPONENTIAL_RAMP);
149
+ updateQueue(event);
111
150
  }
112
151
 
113
- float AudioParam::checkValue(float value) const {
114
- return std::clamp(value, minValue_, maxValue_);
152
+ void AudioParam::setTargetAtTime(
153
+ float target,
154
+ double startTime,
155
+ double timeConstant) {
156
+ if (startTime <= getQueueEndTime()) {
157
+ return;
158
+ }
159
+
160
+ auto calculateValue =
161
+ [timeConstant, target](
162
+ double startTime, double, float startValue, float, double time) {
163
+ if (time < startTime) {
164
+ return startValue;
165
+ }
166
+
167
+ return static_cast<float>(
168
+ target +
169
+ (startValue - target) * exp(-(time - startTime) / timeConstant));
170
+ };
171
+
172
+ auto event = ParamChangeEvent(
173
+ startTime,
174
+ startTime,
175
+ getQueueEndValue(),
176
+ getQueueEndValue(),
177
+ calculateValue,
178
+ ParamChangeEventType::SET_TARGET);
179
+ updateQueue(event);
115
180
  }
116
181
 
117
- double AudioParam::getStartTime() {
118
- if (changesQueue_.empty()) {
119
- return context_->getCurrentTime();
182
+ void AudioParam::setValueCurveAtTime(
183
+ const float *values,
184
+ int length,
185
+ double startTime,
186
+ double duration) {
187
+ if (startTime <= getQueueEndTime()) {
188
+ return;
120
189
  }
121
190
 
122
- return changesQueue_.rbegin()->getEndTime();
191
+ auto calculateValue = [&values, length](
192
+ double startTime,
193
+ double endTime,
194
+ float startValue,
195
+ float endValue,
196
+ double time) {
197
+ if (time < startTime) {
198
+ return startValue;
199
+ }
200
+
201
+ if (time < endTime) {
202
+ auto k = static_cast<int>(std::floor(
203
+ (length - 1) / (endTime - startTime) * (time - startTime)));
204
+ auto factor = static_cast<float>(
205
+ k - (time - startTime) * (length - 1) / (endTime - startTime));
206
+
207
+ return AudioUtils::linearInterpolate(values, k, k + 1, factor);
208
+ }
209
+
210
+ return endValue;
211
+ };
212
+
213
+ auto event = ParamChangeEvent(
214
+ startTime,
215
+ startTime + duration,
216
+ getQueueEndValue(),
217
+ values[length - 1],
218
+ calculateValue,
219
+ ParamChangeEventType::SET_VALUE_CURVE);
220
+ updateQueue(event);
221
+ }
222
+
223
+ void AudioParam::cancelScheduledValues(double cancelTime) {
224
+ auto it = eventsQueue_.rbegin();
225
+ while (it->getEndTime() >= cancelTime) {
226
+ if (it->getStartTime() >= cancelTime ||
227
+ it->getType() == ParamChangeEventType::SET_VALUE_CURVE) {
228
+ eventsQueue_.pop_back();
229
+ }
230
+
231
+ it++;
232
+ }
123
233
  }
124
234
 
125
- float AudioParam::getStartValue() {
126
- if (changesQueue_.empty()) {
127
- return this->value_;
235
+ void AudioParam::cancelAndHoldAtTime(double cancelTime) {
236
+ auto it = eventsQueue_.rbegin();
237
+ while (it->getEndTime() >= cancelTime) {
238
+ if (it->getStartTime() >= cancelTime) {
239
+ eventsQueue_.pop_back();
240
+ }
241
+
242
+ it++;
243
+ }
244
+
245
+ if (eventsQueue_.empty()) {
246
+ endTime_ = cancelTime;
247
+ }
248
+
249
+ if (!eventsQueue_.empty()) {
250
+ auto lastEvent = eventsQueue_.rbegin();
251
+ if (lastEvent->getEndTime() > cancelTime) {
252
+ lastEvent->setEndTime(cancelTime);
253
+ }
254
+ }
255
+ }
256
+
257
+ double AudioParam::getQueueEndTime() {
258
+ if (eventsQueue_.empty()) {
259
+ return endTime_;
260
+ }
261
+
262
+ return eventsQueue_.back().getEndTime();
263
+ }
264
+
265
+ float AudioParam::getQueueEndValue() {
266
+ if (eventsQueue_.empty()) {
267
+ return this->endValue_;
268
+ }
269
+
270
+ return eventsQueue_.back().getEndValue();
271
+ }
272
+
273
+ void AudioParam::updateQueue(ParamChangeEvent &event) {
274
+ if (!eventsQueue_.empty()) {
275
+ auto prev = eventsQueue_.back();
276
+
277
+ if (prev.getType() == ParamChangeEventType::SET_TARGET) {
278
+ prev.setEndTime(event.getStartTime());
279
+ prev.setEndValue(prev.getCalculateValue()(
280
+ prev.getStartTime(),
281
+ prev.getEndTime(),
282
+ prev.getStartValue(),
283
+ prev.getEndValue(),
284
+ event.getStartTime()));
285
+ }
286
+
287
+ event.setStartValue(prev.getEndValue());
128
288
  }
129
289
 
130
- return changesQueue_.rbegin()->getEndValue();
290
+ eventsQueue_.push_back(event);
131
291
  }
132
292
 
133
293
  } // namespace audioapi
@@ -1,40 +1,44 @@
1
1
  #pragma once
2
2
 
3
+ #include <deque>
3
4
  #include <memory>
4
- #include <set>
5
5
  #include <vector>
6
6
 
7
- #include "ParamChange.h"
7
+ #include "ParamChangeEvent.h"
8
+ #include "ParamChangeEventType.h"
8
9
 
9
10
  namespace audioapi {
10
11
 
11
- class BaseAudioContext;
12
-
13
12
  class AudioParam {
14
13
  public:
15
- explicit AudioParam(
16
- BaseAudioContext *context,
17
- float defaultValue,
18
- float minValue,
19
- float maxValue);
14
+ explicit AudioParam(float defaultValue, float minValue, float maxValue);
20
15
 
21
16
  [[nodiscard]] float getValue() const;
22
17
  float getValueAtTime(double time);
23
- void setValue(float value);
24
18
  [[nodiscard]] float getDefaultValue() const;
25
19
  [[nodiscard]] float getMinValue() const;
26
20
  [[nodiscard]] float getMaxValue() const;
21
+
22
+ void setValue(float value);
23
+
27
24
  void setValueAtTime(float value, double startTime);
28
25
  void linearRampToValueAtTime(float value, double endTime);
29
26
  void exponentialRampToValueAtTime(float value, double endTime);
27
+ void setTargetAtTime(float target, double startTime, double timeConstant);
28
+ void setValueCurveAtTime(
29
+ const float *values,
30
+ int length,
31
+ double startTime,
32
+ double duration);
33
+ void cancelScheduledValues(double cancelTime);
34
+ void cancelAndHoldAtTime(double cancelTime);
30
35
 
31
36
  private:
32
37
  float value_;
33
38
  float defaultValue_;
34
39
  float minValue_;
35
40
  float maxValue_;
36
- BaseAudioContext *context_;
37
- std::set<ParamChange> changesQueue_;
41
+ std::deque<ParamChangeEvent> eventsQueue_;
38
42
 
39
43
  double startTime_;
40
44
  double endTime_;
@@ -42,9 +46,9 @@ class AudioParam {
42
46
  float endValue_;
43
47
  std::function<float(double, double, float, float, double)> calculateValue_;
44
48
 
45
- float checkValue(float value) const;
46
- double getStartTime();
47
- float getStartValue();
49
+ double getQueueEndTime();
50
+ float getQueueEndValue();
51
+ void updateQueue(ParamChangeEvent &event);
48
52
  };
49
53
 
50
54
  } // namespace audioapi
@@ -86,7 +86,7 @@ void AudioScheduledSourceNode::updatePlaybackInfo(
86
86
  ? std::max(startFrame, firstFrame) - firstFrame
87
87
  : 0;
88
88
  nonSilentFramesToProcess =
89
- std::min(lastFrame, stopFrame) - startFrame - startOffset;
89
+ std::min(lastFrame, stopFrame) - startFrame;
90
90
  processingBus->zero(0, startOffset);
91
91
  return;
92
92
  }
@@ -107,6 +107,7 @@ void AudioScheduledSourceNode::updatePlaybackInfo(
107
107
  startOffset = 0;
108
108
  nonSilentFramesToProcess = 0;
109
109
  playbackState_ = PlaybackState::FINISHED;
110
+ disable();
110
111
  return;
111
112
  }
112
113
 
@@ -115,4 +116,11 @@ void AudioScheduledSourceNode::updatePlaybackInfo(
115
116
  nonSilentFramesToProcess = framesToProcess;
116
117
  }
117
118
 
119
+ void AudioScheduledSourceNode::handleStopScheduled() {
120
+ if (isPlaying() && stopTime_ > 0 && context_->getCurrentTime() >= stopTime_) {
121
+ playbackState_ = PlaybackState::FINISHED;
122
+ disable();
123
+ }
124
+ }
125
+
118
126
  } // namespace audioapi
@@ -27,13 +27,16 @@ class AudioScheduledSourceNode : public AudioNode {
27
27
  bool isFinished();
28
28
 
29
29
  protected:
30
- std::atomic<PlaybackState> playbackState_;
30
+ PlaybackState playbackState_;
31
+
31
32
  void updatePlaybackInfo(
32
33
  AudioBus *processingBus,
33
34
  int framesToProcess,
34
35
  size_t &startOffset,
35
36
  size_t &nonSilentFramesToProcess);
36
37
 
38
+ void handleStopScheduled();
39
+
37
40
  private:
38
41
  double startTime_;
39
42
  double stopTime_;
@@ -1,8 +1,6 @@
1
1
  #ifdef ANDROID
2
- #include "AudioDecoder.h"
3
2
  #include "AudioPlayer.h"
4
3
  #else
5
- #include "IOSAudioDecoder.h"
6
4
  #include "IOSAudioPlayer.h"
7
5
  #endif
8
6
 
@@ -12,6 +10,7 @@
12
10
  #include "AudioBuffer.h"
13
11
  #include "AudioBufferSourceNode.h"
14
12
  #include "AudioBus.h"
13
+ #include "AudioDecoder.h"
15
14
  #include "AudioDestinationNode.h"
16
15
  #include "AudioNodeManager.h"
17
16
  #include "BiquadFilterNode.h"
@@ -25,13 +24,12 @@ namespace audioapi {
25
24
  BaseAudioContext::BaseAudioContext() {
26
25
  #ifdef ANDROID
27
26
  audioPlayer_ = std::make_shared<AudioPlayer>(this->renderAudio());
28
- audioDecoder_ = std::make_shared<AudioDecoder>(audioPlayer_->getSampleRate());
29
27
  #else
30
28
  audioPlayer_ = std::make_shared<IOSAudioPlayer>(this->renderAudio());
31
- audioDecoder_ =
32
- std::make_shared<IOSAudioDecoder>(audioPlayer_->getSampleRate());
33
29
  #endif
34
30
 
31
+ audioDecoder_ = std::make_shared<AudioDecoder>(audioPlayer_->getSampleRate());
32
+
35
33
  sampleRate_ = audioPlayer_->getSampleRate();
36
34
  bufferSizeInFrames_ = audioPlayer_->getBufferSizeInFrames();
37
35
 
@@ -108,19 +106,11 @@ std::shared_ptr<PeriodicWave> BaseAudioContext::createPeriodicWave(
108
106
  sampleRate_, real, imag, length, disableNormalization);
109
107
  }
110
108
 
111
- #ifdef ANDROID
112
109
  std::shared_ptr<AudioBuffer> BaseAudioContext::decodeAudioDataSource(
113
110
  const std::string &path) {
114
111
  auto audioBus = audioDecoder_->decodeWithFilePath(path);
115
112
  return std::make_shared<AudioBuffer>(audioBus);
116
113
  }
117
- #else
118
- std::shared_ptr<AudioBuffer> BaseAudioContext::decodeAudioDataSource(
119
- const std::string &path) {
120
- auto audioBus = audioDecoder_->decodeWithFilePath(path);
121
- return std::make_shared<AudioBuffer>(audioBus);
122
- }
123
- #endif
124
114
 
125
115
  std::function<void(AudioBus *, int)> BaseAudioContext::renderAudio() {
126
116
  if (!isRunning()) {
@@ -21,13 +21,12 @@ class AudioNodeManager;
21
21
  class BiquadFilterNode;
22
22
  class AudioDestinationNode;
23
23
  class AudioBufferSourceNode;
24
+ class AudioDecoder;
24
25
 
25
26
  #ifdef ANDROID
26
27
  class AudioPlayer;
27
- class AudioDecoder;
28
28
  #else
29
29
  class IOSAudioPlayer;
30
- class IOSAudioDecoder;
31
30
  #endif
32
31
 
33
32
  class BaseAudioContext {
@@ -65,13 +64,12 @@ class BaseAudioContext {
65
64
  protected:
66
65
  static std::string toString(ContextState state);
67
66
  std::shared_ptr<AudioDestinationNode> destination_;
67
+ std::shared_ptr<AudioDecoder> audioDecoder_;
68
68
 
69
69
  #ifdef ANDROID
70
70
  std::shared_ptr<AudioPlayer> audioPlayer_;
71
- std::shared_ptr<AudioDecoder> audioDecoder_;
72
71
  #else
73
72
  std::shared_ptr<IOSAudioPlayer> audioPlayer_;
74
- std::shared_ptr<IOSAudioDecoder> audioDecoder_;
75
73
  #endif
76
74
 
77
75
  int sampleRate_;
@@ -11,13 +11,11 @@ namespace audioapi {
11
11
  BiquadFilterNode::BiquadFilterNode(BaseAudioContext *context)
12
12
  : AudioNode(context) {
13
13
  frequencyParam_ = std::make_shared<AudioParam>(
14
- context, 350.0, MIN_FILTER_FREQUENCY, MAX_FILTER_FREQUENCY);
15
- detuneParam_ =
16
- std::make_shared<AudioParam>(context, 0.0, -MAX_DETUNE, MAX_DETUNE);
17
- QParam_ =
18
- std::make_shared<AudioParam>(context, 1.0, -MAX_FILTER_Q, MAX_FILTER_Q);
19
- gainParam_ = std::make_shared<AudioParam>(
20
- context, 0.0, MIN_FILTER_GAIN, MAX_FILTER_GAIN);
14
+ 350.0, MIN_FILTER_FREQUENCY, MAX_FILTER_FREQUENCY);
15
+ detuneParam_ = std::make_shared<AudioParam>(0.0, -MAX_DETUNE, MAX_DETUNE);
16
+ QParam_ = std::make_shared<AudioParam>(1.0, -MAX_FILTER_Q, MAX_FILTER_Q);
17
+ gainParam_ =
18
+ std::make_shared<AudioParam>(0.0, MIN_FILTER_GAIN, MAX_FILTER_GAIN);
21
19
  type_ = BiquadFilterType::LOWPASS;
22
20
  isInitialized_ = true;
23
21
  }
@@ -8,10 +8,8 @@
8
8
  namespace audioapi {
9
9
  constexpr int SAMPLE_RATE = 44100;
10
10
  constexpr int CHANNEL_COUNT = 2;
11
- constexpr float MOST_POSITIVE_SINGLE_FLOAT =
12
- static_cast<float>(std::numeric_limits<float>::max());
13
- constexpr float MOST_NEGATIVE_SINGLE_FLOAT =
14
- static_cast<float>(std::numeric_limits<float>::lowest());
11
+ constexpr float MOST_POSITIVE_SINGLE_FLOAT = static_cast<float>(std::numeric_limits<float>::max());
12
+ constexpr float MOST_NEGATIVE_SINGLE_FLOAT = static_cast<float>(std::numeric_limits<float>::lowest());
15
13
  constexpr float NYQUIST_FREQUENCY = SAMPLE_RATE / 2.0;
16
14
  static float MAX_DETUNE = 1200 * std::log2(MOST_POSITIVE_SINGLE_FLOAT);
17
15
  constexpr float MAX_GAIN = MOST_POSITIVE_SINGLE_FLOAT;
@@ -6,7 +6,7 @@
6
6
  namespace audioapi {
7
7
 
8
8
  GainNode::GainNode(BaseAudioContext *context) : AudioNode(context) {
9
- gainParam_ = std::make_shared<AudioParam>(context, 1.0, -MAX_GAIN, MAX_GAIN);
9
+ gainParam_ = std::make_shared<AudioParam>(1.0, -MAX_GAIN, MAX_GAIN);
10
10
  isInitialized_ = true;
11
11
  }
12
12
 
@@ -8,9 +8,8 @@ namespace audioapi {
8
8
  OscillatorNode::OscillatorNode(BaseAudioContext *context)
9
9
  : AudioScheduledSourceNode(context) {
10
10
  frequencyParam_ = std::make_shared<AudioParam>(
11
- context, 444.0, -NYQUIST_FREQUENCY, NYQUIST_FREQUENCY);
12
- detuneParam_ =
13
- std::make_shared<AudioParam>(context, 0.0, -MAX_DETUNE, MAX_DETUNE);
11
+ 444.0, -NYQUIST_FREQUENCY, NYQUIST_FREQUENCY);
12
+ detuneParam_ = std::make_shared<AudioParam>(0.0, -MAX_DETUNE, MAX_DETUNE);
14
13
  type_ = OscillatorType::SINE;
15
14
  periodicWave_ = context_->getBasicWaveForm(type_);
16
15
  isInitialized_ = true;
@@ -75,6 +74,8 @@ void OscillatorNode::processNode(AudioBus *processingBus, int framesToProcess) {
75
74
 
76
75
  time += deltaTime;
77
76
  }
77
+
78
+ handleStopScheduled();
78
79
  }
79
80
 
80
81
  } // namespace audioapi
@@ -0,0 +1,58 @@
1
+ #include "ParamChangeEvent.h"
2
+
3
+ #include <utility>
4
+
5
+ namespace audioapi {
6
+
7
+ ParamChangeEvent::ParamChangeEvent(
8
+ double startTime,
9
+ double endTime,
10
+ float startValue,
11
+ float endValue,
12
+ std::function<float(double, double, float, float, double)> calculateValue,
13
+ ParamChangeEventType type)
14
+ : startTime_(startTime),
15
+ endTime_(endTime),
16
+ startValue_(startValue),
17
+ endValue_(endValue),
18
+ calculateValue_(std::move(calculateValue)),
19
+ type_(type) {}
20
+
21
+ double ParamChangeEvent::getEndTime() const {
22
+ return endTime_;
23
+ }
24
+
25
+ double ParamChangeEvent::getStartTime() const {
26
+ return startTime_;
27
+ }
28
+
29
+ float ParamChangeEvent::getEndValue() const {
30
+ return endValue_;
31
+ }
32
+
33
+ float ParamChangeEvent::getStartValue() const {
34
+ return startValue_;
35
+ }
36
+
37
+ std::function<float(double, double, float, float, double)>
38
+ ParamChangeEvent::getCalculateValue() const {
39
+ return calculateValue_;
40
+ }
41
+
42
+ ParamChangeEventType ParamChangeEvent::getType() const {
43
+ return type_;
44
+ }
45
+
46
+ void ParamChangeEvent::setEndTime(double endTime) {
47
+ endTime_ = endTime;
48
+ }
49
+
50
+ void ParamChangeEvent::setStartValue(float startValue) {
51
+ startValue_ = startValue;
52
+ }
53
+
54
+ void ParamChangeEvent::setEndValue(float endValue) {
55
+ endValue_ = endValue;
56
+ }
57
+
58
+ } // namespace audioapi