@gmessier/nitro-speech 0.3.2 → 0.4.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.
Files changed (120) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +165 -148
  3. package/android/build.gradle +0 -1
  4. package/android/src/main/cpp/cpp-adapter.cpp +5 -1
  5. package/android/src/main/java/com/margelo/nitro/nitrospeech/HybridNitroSpeech.kt +2 -0
  6. package/android/src/main/java/com/margelo/nitro/nitrospeech/recognizer/AutoStopper.kt +80 -16
  7. package/android/src/main/java/com/margelo/nitro/nitrospeech/recognizer/HybridRecognizer.kt +93 -20
  8. package/android/src/main/java/com/margelo/nitro/nitrospeech/recognizer/RecognitionListenerSession.kt +27 -15
  9. package/ios/{BufferUtil.swift → Audio/AudioBufferConverter.swift} +3 -34
  10. package/ios/Audio/AudioLevelTracker.swift +66 -0
  11. package/ios/Coordinator.swift +105 -0
  12. package/ios/Engines/AnalyzerEngine.swift +241 -0
  13. package/ios/Engines/DictationRuntime.swift +67 -0
  14. package/ios/Engines/RecognizerEngine.swift +312 -0
  15. package/ios/Engines/SFSpeechEngine.swift +119 -0
  16. package/ios/Engines/SpeechRuntime.swift +58 -0
  17. package/ios/Engines/TranscriberRuntimeProtocol.swift +21 -0
  18. package/ios/HybridNitroSpeech.swift +1 -10
  19. package/ios/HybridRecognizer.swift +135 -192
  20. package/ios/LocaleManager.swift +73 -0
  21. package/ios/{AppStateObserver.swift → Shared/AppStateObserver.swift} +1 -2
  22. package/ios/Shared/AutoStopper.swift +147 -0
  23. package/ios/Shared/HapticImpact.swift +24 -0
  24. package/ios/Shared/Log.swift +41 -0
  25. package/ios/Shared/Permissions.swift +59 -0
  26. package/ios/Shared/Utils.swift +58 -0
  27. package/lib/NitroSpeech.d.ts +2 -0
  28. package/lib/NitroSpeech.js +2 -0
  29. package/lib/Recognizer/RecognizerRef.d.ts +5 -0
  30. package/lib/Recognizer/RecognizerRef.js +13 -0
  31. package/lib/Recognizer/SpeechRecognizer.d.ts +8 -0
  32. package/lib/Recognizer/SpeechRecognizer.js +9 -0
  33. package/lib/Recognizer/methods.d.ts +8 -0
  34. package/lib/Recognizer/methods.js +29 -0
  35. package/lib/Recognizer/types.d.ts +6 -0
  36. package/lib/Recognizer/types.js +1 -0
  37. package/lib/Recognizer/useRecognizer.d.ts +16 -0
  38. package/lib/Recognizer/useRecognizer.js +71 -0
  39. package/lib/Recognizer/useVoiceInputVolume.d.ts +25 -0
  40. package/lib/Recognizer/useVoiceInputVolume.js +52 -0
  41. package/lib/index.d.ts +6 -0
  42. package/lib/index.js +6 -0
  43. package/lib/specs/NitroSpeech.nitro.d.ts +8 -0
  44. package/lib/specs/NitroSpeech.nitro.js +1 -0
  45. package/lib/specs/Recognizer.nitro.d.ts +95 -0
  46. package/lib/specs/Recognizer.nitro.js +1 -0
  47. package/lib/specs/SpeechRecognitionConfig.d.ts +162 -0
  48. package/lib/specs/SpeechRecognitionConfig.js +1 -0
  49. package/lib/specs/VolumeChangeEvent.d.ts +31 -0
  50. package/lib/specs/VolumeChangeEvent.js +1 -0
  51. package/nitro.json +2 -6
  52. package/nitrogen/generated/android/NitroSpeech+autolinking.cmake +2 -2
  53. package/nitrogen/generated/android/NitroSpeechOnLoad.cpp +5 -3
  54. package/nitrogen/generated/android/c++/JFunc_void_VolumeChangeEvent.hpp +78 -0
  55. package/nitrogen/generated/android/c++/JFunc_void_std__vector_std__string_.hpp +14 -14
  56. package/nitrogen/generated/android/c++/JHybridRecognizerSpec.cpp +68 -19
  57. package/nitrogen/generated/android/c++/JHybridRecognizerSpec.hpp +7 -4
  58. package/nitrogen/generated/android/c++/JIosPreset.hpp +58 -0
  59. package/nitrogen/generated/android/c++/JMutableSpeechRecognitionConfig.hpp +79 -0
  60. package/nitrogen/generated/android/c++/{JSpeechToTextParams.hpp → JSpeechRecognitionConfig.hpp} +48 -30
  61. package/nitrogen/generated/android/c++/JVolumeChangeEvent.hpp +65 -0
  62. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrospeech/Func_void_VolumeChangeEvent.kt +80 -0
  63. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrospeech/HybridRecognizerSpec.kt +18 -5
  64. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrospeech/IosPreset.kt +23 -0
  65. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrospeech/MutableSpeechRecognitionConfig.kt +76 -0
  66. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrospeech/SpeechRecognitionConfig.kt +121 -0
  67. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrospeech/VolumeChangeEvent.kt +61 -0
  68. package/nitrogen/generated/ios/NitroSpeech-Swift-Cxx-Bridge.cpp +46 -30
  69. package/nitrogen/generated/ios/NitroSpeech-Swift-Cxx-Bridge.hpp +203 -70
  70. package/nitrogen/generated/ios/NitroSpeech-Swift-Cxx-Umbrella.hpp +13 -3
  71. package/nitrogen/generated/ios/NitroSpeechAutolinking.swift +2 -2
  72. package/nitrogen/generated/ios/c++/HybridRecognizerSpecSwift.hpp +41 -9
  73. package/nitrogen/generated/ios/swift/Func_void_VolumeChangeEvent.swift +46 -0
  74. package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +46 -0
  75. package/nitrogen/generated/ios/swift/HybridRecognizerSpec.swift +6 -3
  76. package/nitrogen/generated/ios/swift/HybridRecognizerSpec_cxx.swift +66 -18
  77. package/nitrogen/generated/ios/swift/IosPreset.swift +40 -0
  78. package/nitrogen/generated/ios/swift/MutableSpeechRecognitionConfig.swift +118 -0
  79. package/nitrogen/generated/ios/swift/{SpeechToTextParams.swift → SpeechRecognitionConfig.swift} +108 -43
  80. package/nitrogen/generated/ios/swift/VolumeChangeEvent.swift +52 -0
  81. package/nitrogen/generated/shared/c++/HybridRecognizerSpec.cpp +4 -1
  82. package/nitrogen/generated/shared/c++/HybridRecognizerSpec.hpp +17 -7
  83. package/nitrogen/generated/shared/c++/IosPreset.hpp +76 -0
  84. package/nitrogen/generated/shared/c++/MutableSpeechRecognitionConfig.hpp +105 -0
  85. package/nitrogen/generated/shared/c++/{SpeechToTextParams.hpp → SpeechRecognitionConfig.hpp} +39 -20
  86. package/nitrogen/generated/shared/c++/VolumeChangeEvent.hpp +91 -0
  87. package/package.json +15 -16
  88. package/src/NitroSpeech.ts +5 -0
  89. package/src/Recognizer/RecognizerRef.ts +23 -0
  90. package/src/Recognizer/SpeechRecognizer.ts +10 -0
  91. package/src/Recognizer/methods.ts +40 -0
  92. package/src/Recognizer/types.ts +33 -0
  93. package/src/Recognizer/useRecognizer.ts +85 -0
  94. package/src/Recognizer/useVoiceInputVolume.ts +65 -0
  95. package/src/index.ts +6 -182
  96. package/src/specs/NitroSpeech.nitro.ts +2 -163
  97. package/src/specs/Recognizer.nitro.ts +110 -0
  98. package/src/specs/SpeechRecognitionConfig.ts +167 -0
  99. package/src/specs/VolumeChangeEvent.ts +31 -0
  100. package/android/proguard-rules.pro +0 -1
  101. package/ios/AnylyzerTranscriber.swift +0 -331
  102. package/ios/AutoStopper.swift +0 -69
  103. package/ios/HapticImpact.swift +0 -32
  104. package/ios/LegacySpeechRecognizer.swift +0 -161
  105. package/lib/commonjs/index.js +0 -145
  106. package/lib/commonjs/index.js.map +0 -1
  107. package/lib/commonjs/package.json +0 -1
  108. package/lib/commonjs/specs/NitroSpeech.nitro.js +0 -6
  109. package/lib/commonjs/specs/NitroSpeech.nitro.js.map +0 -1
  110. package/lib/module/index.js +0 -138
  111. package/lib/module/index.js.map +0 -1
  112. package/lib/module/package.json +0 -1
  113. package/lib/module/specs/NitroSpeech.nitro.js +0 -4
  114. package/lib/module/specs/NitroSpeech.nitro.js.map +0 -1
  115. package/lib/tsconfig.tsbuildinfo +0 -1
  116. package/lib/typescript/index.d.ts +0 -50
  117. package/lib/typescript/index.d.ts.map +0 -1
  118. package/lib/typescript/specs/NitroSpeech.nitro.d.ts +0 -162
  119. package/lib/typescript/specs/NitroSpeech.nitro.d.ts.map +0 -1
  120. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrospeech/SpeechToTextParams.kt +0 -68
