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
@@ -2,110 +2,106 @@
2
2
 
3
3
  #include <algorithm>
4
4
  #include <cmath>
5
+ #include <complex>
5
6
  #include <memory>
6
7
  #include <stdexcept>
7
8
  #include <string>
9
+ #include <vector>
8
10
 
9
11
  #include "AudioNode.h"
10
12
  #include "AudioParam.h"
13
+ #include "BiquadFilterType.h"
11
14
 
12
15
  namespace audioapi {
13
16
 
17
+ class AudioBus;
18
+
14
19
  class BiquadFilterNode : public AudioNode {
15
20
  public:
16
- explicit BiquadFilterNode(AudioContext *context);
21
+ explicit BiquadFilterNode(BaseAudioContext *context);
17
22
 
18
- std::string getType() const;
23
+ [[nodiscard]] std::string getType();
19
24
  void setType(const std::string &type);
20
- std::shared_ptr<AudioParam> getFrequencyParam() const;
21
- std::shared_ptr<AudioParam> getDetuneParam() const;
22
- std::shared_ptr<AudioParam> getQParam() const;
23
- std::shared_ptr<AudioParam> getGainParam() const;
25
+ [[nodiscard]] std::shared_ptr<AudioParam> getFrequencyParam() const;
26
+ [[nodiscard]] std::shared_ptr<AudioParam> getDetuneParam() const;
27
+ [[nodiscard]] std::shared_ptr<AudioParam> getQParam() const;
28
+ [[nodiscard]] std::shared_ptr<AudioParam> getGainParam() const;
29
+ void getFrequencyResponse(
30
+ const std::vector<float> &frequencyArray,
31
+ std::vector<float> &magResponseOutput,
32
+ std::vector<float> &phaseResponseOutput);
24
33
 
25
34
  protected:
26
- bool processAudio(float *audioData, int32_t numFrames) override;
35
+ void processNode(AudioBus *processingBus, int framesToProcess) override;
27
36
 
28
37
  private:
29
- enum class FilterType {
30
- LOWPASS,
31
- HIGHPASS,
32
- BANDPASS,
33
- LOWSHELF,
34
- HIGHSHELF,
35
- PEAKING,
36
- NOTCH,
37
- ALLPASS
38
- };
39
-
40
- static FilterType fromString(const std::string &type) {
38
+ std::shared_ptr<AudioParam> frequencyParam_;
39
+ std::shared_ptr<AudioParam> detuneParam_;
40
+ std::shared_ptr<AudioParam> QParam_;
41
+ std::shared_ptr<AudioParam> gainParam_;
42
+ audioapi::BiquadFilterType type_;
43
+
44
+ // delayed samples
45
+ float x1_ = 0;
46
+ float x2_ = 0;
47
+ float y1_ = 0;
48
+ float y2_ = 0;
49
+
50
+ // coefficients
51
+ float b0_ = 1.0;
52
+ float b1_ = 0;
53
+ float b2_ = 0;
54
+ float a1_ = 1.0;
55
+ float a2_ = 0;
56
+
57
+ static BiquadFilterType fromString(const std::string &type) {
41
58
  std::string lowerType = type;
42
59
  std::transform(
43
60
  lowerType.begin(), lowerType.end(), lowerType.begin(), ::tolower);
44
61
 
45
62
  if (lowerType == "lowpass")
46
- return FilterType::LOWPASS;
63
+ return BiquadFilterType::LOWPASS;
47
64
  if (lowerType == "highpass")
48
- return FilterType::HIGHPASS;
65
+ return BiquadFilterType::HIGHPASS;
49
66
  if (lowerType == "bandpass")
50
- return FilterType::BANDPASS;
67
+ return BiquadFilterType::BANDPASS;
51
68
  if (lowerType == "lowshelf")
52
- return FilterType::LOWSHELF;
69
+ return BiquadFilterType::LOWSHELF;
53
70
  if (lowerType == "highshelf")
54
- return FilterType::HIGHSHELF;
71
+ return BiquadFilterType::HIGHSHELF;
55
72
  if (lowerType == "peaking")
56
- return FilterType::PEAKING;
73
+ return BiquadFilterType::PEAKING;
57
74
  if (lowerType == "notch")
58
- return FilterType::NOTCH;
75
+ return BiquadFilterType::NOTCH;
59
76
  if (lowerType == "allpass")
60
- return FilterType::ALLPASS;
77
+ return BiquadFilterType::ALLPASS;
61
78
 
62
79
  throw std::invalid_argument("Invalid filter type: " + type);
63
80
  }
64
81
 
65
- static std::string toString(FilterType type) {
82
+ static std::string toString(BiquadFilterType type) {
66
83
  switch (type) {
67
- case FilterType::LOWPASS:
84
+ case BiquadFilterType::LOWPASS:
68
85
  return "lowpass";
69
- case FilterType::HIGHPASS:
86
+ case BiquadFilterType::HIGHPASS:
70
87
  return "highpass";
71
- case FilterType::BANDPASS:
88
+ case BiquadFilterType::BANDPASS:
72
89
  return "bandpass";
73
- case FilterType::LOWSHELF:
90
+ case BiquadFilterType::LOWSHELF:
74
91
  return "lowshelf";
75
- case FilterType::HIGHSHELF:
92
+ case BiquadFilterType::HIGHSHELF:
76
93
  return "highshelf";
77
- case FilterType::PEAKING:
94
+ case BiquadFilterType::PEAKING:
78
95
  return "peaking";
79
- case FilterType::NOTCH:
96
+ case BiquadFilterType::NOTCH:
80
97
  return "notch";
81
- case FilterType::ALLPASS:
98
+ case BiquadFilterType::ALLPASS:
82
99
  return "allpass";
83
100
  default:
84
101
  throw std::invalid_argument("Unknown filter type");
85
102
  }
86
103
  }
87
104
 
88
- private:
89
- std::shared_ptr<AudioParam> frequencyParam_;
90
- std::shared_ptr<AudioParam> detuneParam_;
91
- std::shared_ptr<AudioParam> QParam_;
92
- std::shared_ptr<AudioParam> gainParam_;
93
- FilterType type_;
94
-
95
- // delayed samples
96
- float x1_ = 0;
97
- float x2_ = 0;
98
- float y1_ = 0;
99
- float y2_ = 0;
100
-
101
- // coefficients
102
- float b0_ = 1.0;
103
- float b1_ = 0;
104
- float b2_ = 0;
105
- float a1_ = 1.0;
106
- float a2_ = 0;
107
-
108
- static float clamp(float value, float min, float max);
109
105
  void resetCoefficients();
110
106
  void setNormalizedCoefficients(
111
107
  float b0,
@@ -1,30 +1,30 @@
1
1
  #include "GainNode.h"
2
- #include "AudioContext.h"
2
+ #include "AudioArray.h"
3
+ #include "AudioBus.h"
4
+ #include "BaseAudioContext.h"
3
5
 
4
6
  namespace audioapi {
5
7
 
6
- GainNode::GainNode(AudioContext *context) : AudioNode(context) {
8
+ GainNode::GainNode(BaseAudioContext *context) : AudioNode(context) {
7
9
  gainParam_ = std::make_shared<AudioParam>(context, 1.0, -MAX_GAIN, MAX_GAIN);
10
+ isInitialized_ = true;
8
11
  }
9
12
 
10
13
  std::shared_ptr<AudioParam> GainNode::getGainParam() const {
11
14
  return gainParam_;
12
15
  }
13
16
 
14
- bool GainNode::processAudio(float *audioData, int32_t numFrames) {
15
- if (!AudioNode::processAudio(audioData, numFrames)) {
16
- return false;
17
- }
17
+ void GainNode::processNode(AudioBus *processingBus, int framesToProcess) {
18
+ double time = context_->getCurrentTime();
19
+ double deltaTime = 1.0 / context_->getSampleRate();
18
20
 
19
- auto time = context_->getCurrentTime();
20
- auto deltaTime = 1.0 / context_->getSampleRate();
21
+ for (int i = 0; i < framesToProcess; i += 1) {
22
+ for (int j = 0; j < processingBus->getNumberOfChannels(); j += 1) {
23
+ (*processingBus->getChannel(j))[i] *= gainParam_->getValueAtTime(time);
24
+ }
21
25
 
22
- for (int i = 0; i < numFrames * channelCount_; i++) {
23
- audioData[i] *= gainParam_->getValueAtTime(time);
24
26
  time += deltaTime;
25
27
  }
26
-
27
- return true;
28
28
  }
29
29
 
30
30
  } // namespace audioapi
@@ -7,14 +7,16 @@
7
7
 
8
8
  namespace audioapi {
9
9
 
10
+ class AudioBus;
11
+
10
12
  class GainNode : public AudioNode {
11
13
  public:
12
- explicit GainNode(AudioContext *context);
14
+ explicit GainNode(BaseAudioContext *context);
13
15
 
14
- std::shared_ptr<AudioParam> getGainParam() const;
16
+ [[nodiscard]] std::shared_ptr<AudioParam> getGainParam() const;
15
17
 
16
18
  protected:
17
- bool processAudio(float *audioData, int32_t numFrames) override;
19
+ void processNode(AudioBus *processingBus, int framesToProcess) override;
18
20
 
19
21
  private:
20
22
  std::shared_ptr<AudioParam> gainParam_;
@@ -1,14 +1,19 @@
1
1
  #include "OscillatorNode.h"
2
- #include "AudioContext.h"
2
+ #include "AudioArray.h"
3
+ #include "AudioBus.h"
4
+ #include "BaseAudioContext.h"
3
5
 
4
6
  namespace audioapi {
5
7
 
6
- OscillatorNode::OscillatorNode(AudioContext *context)
8
+ OscillatorNode::OscillatorNode(BaseAudioContext *context)
7
9
  : AudioScheduledSourceNode(context) {
8
10
  frequencyParam_ = std::make_shared<AudioParam>(
9
11
  context, 444.0, -NYQUIST_FREQUENCY, NYQUIST_FREQUENCY);
10
12
  detuneParam_ =
11
13
  std::make_shared<AudioParam>(context, 0.0, -MAX_DETUNE, MAX_DETUNE);
14
+ type_ = OscillatorType::SINE;
15
+ periodicWave_ = context_->getBasicWaveForm(type_);
16
+ isInitialized_ = true;
12
17
  }
13
18
 
14
19
  std::shared_ptr<AudioParam> OscillatorNode::getFrequencyParam() const {
@@ -25,42 +30,46 @@ std::string OscillatorNode::getType() {
25
30
 
26
31
  void OscillatorNode::setType(const std::string &type) {
27
32
  type_ = OscillatorNode::fromString(type);
33
+ periodicWave_ = context_->getBasicWaveForm(type_);
28
34
  }
29
35
 
30
- bool OscillatorNode::processAudio(float *audioData, int32_t numFrames) {
31
- if (!isPlaying_) {
32
- return false;
33
- } else {
34
- auto time = context_->getCurrentTime();
35
- auto deltaTime = 1.0 / context_->getSampleRate();
36
-
37
- for (int i = 0; i < numFrames; ++i) {
38
- auto detuneRatio =
39
- std::pow(2.0f, detuneParam_->getValueAtTime(time) / 1200.0f);
40
- auto detunedFrequency =
41
- round(frequencyParam_->getValueAtTime(time) * detuneRatio);
42
- auto phaseIncrement = static_cast<float>(
43
- 2 * M_PI * detunedFrequency / context_->getSampleRate());
36
+ void OscillatorNode::setPeriodicWave(
37
+ const std::shared_ptr<PeriodicWave> &periodicWave) {
38
+ periodicWave_ = periodicWave;
39
+ type_ = OscillatorType::CUSTOM;
40
+ }
44
41
 
45
- float value = OscillatorNode::getWaveBufferElement(phase_, type_);
42
+ void OscillatorNode::processNode(AudioBus *processingBus, int framesToProcess) {
43
+ if (!isPlaying()) {
44
+ processingBus->zero();
45
+ return;
46
+ }
46
47
 
47
- for (int j = 0; j < channelCount_; j++) {
48
- audioData[i * channelCount_ + j] = value;
49
- }
48
+ double time = context_->getCurrentTime();
49
+ double deltaTime = 1.0 / context_->getSampleRate();
50
50
 
51
- phase_ += phaseIncrement;
52
- time += deltaTime;
51
+ for (int i = 0; i < framesToProcess; i += 1) {
52
+ auto detuneRatio =
53
+ std::pow(2.0f, detuneParam_->getValueAtTime(time) / 1200.0f);
54
+ auto detunedFrequency =
55
+ round(frequencyParam_->getValueAtTime(time) * detuneRatio);
56
+ auto phaseIncrement = detunedFrequency * periodicWave_->getScale();
53
57
 
54
- if (phase_ >= 2 * M_PI) {
55
- phase_ -= 2 * M_PI;
56
- }
58
+ float sample =
59
+ periodicWave_->getSample(detunedFrequency, phase_, phaseIncrement);
57
60
 
58
- if (phase_ < 0) {
59
- phase_ += 2 * M_PI;
60
- }
61
+ for (int j = 0; j < processingBus->getNumberOfChannels(); j += 1) {
62
+ (*processingBus->getChannel(j))[i] = sample;
61
63
  }
62
64
 
63
- return true;
65
+ phase_ += phaseIncrement;
66
+ phase_ -=
67
+ floor(
68
+ phase_ / static_cast<float>(periodicWave_->getPeriodicWaveSize())) *
69
+ static_cast<float>(periodicWave_->getPeriodicWaveSize());
70
+
71
+ time += deltaTime;
64
72
  }
65
73
  }
74
+
66
75
  } // namespace audioapi
@@ -6,107 +6,67 @@
6
6
 
7
7
  #include "AudioParam.h"
8
8
  #include "AudioScheduledSourceNode.h"
9
+ #include "OscillatorType.h"
10
+ #include "PeriodicWave.h"
9
11
 
10
12
  namespace audioapi {
11
13
 
14
+ class AudioBus;
15
+
12
16
  class OscillatorNode : public AudioScheduledSourceNode {
13
17
  public:
14
- explicit OscillatorNode(AudioContext *context);
18
+ explicit OscillatorNode(BaseAudioContext *context);
15
19
 
16
- std::shared_ptr<AudioParam> getFrequencyParam() const;
17
- std::shared_ptr<AudioParam> getDetuneParam() const;
18
- std::string getType();
20
+ [[nodiscard]] std::shared_ptr<AudioParam> getFrequencyParam() const;
21
+ [[nodiscard]] std::shared_ptr<AudioParam> getDetuneParam() const;
22
+ [[nodiscard]] std::string getType();
19
23
  void setType(const std::string &type);
24
+ void setPeriodicWave(const std::shared_ptr<PeriodicWave> &periodicWave);
20
25
 
21
26
  protected:
22
- bool processAudio(float *audioData, int32_t numFrames) override;
27
+ void processNode(AudioBus *processingBus, int framesToProcess) override;
23
28
 
24
29
  private:
25
- enum class WaveType { SINE, SQUARE, SAWTOOTH, TRIANGLE, CUSTOM };
26
-
27
- static float sineWave(double wavePhase) {
28
- return static_cast<float>(std::sin(wavePhase));
29
- }
30
-
31
- static float squareWave(double wavePhase) {
32
- return static_cast<float>(std::sin(wavePhase) >= 0 ? 1.0 : -1.0);
33
- }
34
-
35
- static float sawtoothWave(double wavePhase) {
36
- return static_cast<float>(
37
- 2.0 *
38
- (wavePhase / (2 * M_PI) - std::floor(wavePhase / (2 * M_PI) + 0.5)));
39
- }
40
-
41
- static float triangleWave(double wavePhase) {
42
- return static_cast<float>(
43
- 2.0 *
44
- std::abs(
45
- 2.0 *
46
- (wavePhase / (2 * M_PI) -
47
- std::floor(wavePhase / (2 * M_PI) + 0.5))) -
48
- 1.0);
49
- }
50
-
51
- static float getWaveValue(double wavePhase, WaveType type) {
52
- switch (type) {
53
- case WaveType::SINE:
54
- return sineWave(wavePhase);
55
- case WaveType::SQUARE:
56
- return squareWave(wavePhase);
57
- case WaveType::SAWTOOTH:
58
- return sawtoothWave(wavePhase);
59
- case WaveType::TRIANGLE:
60
- return triangleWave(wavePhase);
61
- default:
62
- throw std::invalid_argument("Unknown wave type");
63
- }
64
- }
30
+ std::shared_ptr<AudioParam> frequencyParam_;
31
+ std::shared_ptr<AudioParam> detuneParam_;
32
+ OscillatorType type_;
33
+ float phase_ = 0.0;
34
+ std::shared_ptr<PeriodicWave> periodicWave_;
65
35
 
66
- static WaveType fromString(const std::string &type) {
36
+ static OscillatorType fromString(const std::string &type) {
67
37
  std::string lowerType = type;
68
38
  std::transform(
69
39
  lowerType.begin(), lowerType.end(), lowerType.begin(), ::tolower);
70
40
 
71
41
  if (lowerType == "sine")
72
- return WaveType::SINE;
42
+ return OscillatorType::SINE;
73
43
  if (lowerType == "square")
74
- return WaveType::SQUARE;
44
+ return OscillatorType::SQUARE;
75
45
  if (lowerType == "sawtooth")
76
- return WaveType::SAWTOOTH;
46
+ return OscillatorType::SAWTOOTH;
77
47
  if (lowerType == "triangle")
78
- return WaveType::TRIANGLE;
48
+ return OscillatorType::TRIANGLE;
79
49
  if (lowerType == "custom")
80
- return WaveType::CUSTOM;
50
+ return OscillatorType::CUSTOM;
81
51
 
82
- throw std::invalid_argument("Unknown wave type: " + type);
52
+ throw std::invalid_argument("Unknown oscillator type: " + type);
83
53
  }
84
54
 
85
- static std::string toString(WaveType type) {
55
+ static std::string toString(OscillatorType type) {
86
56
  switch (type) {
87
- case WaveType::SINE:
57
+ case OscillatorType::SINE:
88
58
  return "sine";
89
- case WaveType::SQUARE:
59
+ case OscillatorType::SQUARE:
90
60
  return "square";
91
- case WaveType::SAWTOOTH:
61
+ case OscillatorType::SAWTOOTH:
92
62
  return "sawtooth";
93
- case WaveType::TRIANGLE:
63
+ case OscillatorType::TRIANGLE:
94
64
  return "triangle";
95
- case WaveType::CUSTOM:
65
+ case OscillatorType::CUSTOM:
96
66
  return "custom";
97
67
  default:
98
- throw std::invalid_argument("Unknown wave type");
68
+ throw std::invalid_argument("Unknown oscillator type");
99
69
  }
100
70
  }
101
-
102
- static float getWaveBufferElement(double wavePhase, WaveType waveType) {
103
- return getWaveValue(wavePhase, waveType);
104
- }
105
-
106
- private:
107
- std::shared_ptr<AudioParam> frequencyParam_;
108
- std::shared_ptr<AudioParam> detuneParam_;
109
- WaveType type_ = WaveType::SINE;
110
- float phase_ = 0.0;
111
71
  };
112
72
  } // namespace audioapi
@@ -15,12 +15,12 @@ class ParamChange {
15
15
  std::function<float(double, double, float, float, double)>
16
16
  calculateValue);
17
17
 
18
- double getEndTime() const;
19
- double getStartTime() const;
20
- float getEndValue() const;
21
- float getStartValue() const;
22
- std::function<float(double, double, float, float, double)> getCalculateValue()
23
- const;
18
+ [[nodiscard]] double getEndTime() const;
19
+ [[nodiscard]] double getStartTime() const;
20
+ [[nodiscard]] float getEndValue() const;
21
+ [[nodiscard]] float getStartValue() const;
22
+ [[nodiscard]] std::function<float(double, double, float, float, double)>
23
+ getCalculateValue() const;
24
24
  bool operator<(const ParamChange &other) const;
25
25
 
26
26
  private: