Skip to content

Commit b9a555b

Browse files
committed
Merge branch 'main' into feat/iir-filter-node
2 parents 467e599 + 4e6f25c commit b9a555b

File tree

180 files changed

+2151
-2749
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

180 files changed

+2151
-2749
lines changed

packages/react-native-audio-api/.clang-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
ColumnLimit: 80
2+
ColumnLimit: 100
33
AccessModifierOffset: -1
44
AlignAfterOpenBracket: AlwaysBreak
55
AlignConsecutiveAssignments: false

packages/react-native-audio-api/android/src/main/cpp/audioapi/android/AudioAPIModule.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#include <audioapi/android/AudioAPIModule.h>
2+
#include <memory>
3+
#include <string>
4+
#include <unordered_map>
25

36
namespace audioapi {
47

@@ -25,20 +28,16 @@ jni::local_ref<AudioAPIModule::jhybriddata> AudioAPIModule::initHybrid(
2528
jni::alias_ref<jhybridobject> jThis,
2629
jni::alias_ref<jni::JObject> jWorkletsModule,
2730
jlong jsContext,
28-
jni::alias_ref<facebook::react::CallInvokerHolder::javaobject>
29-
jsCallInvokerHolder) {
31+
jni::alias_ref<facebook::react::CallInvokerHolder::javaobject> jsCallInvokerHolder) {
3032
auto jsCallInvoker = jsCallInvokerHolder->cthis()->getCallInvoker();
3133
auto rnRuntime = reinterpret_cast<jsi::Runtime *>(jsContext);
3234
#if RN_AUDIO_API_ENABLE_WORKLETS
3335
if (jWorkletsModule) {
34-
auto castedModule =
35-
jni::static_ref_cast<WorkletsModule::javaobject>(jWorkletsModule);
36+
auto castedModule = jni::static_ref_cast<WorkletsModule::javaobject>(jWorkletsModule);
3637
auto workletsModuleProxy = castedModule->cthis()->getWorkletsModuleProxy();
37-
return makeCxxInstance(
38-
jThis, workletsModuleProxy, rnRuntime, jsCallInvoker);
38+
return makeCxxInstance(jThis, workletsModuleProxy, rnRuntime, jsCallInvoker);
3939
}
40-
throw std::runtime_error(
41-
"Worklets module is required but not provided from Java/Kotlin side");
40+
throw std::runtime_error("Worklets module is required but not provided from Java/Kotlin side");
4241
#else
4342
return makeCxxInstance(jThis, rnRuntime, jsCallInvoker);
4443
#endif
@@ -57,16 +56,12 @@ void AudioAPIModule::registerNatives() {
5756

5857
void AudioAPIModule::injectJSIBindings() {
5958
#if RN_AUDIO_API_ENABLE_WORKLETS
60-
auto uiWorkletRuntime =
61-
weakWorkletsModuleProxy_.lock()->getUIWorkletRuntime();
59+
auto uiWorkletRuntime = weakWorkletsModuleProxy_.lock()->getUIWorkletRuntime();
6260
#else
6361
auto uiWorkletRuntime = nullptr;
6462
#endif
6563
AudioAPIModuleInstaller::injectJSIBindings(
66-
jsiRuntime_,
67-
jsCallInvoker_,
68-
audioEventHandlerRegistry_,
69-
uiWorkletRuntime);
64+
jsiRuntime_, jsCallInvoker_, audioEventHandlerRegistry_, uiWorkletRuntime);
7065
}
7166

7267
void AudioAPIModule::invokeHandlerWithEventNameAndEventBody(
@@ -98,8 +93,7 @@ void AudioAPIModule::invokeHandlerWithEventNameAndEventBody(
9893
}
9994

10095
if (audioEventHandlerRegistry_ != nullptr) {
101-
audioEventHandlerRegistry_->invokeHandlerWithEventBody(
102-
eventName->toStdString(), body);
96+
audioEventHandlerRegistry_->invokeHandlerWithEventBody(eventName->toStdString(), body);
10397
}
10498
}
10599

packages/react-native-audio-api/android/src/main/cpp/audioapi/android/OnLoad.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@
55
using namespace audioapi;
66

77
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) {
8-
return facebook::jni::initialize(
9-
vm, [] { AudioAPIModule::registerNatives(); });
8+
return facebook::jni::initialize(vm, [] { AudioAPIModule::registerNatives(); });
109
}

packages/react-native-audio-api/android/src/main/cpp/audioapi/android/core/AndroidAudioRecorder.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <audioapi/utils/CircularAudioArray.h>
88
#include <audioapi/utils/CircularOverflowableAudioArray.h>
99

10+
#include <memory>
11+
1012
namespace audioapi {
1113

1214
AndroidAudioRecorder::AndroidAudioRecorder(
@@ -45,8 +47,7 @@ void AndroidAudioRecorder::start() {
4547
}
4648

4749
if (mStream_) {
48-
jni::ThreadScope::WithClassLoader(
49-
[this]() { nativeAudioRecorder_->start(); });
50+
jni::ThreadScope::WithClassLoader([this]() { nativeAudioRecorder_->start(); });
5051
mStream_->requestStart();
5152
}
5253

@@ -61,8 +62,7 @@ void AndroidAudioRecorder::stop() {
6162
isRunning_.store(false);
6263

6364
if (mStream_) {
64-
jni::ThreadScope::WithClassLoader(
65-
[this]() { nativeAudioRecorder_->stop(); });
65+
jni::ThreadScope::WithClassLoader([this]() { nativeAudioRecorder_->stop(); });
6666
mStream_->requestStop();
6767
}
6868

packages/react-native-audio-api/android/src/main/cpp/audioapi/android/core/AudioPlayer.cpp

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
#include <audioapi/utils/AudioBus.h>
77
#include <jni.h>
88

9+
#include <algorithm>
10+
#include <memory>
11+
912
namespace audioapi {
1013

1114
AudioPlayer::AudioPlayer(
1215
const std::function<void(std::shared_ptr<AudioBus>, int)> &renderAudio,
1316
float sampleRate,
1417
int channelCount)
15-
: renderAudio_(renderAudio),
16-
sampleRate_(sampleRate),
17-
channelCount_(channelCount) {
18+
: renderAudio_(renderAudio), sampleRate_(sampleRate), channelCount_(channelCount) {
1819
isInitialized_ = openAudioStream();
1920

2021
nativeAudioPlayer_ = jni::make_global(NativeAudioPlayer::create());
@@ -36,22 +37,17 @@ bool AudioPlayer::openAudioStream() {
3637
auto result = builder.openStream(mStream_);
3738
if (result != oboe::Result::OK || mStream_ == nullptr) {
3839
__android_log_print(
39-
ANDROID_LOG_ERROR,
40-
"AudioPlayer",
41-
"Failed to open stream: %s",
42-
oboe::convertToText(result));
40+
ANDROID_LOG_ERROR, "AudioPlayer", "Failed to open stream: %s", oboe::convertToText(result));
4341
return false;
4442
}
4543

46-
mBus_ = std::make_shared<AudioBus>(
47-
RENDER_QUANTUM_SIZE, channelCount_, sampleRate_);
44+
mBus_ = std::make_shared<AudioBus>(RENDER_QUANTUM_SIZE, channelCount_, sampleRate_);
4845
return true;
4946
}
5047

5148
bool AudioPlayer::start() {
5249
if (mStream_) {
53-
jni::ThreadScope::WithClassLoader(
54-
[this]() { nativeAudioPlayer_->start(); });
50+
jni::ThreadScope::WithClassLoader([this]() { nativeAudioPlayer_->start(); });
5551
auto result = mStream_->requestStart();
5652
return result == oboe::Result::OK;
5753
}
@@ -94,10 +90,8 @@ bool AudioPlayer::isRunning() const {
9490
return mStream_ && mStream_->getState() == oboe::StreamState::Started;
9591
}
9692

97-
DataCallbackResult AudioPlayer::onAudioReady(
98-
AudioStream *oboeStream,
99-
void *audioData,
100-
int32_t numFrames) {
93+
DataCallbackResult
94+
AudioPlayer::onAudioReady(AudioStream *oboeStream, void *audioData, int32_t numFrames) {
10195
if (!isInitialized_) {
10296
return DataCallbackResult::Continue;
10397
}
@@ -108,8 +102,7 @@ DataCallbackResult AudioPlayer::onAudioReady(
108102
assert(buffer != nullptr);
109103

110104
while (processedFrames < numFrames) {
111-
int framesToProcess =
112-
std::min(numFrames - processedFrames, RENDER_QUANTUM_SIZE);
105+
int framesToProcess = std::min(numFrames - processedFrames, RENDER_QUANTUM_SIZE);
113106
renderAudio_(mBus_, framesToProcess);
114107

115108
// TODO: optimize this with SIMD?
@@ -126,9 +119,7 @@ DataCallbackResult AudioPlayer::onAudioReady(
126119
return DataCallbackResult::Continue;
127120
}
128121

129-
void AudioPlayer::onErrorAfterClose(
130-
oboe::AudioStream *stream,
131-
oboe::Result error) {
122+
void AudioPlayer::onErrorAfterClose(oboe::AudioStream *stream, oboe::Result error) {
132123
if (error == oboe::Result::ErrorDisconnected) {
133124
cleanup();
134125
if (openAudioStream()) {

packages/react-native-audio-api/android/src/main/cpp/audioapi/android/core/utils/AudioDecoder.cpp

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,29 @@
1515
#include <audioapi/libs/ffmpeg/FFmpegDecoding.h>
1616
#endif
1717

18+
#include <memory>
19+
#include <string>
20+
#include <vector>
21+
1822
namespace audioapi {
1923

2024
// Decoding audio in fixed-size chunks because total frame count can't be
2125
// determined in advance. Note: ma_decoder_get_length_in_pcm_frames() always
2226
// returns 0 for Vorbis decoders.
23-
std::vector<float> AudioDecoder::readAllPcmFrames(
24-
ma_decoder &decoder,
25-
int outputChannels) {
27+
std::vector<float> AudioDecoder::readAllPcmFrames(ma_decoder &decoder, int outputChannels) {
2628
std::vector<float> buffer;
2729
std::vector<float> temp(CHUNK_SIZE * outputChannels);
2830
ma_uint64 outFramesRead = 0;
2931

3032
#ifndef AUDIO_API_TEST_SUITE
3133
while (true) {
3234
ma_uint64 tempFramesDecoded = 0;
33-
ma_decoder_read_pcm_frames(
34-
&decoder, temp.data(), CHUNK_SIZE, &tempFramesDecoded);
35+
ma_decoder_read_pcm_frames(&decoder, temp.data(), CHUNK_SIZE, &tempFramesDecoded);
3536
if (tempFramesDecoded == 0) {
3637
break;
3738
}
3839

39-
buffer.insert(
40-
buffer.end(),
41-
temp.data(),
42-
temp.data() + tempFramesDecoded * outputChannels);
40+
buffer.insert(buffer.end(), temp.data(), temp.data() + tempFramesDecoded * outputChannels);
4341
outFramesRead += tempFramesDecoded;
4442
}
4543

@@ -59,8 +57,7 @@ std::shared_ptr<AudioBuffer> AudioDecoder::makeAudioBufferFromFloatBuffer(
5957
}
6058

6159
auto outputFrames = buffer.size() / outputChannels;
62-
auto audioBus = std::make_shared<AudioBus>(
63-
outputFrames, outputChannels, outputSampleRate);
60+
auto audioBus = std::make_shared<AudioBus>(outputFrames, outputChannels, outputSampleRate);
6461

6562
for (int ch = 0; ch < outputChannels; ++ch) {
6663
auto channelData = audioBus->getChannel(ch)->getData();
@@ -76,27 +73,21 @@ std::shared_ptr<AudioBuffer> AudioDecoder::decodeWithFilePath(
7673
float sampleRate) {
7774
#ifndef AUDIO_API_TEST_SUITE
7875
if (AudioDecoder::pathHasExtension(path, {".mp4", ".m4a", ".aac"})) {
79-
auto buffer =
80-
ffmpegdecoder::decodeWithFilePath(path, static_cast<int>(sampleRate));
76+
auto buffer = ffmpegdecoder::decodeWithFilePath(path, static_cast<int>(sampleRate));
8177
if (buffer == nullptr) {
8278
__android_log_print(
83-
ANDROID_LOG_ERROR,
84-
"AudioDecoder",
85-
"Failed to decode with FFmpeg: %s",
86-
path.c_str());
79+
ANDROID_LOG_ERROR, "AudioDecoder", "Failed to decode with FFmpeg: %s", path.c_str());
8780
return nullptr;
8881
}
8982
return buffer;
9083
}
9184
ma_decoder decoder;
92-
ma_decoder_config config =
93-
ma_decoder_config_init(ma_format_f32, 0, static_cast<int>(sampleRate));
85+
ma_decoder_config config = ma_decoder_config_init(ma_format_f32, 0, static_cast<int>(sampleRate));
9486
ma_decoding_backend_vtable *customBackends[] = {
9587
ma_decoding_backend_libvorbis, ma_decoding_backend_libopus};
9688

9789
config.ppCustomBackendVTables = customBackends;
98-
config.customBackendCount =
99-
sizeof(customBackends) / sizeof(customBackends[0]);
90+
config.customBackendCount = sizeof(customBackends) / sizeof(customBackends[0]);
10091

10192
if (ma_decoder_init_file(path.c_str(), &config, &decoder) != MA_SUCCESS) {
10293
__android_log_print(
@@ -113,46 +104,36 @@ std::shared_ptr<AudioBuffer> AudioDecoder::decodeWithFilePath(
113104

114105
std::vector<float> buffer = readAllPcmFrames(decoder, outputChannels);
115106
ma_decoder_uninit(&decoder);
116-
return makeAudioBufferFromFloatBuffer(
117-
buffer, outputSampleRate, outputChannels);
107+
return makeAudioBufferFromFloatBuffer(buffer, outputSampleRate, outputChannels);
118108
#else
119109
return nullptr;
120110
#endif
121111
}
122112

123-
std::shared_ptr<AudioBuffer> AudioDecoder::decodeWithMemoryBlock(
124-
const void *data,
125-
size_t size,
126-
float sampleRate) {
113+
std::shared_ptr<AudioBuffer>
114+
AudioDecoder::decodeWithMemoryBlock(const void *data, size_t size, float sampleRate) {
127115
#ifndef AUDIO_API_TEST_SUITE
128116
const AudioFormat format = AudioDecoder::detectAudioFormat(data, size);
129-
if (format == AudioFormat::MP4 || format == AudioFormat::M4A ||
130-
format == AudioFormat::AAC) {
131-
auto buffer = ffmpegdecoder::decodeWithMemoryBlock(
132-
data, size, static_cast<int>(sampleRate));
117+
if (format == AudioFormat::MP4 || format == AudioFormat::M4A || format == AudioFormat::AAC) {
118+
auto buffer = ffmpegdecoder::decodeWithMemoryBlock(data, size, static_cast<int>(sampleRate));
133119
if (buffer == nullptr) {
134-
__android_log_print(
135-
ANDROID_LOG_ERROR, "AudioDecoder", "Failed to decode with FFmpeg");
120+
__android_log_print(ANDROID_LOG_ERROR, "AudioDecoder", "Failed to decode with FFmpeg");
136121
return nullptr;
137122
}
138123
return buffer;
139124
}
140125
ma_decoder decoder;
141-
ma_decoder_config config =
142-
ma_decoder_config_init(ma_format_f32, 0, static_cast<int>(sampleRate));
126+
ma_decoder_config config = ma_decoder_config_init(ma_format_f32, 0, static_cast<int>(sampleRate));
143127

144128
ma_decoding_backend_vtable *customBackends[] = {
145129
ma_decoding_backend_libvorbis, ma_decoding_backend_libopus};
146130

147131
config.ppCustomBackendVTables = customBackends;
148-
config.customBackendCount =
149-
sizeof(customBackends) / sizeof(customBackends[0]);
132+
config.customBackendCount = sizeof(customBackends) / sizeof(customBackends[0]);
150133

151134
if (ma_decoder_init_memory(data, size, &config, &decoder) != MA_SUCCESS) {
152135
__android_log_print(
153-
ANDROID_LOG_ERROR,
154-
"AudioDecoder",
155-
"Failed to initialize decoder for memory block");
136+
ANDROID_LOG_ERROR, "AudioDecoder", "Failed to initialize decoder for memory block");
156137
ma_decoder_uninit(&decoder);
157138
return nullptr;
158139
}
@@ -162,8 +143,7 @@ std::shared_ptr<AudioBuffer> AudioDecoder::decodeWithMemoryBlock(
162143

163144
std::vector<float> buffer = readAllPcmFrames(decoder, outputChannels);
164145
ma_decoder_uninit(&decoder);
165-
return makeAudioBufferFromFloatBuffer(
166-
buffer, outputSampleRate, outputChannels);
146+
return makeAudioBufferFromFloatBuffer(buffer, outputSampleRate, outputChannels);
167147
#else
168148
return nullptr;
169149
#endif
@@ -176,11 +156,9 @@ std::shared_ptr<AudioBuffer> AudioDecoder::decodeWithPCMInBase64(
176156
bool interleaved) {
177157
auto decodedData = base64_decode(data, false);
178158
const auto uint8Data = reinterpret_cast<uint8_t *>(decodedData.data());
179-
size_t numFramesDecoded =
180-
decodedData.size() / (inputChannelCount * sizeof(int16_t));
159+
size_t numFramesDecoded = decodedData.size() / (inputChannelCount * sizeof(int16_t));
181160

182-
auto audioBus = std::make_shared<AudioBus>(
183-
numFramesDecoded, inputChannelCount, inputSampleRate);
161+
auto audioBus = std::make_shared<AudioBus>(numFramesDecoded, inputChannelCount, inputSampleRate);
184162

185163
for (int ch = 0; ch < inputChannelCount; ++ch) {
186164
auto channelData = audioBus->getChannel(ch)->getData();

0 commit comments

Comments
 (0)