@@ -1,5 +1,5 @@
1
1
  ///
2
- /// SpeechToTextParams.hpp
2
+ /// SpeechRecognitionConfig.hpp
3
3
  /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
4
  /// https://github.com/mrousavy/nitro
5
5
  /// Copyright © Marc Rousavy @ Margelo
@@ -30,75 +30,90 @@
30
30
 
31
31
  // Forward declaration of `HapticFeedbackStyle` to properly resolve imports.
32
32
  namespace margelo::nitro::nitrospeech { enum class HapticFeedbackStyle; }
33
+ // Forward declaration of `IosPreset` to properly resolve imports.
34
+ namespace margelo::nitro::nitrospeech { enum class IosPreset; }
33
35
 
34
36
  #include <string>
35
37
  #include <optional>
36
38
  #include <vector>
37
39
  #include "HapticFeedbackStyle.hpp"
40
+ #include "IosPreset.hpp"
38
41
 
39
42
  namespace margelo::nitro::nitrospeech {
40
43
 
41
44
  /**
42
- * A struct which can be represented as a JavaScript object (SpeechToTextParams).
45
+ * A struct which can be represented as a JavaScript object (SpeechRecognitionConfig).
43
46
  */
44
- struct SpeechToTextParams final {
47
+ struct SpeechRecognitionConfig final {
45
48
  public:
46
49
  std::optional<std::string> locale SWIFT_PRIVATE;
50
+ std::optional<std::vector<std::string>> contextualStrings SWIFT_PRIVATE;
51
+ std::optional<bool> maskOffensiveWords SWIFT_PRIVATE;
47
52
  std::optional<double> autoFinishRecognitionMs SWIFT_PRIVATE;
53
+ std::optional<double> autoFinishProgressIntervalMs SWIFT_PRIVATE;
54
+ std::optional<double> resetAutoFinishVoiceSensitivity SWIFT_PRIVATE;
48
55
  std::optional<bool> disableRepeatingFilter SWIFT_PRIVATE;
49
- std::optional<std::vector<std::string>> contextualStrings SWIFT_PRIVATE;
50
56
  std::optional<HapticFeedbackStyle> startHapticFeedbackStyle SWIFT_PRIVATE;
51
57
  std::optional<HapticFeedbackStyle> stopHapticFeedbackStyle SWIFT_PRIVATE;
52
- std::optional<bool> maskOffensiveWords SWIFT_PRIVATE;
53
58
  std::optional<bool> androidFormattingPreferQuality SWIFT_PRIVATE;
54
59
  std::optional<bool> androidUseWebSearchModel SWIFT_PRIVATE;
55
60
  std::optional<bool> androidDisableBatchHandling SWIFT_PRIVATE;
56
61
  std::optional<bool> iosAddPunctuation SWIFT_PRIVATE;
62
+ std::optional<IosPreset> iosPreset SWIFT_PRIVATE;
63
+ std::optional<bool> iosAtypicalSpeech SWIFT_PRIVATE;
57
64
 
58
65
  public:
59
- SpeechToTextParams() = default;
60
- explicit SpeechToTextParams(std::optional<std::string> locale, std::optional<double> autoFinishRecognitionMs, std::optional<bool> disableRepeatingFilter, std::optional<std::vector<std::string>> contextualStrings, std::optional<HapticFeedbackStyle> startHapticFeedbackStyle, std::optional<HapticFeedbackStyle> stopHapticFeedbackStyle, std::optional<bool> maskOffensiveWords, std::optional<bool> androidFormattingPreferQuality, std::optional<bool> androidUseWebSearchModel, std::optional<bool> androidDisableBatchHandling, std::optional<bool> iosAddPunctuation): locale(locale), autoFinishRecognitionMs(autoFinishRecognitionMs), disableRepeatingFilter(disableRepeatingFilter), contextualStrings(contextualStrings), startHapticFeedbackStyle(startHapticFeedbackStyle), stopHapticFeedbackStyle(stopHapticFeedbackStyle), maskOffensiveWords(maskOffensiveWords), androidFormattingPreferQuality(androidFormattingPreferQuality), androidUseWebSearchModel(androidUseWebSearchModel), androidDisableBatchHandling(androidDisableBatchHandling), iosAddPunctuation(iosAddPunctuation) {}
66
+ SpeechRecognitionConfig() = default;
67
+ explicit SpeechRecognitionConfig(std::optional<std::string> locale, std::optional<std::vector<std::string>> contextualStrings, std::optional<bool> maskOffensiveWords, std::optional<double> autoFinishRecognitionMs, std::optional<double> autoFinishProgressIntervalMs, std::optional<double> resetAutoFinishVoiceSensitivity, std::optional<bool> disableRepeatingFilter, std::optional<HapticFeedbackStyle> startHapticFeedbackStyle, std::optional<HapticFeedbackStyle> stopHapticFeedbackStyle, std::optional<bool> androidFormattingPreferQuality, std::optional<bool> androidUseWebSearchModel, std::optional<bool> androidDisableBatchHandling, std::optional<bool> iosAddPunctuation, std::optional<IosPreset> iosPreset, std::optional<bool> iosAtypicalSpeech): locale(locale), contextualStrings(contextualStrings), maskOffensiveWords(maskOffensiveWords), autoFinishRecognitionMs(autoFinishRecognitionMs), autoFinishProgressIntervalMs(autoFinishProgressIntervalMs), resetAutoFinishVoiceSensitivity(resetAutoFinishVoiceSensitivity), disableRepeatingFilter(disableRepeatingFilter), startHapticFeedbackStyle(startHapticFeedbackStyle), stopHapticFeedbackStyle(stopHapticFeedbackStyle), androidFormattingPreferQuality(androidFormattingPreferQuality), androidUseWebSearchModel(androidUseWebSearchModel), androidDisableBatchHandling(androidDisableBatchHandling), iosAddPunctuation(iosAddPunctuation), iosPreset(iosPreset), iosAtypicalSpeech(iosAtypicalSpeech) {}
61
68
 
62
69
  public:
63
- friend bool operator==(const SpeechToTextParams& lhs, const SpeechToTextParams& rhs) = default;
70
+ friend bool operator==(const SpeechRecognitionConfig& lhs, const SpeechRecognitionConfig& rhs) = default;
64
71
  };
65
72
 
66
73
  } // namespace margelo::nitro::nitrospeech
67
74
 
68
75
  namespace margelo::nitro {
69
76
 
70
- // C++ SpeechToTextParams <> JS SpeechToTextParams (object)
77
+ // C++ SpeechRecognitionConfig <> JS SpeechRecognitionConfig (object)
71
78
  template <>
72
- struct JSIConverter<margelo::nitro::nitrospeech::SpeechToTextParams> final {
73
- static inline margelo::nitro::nitrospeech::SpeechToTextParams fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
79
+ struct JSIConverter<margelo::nitro::nitrospeech::SpeechRecognitionConfig> final {
80
+ static inline margelo::nitro::nitrospeech::SpeechRecognitionConfig fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
74
81
  jsi::Object obj = arg.asObject(runtime);
75
- return margelo::nitro::nitrospeech::SpeechToTextParams(
82
+ return margelo::nitro::nitrospeech::SpeechRecognitionConfig(
76
83
  JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "locale"))),
84
+ JSIConverter<std::optional<std::vector<std::string>>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "contextualStrings"))),
85
+ JSIConverter<std::optional<bool>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "maskOffensiveWords"))),
77
86
  JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "autoFinishRecognitionMs"))),
