react-native-audio-api 0.8.3-nightly-ea268f4-20251006 → 0.9.0-nightly-96a5bcd-20251007

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 (74) hide show
  1. package/android/src/main/cpp/audioapi/android/core/{AudioDecoder.cpp → utils/AudioDecoder.cpp} +79 -75
  2. package/common/cpp/audioapi/AudioAPIModuleInstaller.h +99 -43
  3. package/common/cpp/audioapi/HostObjects/BaseAudioContextHostObject.cpp +1 -101
  4. package/common/cpp/audioapi/HostObjects/BaseAudioContextHostObject.h +0 -3
  5. package/common/cpp/audioapi/HostObjects/utils/AudioDecoderHostObject.cpp +107 -0
  6. package/common/cpp/audioapi/HostObjects/utils/AudioDecoderHostObject.h +28 -0
  7. package/common/cpp/audioapi/core/AudioContext.cpp +0 -2
  8. package/common/cpp/audioapi/core/AudioNode.cpp +3 -3
  9. package/common/cpp/audioapi/core/AudioNode.h +1 -1
  10. package/common/cpp/audioapi/core/BaseAudioContext.cpp +0 -35
  11. package/common/cpp/audioapi/core/BaseAudioContext.h +4 -12
  12. package/common/cpp/audioapi/core/OfflineAudioContext.cpp +0 -2
  13. package/common/cpp/audioapi/core/analysis/AnalyserNode.cpp +3 -1
  14. package/common/cpp/audioapi/core/analysis/AnalyserNode.h +1 -1
  15. package/common/cpp/audioapi/core/destinations/AudioDestinationNode.h +1 -1
  16. package/common/cpp/audioapi/core/effects/BiquadFilterNode.cpp +3 -1
  17. package/common/cpp/audioapi/core/effects/BiquadFilterNode.h +1 -1
  18. package/common/cpp/audioapi/core/effects/GainNode.cpp +3 -1
  19. package/common/cpp/audioapi/core/effects/GainNode.h +1 -1
  20. package/common/cpp/audioapi/core/effects/StereoPannerNode.cpp +17 -12
  21. package/common/cpp/audioapi/core/effects/StereoPannerNode.h +1 -1
  22. package/common/cpp/audioapi/core/effects/WorkletNode.cpp +3 -1
  23. package/common/cpp/audioapi/core/effects/WorkletNode.h +2 -2
  24. package/common/cpp/audioapi/core/effects/WorkletProcessingNode.cpp +3 -1
  25. package/common/cpp/audioapi/core/effects/WorkletProcessingNode.h +2 -2
  26. package/common/cpp/audioapi/core/sources/AudioBufferQueueSourceNode.cpp +4 -2
  27. package/common/cpp/audioapi/core/sources/AudioBufferQueueSourceNode.h +1 -1
  28. package/common/cpp/audioapi/core/sources/AudioBufferSourceNode.cpp +4 -2
  29. package/common/cpp/audioapi/core/sources/AudioBufferSourceNode.h +1 -1
  30. package/common/cpp/audioapi/core/sources/ConstantSourceNode.cpp +4 -2
  31. package/common/cpp/audioapi/core/sources/ConstantSourceNode.h +1 -1
  32. package/common/cpp/audioapi/core/sources/OscillatorNode.cpp +7 -2
  33. package/common/cpp/audioapi/core/sources/OscillatorNode.h +1 -1
  34. package/common/cpp/audioapi/core/sources/RecorderAdapterNode.cpp +3 -1
  35. package/common/cpp/audioapi/core/sources/RecorderAdapterNode.h +1 -1
  36. package/common/cpp/audioapi/core/sources/StreamerNode.cpp +9 -1
  37. package/common/cpp/audioapi/core/sources/StreamerNode.h +1 -1
  38. package/common/cpp/audioapi/core/sources/WorkletSourceNode.cpp +6 -4
  39. package/common/cpp/audioapi/core/sources/WorkletSourceNode.h +2 -2
  40. package/common/cpp/audioapi/core/types/AudioFormat.h +16 -0
  41. package/common/cpp/audioapi/core/utils/AudioDecoder.h +36 -90
  42. package/common/cpp/audioapi/libs/ffmpeg/FFmpegDecoding.cpp +241 -282
  43. package/common/cpp/audioapi/libs/ffmpeg/FFmpegDecoding.h +57 -19
  44. package/common/cpp/test/CMakeLists.txt +2 -1
  45. package/common/cpp/test/GainTest.cpp +9 -9
  46. package/common/cpp/test/StereoPannerTest.cpp +129 -0
  47. package/ios/audioapi/ios/core/IOSAudioRecorder.h +1 -2
  48. package/ios/audioapi/ios/core/utils/AudioDecoder.mm +160 -0
  49. package/lib/commonjs/api.js +14 -1
  50. package/lib/commonjs/api.js.map +1 -1
  51. package/lib/commonjs/core/AudioDecoder.js +48 -0
  52. package/lib/commonjs/core/AudioDecoder.js.map +1 -0
  53. package/lib/commonjs/core/BaseAudioContext.js +11 -18
  54. package/lib/commonjs/core/BaseAudioContext.js.map +1 -1
  55. package/lib/module/api.js +2 -1
  56. package/lib/module/api.js.map +1 -1
  57. package/lib/module/core/AudioDecoder.js +42 -0
  58. package/lib/module/core/AudioDecoder.js.map +1 -0
  59. package/lib/module/core/BaseAudioContext.js +11 -18
  60. package/lib/module/core/BaseAudioContext.js.map +1 -1
  61. package/lib/typescript/api.d.ts +3 -1
  62. package/lib/typescript/api.d.ts.map +1 -1
  63. package/lib/typescript/core/AudioDecoder.d.ts +4 -0
  64. package/lib/typescript/core/AudioDecoder.d.ts.map +1 -0
  65. package/lib/typescript/core/BaseAudioContext.d.ts +3 -6
  66. package/lib/typescript/core/BaseAudioContext.d.ts.map +1 -1
  67. package/lib/typescript/interfaces.d.ts +6 -3
  68. package/lib/typescript/interfaces.d.ts.map +1 -1
  69. package/package.json +1 -1
  70. package/src/api.ts +5 -0
  71. package/src/core/AudioDecoder.ts +78 -0
  72. package/src/core/BaseAudioContext.ts +26 -29
  73. package/src/interfaces.ts +18 -6
  74. package/ios/audioapi/ios/core/AudioDecoder.mm +0 -156
