react-native-simple-note-pitch-detector 0.1.8 → 0.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.
@@ -6,25 +6,19 @@ 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
 
14
12
 
15
13
  class PitchAnalyzer {
16
14
 
17
- var onChangePitch: (String, Double) -> Unit
15
+ private val notes = arrayOf("C","C#","D","D#","E","F","F#","G","G#","A","A#","B")
18
16
 
19
- constructor(callback: (String, Double) -> Unit) {
20
- this.onChangePitch = callback
21
- }
22
-
23
- private val tones = arrayOf("C","C#","D","D#","E","F","F#","G","G#","A","A#","B")
17
+ private lateinit var onChangePitch: (String, Double) -> Unit
18
+ private var isRecording = false
24
19
 
25
20
  private var dispatcher: AudioDispatcher? = null
26
21
  private var processor: AudioProcessor? = null
27
-
28
22
  private var runner: Thread? = null
29
23
 
30
24
  private val handler = PitchDetectionHandler { res, e ->
@@ -40,23 +34,34 @@ class PitchAnalyzer {
40
34
  }
41
35
 
42
36
  private fun process(pitchInHz: Float) {
43
- val index = round(12 * (log2(pitchInHz / 440) / log2(2f)) + 69) % 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
- val tone = tones[index.toInt()]
47
- val frequency = pitchInHz.toDouble()
42
+ val note = notes[index.toInt()]
48
43
 
49
- this.onChangePitch(tone, 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
  }
@@ -1,8 +1,5 @@
1
1
  package expo.modules.simplenotepitchdetector
2
2
 
3
- import android.Manifest
4
- import android.content.pm.PackageManager
5
- import androidx.core.app.ActivityCompat
6
3
  import expo.modules.kotlin.modules.Module
7
4
  import expo.modules.kotlin.modules.ModuleDefinition
8
5
  import expo.modules.simplepitchdetector.PitchAnalyzer
@@ -10,36 +7,31 @@ import androidx.core.os.bundleOf
10
7
 
11
8
  class ReactNativeSimpleNotePitchDetectorModule : Module() {
12
9
 
10
+ private val pitchAnalyzer = PitchAnalyzer()
11
+
13
12
  override fun definition() = ModuleDefinition {
13
+
14
14
  Name("ReactNativeSimpleNotePitchDetector")
15
15
 
16
16
  Events("onChangePitch")
17
17
 
18
18
  Function("start") {
19
-
20
- val pitchAnalyzer = PitchAnalyzer { tone: String, frequency: Double ->
19
+ pitchAnalyzer.addOnChangePitchListener { note: String, frequency: Double ->
21
20
  this@ReactNativeSimpleNotePitchDetectorModule.sendEvent(
22
21
  "onChangePitch",
23
- bundleOf("tone" to tone, "frequency" to frequency)
22
+ bundleOf("note" to note, "frequency" to frequency)
24
23
  )
25
24
  }
26
25
 
27
26
  pitchAnalyzer.start()
27
+ }
28
+
29
+ Function("stop") {
30
+ pitchAnalyzer.stop()
31
+ }
28
32
 
29
- // val context = appContext.reactContext!!
30
- // var activity = appContext.currentActivity!!
31
- // var permissionsGranted = ActivityCompat.checkSelfPermission(context, permissions[0]) == PackageManager.PERMISSION_GRANTED
32
- // if (!permissionsGranted) {
33
- // ActivityCompat.requestPermissions(activity, permissions, 200)
34
- // } else {
35
- // val pitchAnalyzer = PitchAnalyzer {
36
- // this@ReactNativeSimpleNotePitchDetectorModule.sendEvent(
37
- // "onChange",
38
- // bundleOf("tone" to it)
39
- // )
40
- // }
41
- // pitchAnalyzer.start()
42
- // }
33
+ Function("isRecording") {
34
+ pitchAnalyzer.isRecording()
43
35
  }
44
36
  }
45
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"]}
@@ -32,7 +32,7 @@ extension ReactNativeSimpleNotePitchDetectorModule: PitchEngineDelegate {
32
32
  return
33
33
  }
34
34
 
35
- self.sendEvent("onChangePitch", ["note" : pitch.note.string, "frequency" : pitch.frequency, "wave" : pitch.wave.wavelength])
35
+ self.sendEvent("onChangePitch", ["note" : pitch.note.string, "frequency" : pitch.frequency])
36
36
  }
37
37
 
38
38
  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.1.8",
3
+ "version": "0.2.0",
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 = {