87
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "autoFinishProgressIntervalMs"))),
88
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "resetAutoFinishVoiceSensitivity"))),
78
89
  JSIConverter<std::optional<bool>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "disableRepeatingFilter"))),
79
- JSIConverter<std::optional<std::vector<std::string>>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "contextualStrings"))),
80
90
  JSIConverter<std::optional<margelo::nitro::nitrospeech::HapticFeedbackStyle>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "startHapticFeedbackStyle"))),
81
91
  JSIConverter<std::optional<margelo::nitro::nitrospeech::HapticFeedbackStyle>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "stopHapticFeedbackStyle"))),
82
- JSIConverter<std::optional<bool>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "maskOffensiveWords"))),
83
92
  JSIConverter<std::optional<bool>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "androidFormattingPreferQuality"))),
84
93
  JSIConverter<std::optional<bool>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "androidUseWebSearchModel"))),
85
94
  JSIConverter<std::optional<bool>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "androidDisableBatchHandling"))),
86
- JSIConverter<std::optional<bool>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "iosAddPunctuation")))
95
+ JSIConverter<std::optional<bool>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "iosAddPunctuation"))),
96
+ JSIConverter<std::optional<margelo::nitro::nitrospeech::IosPreset>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "iosPreset"))),
97
+ JSIConverter<std::optional<bool>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "iosAtypicalSpeech")))
87
98
  );
