@techoptio/react-native-live-pitch-detection 1.0.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/android/build.gradle
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@techoptio/react-native-live-pitch-detection",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Live pitch detection (frequency, note and octave) from device microphone for React Native.",
|
|
5
5
|
"main": "./lib/module/index.js",
|
|
6
6
|
"types": "./lib/typescript/src/index.d.ts",
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"android",
|
|
19
19
|
"ios",
|
|
20
20
|
"cpp",
|
|
21
|
+
"shared",
|
|
21
22
|
"*.podspec",
|
|
22
23
|
"react-native.config.js",
|
|
23
24
|
"!ios/build",
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// Thanks to rnheroes/react-native-pitchy for the original implementation
|
|
2
|
+
// https://github.com/rnheroes/react-native-pitchy/blob/main/cpp/react-native-pitchy.cpp
|
|
3
|
+
|
|
4
|
+
#include "ReactNativeLivePitchDetectionModule.h"
|
|
5
|
+
|
|
6
|
+
#include <vector>
|
|
7
|
+
#include <cmath>
|
|
8
|
+
#include <algorithm>
|
|
9
|
+
#include <numeric>
|
|
10
|
+
#include <limits>
|
|
11
|
+
|
|
12
|
+
namespace techoptio {
|
|
13
|
+
namespace reactnativelivepitchdetection {
|
|
14
|
+
double getVolumeDecibel(double rms)
|
|
15
|
+
{
|
|
16
|
+
return 20 * std::log10(rms);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
double autoCorrelate(const std::vector<double> &buf, double sampleRate, double minVolume)
|
|
20
|
+
{
|
|
21
|
+
int SIZE = buf.size();
|
|
22
|
+
double rms = 0;
|
|
23
|
+
|
|
24
|
+
for (int i = 0; i < SIZE; ++i)
|
|
25
|
+
{
|
|
26
|
+
double val = buf[i];
|
|
27
|
+
rms += val * val;
|
|
28
|
+
}
|
|
29
|
+
rms = std::sqrt(rms / SIZE);
|
|
30
|
+
double decibel = getVolumeDecibel(rms);
|
|
31
|
+
if (decibel < minVolume)
|
|
32
|
+
return -1;
|
|
33
|
+
|
|
34
|
+
int r1 = 0, r2 = SIZE - 1;
|
|
35
|
+
double thres = 0.2;
|
|
36
|
+
for (int i = 0; i < SIZE / 2; ++i)
|
|
37
|
+
if (std::abs(buf[i]) < thres)
|
|
38
|
+
{
|
|
39
|
+
r1 = i;
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
for (int i = 1; i < SIZE / 2; ++i)
|
|
43
|
+
if (std::abs(buf[SIZE - i]) < thres)
|
|
44
|
+
{
|
|
45
|
+
r2 = SIZE - i;
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
std::vector<double> slicedBuf(buf.begin() + r1, buf.begin() + r2);
|
|
50
|
+
SIZE = slicedBuf.size();
|
|
51
|
+
|
|
52
|
+
std::vector<double> c(SIZE, 0);
|
|
53
|
+
for (int i = 0; i < SIZE; ++i)
|
|
54
|
+
for (int j = 0; j < SIZE - i; ++j)
|
|
55
|
+
c[i] += slicedBuf[j] * slicedBuf[j + i];
|
|
56
|
+
|
|
57
|
+
int d = 0;
|
|
58
|
+
while (c[d] > c[d + 1])
|
|
59
|
+
d++;
|
|
60
|
+
double maxval = -1, maxpos = -1;
|
|
61
|
+
for (int i = d; i < SIZE; ++i)
|
|
62
|
+
{
|
|
63
|
+
if (c[i] > maxval)
|
|
64
|
+
{
|
|
65
|
+
maxval = c[i];
|
|
66
|
+
maxpos = i;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
double T0 = maxpos;
|
|
70
|
+
|
|
71
|
+
double x1 = c[T0 - 1], x2 = c[T0], x3 = c[T0 + 1];
|
|
72
|
+
double a = (x1 + x3 - 2 * x2) / 2;
|
|
73
|
+
double b = (x3 - x1) / 2;
|
|
74
|
+
if (a)
|
|
75
|
+
T0 = T0 - b / (2 * a);
|
|
76
|
+
|
|
77
|
+
return sampleRate / T0;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#ifndef REACT_NATIVE_LIVE_PITCH_DETECTION_MODULE_H
|
|
2
|
+
#define REACT_NATIVE_LIVE_PITCH_DETECTION_MODULE_H
|
|
3
|
+
|
|
4
|
+
#include <vector>
|
|
5
|
+
|
|
6
|
+
namespace techoptio {
|
|
7
|
+
namespace reactnativelivepitchdetection {
|
|
8
|
+
double autoCorrelate(const std::vector<double> &buf, double sampleRate, double minVolume);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
#endif
|