@@ -1,3 +1,4 @@
1
+ #include <audioapi/core/sources/AudioBuffer.h>
1
2
  #include <audioapi/core/utils/AudioDecoder.h>
2
3
  #include <audioapi/dsp/VectorMath.h>
3
4
  #include <audioapi/libs/base64/base64.h>
@@ -5,28 +6,28 @@
5
6
  #include <audioapi/utils/AudioBus.h>
6
7
 
7
8
  #define MINIAUDIO_IMPLEMENTATION
9
+ #include <audioapi/libs/miniaudio/decoders/libopus/miniaudio_libopus.h>
10
+ #include <audioapi/libs/miniaudio/decoders/libvorbis/miniaudio_libvorbis.h>
8
11
  #include <audioapi/libs/miniaudio/miniaudio.h>
9
12
 
10
13
  #ifndef AUDIO_API_TEST_SUITE
11
14
  #include <android/log.h>
12
15
  #include <audioapi/libs/ffmpeg/FFmpegDecoding.h>
13
16
  #endif
14
- #include <audioapi/libs/miniaudio/decoders/libopus/miniaudio_libopus.h>
15
- #include <audioapi/libs/miniaudio/decoders/libvorbis/miniaudio_libvorbis.h>
16
17
 
17
18
  namespace audioapi {
18
19
 
19
20
  // Decoding audio in fixed-size chunks because total frame count can't be
20
21
  // determined in advance. Note: ma_decoder_get_length_in_pcm_frames() always
21
22
  // returns 0 for Vorbis decoders.
22
- std::vector<int16_t> AudioDecoder::readAllPcmFrames(
23
+ std::vector<float> AudioDecoder::readAllPcmFrames(
23
24
  ma_decoder &decoder,
24
- int numChannels,
25
- ma_uint64 &outFramesRead) {
26
- std::vector<int16_t> buffer;
27
- std::vector<int16_t> temp(CHUNK_SIZE * numChannels);
28
- outFramesRead = 0;
25
+ int outputChannels) {
26
+ std::vector<float> buffer;
27
+ std::vector<float> temp(CHUNK_SIZE * outputChannels);
28
+ ma_uint64 outFramesRead = 0;
29
29
 
30
+ #ifndef AUDIO_API_TEST_SUITE
30
31
  while (true) {
31
32
  ma_uint64 tempFramesDecoded = 0;
32
33
  ma_decoder_read_pcm_frames(
@@ -38,38 +39,46 @@ std::vector<int16_t> AudioDecoder::readAllPcmFrames(
38
39
  buffer.insert(
39
40
  buffer.end(),
40
41
  temp.data(),
41
- temp.data() + tempFramesDecoded * numChannels);
42
+ temp.data() + tempFramesDecoded * outputChannels);
42
43
  outFramesRead += tempFramesDecoded;
43
44
  }
44
45
 
46
+ if (outFramesRead == 0) {
47
+ __android_log_print(ANDROID_LOG_ERROR, "AudioDecoder", "Failed to decode");
48
+ }
49
+ #endif
45
50
  return buffer;
46
51
  }
47
52
 
48
- std::shared_ptr<AudioBus> AudioDecoder::makeAudioBusFromInt16Buffer(
49
- const std::vector<int16_t> &buffer,
50
- int numChannels,
51
- float sampleRate) {
52
- auto outputFrames = buffer.size() / numChannels;
53
- auto audioBus =
54
- std::make_shared<AudioBus>(outputFrames, numChannels, sampleRate);
53
+ std::shared_ptr<AudioBuffer> AudioDecoder::makeAudioBufferFromFloatBuffer(
54
+ const std::vector<float> &buffer,
55
+ float outputSampleRate,
56
+ int outputChannels) {
57
+ if (buffer.empty()) {
58
+ return nullptr;
59
+ }
55
60
 
56
- for (int ch = 0; ch < numChannels; ++ch) {
61
+ auto outputFrames = buffer.size() / outputChannels;
62
+ auto audioBus = std::make_shared<AudioBus>(
63
+ outputFrames, outputChannels, outputSampleRate);
64
+
65
+ for (int ch = 0; ch < outputChannels; ++ch) {
57
66
  auto channelData = audioBus->getChannel(ch)->getData();
58
67
  for (int i = 0; i < outputFrames; ++i) {
59
- channelData[i] = int16ToFloat(buffer[i * numChannels + ch]);
68
+ channelData[i] = buffer[i * outputChannels + ch];
60
69
  }
61
70
  }
62
- return audioBus;
71
+ return std::make_shared<AudioBuffer>(audioBus);
63
72
  }
64
73
 
65
- std::shared_ptr<AudioBus> AudioDecoder::decodeWithFilePath(
66
- const std::string &path) const {
74
+ std::shared_ptr<AudioBuffer> AudioDecoder::decodeWithFilePath(
75
+ const std::string &path,
76
+ float sampleRate) {
67
77
  #ifndef AUDIO_API_TEST_SUITE
68
- std::vector<int16_t> buffer;
69
78
  if (AudioDecoder::pathHasExtension(path, {".mp4", ".m4a", ".aac"})) {
70
- buffer = ffmpegdecoding::decodeWithFilePath(
71
- path, numChannels_, static_cast<int>(sampleRate_));
72
- if (buffer.empty()) {
79
+ auto buffer =
80
+ ffmpegdecoder::decodeWithFilePath(path, static_cast<int>(sampleRate));
81
+ if (buffer == nullptr) {
73
82
  __android_log_print(
74
83
  ANDROID_LOG_ERROR,
75
84
  "AudioDecoder",
@@ -77,11 +86,11 @@ std::shared_ptr<AudioBus> AudioDecoder::decodeWithFilePath(
77
86
  path.c_str());
78
87
  return nullptr;
79
88
  }
80
- return makeAudioBusFromInt16Buffer(buffer, numChannels_, sampleRate_);
89
+ return buffer;
81
90
  }
82
91
  ma_decoder decoder;
83
- ma_decoder_config config = ma_decoder_config_init(
84
- ma_format_s16, numChannels_, static_cast<int>(sampleRate_));
92
+ ma_decoder_config config =
93
+ ma_decoder_config_init(ma_format_f32, 0, static_cast<int>(sampleRate));
85
94
  ma_decoding_backend_vtable *customBackends[] = {
86
95
  ma_decoding_backend_libvorbis, ma_decoding_backend_libopus};
87
96
 
@@ -99,41 +108,38 @@ std::shared_ptr<AudioBus> AudioDecoder::decodeWithFilePath(
99
108
  return nullptr;
100
109
  }
101
110
 
102
- ma_uint64 framesRead = 0;
103
- buffer = readAllPcmFrames(decoder, numChannels_, framesRead);
104
- if (framesRead == 0) {
105
- __android_log_print(ANDROID_LOG_ERROR, "AudioDecoder", "Failed to decode");
106
- ma_decoder_uninit(&decoder);
107
- return nullptr;
108
- }
111
+ auto outputSampleRate = static_cast<float>(decoder.outputSampleRate);
112
+ auto outputChannels = static_cast<int>(decoder.outputChannels);
109
113
 
114
+ std::vector<float> buffer = readAllPcmFrames(decoder, outputChannels);
110
115
  ma_decoder_uninit(&decoder);
111
- return makeAudioBusFromInt16Buffer(buffer, numChannels_, sampleRate_);
116
+ return makeAudioBufferFromFloatBuffer(
117
+ buffer, outputSampleRate, outputChannels);
112
118
  #else
113
119
  return nullptr;
114
120
  #endif
115
121
  }
116
122
 
117
- std::shared_ptr<AudioBus> AudioDecoder::decodeWithMemoryBlock(
123
+ std::shared_ptr<AudioBuffer> AudioDecoder::decodeWithMemoryBlock(
118
124
  const void *data,
119
- size_t size) const {
125
+ size_t size,
126
+ float sampleRate) {
120
127
  #ifndef AUDIO_API_TEST_SUITE
121
- std::vector<int16_t> buffer;
122
128
  const AudioFormat format = AudioDecoder::detectAudioFormat(data, size);
123
129
  if (format == AudioFormat::MP4 || format == AudioFormat::M4A ||
124
130
  format == AudioFormat::AAC) {
125
- buffer = ffmpegdecoding::decodeWithMemoryBlock(
126
- data, size, numChannels_, sampleRate_);
127
- if (buffer.empty()) {
131
+ auto buffer = ffmpegdecoder::decodeWithMemoryBlock(
132
+ data, size, static_cast<int>(sampleRate));
133
+ if (buffer == nullptr) {
128
134
  __android_log_print(
129
135
  ANDROID_LOG_ERROR, "AudioDecoder", "Failed to decode with FFmpeg");
130
136
  return nullptr;
131
137
  }
132
- return makeAudioBusFromInt16Buffer(buffer, numChannels_, sampleRate_);
138
+ return buffer;
133
139
  }
134
140
  ma_decoder decoder;
135
- ma_decoder_config config = ma_decoder_config_init(
136
- ma_format_s16, numChannels_, static_cast<int>(sampleRate_));
141
+ ma_decoder_config config =
142
+ ma_decoder_config_init(ma_format_f32, 0, static_cast<int>(sampleRate));
137
143
 
138
144
  ma_decoding_backend_vtable *customBackends[] = {
139
145
  ma_decoding_backend_libvorbis, ma_decoding_backend_libopus};
@@ -151,50 +157,48 @@ std::shared_ptr<AudioBus> AudioDecoder::decodeWithMemoryBlock(
151
157
  return nullptr;
152
158
  }
153
159
 
154
- ma_uint64 framesRead = 0;
155
- buffer = readAllPcmFrames(decoder, numChannels_, framesRead);
156
- if (framesRead == 0) {
157
- __android_log_print(ANDROID_LOG_ERROR, "AudioDecoder", "Failed to decode");
158
- ma_decoder_uninit(&decoder);
159
- return nullptr;
160
- }
160
+ auto outputSampleRate = static_cast<float>(decoder.outputSampleRate);
161
+ auto outputChannels = static_cast<int>(decoder.outputChannels);
161
162
 
163
+ std::vector<float> buffer = readAllPcmFrames(decoder, outputChannels);
162
164
  ma_decoder_uninit(&decoder);
163
- return makeAudioBusFromInt16Buffer(buffer, numChannels_, sampleRate_);
165
+ return makeAudioBufferFromFloatBuffer(
166
+ buffer, outputSampleRate, outputChannels);
164
167
  #else
165
168
  return nullptr;
166
169
  #endif
167
170
  }
168
171
 
169
- std::shared_ptr<AudioBus> AudioDecoder::decodeWithPCMInBase64(
172
+ std::shared_ptr<AudioBuffer> AudioDecoder::decodeWithPCMInBase64(
170
173
  const std::string &data,
171
- float playbackSpeed) const {
174
+ float inputSampleRate,
175
+ int inputChannelCount,
176
+ bool interleaved) {
172
177
  auto decodedData = base64_decode(data, false);
173
-
174
178
  const auto uint8Data = reinterpret_cast<uint8_t *>(decodedData.data());
175
- size_t framesDecoded = decodedData.size() / 2;
176
-
177
- std::vector<int16_t> buffer(framesDecoded);
178
- for (size_t i = 0; i < framesDecoded; ++i) {
179
- buffer[i] =
180
- static_cast<int16_t>((uint8Data[i * 2 + 1] << 8) | uint8Data[i * 2]);
181
- }
179
+ size_t numFramesDecoded =
180
+ decodedData.size() / (inputChannelCount * sizeof(int16_t));
182
181
 
183
- changePlaybackSpeedIfNeeded(buffer, framesDecoded, 1, playbackSpeed);
184
- auto outputFrames = buffer.size();
182
+ auto audioBus = std::make_shared<AudioBus>(
183
+ numFramesDecoded, inputChannelCount, inputSampleRate);
185
184
 
186
- auto audioBus =
187
- std::make_shared<AudioBus>(outputFrames, numChannels_, sampleRate_);
188
- auto leftChannelData = audioBus->getChannel(0)->getData();
189
- auto rightChannelData = audioBus->getChannel(1)->getData();
185
+ for (int ch = 0; ch < inputChannelCount; ++ch) {
186
+ auto channelData = audioBus->getChannel(ch)->getData();
190
187
 
191
- for (size_t i = 0; i < outputFrames; ++i) {
192
- auto sample = int16ToFloat(buffer[i]);
193
- leftChannelData[i] = sample;
194
- rightChannelData[i] = sample;
188
+ for (size_t i = 0; i < numFramesDecoded; ++i) {
189
+ size_t offset;
190
+ if (interleaved) {
191
+ // Ch1, Ch2, Ch1, Ch2, ...
192
+ offset = (i * inputChannelCount + ch) * sizeof(int16_t);
193
+ } else {
194
+ // Ch1, Ch1, Ch1, ..., Ch2, Ch2, Ch2, ...
195
+ offset = (ch * numFramesDecoded + i) * sizeof(int16_t);
196
+ }
197
+
198
+ channelData[i] = uint8ToFloat(uint8Data[offset], uint8Data[offset + 1]);
199
+ }
195
200
  }
196
-
197
- return audioBus;
201
+ return std::make_shared<AudioBuffer>(audioBus);
198
202
  }
199
203
 
200
204
  } // namespace audioapi
@@ -1,15 +1,16 @@
1
1
  #pragma once
2
2
 
3
- #include <audioapi/jsi/JsiPromise.h>
4
- #include <audioapi/core/AudioContext.h>
5
- #include <audioapi/core/OfflineAudioContext.h>
6
- #include <audioapi/core/inputs/AudioRecorder.h>
7
3
  #include <audioapi/HostObjects/AudioContextHostObject.h>
8
4
  #include <audioapi/HostObjects/OfflineAudioContextHostObject.h>
9
5
  #include <audioapi/HostObjects/inputs/AudioRecorderHostObject.h>
6
+ #include <audioapi/HostObjects/utils/AudioDecoderHostObject.h>
7
+ #include <audioapi/core/AudioContext.h>
8
+ #include <audioapi/core/OfflineAudioContext.h>
9
+ #include <audioapi/core/inputs/AudioRecorder.h>
10
+ #include <audioapi/jsi/JsiPromise.h>
10
11
 
11
- #include <audioapi/events/AudioEventHandlerRegistry.h>
12
12
  #include <audioapi/HostObjects/events/AudioEventHandlerRegistryHostObject.h>
13
+ #include <audioapi/events/AudioEventHandlerRegistry.h>
13
14
 
14
15
  #include <audioapi/core/utils/worklets/SafeIncludes.h>
15
16
 
@@ -22,29 +23,46 @@ using namespace facebook;
22
23
  class AudioAPIModuleInstaller {
23
24
  public:
24
25
  static void injectJSIBindings(
25
- jsi::Runtime *jsiRuntime,
26
- const std::shared_ptr<react::CallInvoker> &jsCallInvoker,
27
- const std::shared_ptr<AudioEventHandlerRegistry> &audioEventHandlerRegistry,
28
- std::shared_ptr<worklets::WorkletRuntime> uiRuntime = nullptr) {
29
-
30
- auto createAudioContext = getCreateAudioContextFunction(jsiRuntime, jsCallInvoker, audioEventHandlerRegistry, uiRuntime);
31
- auto createAudioRecorder = getCreateAudioRecorderFunction(jsiRuntime, audioEventHandlerRegistry);
32
- auto createOfflineAudioContext = getCreateOfflineAudioContextFunction(jsiRuntime, jsCallInvoker, audioEventHandlerRegistry, uiRuntime);
33
-
34
- jsiRuntime->global().setProperty(*jsiRuntime, "createAudioContext", createAudioContext);
35
- jsiRuntime->global().setProperty(*jsiRuntime, "createAudioRecorder", createAudioRecorder);
36
- jsiRuntime->global().setProperty(*jsiRuntime, "createOfflineAudioContext", createOfflineAudioContext);
37
-
38
- auto audioEventHandlerRegistryHostObject = std::make_shared<AudioEventHandlerRegistryHostObject>(audioEventHandlerRegistry);
39
- jsiRuntime->global().setProperty(*jsiRuntime, "AudioEventEmitter", jsi::Object::createFromHostObject(*jsiRuntime, audioEventHandlerRegistryHostObject));
26
+ jsi::Runtime *jsiRuntime,
27
+ const std::shared_ptr<react::CallInvoker> &jsCallInvoker,
28
+ const std::shared_ptr<AudioEventHandlerRegistry>
29
+ &audioEventHandlerRegistry,
30
+ std::shared_ptr<worklets::WorkletRuntime> uiRuntime = nullptr) {
31
+ auto createAudioContext = getCreateAudioContextFunction(
32
+ jsiRuntime, jsCallInvoker, audioEventHandlerRegistry, uiRuntime);
33
+ auto createAudioRecorder =
34
+ getCreateAudioRecorderFunction(jsiRuntime, audioEventHandlerRegistry);
35
+ auto createOfflineAudioContext = getCreateOfflineAudioContextFunction(
36
+ jsiRuntime, jsCallInvoker, audioEventHandlerRegistry, uiRuntime);
37
+ auto createAudioDecoder =
38
+ getCreateAudioDecoderFunction(jsiRuntime, jsCallInvoker);
39
+
40
+ jsiRuntime->global().setProperty(
41
+ *jsiRuntime, "createAudioContext", createAudioContext);
42
+ jsiRuntime->global().setProperty(
43
+ *jsiRuntime, "createAudioRecorder", createAudioRecorder);
44
+ jsiRuntime->global().setProperty(
45
+ *jsiRuntime, "createOfflineAudioContext", createOfflineAudioContext);
46
+ jsiRuntime->global().setProperty(
47
+ *jsiRuntime, "createAudioDecoder", createAudioDecoder);
48
+
49
+ auto audioEventHandlerRegistryHostObject =
50
+ std::make_shared<AudioEventHandlerRegistryHostObject>(
51
+ audioEventHandlerRegistry);
52
+ jsiRuntime->global().setProperty(
53
+ *jsiRuntime,
54
+ "AudioEventEmitter",
55
+ jsi::Object::createFromHostObject(
56
+ *jsiRuntime, audioEventHandlerRegistryHostObject));
40
57
  }
41
58
 
42
59
  private:
43
60
  static jsi::Function getCreateAudioContextFunction(
44
- jsi::Runtime *jsiRuntime,
45
- const std::shared_ptr<react::CallInvoker> &jsCallInvoker,
46
- const std::shared_ptr<AudioEventHandlerRegistry> &audioEventHandlerRegistry,
47
- const std::weak_ptr<worklets::WorkletRuntime> &uiRuntime) {
61
+ jsi::Runtime *jsiRuntime,
62
+ const std::shared_ptr<react::CallInvoker> &jsCallInvoker,
63
+ const std::shared_ptr<AudioEventHandlerRegistry>
64
+ &audioEventHandlerRegistry,
65
+ const std::weak_ptr<worklets::WorkletRuntime> &uiRuntime) {
48
66
  return jsi::Function::createFromHostFunction(
49
67
  *jsiRuntime,
50
68
  jsi::PropNameID::forAscii(*jsiRuntime, "createAudioContext"),
@@ -67,9 +85,14 @@ class AudioAPIModuleInstaller {
67
85
  auto runtimeRegistry = RuntimeRegistry{};
68
86
  #endif
69
87
 
70
- audioContext = std::make_shared<AudioContext>(sampleRate, initSuspended, audioEventHandlerRegistry, runtimeRegistry);
71
- auto audioContextHostObject = std::make_shared<AudioContextHostObject>(
72
- audioContext, &runtime, jsCallInvoker);
88
+ audioContext = std::make_shared<AudioContext>(
89
+ sampleRate,
90
+ initSuspended,
91
+ audioEventHandlerRegistry,
92
+ runtimeRegistry);
93
+ auto audioContextHostObject =
94
+ std::make_shared<AudioContextHostObject>(
95
+ audioContext, &runtime, jsCallInvoker);
73
96
 
74
97
  return jsi::Object::createFromHostObject(
75
98
  runtime, audioContextHostObject);
@@ -77,10 +100,11 @@ class AudioAPIModuleInstaller {
77
100
  }
78
101
 
79
102
  static jsi::Function getCreateOfflineAudioContextFunction(
80
- jsi::Runtime *jsiRuntime,
81
- const std::shared_ptr<react::CallInvoker> &jsCallInvoker,
82
- const std::shared_ptr<AudioEventHandlerRegistry> &audioEventHandlerRegistry,
83
- const std::weak_ptr<worklets::WorkletRuntime> &uiRuntime) {
103
+ jsi::Runtime *jsiRuntime,
104
+ const std::shared_ptr<react::CallInvoker> &jsCallInvoker,
105
+ const std::shared_ptr<AudioEventHandlerRegistry>
106
+ &audioEventHandlerRegistry,
107
+ const std::weak_ptr<worklets::WorkletRuntime> &uiRuntime) {
84
108
  return jsi::Function::createFromHostFunction(
85
109
  *jsiRuntime,
86
110
  jsi::PropNameID::forAscii(*jsiRuntime, "createOfflineAudioContext"),
@@ -90,9 +114,9 @@ class AudioAPIModuleInstaller {
90
114
  const jsi::Value &thisValue,
91
115
  const jsi::Value *args,
92
116
  size_t count) -> jsi::Value {
93
- auto numberOfChannels = static_cast<int>(args[0].getNumber());
94
- auto length = static_cast<size_t>(args[1].getNumber());
95
- auto sampleRate = static_cast<float>(args[2].getNumber());
117
+ auto numberOfChannels = static_cast<int>(args[0].getNumber());
118
+ auto length = static_cast<size_t>(args[1].getNumber());
119
+ auto sampleRate = static_cast<float>(args[2].getNumber());
96
120
 
97
121
  #if RN_AUDIO_API_ENABLE_WORKLETS
98
122
  auto runtimeRegistry = RuntimeRegistry{
@@ -103,9 +127,15 @@ class AudioAPIModuleInstaller {
103
127
  auto runtimeRegistry = RuntimeRegistry{};
104
128
  #endif
105
129
 
106
- auto offlineAudioContext = std::make_shared<OfflineAudioContext>(numberOfChannels, length, sampleRate, audioEventHandlerRegistry, runtimeRegistry);
107
- auto audioContextHostObject = std::make_shared<OfflineAudioContextHostObject>(
108
- offlineAudioContext, &runtime, jsCallInvoker);
130
+ auto offlineAudioContext = std::make_shared<OfflineAudioContext>(
131
+ numberOfChannels,
132
+ length,
133
+ sampleRate,
134
+ audioEventHandlerRegistry,
135
+ runtimeRegistry);
136
+ auto audioContextHostObject =
137
+ std::make_shared<OfflineAudioContextHostObject>(
138
+ offlineAudioContext, &runtime, jsCallInvoker);
109
139
 
110
140
  return jsi::Object::createFromHostObject(
111
141
  runtime, audioContextHostObject);
@@ -113,8 +143,9 @@ class AudioAPIModuleInstaller {
113
143
  }
114
144
 
115
145
  static jsi::Function getCreateAudioRecorderFunction(
116
- jsi::Runtime *jsiRuntime,
117
- const std::shared_ptr<AudioEventHandlerRegistry> &audioEventHandlerRegistry) {
146
+ jsi::Runtime *jsiRuntime,
147
+ const std::shared_ptr<AudioEventHandlerRegistry>
148
+ &audioEventHandlerRegistry) {
118
149
  return jsi::Function::createFromHostFunction(
119
150
  *jsiRuntime,
120
151
  jsi::PropNameID::forAscii(*jsiRuntime, "createAudioRecorder"),
@@ -126,12 +157,37 @@ class AudioAPIModuleInstaller {
126
157
  size_t count) -> jsi::Value {
127
158
  auto options = args[0].getObject(runtime);
128
159
 
129
- auto sampleRate = static_cast<float>(options.getProperty(runtime, "sampleRate").getNumber());
130
- auto bufferLength = static_cast<int>(options.getProperty(runtime, "bufferLengthInSamples").getNumber());
160
+ auto sampleRate = static_cast<float>(
161
+ options.getProperty(runtime, "sampleRate").getNumber());
162
+ auto bufferLength = static_cast<int>(
163
+ options.getProperty(runtime, "bufferLengthInSamples")
164
+ .getNumber());
131
165
 
132
- auto audioRecorderHostObject = std::make_shared<AudioRecorderHostObject>(audioEventHandlerRegistry, sampleRate, bufferLength);
166
+ auto audioRecorderHostObject =
167
+ std::make_shared<AudioRecorderHostObject>(
168
+ audioEventHandlerRegistry, sampleRate, bufferLength);
133
169
 
134
- return jsi::Object::createFromHostObject(runtime, audioRecorderHostObject);
170
+ return jsi::Object::createFromHostObject(
171
+ runtime, audioRecorderHostObject);
172
+ });
173
+ }
174
+
175
+ static jsi::Function getCreateAudioDecoderFunction(
176
+ jsi::Runtime *jsiRuntime,
177
+ const std::shared_ptr<react::CallInvoker> &jsCallInvoker) {
178
+ return jsi::Function::createFromHostFunction(
179
+ *jsiRuntime,
180
+ jsi::PropNameID::forAscii(*jsiRuntime, "createAudioDecoder"),
181
+ 0,
182
+ [jsCallInvoker](
183
+ jsi::Runtime &runtime,
184
+ const jsi::Value &thisValue,
185
+ const jsi::Value *args,
186
+ size_t count) -> jsi::Value {
187
+ auto audioDecoderHostObject =
188
+ std::make_shared<AudioDecoderHostObject>(&runtime, jsCallInvoker);
189
+ return jsi::Object::createFromHostObject(
190
+ runtime, audioDecoderHostObject);
135
191
  });
136
192
  }
137
193
  };
@@ -49,11 +49,7 @@ BaseAudioContextHostObject::BaseAudioContextHostObject(
49
49
  JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, createBufferQueueSource),
50
50
  JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, createBuffer),
51
51
  JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, createPeriodicWave),
52
- JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, createAnalyser),
53
- JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, decodeAudioData),
54
- JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, decodeAudioDataSource),
55
- JSI_EXPORT_FUNCTION(
56
- BaseAudioContextHostObject, decodePCMAudioDataInBase64));
52
+ JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, createAnalyser));
57
53
  }
58
54
 
59
55
  JSI_PROPERTY_GETTER_IMPL(BaseAudioContextHostObject, destination) {
@@ -266,100 +262,4 @@ JSI_HOST_FUNCTION_IMPL(BaseAudioContextHostObject, createAnalyser) {
266
262
  auto analyserHostObject = std::make_shared<AnalyserNodeHostObject>(analyser);
267
263
  return jsi::Object::createFromHostObject(runtime, analyserHostObject);
268
264
  }
269
-
270
- JSI_HOST_FUNCTION_IMPL(BaseAudioContextHostObject, decodeAudioDataSource) {
271
- auto sourcePath = args[0].getString(runtime).utf8(runtime);
272
-
273
- auto promise = promiseVendor_->createPromise(
274
- [this, sourcePath](std::shared_ptr<Promise> promise) {
275
- std::thread([this, sourcePath, promise = std::move(promise)]() {
276
- auto results = context_->decodeAudioDataSource(sourcePath);
277
-
278
- if (!results) {
279
- promise->reject("Failed to decode audio data source.");
280
- return;
281
- }
282
-
283
- auto audioBufferHostObject =
284
- std::make_shared<AudioBufferHostObject>(results);
285
-
286
- promise->resolve([audioBufferHostObject = std::move(
287
- audioBufferHostObject)](jsi::Runtime &runtime) {
288
- auto jsiObject = jsi::Object::createFromHostObject(
289
- runtime, audioBufferHostObject);
290
- jsiObject.setExternalMemoryPressure(
291
- runtime, audioBufferHostObject->getSizeInBytes());
292
- return jsiObject;
293
- });
294
- }).detach();
295
- });
296
-
297
- return promise;
298
- }
299
-
300
- JSI_HOST_FUNCTION_IMPL(BaseAudioContextHostObject, decodeAudioData) {
301
- auto arrayBuffer = args[0]
302
- .getObject(runtime)
303
- .getPropertyAsObject(runtime, "buffer")
304
- .getArrayBuffer(runtime);
305
- auto data = arrayBuffer.data(runtime);
306
- auto size = static_cast<int>(arrayBuffer.size(runtime));
307
-
308
- auto promise = promiseVendor_->createPromise(
309
- [this, data, size](std::shared_ptr<Promise> promise) {
310
- std::thread([this, data, size, promise = std::move(promise)]() {
311
- auto results = context_->decodeAudioData(data, size);
312
-
313
- if (!results) {
314
- promise->reject("Failed to decode audio data source.");
315
- return;
316
- }
317
-
318
- auto audioBufferHostObject =
319
- std::make_shared<AudioBufferHostObject>(results);
320
-
321
- promise->resolve([audioBufferHostObject = std::move(
322
- audioBufferHostObject)](jsi::Runtime &runtime) {
323
- auto jsiObject = jsi::Object::createFromHostObject(
324
- runtime, audioBufferHostObject);
325
- jsiObject.setExternalMemoryPressure(
326
- runtime, audioBufferHostObject->getSizeInBytes());
327
- return jsiObject;
328
- });
329
- }).detach();
330
- });
331
-
332
- return promise;
333
- }
334
-
335
- JSI_HOST_FUNCTION_IMPL(BaseAudioContextHostObject, decodePCMAudioDataInBase64) {
336
- auto b64 = args[0].getString(runtime).utf8(runtime);
337
- auto playbackSpeed = static_cast<float>(args[1].getNumber());
338
-
339
- auto promise = promiseVendor_->createPromise(
340
- [this, b64, playbackSpeed](std::shared_ptr<Promise> promise) {
341
- std::thread([this, b64, playbackSpeed, promise = std::move(promise)]() {
342
- auto results = context_->decodeWithPCMInBase64(b64, playbackSpeed);
343
-
344
- if (!results) {
345
- promise->reject("Failed to decode audio data source.");
346
- return;
347
- }
348
-
349
- auto audioBufferHostObject =
350
- std::make_shared<AudioBufferHostObject>(results);
351
-
352
- promise->resolve([audioBufferHostObject = std::move(
353
- audioBufferHostObject)](jsi::Runtime &runtime) {
354
- auto jsiObject = jsi::Object::createFromHostObject(
355
- runtime, audioBufferHostObject);
356
- jsiObject.setExternalMemoryPressure(
357
- runtime, audioBufferHostObject->getSizeInBytes());
358
- return jsiObject;
359
- });
360
- }).detach();
361
- });
362
-
363
- return promise;
364
- }
365
265
  } // namespace audioapi
@@ -41,9 +41,6 @@ class BaseAudioContextHostObject : public JsiHostObject {
41
41
  JSI_HOST_FUNCTION_DECL(createBuffer);
42
42
  JSI_HOST_FUNCTION_DECL(createPeriodicWave);
43
43
  JSI_HOST_FUNCTION_DECL(createAnalyser);
44
- JSI_HOST_FUNCTION_DECL(decodeAudioDataSource);
45
- JSI_HOST_FUNCTION_DECL(decodeAudioData);
46
- JSI_HOST_FUNCTION_DECL(decodePCMAudioDataInBase64);
47
44
 
48
45
  std::shared_ptr<BaseAudioContext> context_;
49
46