88
99
  }
89
- static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::nitrospeech::SpeechToTextParams& arg) {
100
+ static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::nitrospeech::SpeechRecognitionConfig& arg) {
90
101
  jsi::Object obj(runtime);
91
102
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "locale"), JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.locale));
103
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "contextualStrings"), JSIConverter<std::optional<std::vector<std::string>>>::toJSI(runtime, arg.contextualStrings));
104
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "maskOffensiveWords"), JSIConverter<std::optional<bool>>::toJSI(runtime, arg.maskOffensiveWords));
92
105
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "autoFinishRecognitionMs"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.autoFinishRecognitionMs));
106
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "autoFinishProgressIntervalMs"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.autoFinishProgressIntervalMs));
107
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "resetAutoFinishVoiceSensitivity"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.resetAutoFinishVoiceSensitivity));
93
108
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "disableRepeatingFilter"), JSIConverter<std::optional<bool>>::toJSI(runtime, arg.disableRepeatingFilter));
94
- obj.setProperty(runtime, PropNameIDCache::get(runtime, "contextualStrings"), JSIConverter<std::optional<std::vector<std::string>>>::toJSI(runtime, arg.contextualStrings));
95
109
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "startHapticFeedbackStyle"), JSIConverter<std::optional<margelo::nitro::nitrospeech::HapticFeedbackStyle>>::toJSI(runtime, arg.startHapticFeedbackStyle));
96
110
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "stopHapticFeedbackStyle"), JSIConverter<std::optional<margelo::nitro::nitrospeech::HapticFeedbackStyle>>::toJSI(runtime, arg.stopHapticFeedbackStyle));
97
- obj.setProperty(runtime, PropNameIDCache::get(runtime, "maskOffensiveWords"), JSIConverter<std::optional<bool>>::toJSI(runtime, arg.maskOffensiveWords));
98
111
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "androidFormattingPreferQuality"), JSIConverter<std::optional<bool>>::toJSI(runtime, arg.androidFormattingPreferQuality));
99
112
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "androidUseWebSearchModel"), JSIConverter<std::optional<bool>>::toJSI(runtime, arg.androidUseWebSearchModel));
100
113
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "androidDisableBatchHandling"), JSIConverter<std::optional<bool>>::toJSI(runtime, arg.androidDisableBatchHandling));
101
114
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "iosAddPunctuation"), JSIConverter<std::optional<bool>>::toJSI(runtime, arg.iosAddPunctuation));
115
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "iosPreset"), JSIConverter<std::optional<margelo::nitro::nitrospeech::IosPreset>>::toJSI(runtime, arg.iosPreset));
116
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "iosAtypicalSpeech"), JSIConverter<std::optional<bool>>::toJSI(runtime, arg.iosAtypicalSpeech));
102
117
  return obj;
