react-native-simple-note-pitch-detector 0.1.9 → 0.2.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.
@@ -6,8 +6,6 @@ import be.tarsos.dsp.io.android.AudioDispatcherFactory
6
6
  import be.tarsos.dsp.pitch.PitchDetectionHandler
7
7
  import be.tarsos.dsp.pitch.PitchProcessor
8
8
  import be.tarsos.dsp.pitch.PitchProcessor.PitchEstimationAlgorithm
9
- import com.facebook.react.bridge.WritableMap
10
- import com.facebook.react.bridge.WritableNativeMap
11
9
  import kotlin.math.log2
12
10
  import kotlin.math.round
13
11
 
@@ -16,16 +14,13 @@ class PitchAnalyzer {
16
14
 
17
15
  private val notes = arrayOf("C","C#","D","D#","E","F","F#","G","G#","A","A#","B")
18
16
 
19
- private var onChangePitch: (String, Double) -> Unit
17
+ private lateinit var onChangePitch: (String, Double) -> Unit
18
+ private var isRecording = false
20
19
 
21
20
  private var dispatcher: AudioDispatcher? = null
22
21
  private var processor: AudioProcessor? = null
23
22
  private var runner: Thread? = null
24
23
 
25
- constructor(callback: (String, Double) -> Unit) {
26
- this.onChangePitch = callback
27
- }
28
-
29
24
  private val handler = PitchDetectionHandler { res, e ->
30
25
  val pitchInHz = res.pitch
31
26
  process(pitchInHz)
@@ -39,24 +34,34 @@ class PitchAnalyzer {
39
34
  }
40
35
 
41
36
  private fun process(pitchInHz: Float) {
42
- val octave = round(12 * (log2(pitchInHz / 440) / log2(2f)) + 69)
43
- val index = octave % 12
37
+ val freq = round(12 * (log2(pitchInHz / 440) / log2(2f)) + 69)
38
+ val octave = freq / 12
39
+ val index = freq % 12
44
40
 
45
41
  if (!index.isNaN() && pitchInHz > 0) {
46
42
  val note = notes[index.toInt()]
47
- val frequency = pitchInHz.toDouble()
48
43
 
49
- this.onChangePitch("$note$octave", frequency)
44
+ this.onChangePitch("$note$octave", pitchInHz.toDouble())
50
45
  }
51
46
  }
52
47
 
48
+ fun addOnChangePitchListener(onChangePitch: (String, Double) -> Unit) {
49
+ this.onChangePitch = onChangePitch
50
+ }
51
+
53
52
  fun start() {
54
53
  prepare()
55
54
  runner = Thread(dispatcher)
56
55
  runner?.start()
56
+ isRecording = true
57
57
  }
58
58
  fun stop() {
59
59
  dispatcher?.stop()
60
60
  runner?.interrupt()
61
+ isRecording = false
62
+ }
63
+
64
+ fun isRecording(): Boolean {
65
+ return isRecording
61
66
  }
62
67
  }
@@ -7,21 +7,31 @@ import androidx.core.os.bundleOf
7
7
 
8
8
  class ReactNativeSimpleNotePitchDetectorModule : Module() {
9
9
 
10
+ private val pitchAnalyzer = PitchAnalyzer()
11
+
10
12
  override fun definition() = ModuleDefinition {
13
+
11
14
  Name("ReactNativeSimpleNotePitchDetector")
12
15
 
13
16
  Events("onChangePitch")
14
17
 
15
18
  Function("start") {
16
-
17
- val pitchAnalyzer = PitchAnalyzer { tone: String, frequency: Double ->
19
+ pitchAnalyzer.addOnChangePitchListener { note: String, frequency: Double ->
18
20
  this@ReactNativeSimpleNotePitchDetectorModule.sendEvent(
19
21
  "onChangePitch",
20
- bundleOf("tone" to tone, "frequency" to frequency)
22
+ bundleOf("note" to note, "frequency" to frequency)
21
23
  )
22
24
  }
23
25
 
24
26
  pitchAnalyzer.start()
25
27
  }
28
+
29
+ Function("stop") {
30
+ pitchAnalyzer.stop()
31
+ }
32
+
33
+ Function("isRecording") {
34
+ pitchAnalyzer.isRecording()
35
+ }
26
36
  }
27
37
  }
@@ -1,5 +1,6 @@
1
1
  export type ChangeEventPayload = {
2
- value: string;
2
+ note: string;
3
+ frequency: number;
3
4
  };
4
5
  export type ReactNativeSimpleNotePitchDetectorViewProps = {
5
6
  name: string;
@@ -1 +1 @@
1
- {"version":3,"file":"ReactNativeSimpleNotePitchDetector.types.d.ts","sourceRoot":"","sources":["../src/ReactNativeSimpleNotePitchDetector.types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,kBAAkB,GAAG;IAC/B,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,2CAA2C,GAAG;IACxD,IAAI,EAAE,MAAM,CAAC;CACd,CAAC"}
1
+ {"version":3,"file":"ReactNativeSimpleNotePitchDetector.types.d.ts","sourceRoot":"","sources":["../src/ReactNativeSimpleNotePitchDetector.types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,kBAAkB,GAAG;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,2CAA2C,GAAG;IACxD,IAAI,EAAE,MAAM,CAAC;CACd,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"ReactNativeSimpleNotePitchDetector.types.js","sourceRoot":"","sources":["../src/ReactNativeSimpleNotePitchDetector.types.ts"],"names":[],"mappings":"","sourcesContent":["export type ChangeEventPayload = {\n value: string;\n};\n\nexport type ReactNativeSimpleNotePitchDetectorViewProps = {\n name: string;\n};\n"]}
1
+ {"version":3,"file":"ReactNativeSimpleNotePitchDetector.types.js","sourceRoot":"","sources":["../src/ReactNativeSimpleNotePitchDetector.types.ts"],"names":[],"mappings":"","sourcesContent":["export type ChangeEventPayload = {\n note: string;\n frequency: number;\n};\n\nexport type ReactNativeSimpleNotePitchDetectorViewProps = {\n name: string;\n};\n"]}
@@ -9,37 +9,50 @@ public class ReactNativeSimpleNotePitchDetectorModule: Module {
9
9
  let pitchEngine = PitchEngine(config: config, delegate: self)
10
10
  return pitchEngine
11
11
  }()
12
+
13
+ var isRecording = false
14
+
15
+ public func definition() -> ModuleDefinition {
12
16
 
13
- public func definition() -> ModuleDefinition {
17
+ Name("ReactNativeSimpleNotePitchDetector")
14
18
 
15
- Name("ReactNativeSimpleNotePitchDetector")
19
+ Events("onChangePitch")
16
20
 
17
- Events("onChangePitch")
21
+ Function("start") {
22
+ pitchEngine.start()
23
+ isRecording = true
24
+ }
18
25
 
19
- Function("start") {
20
- pitchEngine.start()
26
+ Function("stop") {
27
+ pitchEngine.stop()
28
+ isRecording = false
29
+ }
30
+
31
+ Function("isRecording") {
32
+ return isRecording
33
+ }
21
34
  }
22
- }
23
35
  }
24
36
 
25
37
  extension ReactNativeSimpleNotePitchDetectorModule: PitchEngineDelegate {
26
- public func pitchEngine(_ pitchEngine: PitchEngine, didReceivePitch pitch: Pitch) {
38
+
39
+ public func pitchEngine(_ pitchEngine: PitchEngine, didReceivePitch pitch: Pitch) {
27
40
 
28
- let offsetPercentage = pitch.closestOffset.percentage
29
- let absOffsetPercentage = abs(offsetPercentage)
41
+ let offsetPercentage = pitch.closestOffset.percentage
42
+ let absOffsetPercentage = abs(offsetPercentage)
30
43
 
31
- guard absOffsetPercentage > 1.0 else {
32
- return
33
- }
44
+ guard absOffsetPercentage > 1.0 else {
45
+ return
46
+ }
34
47
 
35
- self.sendEvent("onChangePitch", ["note" : pitch.note.string, "frequency" : pitch.frequency])
36
- }
48
+ self.sendEvent("onChangePitch", ["note" : pitch.note.string, "frequency" : pitch.frequency])
49
+ }
37
50
 
38
- public func pitchEngine(_ pitchEngine: PitchEngine, didReceiveError error: Error) {
39
-
40
- }
51
+ public func pitchEngine(_ pitchEngine: PitchEngine, didReceiveError error: Error) {
41
52
 
42
- public func pitchEngineWentBelowLevelThreshold(_ pitchEngine: PitchEngine) {
43
-
44
- }
53
+ }
54
+
55
+ public func pitchEngineWentBelowLevelThreshold(_ pitchEngine: PitchEngine) {
56
+
57
+ }
45
58
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-simple-note-pitch-detector",
3
- "version": "0.1.9",
3
+ "version": "0.2.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",
@@ -1,5 +1,6 @@
1
1
  export type ChangeEventPayload = {
2
- value: string;
2
+ note: string;
3
+ frequency: number;
3
4
  };
4
5
 
5
6
  export type ReactNativeSimpleNotePitchDetectorViewProps = {