react-native-simple-note-pitch-detector 0.1.7 → 0.1.9

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.
@@ -14,19 +14,18 @@ import kotlin.math.round
14
14
 
15
15
  class PitchAnalyzer {
16
16
 
17
- var onChangePitch: (String, Double) -> Unit
17
+ private val notes = arrayOf("C","C#","D","D#","E","F","F#","G","G#","A","A#","B")
18
18
 
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")
19
+ private var onChangePitch: (String, Double) -> Unit
24
20
 
25
21
  private var dispatcher: AudioDispatcher? = null
26
22
  private var processor: AudioProcessor? = null
27
-
28
23
  private var runner: Thread? = null
29
24
 
25
+ constructor(callback: (String, Double) -> Unit) {
26
+ this.onChangePitch = callback
27
+ }
28
+
30
29
  private val handler = PitchDetectionHandler { res, e ->
31
30
  val pitchInHz = res.pitch
32
31
  process(pitchInHz)
@@ -40,13 +39,14 @@ class PitchAnalyzer {
40
39
  }
41
40
 
42
41
  private fun process(pitchInHz: Float) {
43
- val index = round(12 * (log2(pitchInHz / 440) / log2(2f)) + 69) % 12
42
+ val octave = round(12 * (log2(pitchInHz / 440) / log2(2f)) + 69)
43
+ val index = octave % 12
44
44
 
45
45
  if (!index.isNaN() && pitchInHz > 0) {
46
- val tone = tones[index.toInt()]
46
+ val note = notes[index.toInt()]
47
47
  val frequency = pitchInHz.toDouble()
48
48
 
49
- this.onChangePitch(tone, frequency)
49
+ this.onChangePitch("$note$octave", frequency)
50
50
  }
51
51
  }
52
52
 
@@ -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
@@ -25,21 +22,6 @@ class ReactNativeSimpleNotePitchDetectorModule : Module() {
25
22
  }
26
23
 
27
24
  pitchAnalyzer.start()
28
-
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
- // }
43
25
  }
44
26
  }
45
27
  }
@@ -16,6 +16,7 @@ Pod::Spec.new do |s|
16
16
  s.static_framework = true
17
17
 
18
18
  s.dependency 'ExpoModulesCore'
19
+ s.dependency 'Beethoven', '~> 5.0'
19
20
 
20
21
  # Swift/Objective-C compatibility
21
22
  s.pod_target_xcconfig = {
@@ -1,25 +1,45 @@
1
1
  import ExpoModulesCore
2
+ import Beethoven
3
+ import Pitchy
2
4
 
3
5
  public class ReactNativeSimpleNotePitchDetectorModule: Module {
6
+
7
+ lazy var pitchEngine: PitchEngine = { [weak self] in
8
+ let config = Config(estimationStrategy: .yin)
9
+ let pitchEngine = PitchEngine(config: config, delegate: self)
10
+ return pitchEngine
11
+ }()
4
12
 
5
13
  public func definition() -> ModuleDefinition {
6
14
 
7
15
  Name("ReactNativeSimpleNotePitchDetector")
8
16
 
9
- Events("onChange")
17
+ Events("onChangePitch")
10
18
 
11
- Function("hello") {
12
- return "Hello world! 👋"
19
+ Function("start") {
20
+ pitchEngine.start()
13
21
  }
22
+ }
23
+ }
14
24
 
15
- // Defines a JavaScript function that always returns a Promise and whose native code
16
- // is by default dispatched on the different thread than the JavaScript runtime runs on.
17
- AsyncFunction("setValueAsync") { (value: String) in
18
- // Send an event to JavaScript.
19
- self.sendEvent("onChange", [
20
- "value": value
21
- ])
22
- }
25
+ extension ReactNativeSimpleNotePitchDetectorModule: PitchEngineDelegate {
26
+ public func pitchEngine(_ pitchEngine: PitchEngine, didReceivePitch pitch: Pitch) {
27
+
28
+ let offsetPercentage = pitch.closestOffset.percentage
29
+ let absOffsetPercentage = abs(offsetPercentage)
30
+
31
+ guard absOffsetPercentage > 1.0 else {
32
+ return
33
+ }
34
+
35
+ self.sendEvent("onChangePitch", ["note" : pitch.note.string, "frequency" : pitch.frequency])
36
+ }
37
+
38
+ public func pitchEngine(_ pitchEngine: PitchEngine, didReceiveError error: Error) {
39
+
40
+ }
23
41
 
42
+ public func pitchEngineWentBelowLevelThreshold(_ pitchEngine: PitchEngine) {
43
+
24
44
  }
25
45
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "react-native-simple-note-pitch-detector",
3
- "version": "0.1.7",
4
- "description": "asdf",
3
+ "version": "0.1.9",
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",
7
7
  "scripts": {