103
118
  }
104
119
  static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
@@ -110,16 +125,20 @@ namespace margelo::nitro {
110
125
  return false;
111
126
  }
112
127
  if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "locale")))) return false;
128
+ if (!JSIConverter<std::optional<std::vector<std::string>>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "contextualStrings")))) return false;
129
+ if (!JSIConverter<std::optional<bool>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "maskOffensiveWords")))) return false;
113
130
  if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "autoFinishRecognitionMs")))) return false;
131
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "autoFinishProgressIntervalMs")))) return false;
132
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "resetAutoFinishVoiceSensitivity")))) return false;
114
133
  if (!JSIConverter<std::optional<bool>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "disableRepeatingFilter")))) return false;
115
- if (!JSIConverter<std::optional<std::vector<std::string>>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "contextualStrings")))) return false;
116
134
  if (!JSIConverter<std::optional<margelo::nitro::nitrospeech::HapticFeedbackStyle>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "startHapticFeedbackStyle")))) return false;
117
135
  if (!JSIConverter<std::optional<margelo::nitro::nitrospeech::HapticFeedbackStyle>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "stopHapticFeedbackStyle")))) return false;
118
- if (!JSIConverter<std::optional<bool>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "maskOffensiveWords")))) return false;
119
136
  if (!JSIConverter<std::optional<bool>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "androidFormattingPreferQuality")))) return false;
120
137
  if (!JSIConverter<std::optional<bool>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "androidUseWebSearchModel")))) return false;
121
138
  if (!JSIConverter<std::optional<bool>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "androidDisableBatchHandling")))) return false;
122
139
  if (!JSIConverter<std::optional<bool>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "iosAddPunctuation")))) return false;
140
+ if (!JSIConverter<std::optional<margelo::nitro::nitrospeech::IosPreset>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "iosPreset")))) return false;
141
+ if (!JSIConverter<std::optional<bool>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "iosAtypicalSpeech")))) return false;
123
142
  return true;
124
143
  }
125
144
  };
