diff --git a/.github/actions/setup/action.yml b/.github/actions/setup/action.yml index f7f819d4..23c00939 100644 --- a/.github/actions/setup/action.yml +++ b/.github/actions/setup/action.yml @@ -14,7 +14,7 @@ runs: - name: Install cpplint shell: bash - run: pip install 'cpplint==1.6.1' + run: pip install 'cpplint==2.0.0' - name: Install ktlint shell: bash diff --git a/apps/fabric-example/ios/Podfile.lock b/apps/fabric-example/ios/Podfile.lock index e13da059..543e415d 100644 --- a/apps/fabric-example/ios/Podfile.lock +++ b/apps/fabric-example/ios/Podfile.lock @@ -2152,7 +2152,7 @@ SPEC CHECKSUMS: React-utils: e74516d5b9483c5530ec61e249e28b88729321d2 ReactCodegen: ff7512e124e3dc1363a4930a209d033354d2042a ReactCommon: cde69a75746e8d7131f61c27155ee9dc42117003 - RNAudioAPI: edc2f2460642e089af187b2a8e44fd750155c70f + RNAudioAPI: 6910ec8d0609b3dc4072a8d6aafa9b8bfdd28406 RNGestureHandler: e1dcb274c17ca0680a04d7ff357e35e37c384185 RNReanimated: 4335a2d4b358c01a28fe1881db59d897e8724681 RNScreens: 74536418fef8086457d39df36a55b36efd5329c9 diff --git a/packages/react-native-audio-api/.clang-format b/packages/react-native-audio-api/.clang-format index 1b3aa8e5..199e37c4 100644 --- a/packages/react-native-audio-api/.clang-format +++ b/packages/react-native-audio-api/.clang-format @@ -81,7 +81,7 @@ SpacesInContainerLiterals: true SpacesInCStyleCastParentheses: false SpacesInParentheses: false SpacesInSquareBrackets: false -Standard: Cpp11 +Standard: Cpp17 TabWidth: 4 UseTab: Never --- diff --git a/packages/react-native-audio-api/RNAudioAPI.podspec b/packages/react-native-audio-api/RNAudioAPI.podspec index 5f4a409a..e2a66859 100644 --- a/packages/react-native-audio-api/RNAudioAPI.podspec +++ b/packages/react-native-audio-api/RNAudioAPI.podspec @@ -16,7 +16,7 @@ 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 = 'CoreFoundation', 'CoreAudio', 'AudioToolbox', 'Accelerate' s.xcconfig = { 'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) HAVE_ACCELERATE=1' } diff --git a/packages/react-native-audio-api/android/CMakeLists.txt b/packages/react-native-audio-api/android/CMakeLists.txt index f15a8abd..1fb59e23 100644 --- a/packages/react-native-audio-api/android/CMakeLists.txt +++ b/packages/react-native-audio-api/android/CMakeLists.txt @@ -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}) diff --git a/packages/react-native-audio-api/android/src/main/cpp/AudioAPIInstaller/AudioAPIInstaller.cpp b/packages/react-native-audio-api/android/src/main/cpp/core/AudioAPIInstaller.cpp similarity index 100% rename from packages/react-native-audio-api/android/src/main/cpp/AudioAPIInstaller/AudioAPIInstaller.cpp rename to packages/react-native-audio-api/android/src/main/cpp/core/AudioAPIInstaller.cpp diff --git a/packages/react-native-audio-api/android/src/main/cpp/AudioAPIInstaller/AudioAPIInstaller.h b/packages/react-native-audio-api/android/src/main/cpp/core/AudioAPIInstaller.h similarity index 100% rename from packages/react-native-audio-api/android/src/main/cpp/AudioAPIInstaller/AudioAPIInstaller.h rename to packages/react-native-audio-api/android/src/main/cpp/core/AudioAPIInstaller.h diff --git a/packages/react-native-audio-api/android/src/main/cpp/AudioDecoder/AudioDecoder.cpp b/packages/react-native-audio-api/android/src/main/cpp/core/AudioDecoder.cpp similarity index 88% rename from packages/react-native-audio-api/android/src/main/cpp/AudioDecoder/AudioDecoder.cpp rename to packages/react-native-audio-api/android/src/main/cpp/core/AudioDecoder.cpp index 298d0c3a..c21a7c13 100644 --- a/packages/react-native-audio-api/android/src/main/cpp/AudioDecoder/AudioDecoder.cpp +++ b/packages/react-native-audio-api/android/src/main/cpp/core/AudioDecoder.cpp @@ -9,8 +9,6 @@ namespace audioapi { -AudioDecoder::AudioDecoder(int sampleRate) : sampleRate_(sampleRate) {} - AudioBus *AudioDecoder::decodeWithFilePath(const std::string &path) const { ma_decoder decoder; ma_decoder_config config = @@ -55,10 +53,4 @@ AudioBus *AudioDecoder::decodeWithFilePath(const std::string &path) const { return audioBus; } - -AudioBus *AudioDecoder::decodeWithArrayBuffer() const { - // TODO: implement this - return new AudioBus(1, 1, 1); -} - } // namespace audioapi diff --git a/packages/react-native-audio-api/android/src/main/cpp/AudioPlayer/AudioPlayer.cpp b/packages/react-native-audio-api/android/src/main/cpp/core/AudioPlayer.cpp similarity index 100% rename from packages/react-native-audio-api/android/src/main/cpp/AudioPlayer/AudioPlayer.cpp rename to packages/react-native-audio-api/android/src/main/cpp/core/AudioPlayer.cpp diff --git a/packages/react-native-audio-api/android/src/main/cpp/AudioPlayer/AudioPlayer.h b/packages/react-native-audio-api/android/src/main/cpp/core/AudioPlayer.h similarity index 100% rename from packages/react-native-audio-api/android/src/main/cpp/AudioPlayer/AudioPlayer.h rename to packages/react-native-audio-api/android/src/main/cpp/core/AudioPlayer.h diff --git a/packages/react-native-audio-api/common/cpp/AudioAPIInstaller/AudioAPIInstallerHostObject.h b/packages/react-native-audio-api/common/cpp/HostObjects/AudioAPIInstallerHostObject.h similarity index 100% rename from packages/react-native-audio-api/common/cpp/AudioAPIInstaller/AudioAPIInstallerHostObject.h rename to packages/react-native-audio-api/common/cpp/HostObjects/AudioAPIInstallerHostObject.h diff --git a/packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.cpp b/packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.cpp index 3983e5c4..20e2b81e 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.cpp +++ b/packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.cpp @@ -19,10 +19,9 @@ AudioBufferSourceNode::AudioBufferSourceNode(BaseAudioContext *context) numberOfInputs_ = 0; buffer_ = std::shared_ptr(nullptr); - detuneParam_ = - std::make_shared(context, 0.0, -MAX_DETUNE, MAX_DETUNE); + detuneParam_ = std::make_shared(0.0, -MAX_DETUNE, MAX_DETUNE); playbackRateParam_ = std::make_shared( - context, 1.0, MOST_NEGATIVE_SINGLE_FLOAT, MOST_POSITIVE_SINGLE_FLOAT); + 1.0, MOST_NEGATIVE_SINGLE_FLOAT, MOST_POSITIVE_SINGLE_FLOAT); isInitialized_ = true; } diff --git a/packages/react-native-audio-api/android/src/main/cpp/AudioDecoder/AudioDecoder.h b/packages/react-native-audio-api/common/cpp/core/AudioDecoder.h similarity index 65% rename from packages/react-native-audio-api/android/src/main/cpp/AudioDecoder/AudioDecoder.h rename to packages/react-native-audio-api/common/cpp/core/AudioDecoder.h index 095e4ee4..0eca59c5 100644 --- a/packages/react-native-audio-api/android/src/main/cpp/AudioDecoder/AudioDecoder.h +++ b/packages/react-native-audio-api/common/cpp/core/AudioDecoder.h @@ -8,11 +8,9 @@ class AudioBus; class AudioDecoder { public: - explicit AudioDecoder(int sampleRate); + explicit AudioDecoder(int sampleRate) : sampleRate_(sampleRate) {} [[nodiscard]] AudioBus *decodeWithFilePath(const std::string &path) const; - // TODO: implement this - [[nodiscard]] AudioBus *decodeWithArrayBuffer() const; private: int sampleRate_; diff --git a/packages/react-native-audio-api/common/cpp/core/AudioParam.cpp b/packages/react-native-audio-api/common/cpp/core/AudioParam.cpp index 78f11cc4..08884450 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioParam.cpp +++ b/packages/react-native-audio-api/common/cpp/core/AudioParam.cpp @@ -5,16 +5,11 @@ namespace audioapi { -AudioParam::AudioParam( - BaseAudioContext *context, - float defaultValue, - float minValue, - float maxValue) +AudioParam::AudioParam(float defaultValue, float minValue, float maxValue) : value_(defaultValue), defaultValue_(defaultValue), minValue_(minValue), - maxValue_(maxValue), - context_(context) { + maxValue_(maxValue) { startTime_ = 0; endTime_ = 0; startValue_ = value_; diff --git a/packages/react-native-audio-api/common/cpp/core/AudioParam.h b/packages/react-native-audio-api/common/cpp/core/AudioParam.h index cc32eeee..ed55d7f9 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioParam.h +++ b/packages/react-native-audio-api/common/cpp/core/AudioParam.h @@ -9,15 +9,9 @@ namespace audioapi { -class BaseAudioContext; - class AudioParam { public: - explicit AudioParam( - BaseAudioContext *context, - float defaultValue, - float minValue, - float maxValue); + explicit AudioParam(float defaultValue, float minValue, float maxValue); [[nodiscard]] float getValue() const; float getValueAtTime(double time); @@ -44,7 +38,6 @@ class AudioParam { float defaultValue_; float minValue_; float maxValue_; - BaseAudioContext *context_; std::deque eventsQueue_; double startTime_; diff --git a/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.cpp b/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.cpp index 6ca58b8c..2faef820 100644 --- a/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.cpp +++ b/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.cpp @@ -1,8 +1,6 @@ #ifdef ANDROID -#include "AudioDecoder.h" #include "AudioPlayer.h" #else -#include "IOSAudioDecoder.h" #include "IOSAudioPlayer.h" #endif @@ -12,6 +10,7 @@ #include "AudioBuffer.h" #include "AudioBufferSourceNode.h" #include "AudioBus.h" +#include "AudioDecoder.h" #include "AudioDestinationNode.h" #include "AudioNodeManager.h" #include "BiquadFilterNode.h" @@ -25,13 +24,12 @@ namespace audioapi { BaseAudioContext::BaseAudioContext() { #ifdef ANDROID audioPlayer_ = std::make_shared(this->renderAudio()); - audioDecoder_ = std::make_shared(audioPlayer_->getSampleRate()); #else audioPlayer_ = std::make_shared(this->renderAudio()); - audioDecoder_ = - std::make_shared(audioPlayer_->getSampleRate()); #endif + audioDecoder_ = std::make_shared(audioPlayer_->getSampleRate()); + sampleRate_ = audioPlayer_->getSampleRate(); bufferSizeInFrames_ = audioPlayer_->getBufferSizeInFrames(); @@ -108,19 +106,11 @@ std::shared_ptr BaseAudioContext::createPeriodicWave( sampleRate_, real, imag, length, disableNormalization); } -#ifdef ANDROID std::shared_ptr BaseAudioContext::decodeAudioDataSource( const std::string &path) { auto audioBus = audioDecoder_->decodeWithFilePath(path); return std::make_shared(audioBus); } -#else -std::shared_ptr BaseAudioContext::decodeAudioDataSource( - const std::string &path) { - auto audioBus = audioDecoder_->decodeWithFilePath(path); - return std::make_shared(audioBus); -} -#endif std::function BaseAudioContext::renderAudio() { if (!isRunning()) { diff --git a/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.h b/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.h index 53f07e57..de039935 100644 --- a/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.h +++ b/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.h @@ -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 { @@ -65,13 +64,12 @@ class BaseAudioContext { protected: static std::string toString(ContextState state); std::shared_ptr destination_; + std::shared_ptr audioDecoder_; #ifdef ANDROID std::shared_ptr audioPlayer_; - std::shared_ptr audioDecoder_; #else std::shared_ptr audioPlayer_; - std::shared_ptr audioDecoder_; #endif int sampleRate_; diff --git a/packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.cpp b/packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.cpp index 63a0ea50..a79e3bb7 100644 --- a/packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.cpp +++ b/packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.cpp @@ -11,13 +11,11 @@ namespace audioapi { BiquadFilterNode::BiquadFilterNode(BaseAudioContext *context) : AudioNode(context) { frequencyParam_ = std::make_shared( - context, 350.0, MIN_FILTER_FREQUENCY, MAX_FILTER_FREQUENCY); - detuneParam_ = - std::make_shared(context, 0.0, -MAX_DETUNE, MAX_DETUNE); - QParam_ = - std::make_shared(context, 1.0, -MAX_FILTER_Q, MAX_FILTER_Q); - gainParam_ = std::make_shared( - context, 0.0, MIN_FILTER_GAIN, MAX_FILTER_GAIN); + 350.0, MIN_FILTER_FREQUENCY, MAX_FILTER_FREQUENCY); + detuneParam_ = std::make_shared(0.0, -MAX_DETUNE, MAX_DETUNE); + QParam_ = std::make_shared(1.0, -MAX_FILTER_Q, MAX_FILTER_Q); + gainParam_ = + std::make_shared(0.0, MIN_FILTER_GAIN, MAX_FILTER_GAIN); type_ = BiquadFilterType::LOWPASS; isInitialized_ = true; } diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/Constants.h b/packages/react-native-audio-api/common/cpp/core/Constants.h similarity index 76% rename from packages/react-native-audio-api/common/cpp/HostObjects/Constants.h rename to packages/react-native-audio-api/common/cpp/core/Constants.h index 8b187578..61eaa8ae 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/Constants.h +++ b/packages/react-native-audio-api/common/cpp/core/Constants.h @@ -8,10 +8,8 @@ namespace audioapi { constexpr int SAMPLE_RATE = 44100; constexpr int CHANNEL_COUNT = 2; -constexpr float MOST_POSITIVE_SINGLE_FLOAT = - static_cast(std::numeric_limits::max()); -constexpr float MOST_NEGATIVE_SINGLE_FLOAT = - static_cast(std::numeric_limits::lowest()); +constexpr float MOST_POSITIVE_SINGLE_FLOAT = static_cast(std::numeric_limits::max()); +constexpr float MOST_NEGATIVE_SINGLE_FLOAT = static_cast(std::numeric_limits::lowest()); constexpr float NYQUIST_FREQUENCY = SAMPLE_RATE / 2.0; static float MAX_DETUNE = 1200 * std::log2(MOST_POSITIVE_SINGLE_FLOAT); constexpr float MAX_GAIN = MOST_POSITIVE_SINGLE_FLOAT; diff --git a/packages/react-native-audio-api/common/cpp/core/GainNode.cpp b/packages/react-native-audio-api/common/cpp/core/GainNode.cpp index 886f3c9d..baffdebd 100644 --- a/packages/react-native-audio-api/common/cpp/core/GainNode.cpp +++ b/packages/react-native-audio-api/common/cpp/core/GainNode.cpp @@ -6,7 +6,7 @@ namespace audioapi { GainNode::GainNode(BaseAudioContext *context) : AudioNode(context) { - gainParam_ = std::make_shared(context, 1.0, -MAX_GAIN, MAX_GAIN); + gainParam_ = std::make_shared(1.0, -MAX_GAIN, MAX_GAIN); isInitialized_ = true; } diff --git a/packages/react-native-audio-api/common/cpp/core/OscillatorNode.cpp b/packages/react-native-audio-api/common/cpp/core/OscillatorNode.cpp index 61f68d75..2b2bed37 100644 --- a/packages/react-native-audio-api/common/cpp/core/OscillatorNode.cpp +++ b/packages/react-native-audio-api/common/cpp/core/OscillatorNode.cpp @@ -8,9 +8,8 @@ namespace audioapi { OscillatorNode::OscillatorNode(BaseAudioContext *context) : AudioScheduledSourceNode(context) { frequencyParam_ = std::make_shared( - context, 444.0, -NYQUIST_FREQUENCY, NYQUIST_FREQUENCY); - detuneParam_ = - std::make_shared(context, 0.0, -MAX_DETUNE, MAX_DETUNE); + 444.0, -NYQUIST_FREQUENCY, NYQUIST_FREQUENCY); + detuneParam_ = std::make_shared(0.0, -MAX_DETUNE, MAX_DETUNE); type_ = OscillatorType::SINE; periodicWave_ = context_->getBasicWaveForm(type_); isInitialized_ = true; diff --git a/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.cpp b/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.cpp index 919a1a99..08dc123f 100644 --- a/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.cpp +++ b/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.cpp @@ -11,7 +11,7 @@ namespace audioapi { StereoPannerNode::StereoPannerNode(BaseAudioContext *context) : AudioNode(context) { channelCountMode_ = ChannelCountMode::CLAMPED_MAX; - panParam_ = std::make_shared(context, 0.0, -MAX_PAN, MAX_PAN); + panParam_ = std::make_shared(0.0, -MAX_PAN, MAX_PAN); isInitialized_ = true; } diff --git a/packages/react-native-audio-api/android/libs/include/miniaudio.h b/packages/react-native-audio-api/common/cpp/libs/miniaudio.h similarity index 100% rename from packages/react-native-audio-api/android/libs/include/miniaudio.h rename to packages/react-native-audio-api/common/cpp/libs/miniaudio.h diff --git a/packages/react-native-audio-api/common/cpp/utils/AudioUtils.h b/packages/react-native-audio-api/common/cpp/utils/AudioUtils.h index 4b8c0790..21c9da0b 100644 --- a/packages/react-native-audio-api/common/cpp/utils/AudioUtils.h +++ b/packages/react-native-audio-api/common/cpp/utils/AudioUtils.h @@ -7,10 +7,6 @@ size_t timeToSampleFrame(double time, int sampleRate); double sampleFrameToTime(int sampleFrame, int sampleRate); -float linearInterpolate( - const float *source, - size_t firstIndex, - size_t secondIndex, - float factor); +float linearInterpolate(const float *source, size_t firstIndex, size_t secondIndex, float factor); } // namespace audioapi::AudioUtils diff --git a/packages/react-native-audio-api/common/cpp/utils/ios/FFTFrame.cpp b/packages/react-native-audio-api/common/cpp/utils/FFTFrame.cpp similarity index 57% rename from packages/react-native-audio-api/common/cpp/utils/ios/FFTFrame.cpp rename to packages/react-native-audio-api/common/cpp/utils/FFTFrame.cpp index ec467843..0613fa73 100644 --- a/packages/react-native-audio-api/common/cpp/utils/ios/FFTFrame.cpp +++ b/packages/react-native-audio-api/common/cpp/utils/FFTFrame.cpp @@ -1,8 +1,15 @@ -#if defined(HAVE_ACCELERATE) #include "FFTFrame.h" -#include "Accelerate/Accelerate.h" + +#if defined(HAVE_ACCELERATE) +#include +#endif + +#if defined(ANDROID) +#include +#endif namespace audioapi { +#if defined(HAVE_ACCELERATE) void FFTFrame::inverse(float *timeDomainData) { FFTSetup fftSetup_ = vDSP_create_fftsetup(log2Size_, FFT_RADIX2); @@ -25,5 +32,25 @@ void FFTFrame::inverse(float *timeDomainData) { vDSP_destroy_fftsetup(fftSetup_); } -} // namespace audioapi + +#elif defined(ANDROID) + +void FFTFrame::inverse(float *timeDomainData) { + fftwf_complex *freqDomainData = fftwf_alloc_complex(size_ / 2); + for (int i = 0; i < size_ / 2; i++) { + freqDomainData[i][0] = realData_[i]; + freqDomainData[i][1] = imaginaryData_[i]; + } + + auto plan = fftwf_plan_dft_c2r_1d( + size_, freqDomainData, timeDomainData, FFTW_ESTIMATE); + fftwf_execute(plan); + fftwf_destroy_plan(plan); + fftwf_free(freqDomainData); + + VectorMath::multiplyByScalar( + timeDomainData, 1.0f / static_cast(size_), timeDomainData, size_); +} + #endif +} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/utils/VectorMath.h b/packages/react-native-audio-api/common/cpp/utils/VectorMath.h index c29f58f8..aed401ac 100644 --- a/packages/react-native-audio-api/common/cpp/utils/VectorMath.h +++ b/packages/react-native-audio-api/common/cpp/utils/VectorMath.h @@ -32,40 +32,14 @@ namespace audioapi::VectorMath { -void multiplyByScalarThenAddToOutput( - const float *inputVector, - float scalar, - float *outputVector, - size_t numberOfElementsToProcess); +void multiplyByScalarThenAddToOutput(const float *inputVector, float scalar, float *outputVector, size_t numberOfElementsToProcess); -void multiplyByScalar( - const float *inputVector, - float scalar, - float *outputVector, - size_t numberOfElementsToProcess); -void addScalar( - const float *inputVector, - float scalar, - float *outputVector, - size_t numberOfElementsToProcess); -void add( - const float *inputVector1, - const float *inputVector2, - float *outputVector, - size_t numberOfElementsToProcess); -void subtract( - const float *inputVector1, - const float *inputVector2, - float *outputVector, - size_t numberOfElementsToProcess); -void multiply( - const float *inputVector1, - const float *inputVector2, - float *outputVector, - size_t numberOfElementsToProcess); +void multiplyByScalar(const float *inputVector, float scalar, float *outputVector, size_t numberOfElementsToProcess); +void addScalar(const float *inputVector, float scalar, float *outputVector, size_t numberOfElementsToProcess); +void add(const float *inputVector1, const float *inputVector2, float *outputVector, size_t numberOfElementsToProcess); +void subtract(const float *inputVector1, const float *inputVector2, float *outputVector, size_t numberOfElementsToProcess); +void multiply(const float *inputVector1, const float *inputVector2, float *outputVector, size_t numberOfElementsToProcess); // Finds the maximum magnitude of a float vector. -float maximumMagnitude( - const float *inputVector, - size_t numberOfElementsToProcess); +float maximumMagnitude(const float *inputVector, size_t numberOfElementsToProcess); } // namespace audioapi::VectorMath diff --git a/packages/react-native-audio-api/common/cpp/utils/android/FFTFrame.cpp b/packages/react-native-audio-api/common/cpp/utils/android/FFTFrame.cpp deleted file mode 100644 index df4b34f0..00000000 --- a/packages/react-native-audio-api/common/cpp/utils/android/FFTFrame.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#if defined(ANDROID) -#include "FFTFrame.h" -#include - -namespace audioapi { -void FFTFrame::inverse(float *timeDomainData) { - fftwf_complex *freqDomainData = fftwf_alloc_complex(size_ / 2); - for (int i = 0; i < size_ / 2; i++) { - freqDomainData[i][0] = realData_[i]; - freqDomainData[i][1] = imaginaryData_[i]; - } - - auto plan = fftwf_plan_dft_c2r_1d( - size_, freqDomainData, timeDomainData, FFTW_ESTIMATE); - fftwf_execute(plan); - fftwf_destroy_plan(plan); - fftwf_free(freqDomainData); - - VectorMath::multiplyByScalar( - timeDomainData, 1.0f / static_cast(size_), timeDomainData, size_); -} -} // namespace audioapi -#endif diff --git a/packages/react-native-audio-api/ios/AudioDecoder/AudioDecoder.h b/packages/react-native-audio-api/ios/AudioDecoder/AudioDecoder.h deleted file mode 100644 index 74295ef5..00000000 --- a/packages/react-native-audio-api/ios/AudioDecoder/AudioDecoder.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#import -#import - -@interface AudioDecoder : NSObject - -@property (nonatomic, strong) AVAudioPCMBuffer *buffer; -@property (nonatomic, assign) int sampleRate; - -- (instancetype)initWithSampleRate:(int)sampleRate; - -- (const AudioBufferList *)decodeWithFile:(NSString *)path; - -- (void)cleanup; - -@end diff --git a/packages/react-native-audio-api/ios/AudioDecoder/AudioDecoder.m b/packages/react-native-audio-api/ios/AudioDecoder/AudioDecoder.m deleted file mode 100644 index 619a8d06..00000000 --- a/packages/react-native-audio-api/ios/AudioDecoder/AudioDecoder.m +++ /dev/null @@ -1,79 +0,0 @@ -#import - -@implementation AudioDecoder - -- (instancetype)initWithSampleRate:(int)sampleRate -{ - if (self = [super init]) { - self.sampleRate = sampleRate; - } - return self; -} - -- (const AudioBufferList *)decodeWithFile:(NSString *)path -{ - NSError *error = nil; - NSURL *fileURL = [NSURL fileURLWithPath:path]; - AVAudioFile *audioFile = [[AVAudioFile alloc] initForReading:fileURL error:&error]; - - if (error) { - NSLog(@"Error occurred while opening the audio file: %@", [error localizedDescription]); - return nil; - } - AVAudioPCMBuffer *buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:[audioFile processingFormat] - frameCapacity:[audioFile length]]; - - AVAudioFormat *format = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatFloat32 - sampleRate:self.sampleRate - channels:buffer.audioBufferList->mNumberBuffers - interleaved:NO]; - - [audioFile readIntoBuffer:buffer error:&error]; - if (error) { - NSLog(@"Error occurred while reading the audio file: %@", [error localizedDescription]); - return nil; - } - - if (self.sampleRate != audioFile.processingFormat.sampleRate) { - self.buffer = [self convertBuffer:buffer ToFormat:format]; - } else { - self.buffer = buffer; - } - - return self.buffer.audioBufferList; -} - -- (AVAudioPCMBuffer *)convertBuffer:(AVAudioPCMBuffer *)buffer ToFormat:(AVAudioFormat *)format -{ - NSError *error = nil; - AVAudioConverter *converter = [[AVAudioConverter alloc] initFromFormat:buffer.format toFormat:format]; - AVAudioPCMBuffer *convertedBuffer = - [[AVAudioPCMBuffer alloc] initWithPCMFormat:format frameCapacity:(AVAudioFrameCount)buffer.frameCapacity]; - - AVAudioConverterInputBlock inputBlock = - ^AVAudioBuffer *(AVAudioPacketCount inNumberOfPackets, AVAudioConverterInputStatus *outStatus) { - if (buffer.frameLength > 0) { - *outStatus = AVAudioConverterInputStatus_HaveData; - return buffer; - } else { - *outStatus = AVAudioConverterInputStatus_NoDataNow; - return nil; - } - }; - - [converter convertToBuffer:convertedBuffer error:&error withInputFromBlock:inputBlock]; - - if (error) { - NSLog(@"Error occurred while converting the audio file: %@", [error localizedDescription]); - return nil; - } - - return convertedBuffer; -} - -- (void)cleanup -{ - self.buffer = nil; -} - -@end diff --git a/packages/react-native-audio-api/ios/AudioDecoder/IOSAudioDecoder.h b/packages/react-native-audio-api/ios/AudioDecoder/IOSAudioDecoder.h deleted file mode 100644 index 4d448d4c..00000000 --- a/packages/react-native-audio-api/ios/AudioDecoder/IOSAudioDecoder.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -#include - -#ifdef __OBJC__ // when compiled as Objective-C++ -#import -#else // when compiled as C++ -typedef struct objc_object AudioDecoder; -#endif // __OBJC__ - -namespace audioapi { - -class AudioBus; - -class IOSAudioDecoder { - protected: - AudioDecoder *audioDecoder_; - int sampleRate_; - - public: - IOSAudioDecoder(int sampleRate); - ~IOSAudioDecoder(); - - AudioBus *decodeWithFilePath(const std::string &path); - // TODO: implement this - AudioBus *decodeWithArrayBuffer(); -}; -} // namespace audioapi diff --git a/packages/react-native-audio-api/ios/AudioDecoder/IOSAudioDecoder.mm b/packages/react-native-audio-api/ios/AudioDecoder/IOSAudioDecoder.mm deleted file mode 100644 index 6f34ebc7..00000000 --- a/packages/react-native-audio-api/ios/AudioDecoder/IOSAudioDecoder.mm +++ /dev/null @@ -1,46 +0,0 @@ -#import - -#include -#include -#include - -namespace audioapi { - -IOSAudioDecoder::IOSAudioDecoder(int sampleRate) : sampleRate_(sampleRate) -{ - audioDecoder_ = [[AudioDecoder alloc] initWithSampleRate:sampleRate_]; -} - -IOSAudioDecoder::~IOSAudioDecoder() -{ - [audioDecoder_ cleanup]; - audioDecoder_ = nullptr; -} - -AudioBus *IOSAudioDecoder::decodeWithFilePath(const std::string &path) -{ - auto bufferList = [audioDecoder_ decodeWithFile:[NSString stringWithUTF8String:path.c_str()]]; - AudioBus *audioBus; - if (bufferList) { - auto numberOfChannels = bufferList->mNumberBuffers; - auto size = bufferList->mBuffers[0].mDataByteSize / sizeof(float); - - audioBus = new AudioBus(sampleRate_, size, numberOfChannels); - - for (int i = 0; i < numberOfChannels; i++) { - float *data = (float *)bufferList->mBuffers[i].mData; - memcpy(audioBus->getChannel(i)->getData(), data, sizeof(float) * size); - } - } else { - audioBus = new AudioBus(sampleRate_, 1, 1); - } - - return audioBus; -} - -AudioBus *IOSAudioDecoder::decodeWithArrayBuffer() -{ - // TODO: implement his - return new AudioBus(sampleRate_, 1, 1); -} -} // namespace audioapi diff --git a/packages/react-native-audio-api/ios/core/AudioDecoder.mm b/packages/react-native-audio-api/ios/core/AudioDecoder.mm new file mode 100644 index 00000000..9ff76258 --- /dev/null +++ b/packages/react-native-audio-api/ios/core/AudioDecoder.mm @@ -0,0 +1,45 @@ +#define MINIAUDIO_IMPLEMENTATION +#import + +#include +#include +#include + +namespace audioapi { + +AudioBus *AudioDecoder::decodeWithFilePath(const std::string &path) const +{ + ma_decoder decoder; + ma_decoder_config config = 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) { + NSLog(@"Failed to initialize decoder for file: %s", path.c_str()); + return new AudioBus(1, 1, 1); + } + + ma_uint64 totalFrameCount; + ma_decoder_get_length_in_pcm_frames(&decoder, &totalFrameCount); + + auto *audioBus = new AudioBus(sampleRate_, static_cast(totalFrameCount), 2); + auto *buffer = new float[totalFrameCount * 2]; + + ma_uint64 framesDecoded; + ma_decoder_read_pcm_frames(&decoder, buffer, totalFrameCount, &framesDecoded); + if (framesDecoded == 0) { + NSLog(@"Failed to decode audio file: %s", path.c_str()); + } + + for (int i = 0; i < decoder.outputChannels; ++i) { + float *channelData = audioBus->getChannel(i)->getData(); + + for (ma_uint64 j = 0; j < framesDecoded; ++j) { + channelData[j] = buffer[j * decoder.outputChannels + i]; + } + } + + delete[] buffer; + ma_decoder_uninit(&decoder); + + return audioBus; +} +} // namespace audioapi diff --git a/packages/react-native-audio-api/ios/AudioPlayer/AudioPlayer.h b/packages/react-native-audio-api/ios/core/AudioPlayer.h similarity index 100% rename from packages/react-native-audio-api/ios/AudioPlayer/AudioPlayer.h rename to packages/react-native-audio-api/ios/core/AudioPlayer.h diff --git a/packages/react-native-audio-api/ios/AudioPlayer/AudioPlayer.m b/packages/react-native-audio-api/ios/core/AudioPlayer.m similarity index 100% rename from packages/react-native-audio-api/ios/AudioPlayer/AudioPlayer.m rename to packages/react-native-audio-api/ios/core/AudioPlayer.m diff --git a/packages/react-native-audio-api/ios/AudioPlayer/IOSAudioPlayer.h b/packages/react-native-audio-api/ios/core/IOSAudioPlayer.h similarity index 100% rename from packages/react-native-audio-api/ios/AudioPlayer/IOSAudioPlayer.h rename to packages/react-native-audio-api/ios/core/IOSAudioPlayer.h diff --git a/packages/react-native-audio-api/ios/AudioPlayer/IOSAudioPlayer.mm b/packages/react-native-audio-api/ios/core/IOSAudioPlayer.mm similarity index 100% rename from packages/react-native-audio-api/ios/AudioPlayer/IOSAudioPlayer.mm rename to packages/react-native-audio-api/ios/core/IOSAudioPlayer.mm diff --git a/packages/react-native-audio-api/package.json b/packages/react-native-audio-api/package.json index a1178839..fdc05f5c 100644 --- a/packages/react-native-audio-api/package.json +++ b/packages/react-native-audio-api/package.json @@ -36,7 +36,7 @@ "lint:kotlin": "ktlint 'android/src/main/java/**'", "format:android": "find android/src/ -iname \"*.h\" -o -iname \"*.cpp\" | xargs clang-format -i", "format:ios": "find ios/ -iname \"*.h\" -o -iname \"*.m\" -o -iname \"*.mm\" -o -iname \"*.cpp\" | xargs clang-format -i --Werror", - "format:common": "find common/cpp/ -iname \"*.h\" -o -iname \"*.cpp\" | xargs clang-format -i", + "format:common": "find common/cpp/ -path 'common/cpp/libs' -prune -iname \"*.h\" -o -iname \"*.cpp\" | xargs clang-format -i", "format:kotlin": "ktlint -F 'android/src/main/java/**'", "build": "bob build", "prepack": "cp ../../README.md ./README.md", diff --git a/packages/react-native-audio-api/scripts/cpplint.sh b/packages/react-native-audio-api/scripts/cpplint.sh index 62011364..8297f712 100755 --- a/packages/react-native-audio-api/scripts/cpplint.sh +++ b/packages/react-native-audio-api/scripts/cpplint.sh @@ -1,7 +1,7 @@ #!/bin/bash if which cpplint >/dev/null; then - cpplint --linelength=230 --filter=-legal/copyright,-readability/todo,-build/namespaces,-whitespace/comments,-whitespace/parens,-build/c++11,-runtime/references --quiet --recursive common/cpp android/src/main/cpp + find common/cpp android/src/main/cpp -path 'common/cpp/libs' -prune -o -name '*.cpp' -o -name '*.h' -print | xargs cpplint --linelength=230 --filter=-legal/copyright,-readability/todo,-build/namespaces,-whitespace/comments,-whitespace/parens,-build/c++17,-runtime/references --quiet else echo "error: cpplint not installed, download from https://github.com/cpplint/cpplint" 1>&2 exit 1