Skip to content

Commit

Permalink
feat: add calculate volume method, minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
yigithanyucedag committed Jul 27, 2024
1 parent 85b5441 commit b6f78e7
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 47 deletions.
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,24 @@ npm install react-native-pitchy

## Usage

### Autocorrelation

```js
import { multiply } from 'react-native-pitchy';
import { autoCorrelate } from 'react-native-pitchy';

// ...

const result = await multiply(3, 7);
const audioBuffer = new Float32Array(4096); // Dummy audio buffer
const sampleRate = 44100;
const pitch = await autoCorrelate(audioBuffer, sampleRate); // returns the pitch in Hz
```

### Get volume (dB)

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

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

## Contributing

Expand All @@ -27,7 +36,3 @@ See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the
## License

MIT

---

Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
18 changes: 16 additions & 2 deletions android/cpp-adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@

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

extern "C"
JNIEXPORT jdouble JNICALL
Java_com_pitchy_PitchyModule_nativeCalculateVolume(JNIEnv *env, jclass type, jdoubleArray buf) {
jsize len = env->GetArrayLength(buf);
jdouble *bufArray = env->GetDoubleArrayElements(buf, 0);
std::vector<double> bufVector(bufArray, bufArray + len);
env->ReleaseDoubleArrayElements(buf, bufArray, 0);
return pitchy::calculateVolume(bufVector);
}
20 changes: 16 additions & 4 deletions android/src/main/java/com/pitchy/PitchyModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,25 @@ class PitchyModule(reactContext: ReactApplicationContext) :
return NAME
}

// Example method
// See https://reactnative.dev/docs/native-modules-android
@ReactMethod
fun multiply(a: Double, b: Double, promise: Promise) {
promise.resolve(a * b)
fun nativeAutoCorrelate(buf: DoubleArray, sampleRate: Double, promise: Promise) {
// Call native method
val result = autoCorrelate(buf, sampleRate)

// Resolve promise
promise.resolve(result)
}

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

// Resolve promise
promise.resolve(result)
}


companion object {
const val NAME = "Pitchy"
}
Expand Down
18 changes: 13 additions & 5 deletions cpp/react-native-pitchy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@

namespace pitchy
{
double multiply(double a, double b)
{
return a * b;
}

double autoCorrelate(const std::vector<double> &buf, double sampleRate)
{
// Implements the ACF2+ algorithm
Expand Down Expand Up @@ -73,4 +68,17 @@ namespace pitchy

return sampleRate / T0;
}

double calculateVolume(const std::vector<double> &buf)
{
// Calculate RMS (Root Mean Square)
double sumSquares = std::accumulate(buf.begin(), buf.end(), 0.0, [](double a, double b)
{ return a + b * b; });
double rms = std::sqrt(sumSquares / buf.size());

// Convert RMS to decibels
double decibels = 20 * std::log10(rms);

return decibels;
}
}
2 changes: 1 addition & 1 deletion cpp/react-native-pitchy.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

namespace pitchy
{
double multiply(double a, double b);
double autoCorrelate(const std::vector<double> &buf, double sampleRate);
double calculateVolume(const std::vector<double> &buf);
}

#endif /* PITCHY_H */
2 changes: 0 additions & 2 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ export default function App() {
const [result, setResult] = useState<number | undefined>();

useEffect(() => {
// multiply(3, 7).then(setResult);
// 2048 length
const data = new Array(2048)
.fill(0)
.map((_, i) => Math.sin((i / 2048) * Math.PI * 2 * 440));
Expand Down
37 changes: 18 additions & 19 deletions ios/Pitchy.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@
@implementation Pitchy
RCT_EXPORT_MODULE()

// Example method
// See // https://reactnative.dev/docs/native-modules-ios
RCT_EXPORT_METHOD(multiply:(double)a
b:(double)b
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
{
NSNumber *result = @(pitchy::multiply(a, b));

resolve(result);
}

// Method to expose autoCorrelate
RCT_EXPORT_METHOD(autoCorrelate:(NSArray<NSNumber *> *)buf
sampleRate:(double)sampleRate
Expand All @@ -29,14 +17,25 @@ @implementation Pitchy

// Call the autoCorrelate function
double result = pitchy::autoCorrelate(cBuf, sampleRate);

if (result < 0) {
reject(@"autoCorrelate_error", @"Not enough signal", nil);
} else {
NSNumber *resultNumber = @(result);
resolve(resultNumber);
}

resolve([NSNumber numberWithDouble:result]);
}

// Method to expose calculateVolume
RCT_EXPORT_METHOD(calculateVolume:(NSArray<NSNumber *> *)buf
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
{
// Convert NSArray<NSNumber *> to std::vector<double>
std::vector<double> cBuf;
for (NSNumber *num in buf) {
cBuf.push_back([num doubleValue]);
}

// Call the calculateVolume function
double result = pitchy::calculateVolume(cBuf);

resolve([NSNumber numberWithDouble:result]);
}

@end
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-pitchy",
"version": "0.1.0",
"version": "1.0.0",
"description": "A simple pitch detection module for React Native",
"source": "./src/index.tsx",
"main": "./lib/commonjs/index.js",
Expand Down
21 changes: 16 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,24 @@ const Pitchy = NativeModules.Pitchy
}
);

export function multiply(a: number, b: number): Promise<number> {
return Pitchy.multiply(a, b);
}

/**
* Detects the pitch of the audio data in the buffer.
* @param buf The audio data buffer.
* @param sampleRate The sample rate of the audio data.
* @returns A promise that resolves to the detected pitch in Hz.
*/
export function autoCorrelate(
buf: number[],
buf: ArrayLike<number>,
sampleRate: number
): Promise<number> {
return Pitchy.autoCorrelate(buf, sampleRate);
}

/**
* Calculates the volume of the audio data in the buffer.
* @param buf The audio data buffer.
* @returns A promise that resolves to the calculated volume in dB.
*/
export function calculateVolume(buf: ArrayLike<number>): Promise<number> {
return Pitchy.calculateVolume(buf);
}

0 comments on commit b6f78e7

Please sign in to comment.