Skip to content

Commit

Permalink
refactor: trying to share decoding logic from miniaudio between andro…
Browse files Browse the repository at this point in the history
…id and ios
  • Loading branch information
Maciej Makowski committed Jan 7, 2025
1 parent 4cacd9a commit 7fdfcac
Show file tree
Hide file tree
Showing 12 changed files with 15 additions and 206 deletions.
2 changes: 1 addition & 1 deletion apps/fabric-example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2152,7 +2152,7 @@ SPEC CHECKSUMS:
React-utils: d9624101245ebaab39c9f1bd786132da0b4f27ff
ReactCodegen: dbfef1fef26f42c900bb1884fa149d49d501d64d
ReactCommon: 429ca28cd813c31359c73ffac6dc24f93347d522
RNAudioAPI: e7c191e81df9b2a725b8409767130b0af869f9bc
RNAudioAPI: a68cc14b74ca9be8d6bf17ba52090e3017091be4
RNGestureHandler: 0e5ae8d72ef4afb855e98dcdbe60f27d938abe13
RNReanimated: 006a5d3961bf09c1e96d62ed436e02b2e43b89bb
RNScreens: e389d6a6a66a4f0d3662924ecae803073ccce8ec
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native-audio-api/RNAudioAPI.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ Pod::Spec.new do |s|

s.source_files = "ios/**/*.{h,m,mm}", "common/cpp/**/*.{hpp,cpp,c,h}"

s.ios.frameworks = 'Accelerate'
s.ios.frameworks = ['Accelerate', 'AudioUnit', 'AudioToolbox']
s.xcconfig = {
'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) HAVE_ACCELERATE=1'
'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) HAVE_ACCELERATE=1 MA_NO_RUNTIME_LINKING=1'
}

# Use install_modules_dependencies helper to install the dependencies if React Native version >=0.71.0.
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-audio-api/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ file(GLOB_RECURSE SOURCE_FILES
"../common/cpp/jsi/*.h"
"../common/cpp/jsi/*.cpp"
"../common/cpp/types/*.h"
"../common/cpp/libs/*.h"
)

add_library(react-native-audio-api SHARED ${SOURCE_FILES})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include "AudioDecoder.h"

#define MINIAUDIO_IMPLEMENTATION
#include <android/log.h>
#include <miniaudio.h>

#include "AudioArray.h"
#include "AudioBus.h"

#define MINIAUDIO_IMPLEMENTATION
#include <miniaudio.h>

namespace audioapi {

AudioDecoder::AudioDecoder(int sampleRate) : sampleRate_(sampleRate) {}
Expand All @@ -17,11 +16,6 @@ AudioBus *AudioDecoder::decodeWithFilePath(const std::string &path) const {
ma_decoder_config_init(ma_format_f32, 2, sampleRate_);
ma_result result = ma_decoder_init_file(path.c_str(), &config, &decoder);
if (result != MA_SUCCESS) {
__android_log_print(
ANDROID_LOG_ERROR,
"AudioDecoder",
"Failed to initialize decoder for file: %s",
path.c_str());
return new AudioBus(1, 1, 1);
}

Expand All @@ -35,11 +29,7 @@ AudioBus *AudioDecoder::decodeWithFilePath(const std::string &path) const {
ma_uint64 framesDecoded;
ma_decoder_read_pcm_frames(&decoder, buffer, totalFrameCount, &framesDecoded);
if (framesDecoded == 0) {
__android_log_print(
ANDROID_LOG_ERROR,
"AudioDecoder",
"Failed to decode audio file: %s",
path.c_str());
return new AudioBus(1, 1, 1);
}

for (int i = 0; i < decoder.outputChannels; ++i) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#include "BaseAudioContext.h"

#ifdef ANDROID
#include "AudioDecoder.h"
#include "AudioPlayer.h"
#else
#include "IOSAudioDecoder.h"
#include "IOSAudioPlayer.h"
#endif

#include "BaseAudioContext.h"

#include "AudioArray.h"
#include "AudioBuffer.h"
#include "AudioBufferSourceNode.h"
#include "AudioBus.h"
#include "AudioDecoder.h"
#include "AudioDestinationNode.h"
#include "AudioNodeManager.h"
#include "BiquadFilterNode.h"
Expand All @@ -25,13 +24,12 @@ namespace audioapi {
BaseAudioContext::BaseAudioContext() {
#ifdef ANDROID
audioPlayer_ = std::make_shared<AudioPlayer>(this->renderAudio());
audioDecoder_ = std::make_shared<AudioDecoder>(audioPlayer_->getSampleRate());
#else
audioPlayer_ = std::make_shared<IOSAudioPlayer>(this->renderAudio());
audioDecoder_ =
std::make_shared<IOSAudioDecoder>(audioPlayer_->getSampleRate());
#endif

audioDecoder_ = std::make_shared<AudioDecoder>(audioPlayer_->getSampleRate());

sampleRate_ = audioPlayer_->getSampleRate();
bufferSizeInFrames_ = audioPlayer_->getBufferSizeInFrames();

Expand Down Expand Up @@ -108,19 +106,11 @@ std::shared_ptr<PeriodicWave> BaseAudioContext::createPeriodicWave(
sampleRate_, real, imag, length, disableNormalization);
}

#ifdef ANDROID
std::shared_ptr<AudioBuffer> BaseAudioContext::decodeAudioDataSource(
const std::string &path) {
auto audioBus = audioDecoder_->decodeWithFilePath(path);
return std::make_shared<AudioBuffer>(audioBus);
}
#else
std::shared_ptr<AudioBuffer> BaseAudioContext::decodeAudioDataSource(
const std::string &path) {
auto audioBus = audioDecoder_->decodeWithFilePath(path);
return std::make_shared<AudioBuffer>(audioBus);
}
#endif

std::function<void(AudioBus *, int)> BaseAudioContext::renderAudio() {
if (!isRunning()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ class AudioNodeManager;
class BiquadFilterNode;
class AudioDestinationNode;
class AudioBufferSourceNode;
class AudioDecoder;

#ifdef ANDROID
class AudioPlayer;
class AudioDecoder;
#else
class IOSAudioPlayer;
class IOSAudioDecoder;
#endif

class BaseAudioContext {
Expand Down Expand Up @@ -65,13 +64,12 @@ class BaseAudioContext {
protected:
static std::string toString(ContextState state);
std::shared_ptr<AudioDestinationNode> destination_;
std::shared_ptr<AudioDecoder> audioDecoder_;

#ifdef ANDROID
std::shared_ptr<AudioPlayer> audioPlayer_;
std::shared_ptr<AudioDecoder> audioDecoder_;
#else
std::shared_ptr<IOSAudioPlayer> audioPlayer_;
std::shared_ptr<IOSAudioDecoder> audioDecoder_;
#endif

int sampleRate_;
Expand Down
17 changes: 0 additions & 17 deletions packages/react-native-audio-api/ios/AudioDecoder/AudioDecoder.h

This file was deleted.

79 changes: 0 additions & 79 deletions packages/react-native-audio-api/ios/AudioDecoder/AudioDecoder.m

This file was deleted.

28 changes: 0 additions & 28 deletions packages/react-native-audio-api/ios/AudioDecoder/IOSAudioDecoder.h

This file was deleted.

This file was deleted.

0 comments on commit 7fdfcac

Please sign in to comment.