@@ -0,0 +1,91 @@
1
+ ///
2
+ /// VolumeChangeEvent.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ #if __has_include(<NitroModules/JSIConverter.hpp>)
11
+ #include <NitroModules/JSIConverter.hpp>
12
+ #else
13
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
14
+ #endif
15
+ #if __has_include(<NitroModules/NitroDefines.hpp>)
16
+ #include <NitroModules/NitroDefines.hpp>
17
+ #else
18
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
19
+ #endif
20
+ #if __has_include(<NitroModules/JSIHelpers.hpp>)
21
+ #include <NitroModules/JSIHelpers.hpp>
22
+ #else
23
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
24
+ #endif
25
+ #if __has_include(<NitroModules/PropNameIDCache.hpp>)
26
+ #include <NitroModules/PropNameIDCache.hpp>
27
+ #else
28
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
29
+ #endif
30
+
31
+
32
+
33
+ #include <optional>
34
+
35
+ namespace margelo::nitro::nitrospeech {
36
+
37
+ /**
38
+ * A struct which can be represented as a JavaScript object (VolumeChangeEvent).
39
+ */
40
+ struct VolumeChangeEvent final {
41
+ public:
42
+ double smoothedVolume SWIFT_PRIVATE;
43
+ double rawVolume SWIFT_PRIVATE;
44
+ std::optional<double> db SWIFT_PRIVATE;
45
+
46
+ public:
47
+ VolumeChangeEvent() = default;
48
+ explicit VolumeChangeEvent(double smoothedVolume, double rawVolume, std::optional<double> db): smoothedVolume(smoothedVolume), rawVolume(rawVolume), db(db) {}
49
+
50
+ public:
51
+ friend bool operator==(const VolumeChangeEvent& lhs, const VolumeChangeEvent& rhs) = default;
52
+ };
53
+
54
+ } // namespace margelo::nitro::nitrospeech
55
+
56
+ namespace margelo::nitro {
57
+
58
+ // C++ VolumeChangeEvent <> JS VolumeChangeEvent (object)
59
+ template <>
60
+ struct JSIConverter<margelo::nitro::nitrospeech::VolumeChangeEvent> final {
61
+ static inline margelo::nitro::nitrospeech::VolumeChangeEvent fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
62
+ jsi::Object obj = arg.asObject(runtime);
63
+ return margelo::nitro::nitrospeech::VolumeChangeEvent(
64
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "smoothedVolume"))),
65
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "rawVolume"))),
66
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "db")))
67
+ );
68
+ }
69
+ static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::nitrospeech::VolumeChangeEvent& arg) {
70
+ jsi::Object obj(runtime);
71
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "smoothedVolume"), JSIConverter<double>::toJSI(runtime, arg.smoothedVolume));
72
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "rawVolume"), JSIConverter<double>::toJSI(runtime, arg.rawVolume));
73
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "db"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.db));
74
+ return obj;
75
+ }
76
+ static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
77
+ if (!value.isObject()) {
78
+ return false;
79
+ }
80
+ jsi::Object obj = value.getObject(runtime);
81
+ if (!nitro::isPlainObject(runtime, obj)) {
82
+ return false;
83
+ }
84
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "smoothedVolume")))) return false;
85
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "rawVolume")))) return false;
86
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "db")))) return false;
87
+ return true;
88
+ }
89
+ };
90
+
91
+ } // namespace margelo::nitro
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@gmessier/nitro-speech",
3
- "version": "0.3.2",
4
- "description": "React Native Speech Recognition Library powered by Nitro Modules",
5
- "main": "./lib/commonjs/index.js",
6
- "module": "./lib/module/index.js",
7
- "types": "./lib/typescript/index.d.ts",
8
- "react-native": "./lib/module/index.js",
9
- "source": "src/index.ts",
3
+ "version": "0.4.0",
4
+ "description": "React Native Real-time Speech Recognition Library powered by Nitro Modules",
5
+ "main": "./lib/index.js",
6
+ "module": "./lib/index.js",
7
+ "types": "./lib/index.d.ts",
8
+ "react-native": "./src/index.ts",
9
+ "source": "./src/index.ts",
10
10
  "files": [
11
11
  "src",
12
12
  "react-native.config.js",
@@ -15,7 +15,6 @@
15
15
  "android/build.gradle",
16
16
  "android/gradle.properties",
17
17
  "android/fix-prefab.gradle",
18
- "android/proguard-rules.pro",
19
18
  "android/CMakeLists.txt",
20
19
  "android/src",
21
20
  "ios/**/*.h",
@@ -26,16 +25,15 @@
26
25
  "app.plugin.js",
27
26
  "nitro.json",
28
27
  "*.podspec",
29
- "README.md"
28
+ "README.md",
29
+ "LICENSE"
30
30
  ],
31
31
  "scripts": {
32
32
  "typecheck": "tsc --noEmit",
33
- "clean": "rm -rf android/build node_modules/**/android/build lib",
33
+ "build": "rm -rf lib && rm -f tsconfig.tsbuildinfo && bun tsc",
34
34
  "lint": "eslint \"**/*.{js,ts,tsx}\" --fix",
35
- "lint-ci": "eslint \"**/*.{js,ts,tsx}\" -f @jamesacarr/github-actions",
36
- "typescript": "tsc",
37
- "build": "rm -rf lib && npm run typecheck && bob build",
38
- "specs": "tsc --noEmit false && nitrogen --logLevel=\"debug\""
35
+ "nitro": "rm -rf nitrogen && npx nitrogen@latest",
36
+ "localnitro": "rm -rf nitrogen && node ../../../nitro/nitro/packages/nitrogen/lib/index.js"
39
37
  },
40
38
  "keywords": [
41
39
  "react-native",
@@ -50,6 +48,7 @@
50
48
  "voice",
51
49
  "voice-recognition",
52
50
  "transcription",
51
+ "transcriber",
53
52
  "audio",
54
53
  "microphone",
55
54
  "nitro",
@@ -81,8 +80,8 @@
81
80
  "prettier": "^3.3.3",
82
81
  "react": "19.2.4",
83
82
  "react-native": "0.84.1",
84
- "react-native-nitro-modules": "0.35.4",
85
- "nitrogen": "0.35.4",
83
+ "react-native-nitro-modules": "0.35.6",
84
+ "nitrogen": "0.35.6",
86
85
  "react-native-builder-bob": "0.37.0",
87
86
  "typescript": "^5.8.3"
88
87
  },
