react-native-simple-note-pitch-detector 0.2.9 → 0.3.1

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.
@@ -10,12 +10,18 @@ import kotlin.math.floor
10
10
  import kotlin.math.log2
11
11
  import kotlin.math.round
12
12
 
13
+ data class NoteAndDecibel(
14
+ val note: String = "",
15
+ val decibel: Float = 0f
16
+ )
13
17
 
14
18
  class PitchAnalyzer {
15
19
 
16
20
  private val notes = arrayOf("C","C#","D","D#","E","F","F#","G","G#","A","A#","B")
21
+ private val notesBuffer = arrayOf(NoteAndDecibel(), NoteAndDecibel(), NoteAndDecibel(), NoteAndDecibel(), NoteAndDecibel())
22
+ private var counter = 0
17
23
 
18
- private lateinit var onChangePitch: (String, Double) -> Unit
24
+ private lateinit var onChangePitch: (String) -> Unit
19
25
  private var isRecording = false
20
26
 
21
27
  private var dispatcher: AudioDispatcher? = null
@@ -24,9 +30,8 @@ class PitchAnalyzer {
24
30
 
25
31
  private val handler = PitchDetectionHandler { res, e ->
26
32
  val pitchInHz = res.pitch
27
- val soundPressure = e.getdBSPL()
28
-
29
- process(pitchInHz, soundPressure)
33
+ val decibel = e.getdBSPL().toFloat()
34
+ process(pitchInHz, decibel)
30
35
  }
31
36
 
32
37
  private fun prepare() {
@@ -36,19 +41,30 @@ class PitchAnalyzer {
36
41
  dispatcher?.addAudioProcessor(processor)
37
42
  }
38
43
 
39
- private fun process(pitchInHz: Float, soundPressure: Double) {
44
+ private fun process(pitchInHz: Float, decibel: Float) {
40
45
  val freq = round(12 * (log2(pitchInHz / 440) / log2(2f)) + 69)
41
46
  val octave = (floor(freq / 12) - 1).toInt()
42
47
  val index = freq % 12
43
48
 
44
49
  if (!index.isNaN() && pitchInHz > 0) {
45
50
  val note = notes[index.toInt()]
51
+ var noteAndDecibel = NoteAndDecibel(note, decibel)
52
+
53
+ if (counter == notesBuffer.size) {
54
+ var s = ""
55
+ for (noteAndDecibel in notesBuffer) {
56
+ s += noteAndDecibel.note + " " + noteAndDecibel.decibel
57
+ }
58
+ this.onChangePitch(s)
59
+ counter = 0
60
+ }
46
61
 
47
- this.onChangePitch("$note$octave", soundPressure)
62
+ notesBuffer[counter] = noteAndDecibel
63
+ counter += 1
48
64
  }
49
65
  }
50
66
 
51
- fun addOnChangePitchListener(onChangePitch: (String, Double) -> Unit) {
67
+ fun addOnChangePitchListener(onChangePitch: (String) -> Unit) {
52
68
  this.onChangePitch = onChangePitch
53
69
  }
54
70
 
@@ -20,10 +20,10 @@ class ReactNativeSimpleNotePitchDetectorModule : Module() {
20
20
  }
21
21
 
22
22
  Function("start") {
23
- pitchAnalyzer.addOnChangePitchListener { note: String, soundPressure: Double ->
23
+ pitchAnalyzer.addOnChangePitchListener { note: String->
24
24
  this@ReactNativeSimpleNotePitchDetectorModule.sendEvent(
25
25
  "onChangePitch",
26
- bundleOf("note" to note, "soundPressure" to soundPressure)
26
+ bundleOf("note" to note, "soundPressure" to "")
27
27
  )
28
28
  }
29
29
 
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-simple-note-pitch-detector",
3
- "version": "0.2.9",
3
+ "version": "0.3.1",
4
4
  "description": "a simple react native library to detect the pitch of the input recording",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",