react-native-simple-note-pitch-detector 0.2.9 → 0.3.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.
|
@@ -4,6 +4,10 @@ import Pitchy
|
|
|
4
4
|
|
|
5
5
|
public class ReactNativeSimpleNotePitchDetectorModule: Module {
|
|
6
6
|
|
|
7
|
+
var bufferSize = 3
|
|
8
|
+
var notes = Array(repeating: "", count: 3)
|
|
9
|
+
var counter = 0
|
|
10
|
+
|
|
7
11
|
lazy var pitchEngine: PitchEngine = { [weak self] in
|
|
8
12
|
let config = Config(estimationStrategy: .yin)
|
|
9
13
|
let pitchEngine = PitchEngine(config: config, delegate: self)
|
|
@@ -11,6 +15,23 @@ public class ReactNativeSimpleNotePitchDetectorModule: Module {
|
|
|
11
15
|
return pitchEngine
|
|
12
16
|
}()
|
|
13
17
|
|
|
18
|
+
func mostFrequent(array: [String]) -> String? {
|
|
19
|
+
var counts = [String: Int]()
|
|
20
|
+
|
|
21
|
+
// Count the values with using forEach
|
|
22
|
+
array.forEach { counts[$0] = (counts[$0] ?? 0) + 1 }
|
|
23
|
+
|
|
24
|
+
// Find the most frequent value and its count with max(by:)
|
|
25
|
+
if let (value, count) = counts.max(by: {$0.1 < $1.1}) {
|
|
26
|
+
if (count <= 1) {
|
|
27
|
+
return nil
|
|
28
|
+
}
|
|
29
|
+
return value
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return nil
|
|
33
|
+
}
|
|
34
|
+
|
|
14
35
|
var isRecording = false
|
|
15
36
|
|
|
16
37
|
public func definition() -> ModuleDefinition {
|
|
@@ -45,8 +66,24 @@ extension ReactNativeSimpleNotePitchDetectorModule: PitchEngineDelegate {
|
|
|
45
66
|
guard absOffsetPercentage > 1.0 else {
|
|
46
67
|
return
|
|
47
68
|
}
|
|
69
|
+
|
|
70
|
+
if (counter == bufferSize) {
|
|
71
|
+
var result = mostFrequent(array: notes)
|
|
72
|
+
if (result != nil) {
|
|
73
|
+
self.sendEvent("onChangePitch", ["note" : result, "soundPressure" : ""])
|
|
74
|
+
}
|
|
75
|
+
counter = 0
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
do {
|
|
79
|
+
let note = try Note(frequency: pitch.wave.frequency)
|
|
80
|
+
notes[counter] = note.string
|
|
81
|
+
counter += 1
|
|
82
|
+
} catch {
|
|
83
|
+
|
|
84
|
+
}
|
|
48
85
|
|
|
49
|
-
self.sendEvent("onChangePitch", ["note" : pitch.note.string, "soundPressure" : pitchEngine.signalLevel])
|
|
86
|
+
// self.sendEvent("onChangePitch", ["note" : pitch.note.string, "soundPressure" : pitchEngine.signalLevel])
|
|
50
87
|
}
|
|
51
88
|
|
|
52
89
|
public func pitchEngine(_ pitchEngine: PitchEngine, didReceiveError error: Error) {
|
package/package.json
CHANGED