@@ -0,0 +1,5 @@
1
+ import { NitroModules } from 'react-native-nitro-modules'
2
+ import type { NitroSpeech as NitroSpeechSpec } from './specs/NitroSpeech.nitro'
3
+
4
+ export const NitroSpeech =
5
+ NitroModules.createHybridObject<NitroSpeechSpec>('NitroSpeech')
@@ -0,0 +1,23 @@
1
+ import type { RecognizerMethods } from './types'
2
+ import {
3
+ recognizerAddAutoFinishTime,
4
+ recognizerGetSupportedLocalesIOS,
5
+ recognizerGetIsActive,
6
+ recognizerResetAutoFinishTime,
7
+ recognizerStartListening,
8
+ recognizerStopListening,
9
+ recognizerUpdateConfig,
10
+ } from './methods'
11
+
12
+ /**
13
+ * Safe cross-component reference to the Speech Recognizer methods.
14
+ */
15
+ export const RecognizerRef: RecognizerMethods = {
16
+ startListening: recognizerStartListening,
17
+ stopListening: recognizerStopListening,
18
+ resetAutoFinishTime: recognizerResetAutoFinishTime,
19
+ addAutoFinishTime: recognizerAddAutoFinishTime,
20
+ updateConfig: recognizerUpdateConfig,
21
+ getIsActive: recognizerGetIsActive,
22
+ getSupportedLocalesIOS: recognizerGetSupportedLocalesIOS,
23
+ }
@@ -0,0 +1,10 @@
1
+ import { NitroSpeech } from '../NitroSpeech'
2
+
3
+ /**
4
+ * Static Speech Recognizer instance.
5
+ *
6
+ * Direct access to the all Speech Recognizer methods and callbacks.
7
+ *
8
+ * @note unsafe, might lead to race conditions
9
+ */
10
+ export const SpeechRecognizer = NitroSpeech.recognizer
@@ -0,0 +1,40 @@
1
+ import { SpeechRecognizer } from './SpeechRecognizer'
2
+ import type { SpeechRecognitionConfig } from './types'
3
+
4
+ export const recognizerStartListening = (params: SpeechRecognitionConfig) => {
5
+ 'worklet'
6
+ SpeechRecognizer.startListening(params)
7
+ }
8
+
9
+ export const recognizerStopListening = () => {
10
+ 'worklet'
11
+ SpeechRecognizer.stopListening()
12
+ }
13
+
14
+ export const recognizerResetAutoFinishTime = () => {
15
+ 'worklet'
16
+ SpeechRecognizer.resetAutoFinishTime()
17
+ }
18
+
19
+ export const recognizerAddAutoFinishTime = (additionalTimeMs?: number) => {
20
+ 'worklet'
21
+ SpeechRecognizer.addAutoFinishTime(additionalTimeMs)
22
+ }
23
+
24
+ export const recognizerUpdateConfig = (
25
+ newConfig: SpeechRecognitionConfig,
26
+ resetAutoFinishTime?: boolean
27
+ ) => {
28
+ 'worklet'
29
+ SpeechRecognizer.updateConfig(newConfig, resetAutoFinishTime)
30
+ }
31
+
32
+ export const recognizerGetIsActive = () => {
33
+ 'worklet'
34
+ return SpeechRecognizer.getIsActive()
35
+ }
36
+
37
+ export const recognizerGetSupportedLocalesIOS = () => {
38
+ 'worklet'
39
+ return SpeechRecognizer.getSupportedLocalesIOS().sort()
40
+ }
@@ -0,0 +1,33 @@
1
+ import type { Recognizer as RecognizerSpec } from '../specs/Recognizer.nitro'
2
+ import type { SpeechRecognitionConfig } from '../specs/SpeechRecognitionConfig'
3
+ import type { VolumeChangeEvent } from '../specs/VolumeChangeEvent'
4
+
5
+ type RecognizerCallbacks = Pick<
6
+ RecognizerSpec,
7
+ | 'onReadyForSpeech'
8
+ | 'onRecordingStopped'
9
+ | 'onResult'
10
+ | 'onAutoFinishProgress'
11
+ | 'onError'
12
+ | 'onPermissionDenied'
13
+ | 'onVolumeChange'
14
+ >
15
+
16
+ type RecognizerMethods = Pick<
17
+ RecognizerSpec,
18
+ | 'startListening'
19
+ | 'stopListening'
20
+ | 'resetAutoFinishTime'
21
+ | 'addAutoFinishTime'
22
+ | 'updateConfig'
23
+ | 'getIsActive'
24
+ | 'getSupportedLocalesIOS'
25
+ >
26
+
27
+ export type {
28
+ RecognizerSpec,
29
+ SpeechRecognitionConfig,
30
+ VolumeChangeEvent,
31
+ RecognizerCallbacks,
32
+ RecognizerMethods,
33
+ }
@@ -0,0 +1,85 @@
1
+ import { useEffect, type DependencyList } from 'react'
2
+ import {
3
+ recognizerResetAutoFinishTime,
4
+ recognizerAddAutoFinishTime,
5
+ recognizerUpdateConfig,
6
+ recognizerGetIsActive,
7
+ recognizerGetSupportedLocalesIOS,
8
+ recognizerStartListening,
9
+ recognizerStopListening,
10
+ } from './methods'
11
+ import type { RecognizerCallbacks, RecognizerMethods } from './types'
12
+ import { SpeechRecognizer } from './SpeechRecognizer'
13
+ import { speechRecognizerVolumeChangeHandler } from './useVoiceInputVolume'
14
+
15
+ /**
16
+ * Safe, lifecycle-aware hook to use the recognizer.
17
+ *
18
+ * @param callbacks - The callbacks to use for the recognizer.
19
+ * @param destroyDeps - The additional dependencies to use for the cleanup effect.
20
+ *
21
+ * Example: To cleanup when the screen is unfocused.
22
+ *
23
+ * ```typescript
24
+ * const isFocused = useIsFocused()
25
+ * useRecognizer({ ... }, [isFocused])
26
+ * ```
27
+ */
28
+ export const useRecognizer = (
29
+ callbacks: RecognizerCallbacks,
30
+ destroyDeps: DependencyList = []
31
+ ): RecognizerMethods => {
32
+ useEffect(() => {
33
+ if (callbacks.onVolumeChange) {
34
+ SpeechRecognizer.onVolumeChange = (event) => {
35
+ callbacks.onVolumeChange?.(event)
36
+ }
37
+ } else {
38
+ SpeechRecognizer.onVolumeChange = speechRecognizerVolumeChangeHandler
39
+ }
40
+ SpeechRecognizer.onReadyForSpeech = () => {
41
+ callbacks.onReadyForSpeech?.()
42
+ }
43
+ SpeechRecognizer.onRecordingStopped = () => {
44
+ callbacks.onRecordingStopped?.()
45
+ }
46
+ SpeechRecognizer.onResult = (resultBatches: string[]) => {
47
+ callbacks.onResult?.(resultBatches)
48
+ }
49
+ SpeechRecognizer.onAutoFinishProgress = (timeLeftMs: number) => {
50
+ callbacks.onAutoFinishProgress?.(timeLeftMs)
51
+ }
52
+ SpeechRecognizer.onError = (message: string) => {
53
+ callbacks.onError?.(message)
54
+ }
55
+ SpeechRecognizer.onPermissionDenied = () => {
56
+ callbacks.onPermissionDenied?.()
57
+ }
58
+ return () => {
59
+ SpeechRecognizer.onReadyForSpeech = undefined
60
+ SpeechRecognizer.onRecordingStopped = undefined
61
+ SpeechRecognizer.onResult = undefined
62
+ SpeechRecognizer.onAutoFinishProgress = undefined
63
+ SpeechRecognizer.onError = undefined
64
+ SpeechRecognizer.onPermissionDenied = undefined
65
+ SpeechRecognizer.onVolumeChange = undefined
66
+ }
67
+ }, [callbacks])
68
+
69
+ useEffect(() => {
70
+ return () => {
71
+ SpeechRecognizer.stopListening()
72
+ }
73
+ // eslint-disable-next-line react-hooks/exhaustive-deps
74
+ }, [...destroyDeps])
75
+
76
+ return {
77
+ startListening: recognizerStartListening,
78
+ stopListening: recognizerStopListening,
79
+ resetAutoFinishTime: recognizerResetAutoFinishTime,
80
+ addAutoFinishTime: recognizerAddAutoFinishTime,
81
+ updateConfig: recognizerUpdateConfig,
82
+ getIsActive: recognizerGetIsActive,
83
+ getSupportedLocalesIOS: recognizerGetSupportedLocalesIOS,
84
+ }
85
+ }
@@ -0,0 +1,65 @@
1
+ import { useSyncExternalStore } from 'react'
2
+ import type { RecognizerSpec, VolumeChangeEvent } from './types'
3
+
4
+ type OnVolumeChange = RecognizerSpec['onVolumeChange']
5
+
6
+ const subscribers = new Set<OnVolumeChange>()
7
+
8
+ let current: VolumeChangeEvent = {
9
+ smoothedVolume: 0,
10
+ rawVolume: 0,
11
+ db: undefined,
12
+ }
13
+
14
+ let snapshot: VolumeChangeEvent = { ...current }
15
+
16
+ const getSnapshot = () => {
17
+ if (
18
+ snapshot.smoothedVolume === current.smoothedVolume &&
19
+ snapshot.rawVolume === current.rawVolume &&
20
+ snapshot.db === current.db
21
+ ) {
22
+ return snapshot
23
+ }
24
+ snapshot = { ...current }
25
+ return snapshot
26
+ }
27
+
28
+ /**
29
+ * Subscription to the voice input volume changes
30
+ *
31
+ * Updates with arbitrary frequency (many times per second) while audio recording is active.
32
+ *
33
+ * @returns The current voice input volume normalized to a range of 0 to 1.
34
+ */
35
+ export const useVoiceInputVolume = () => {
36
+ return useSyncExternalStore((subscriber) => {
37
+ subscribers.add(subscriber)
38
+ return () => subscribers.delete(subscriber)
39
+ }, getSnapshot)
40
+ }
41
+
42
+ /**
43
+ * Direct access to default Speech Recognizer volume change handler.
44
+ *
45
+ * In case you use static Speech Recognizer:
46
+ *
47
+ * ```typescript
48
+ * import { speechRecognizerVolumeChangeHandler } from '@gmessier/nitro-speech'
49
+ *
50
+ * SpeechRecognizer.onVolumeChange = speechRecognizerVolumeChangeHandler
51
+ * ... // setup everything else
52
+ * SpeechRecognizer.startListening({ locale: 'en-US' })
53
+ * ```
54
+ */
55
+ export const speechRecognizerVolumeChangeHandler: OnVolumeChange = (event) => {
56
+ if (
57
+ event.smoothedVolume === current.smoothedVolume &&
58
+ event.rawVolume === current.rawVolume &&
59
+ event.db === current.db
60
+ ) {
61
+ return
62
+ }
63
+ current = event
64
+ subscribers.forEach((subscriber) => subscriber?.(event))
65
+ }