react-native-audio-api 0.10.0-nightly-034f768-20251020 → 0.10.0-nightly-75aa9d0-20251022

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 (65) hide show
  1. package/android/build.gradle +1 -0
  2. package/common/cpp/audioapi/HostObjects/BaseAudioContextHostObject.cpp +12 -5
  3. package/common/cpp/audioapi/HostObjects/effects/BiquadFilterNodeHostObject.cpp +1 -1
  4. package/common/cpp/audioapi/core/BaseAudioContext.cpp +12 -6
  5. package/common/cpp/audioapi/core/BaseAudioContext.h +14 -3
  6. package/common/cpp/audioapi/core/effects/BiquadFilterNode.cpp +69 -32
  7. package/common/cpp/audioapi/core/effects/BiquadFilterNode.h +37 -1
  8. package/common/cpp/audioapi/core/effects/WorkletNode.cpp +31 -32
  9. package/common/cpp/audioapi/core/effects/WorkletNode.h +4 -7
  10. package/common/cpp/audioapi/core/effects/WorkletProcessingNode.cpp +12 -13
  11. package/common/cpp/audioapi/core/effects/WorkletProcessingNode.h +2 -5
  12. package/common/cpp/audioapi/core/sources/WorkletSourceNode.cpp +12 -13
  13. package/common/cpp/audioapi/core/sources/WorkletSourceNode.h +2 -5
  14. package/common/cpp/audioapi/core/utils/Constants.h +2 -1
  15. package/common/cpp/audioapi/core/utils/worklets/SafeIncludes.h +32 -3
  16. package/common/cpp/audioapi/core/utils/worklets/WorkletsRunner.cpp +75 -2
  17. package/common/cpp/audioapi/core/utils/worklets/WorkletsRunner.h +50 -36
  18. package/common/cpp/audioapi/libs/ffmpeg/FFmpegDecoding.cpp +2 -3
  19. package/common/cpp/test/CMakeLists.txt +3 -0
  20. package/common/cpp/test/src/biquad/BiquadFilterChromium.cpp +389 -0
  21. package/common/cpp/test/src/biquad/BiquadFilterChromium.h +64 -0
  22. package/common/cpp/test/src/biquad/BiquadFilterTest.cpp +284 -0
  23. package/common/cpp/test/src/biquad/BiquadFilterTest.h +40 -0
  24. package/lib/commonjs/api.js +43 -0
  25. package/lib/commonjs/api.js.map +1 -1
  26. package/lib/commonjs/api.web.js +50 -0
  27. package/lib/commonjs/api.web.js.map +1 -1
  28. package/lib/commonjs/core/AudioContext.js +0 -4
  29. package/lib/commonjs/core/AudioContext.js.map +1 -1
  30. package/lib/commonjs/core/OfflineAudioContext.js +0 -4
  31. package/lib/commonjs/core/OfflineAudioContext.js.map +1 -1
  32. package/lib/commonjs/utils/index.js +1 -0
  33. package/lib/commonjs/utils/index.js.map +1 -1
  34. package/lib/commonjs/web-core/AudioScheduledSourceNode.js.map +1 -1
  35. package/lib/module/api.js +1 -0
  36. package/lib/module/api.js.map +1 -1
  37. package/lib/module/api.web.js +1 -0
  38. package/lib/module/api.web.js.map +1 -1
  39. package/lib/module/core/AudioContext.js +0 -4
  40. package/lib/module/core/AudioContext.js.map +1 -1
  41. package/lib/module/core/OfflineAudioContext.js +0 -4
  42. package/lib/module/core/OfflineAudioContext.js.map +1 -1
  43. package/lib/module/utils/index.js +1 -0
  44. package/lib/module/utils/index.js.map +1 -1
  45. package/lib/module/web-core/AudioScheduledSourceNode.js.map +1 -1
  46. package/lib/typescript/api.d.ts +1 -0
  47. package/lib/typescript/api.d.ts.map +1 -1
  48. package/lib/typescript/api.web.d.ts +1 -0
  49. package/lib/typescript/api.web.d.ts.map +1 -1
  50. package/lib/typescript/core/AudioContext.d.ts +0 -1
  51. package/lib/typescript/core/AudioContext.d.ts.map +1 -1
  52. package/lib/typescript/core/OfflineAudioContext.d.ts +0 -1
  53. package/lib/typescript/core/OfflineAudioContext.d.ts.map +1 -1
  54. package/lib/typescript/web-core/AudioBufferSourceNode.d.ts +2 -2
  55. package/lib/typescript/web-core/AudioBufferSourceNode.d.ts.map +1 -1
  56. package/lib/typescript/web-core/AudioScheduledSourceNode.d.ts +1 -1
  57. package/lib/typescript/web-core/AudioScheduledSourceNode.d.ts.map +1 -1
  58. package/package.json +8 -8
  59. package/src/api.ts +10 -0
  60. package/src/api.web.ts +10 -0
  61. package/src/core/AudioContext.ts +0 -5
  62. package/src/core/OfflineAudioContext.ts +0 -5
  63. package/src/utils/index.ts +1 -0
  64. package/src/web-core/AudioBufferSourceNode.tsx +2 -2
  65. package/src/web-core/AudioScheduledSourceNode.tsx +1 -1
