react-native-simple-note-pitch-detector 0.3.0 → 0.3.2
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 = Array(8) { NoteAndDecibel() }
|
|
22
|
+
private var counter = 0
|
|
17
23
|
|
|
18
|
-
private lateinit var onChangePitch: (String
|
|
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
|
|
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,
|
|
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.substring(0, s.length - 1))
|
|
59
|
+
counter = 0
|
|
60
|
+
}
|
|
46
61
|
|
|
47
|
-
|
|
62
|
+
notesBuffer[counter] = noteAndDecibel
|
|
63
|
+
counter += 1
|
|
48
64
|
}
|
|
49
65
|
}
|
|
50
66
|
|
|
51
|
-
fun addOnChangePitchListener(onChangePitch: (String
|
|
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
|
|
23
|
+
pitchAnalyzer.addOnChangePitchListener { note: String->
|
|
24
24
|
this@ReactNativeSimpleNotePitchDetectorModule.sendEvent(
|
|
25
25
|
"onChangePitch",
|
|
26
|
-
bundleOf("note" to note, "soundPressure" to
|
|
26
|
+
bundleOf("note" to note, "soundPressure" to "")
|
|
27
27
|
)
|
|
28
28
|
}
|
|
29
29
|
|
package/package.json
CHANGED