Skip to content

Commit

Permalink
fix: android build error
Browse files Browse the repository at this point in the history
  • Loading branch information
yigithanyucedag committed Jul 27, 2024
1 parent aab9dfd commit 6415490
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ const pitch = await autoCorrelate(audioBuffer, sampleRate); // returns the pitch
### Get volume (dB)

```js
import { getVolume } from 'react-native-pitchy';
import { calculateVolume } from 'react-native-pitchy';

const audioBuffer = new Float32Array(4096); // Dummy audio buffer
const volume = await getVolume(audioBuffer); // returns the volume in dB
const volume = await calculateVolume(audioBuffer); // returns the volume in dB
```

## Contributing
Expand Down
25 changes: 15 additions & 10 deletions android/cpp-adapter.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
#include <jni.h>
#include "react-native-pitchy.h"
#include <vector>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <limits>

extern "C"
JNIEXPORT jdouble JNICALL
Java_com_pitchy_PitchyModule_nativeAutoCorrelate(JNIEnv *env, jclass type, jdoubleArray buf, jdouble sampleRate) {
extern "C" JNIEXPORT jdouble JNICALL
Java_com_pitchy_PitchyModule_nativeAutoCorrelate(JNIEnv *env, jobject /* this */, jdoubleArray buf, jdouble sampleRate)
{
jsize len = env->GetArrayLength(buf);
jdouble *bufArray = env->GetDoubleArrayElements(buf, 0);
std::vector<double> bufVector(bufArray, bufArray + len);
std::vector<double> vec(bufArray, bufArray + len);
env->ReleaseDoubleArrayElements(buf, bufArray, 0);
return pitchy::autoCorrelate(bufVector, sampleRate);
return pitchy::autoCorrelate(vec, sampleRate);
}

extern "C"
JNIEXPORT jdouble JNICALL
Java_com_pitchy_PitchyModule_nativeCalculateVolume(JNIEnv *env, jclass type, jdoubleArray buf) {
extern "C" JNIEXPORT jdouble JNICALL
Java_com_pitchy_PitchyModule_nativeCalculateVolume(JNIEnv *env, jobject /* this */, jdoubleArray buf)
{
jsize len = env->GetArrayLength(buf);
jdouble *bufArray = env->GetDoubleArrayElements(buf, 0);
std::vector<double> bufVector(bufArray, bufArray + len);
std::vector<double> vec(bufArray, bufArray + len);
env->ReleaseDoubleArrayElements(buf, bufArray, 0);
return pitchy::calculateVolume(bufVector);
return pitchy::calculateVolume(vec);
}
28 changes: 15 additions & 13 deletions android/src/main/java/com/pitchy/PitchyModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReadableArray

class PitchyModule(reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {
Expand All @@ -13,25 +14,26 @@ class PitchyModule(reactContext: ReactApplicationContext) :
}

@ReactMethod
fun nativeAutoCorrelate(buf: DoubleArray, sampleRate: Double, promise: Promise) {
// Call native method
val result = autoCorrelate(buf, sampleRate)

// Resolve promise
promise.resolve(result)
fun autoCorrelate(buf: ReadableArray, sampleRate: Double, promise: Promise) {
val bufArray = buf.toArrayList().map { it as Double }.toDoubleArray()
val pitch = nativeAutoCorrelate(bufArray, sampleRate)
promise.resolve(pitch)
}

@ReactMethod
fun calculateVolume(buf: DoubleArray, promise: Promise) {
// Call native method
val result = calculateVolume(buf)

// Resolve promise
promise.resolve(result)
fun calculateVolume(buf: ReadableArray, promise: Promise) {
val bufArray = buf.toArrayList().map { it as Double }.toDoubleArray()
val volume = nativeCalculateVolume(bufArray)
promise.resolve(volume)
}

private external fun nativeAutoCorrelate(buf: DoubleArray, sampleRate: Double): Double
private external fun nativeCalculateVolume(buf: DoubleArray): Double

companion object {
const val NAME = "Pitchy"
init {
System.loadLibrary("react-native-pitchy")
}
}
}
}

0 comments on commit 6415490

Please sign in to comment.