@@ -5,11 +5,9 @@ namespace audioapi {
5
5
 
6
6
  WorkletSourceNode::WorkletSourceNode(
7
7
  BaseAudioContext *context,
8
- std::shared_ptr<worklets::SerializableWorklet> &worklet,
9
- std::weak_ptr<worklets::WorkletRuntime> runtime)
8
+ WorkletsRunner &&workletRunner)
10
9
  : AudioScheduledSourceNode(context),
11
- workletRunner_(runtime),
12
- shareableWorklet_(worklet) {
10
+ workletRunner_(std::move(workletRunner)) {
13
11
  isInitialized_ = true;
14
12
 
15
13
  // Prepare buffers for audio processing
@@ -42,21 +40,22 @@ std::shared_ptr<AudioBus> WorkletSourceNode::processNode(
42
40
 
43
41
  size_t outputChannelCount = processingBus->getNumberOfChannels();
44
42
 
45
- auto result = workletRunner_.executeOnRuntimeGuardedSync(
43
+ auto result = workletRunner_.executeOnRuntimeSync(
46
44
  [this, nonSilentFramesToProcess, startOffset](jsi::Runtime &rt) {
47
45
  auto jsiArray = jsi::Array(rt, this->outputBuffsHandles_.size());
48
46
  for (size_t i = 0; i < this->outputBuffsHandles_.size(); ++i) {
49
47
  auto arrayBuffer = jsi::ArrayBuffer(rt, this->outputBuffsHandles_[i]);
50
48
  jsiArray.setValueAtIndex(rt, i, arrayBuffer);
51
49
  }
52
- return workletRunner_
53
- .executeWorklet(
54
- shareableWorklet_,
55
- jsiArray,
56
- jsi::Value(rt, static_cast<int>(nonSilentFramesToProcess)),
57
- jsi::Value(rt, this->context_->getCurrentTime()),
58
- jsi::Value(rt, static_cast<int>(startOffset)))
59
- .value_or(jsi::Value::undefined());
50
+
51
+ // We call unsafely here because we are already on the runtime thread
52
+ // and the runtime is locked by executeOnRuntimeSync (if
53
+ // shouldLockRuntime is true)
54
+ return workletRunner_.callUnsafe(
55
+ jsiArray,
56
+ jsi::Value(rt, static_cast<int>(nonSilentFramesToProcess)),
57
+ jsi::Value(rt, this->context_->getCurrentTime()),
58
+ jsi::Value(rt, static_cast<int>(startOffset)));
60
59
  });
61
60
 
62
61
  // If the worklet execution failed, zero the output
@@ -18,8 +18,7 @@ class WorkletSourceNode : public AudioScheduledSourceNode {
18
18
  public:
19
19
  explicit WorkletSourceNode(
20
20
  BaseAudioContext *context,
21
- std::shared_ptr<worklets::SerializableWorklet> &worklet,
22
- std::weak_ptr<worklets::WorkletRuntime> runtime
21
+ WorkletsRunner &&workletRunner
23
22
  ) : AudioScheduledSourceNode(context) {}
24
23
 
25
24
  protected:
@@ -31,15 +30,13 @@ class WorkletSourceNode : public AudioScheduledSourceNode {
31
30
  public:
32
31
  explicit WorkletSourceNode(
33
32
  BaseAudioContext *context,
34
- std::shared_ptr<worklets::SerializableWorklet> &worklet,
35
- std::weak_ptr<worklets::WorkletRuntime> runtime
33
+ WorkletsRunner &&workletRunner
36
34
  );
37
35
 
38
36
  protected:
39
37
  std::shared_ptr<AudioBus> processNode(const std::shared_ptr<AudioBus>& processingBus, int framesToProcess) override;
40
38
  private:
41
39
  WorkletsRunner workletRunner_;
42
- std::shared_ptr<worklets::SerializableWorklet> shareableWorklet_;
43
40
  std::vector<std::shared_ptr<AudioArrayBuffer>> outputBuffsHandles_;
44
41
  };
45
42
  #endif // RN_AUDIO_API_TEST
@@ -1,5 +1,6 @@
1
1
  #pragma once
2
2
 
3
+ #include <numbers>
3
4
  #include <cmath>
4
5
  #include <limits>
5
6
 
@@ -19,7 +20,7 @@ static constexpr float MOST_POSITIVE_SINGLE_FLOAT = static_cast<float>(std::nume
19
20
  static constexpr float MOST_NEGATIVE_SINGLE_FLOAT = static_cast<float>(std::numeric_limits<float>::lowest());
20
21
  static float LOG2_MOST_POSITIVE_SINGLE_FLOAT = std::log2(MOST_POSITIVE_SINGLE_FLOAT);
21
22
  static float LOG10_MOST_POSITIVE_SINGLE_FLOAT = std::log10(MOST_POSITIVE_SINGLE_FLOAT);
22
- static constexpr float PI = static_cast<float>(M_PI);
23
+ static constexpr float PI = std::numbers::pi_v<float>;
23
24
 
24
25
  // buffer sizes
25
26
  static constexpr size_t PROMISE_VENDOR_THREAD_POOL_WORKER_COUNT = 4;
@@ -27,6 +27,11 @@
27
27
  #include <worklets/android/WorkletsModule.h>
28
28
  #endif
29
29
  #else
30
+
31
+ #define RN_AUDIO_API_WORKLETS_DISABLED_ERROR \
32
+ std::runtime_error( \
33
+ "Worklets are disabled. Please install react-native-worklets or check if you have supported version to enable these features.");
34
+
30
35
  /// @brief Dummy implementation of worklets for non-worklet builds they should do nothing and mock necessary methods
31
36
  /// @note It helps to reduce compile time branching across codebase
32
37
  /// @note If you need to base some c++ implementation on if the worklets are enabled use `#if RN_AUDIO_API_ENABLE_WORKLETS`
@@ -36,17 +41,41 @@ using namespace facebook;
36
41
  class MessageQueueThread {};
37
42
  class WorkletsModuleProxy {};
38
43
  class WorkletRuntime {
39
- explicit WorkletRuntime(uint64_t, const std::shared_ptr<MessageQueueThread> &, const std::string &, const bool);
44
+ public:
45
+ explicit WorkletRuntime(uint64_t, const std::shared_ptr<MessageQueueThread> &, const std::string &, const bool) {
46
+ throw RN_AUDIO_API_WORKLETS_DISABLED_ERROR
47
+ }
48
+ jsi::Runtime &getJSIRuntime() const {
49
+ throw RN_AUDIO_API_WORKLETS_DISABLED_ERROR
50
+ }
51
+ jsi::Value executeSync(jsi::Runtime &rt, const jsi::Value &worklet) const {
52
+ throw RN_AUDIO_API_WORKLETS_DISABLED_ERROR
53
+ }
54
+ jsi::Value executeSync(std::function<jsi::Value(jsi::Runtime &)> &&job) const {
55
+ throw RN_AUDIO_API_WORKLETS_DISABLED_ERROR
56
+ }
57
+ jsi::Value executeSync(const std::function<jsi::Value(jsi::Runtime &)> &job) const {
58
+ throw RN_AUDIO_API_WORKLETS_DISABLED_ERROR
59
+ }
40
60
  };
41
61
  class SerializableWorklet {
42
- SerializableWorklet(jsi::Runtime*, const jsi::Object &);
62
+ public:
63
+ SerializableWorklet(jsi::Runtime*, const jsi::Object &) {
64
+ throw RN_AUDIO_API_WORKLETS_DISABLED_ERROR
65
+ }
66
+ jsi::Value toJSValue(jsi::Runtime &rt) {
67
+ throw RN_AUDIO_API_WORKLETS_DISABLED_ERROR
68
+ }
43
69
  };
44
70
  } // namespace worklets
71
+
72
+ #undef RN_AUDIO_API_WORKLETS_DISABLED_ERROR
73
+
45
74
  #endif
46
75
 
47
76
  /// @brief Struct to hold references to different runtimes used in the AudioAPI
48
77
  /// @note it is used to pass them around and avoid creating multiple instances of the same runtime
49
78
  struct RuntimeRegistry {
50
79
  std::weak_ptr<worklets::WorkletRuntime> uiRuntime;
51
- std::weak_ptr<worklets::WorkletRuntime> audioRuntime;
80
+ std::shared_ptr<worklets::WorkletRuntime> audioRuntime;
52
81
  };
@@ -3,7 +3,80 @@
3
3
  namespace audioapi {
4
4
 
5
5
  WorkletsRunner::WorkletsRunner(
6
- std::weak_ptr<worklets::WorkletRuntime> weakUiRuntime) noexcept
7
- : weakUiRuntime_(std::move(weakUiRuntime)) {}
6
+ std::weak_ptr<worklets::WorkletRuntime> weakRuntime,
7
+ std::shared_ptr<worklets::SerializableWorklet> shareableWorklet,
8
+ bool shouldLockRuntime)
9
+ : weakRuntime_(std::move(weakRuntime)),
10
+ shouldLockRuntime(shouldLockRuntime) {
11
+ auto strongRuntime = weakRuntime_.lock();
12
+ if (strongRuntime == nullptr) {
13
+ return;
14
+ }
15
+ #if RN_AUDIO_API_ENABLE_WORKLETS
16
+ unsafeRuntimePtr = &strongRuntime->getJSIRuntime();
17
+ strongRuntime->executeSync(
18
+ [this, shareableWorklet](jsi::Runtime &rt) -> jsi::Value {
19
+ /// Placement new to avoid dynamic memory allocation
20
+ new (reinterpret_cast<jsi::Function *>(&unsafeWorklet))
21
+ jsi::Function(shareableWorklet->toJSValue(*unsafeRuntimePtr)
22
+ .asObject(*unsafeRuntimePtr)
23
+ .asFunction(*unsafeRuntimePtr));
24
+ return jsi::Value::undefined();
25
+ });
26
+ workletInitialized = true;
27
+ #else
28
+ unsafeRuntimePtr = nullptr;
29
+ workletInitialized = false;
30
+ #endif
31
+ }
32
+
33
+ WorkletsRunner::WorkletsRunner(WorkletsRunner &&other)
34
+ : weakRuntime_(std::move(other.weakRuntime_)),
35
+ unsafeRuntimePtr(other.unsafeRuntimePtr),
36
+ shouldLockRuntime(other.shouldLockRuntime),
37
+ workletInitialized(other.workletInitialized) {
38
+ if (workletInitialized) {
39
+ std::memcpy(&unsafeWorklet, &other.unsafeWorklet, sizeof(unsafeWorklet));
40
+ other.workletInitialized = false;
41
+ other.unsafeRuntimePtr = nullptr;
42
+ }
43
+ }
44
+
45
+ WorkletsRunner::~WorkletsRunner() {
46
+ if (!workletInitialized) {
47
+ return;
48
+ }
49
+ auto strongRuntime = weakRuntime_.lock();
50
+ if (strongRuntime == nullptr) {
51
+ // We cannot safely destroy the worklet without a valid runtime
52
+ return;
53
+ }
54
+ reinterpret_cast<jsi::Function *>(&unsafeWorklet)->~Function();
55
+ workletInitialized = false;
56
+ }
57
+
58
+ std::optional<jsi::Value> WorkletsRunner::executeOnRuntimeGuarded(
59
+ const std::function<jsi::Value(jsi::Runtime &)> &&job) const
60
+ noexcept(noexcept(job)) {
61
+ auto strongRuntime = weakRuntime_.lock();
62
+ if (strongRuntime == nullptr) {
63
+ return std::nullopt;
64
+ }
65
+ #if RN_AUDIO_API_ENABLE_WORKLETS
66
+ return strongRuntime->executeSync(std::move(job));
67
+ #else
68
+ return std::nullopt;
69
+ #endif
70
+ }
71
+
72
+ std::optional<jsi::Value> WorkletsRunner::executeOnRuntimeUnsafe(
73
+ const std::function<jsi::Value(jsi::Runtime &)> &&job) const
74
+ noexcept(noexcept(job)) {
75
+ #if RN_AUDIO_API_ENABLE_WORKLETS
76
+ return job(*unsafeRuntimePtr);
77
+ #else
78
+ return std::nullopt;
79
+ #endif
80
+ };
8
81
 
9
82
  }; // namespace audioapi
@@ -26,48 +26,62 @@ using namespace facebook;
26
26
 
27
27
  class WorkletsRunner {
28
28
  public:
29
- explicit WorkletsRunner(std::weak_ptr<worklets::WorkletRuntime> weakUiRuntime) noexcept;
29
+ explicit WorkletsRunner(
30
+ std::weak_ptr<worklets::WorkletRuntime> weakRuntime,
31
+ std::shared_ptr<worklets::SerializableWorklet> shareableWorklet,
32
+ bool shouldLockRuntime = true);
33
+ WorkletsRunner(WorkletsRunner&&);
34
+ ~WorkletsRunner();
30
35
 
31
- /// @brief Execute a job on the UI runtime safely.
32
- /// @param job
33
- /// @return nullopt if the runtime is not available or the result of the job execution
34
- /// @note Execution is synchronous
35
- std::optional<jsi::Value> executeOnRuntimeGuardedSync(const std::function<jsi::Value(jsi::Runtime&)>&& job) const noexcept(noexcept(job)) {
36
- auto strongRuntime = weakUiRuntime_.lock();
37
- if (strongRuntime == nullptr) {
38
- return std::nullopt;
39
- }
40
- #if RN_AUDIO_API_ENABLE_WORKLETS
41
- return strongRuntime->executeSync(std::move(job));
42
- #else
43
- return std::nullopt;
44
- #endif
45
- }
36
+ /// @brief Call the worklet function with the given arguments.
37
+ /// @tparam ...Args
38
+ /// @param ...args
39
+ /// @return The result of the worklet function call.
40
+ /// @note This method is unsafe and should be used with caution. It assumes that the runtime and worklet are valid and runtime is locked.
41
+ template<typename... Args>
42
+ inline jsi::Value callUnsafe(Args&&... args) {
43
+ return getUnsafeWorklet().call(*unsafeRuntimePtr, std::forward<Args>(args)...);
44
+ }
46
45
 
47
- /// @brief Execute a worklet with the given arguments.
48
- /// @tparam ...Args
49
- /// @param shareableWorklet
50
- /// @param ...args
51
- /// @note Execution is synchronous, this method can be used in `executeOnRuntimeGuardedSync` and `...Async` methods arguments
52
- /// @return nullopt if the runtime is not available or the result of the worklet execution
53
- template<typename... Args>
54
- std::optional<jsi::Value> executeWorklet(const std::shared_ptr<worklets::SerializableWorklet>& shareableWorklet, Args&&... args) {
55
- auto strongRuntime = weakUiRuntime_.lock();
56
- if (strongRuntime == nullptr) {
57
- return std::nullopt;
58
- }
59
46
 
60
- #if RN_AUDIO_API_ENABLE_WORKLETS
47
+ /// @brief Call the worklet function with the given arguments.
48
+ /// @tparam ...Args
49
+ /// @param ...args
50
+ /// @return The result of the worklet function call.
51
+ /// @note This method is safe and will check if the runtime is available before calling the worklet. If the runtime is not available, it will return nullopt.
52
+ template<typename... Args>
53
+ inline std::optional<jsi::Value> call(Args&&... args) {
54
+ return executeOnRuntimeGuarded([this, args...](jsi::Runtime &rt) -> jsi::Value {
55
+ return callUnsafe(std::forward<Args>(args)...);
56
+ });
57
+ }
61
58
 
62
- return strongRuntime->runGuarded(shareableWorklet, std::forward<Args>(args)...);
63
-
64
- #else
65
- return std::nullopt;
66
- #endif
67
- }
59
+ /// @brief Execute a job on the UI runtime safely.
60
+ /// @param job
61
+ /// @return nullopt if the runtime is not available or the result of the job execution
62
+ /// @note Execution is synchronous and will be guarded if shouldLockRuntime is true.
63
+ inline std::optional<jsi::Value> executeOnRuntimeSync(const std::function<jsi::Value(jsi::Runtime&)>&& job) const noexcept(noexcept(job)) {
64
+ if (shouldLockRuntime) return executeOnRuntimeGuarded(std::move(job));
65
+ else return executeOnRuntimeUnsafe(std::move(job));
66
+ }
68
67
 
69
68
  private:
70
- std::weak_ptr<worklets::WorkletRuntime> weakUiRuntime_;
69
+ std::weak_ptr<worklets::WorkletRuntime> weakRuntime_;
70
+ jsi::Runtime* unsafeRuntimePtr = nullptr;
71
+
72
+ /// @note We want to avoid automatic destruction as
73
+ /// when runtime is destroyed, underlying pointer will be invalid
74
+ char unsafeWorklet[sizeof(jsi::Function)];
75
+ bool workletInitialized = false;
76
+ bool shouldLockRuntime = true;
77
+
78
+ inline jsi::Function &getUnsafeWorklet() {
79
+ return *reinterpret_cast<jsi::Function*>(&unsafeWorklet);
80
+ }
81
+
82
+ std::optional<jsi::Value> executeOnRuntimeGuarded(const std::function<jsi::Value(jsi::Runtime&)>&& job) const noexcept(noexcept(job));
83
+
84
+ std::optional<jsi::Value> executeOnRuntimeUnsafe(const std::function<jsi::Value(jsi::Runtime&)>&& job) const noexcept(noexcept(job));
71
85
  };
72
86
 
73
87
  } // namespace audioapi
@@ -277,8 +277,7 @@ decodeWithMemoryBlock(const void *data, size_t size, int sample_rate) {
277
277
  MemoryIOContext io_ctx{static_cast<const uint8_t *>(data), size, 0};
278
278
 
279
279
  constexpr size_t buffer_size = 4096;
280
- auto io_buffer = std::unique_ptr<uint8_t, decltype(&av_free)>(
281
- static_cast<uint8_t *>(av_malloc(buffer_size)), &av_free);
280
+ uint8_t *io_buffer = static_cast<uint8_t *>(av_malloc(buffer_size));
282
281
  if (io_buffer == nullptr) {
283
282
  return nullptr;
284
283
  }
@@ -286,7 +285,7 @@ decodeWithMemoryBlock(const void *data, size_t size, int sample_rate) {
286
285
  auto avio_ctx =
287
286
  std::unique_ptr<AVIOContext, std::function<void(AVIOContext *)>>(
288
287
  avio_alloc_context(
289
- io_buffer.get(),
288
+ io_buffer,
290
289
  buffer_size,
291
290
  0,
292
291
  &io_ctx,
@@ -12,6 +12,7 @@ FetchContent_Declare(
12
12
  googletest
13
13
  URL https://github.com/google/googletest/archive/3983f67e32fb3e9294487b9d4f9586efa6e5d088.zip
14
14
  )
15
+
15
16
  # For Windows: Prevent overriding the parent project's compiler/linker settings
16
17
  set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
17
18
  FetchContent_MakeAvailable(googletest)
@@ -57,6 +58,8 @@ target_include_directories(rnaudioapi PUBLIC
57
58
  ${JSI_DIR}
58
59
  "${REACT_NATIVE_DIR}/ReactCommon"
59
60
  "${REACT_NATIVE_DIR}/ReactCommon/callinvoker"
61
+ ${gtest_SOURCE_DIR}/include
62
+ ${gmock_SOURCE_DIR}/include
60
63
  )
61
64
 
62
65
  target_include_directories(rnaudioapi_libs PUBLIC