@gmessier/nitro-speech 0.3.3 → 0.4.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.
- package/LICENSE +21 -0
- package/README.md +176 -148
- package/android/build.gradle +0 -1
- package/android/src/main/cpp/cpp-adapter.cpp +5 -1
- package/android/src/main/java/com/margelo/nitro/nitrospeech/HybridNitroSpeech.kt +2 -0
- package/android/src/main/java/com/margelo/nitro/nitrospeech/recognizer/AutoStopper.kt +82 -18
- package/android/src/main/java/com/margelo/nitro/nitrospeech/recognizer/HybridRecognizer.kt +118 -30
- package/android/src/main/java/com/margelo/nitro/nitrospeech/recognizer/Logger.kt +16 -0
- package/android/src/main/java/com/margelo/nitro/nitrospeech/recognizer/RecognitionListenerSession.kt +35 -24
- package/ios/{BufferUtil.swift → Audio/AudioBufferConverter.swift} +3 -34
- package/ios/Audio/AudioLevelTracker.swift +60 -0
- package/ios/Coordinator.swift +105 -0
- package/ios/Engines/AnalyzerEngine.swift +241 -0
- package/ios/Engines/DictationRuntime.swift +67 -0
- package/ios/Engines/RecognizerEngine.swift +315 -0
- package/ios/Engines/SFSpeechEngine.swift +119 -0
- package/ios/Engines/SpeechRuntime.swift +58 -0
- package/ios/Engines/TranscriberRuntimeProtocol.swift +21 -0
- package/ios/HybridNitroSpeech.swift +1 -10
- package/ios/HybridRecognizer.swift +142 -191
- package/ios/LocaleManager.swift +73 -0
- package/ios/{AppStateObserver.swift → Shared/AppStateObserver.swift} +1 -2
- package/ios/Shared/AutoStopper.swift +147 -0
- package/ios/Shared/HapticImpact.swift +24 -0
- package/ios/Shared/Log.swift +41 -0
- package/ios/Shared/Permissions.swift +59 -0
- package/ios/Shared/Utils.swift +58 -0
- package/lib/NitroSpeech.d.ts +2 -0
- package/lib/NitroSpeech.js +2 -0
- package/lib/Recognizer/RecognizerRef.d.ts +7 -0
- package/lib/Recognizer/RecognizerRef.js +16 -0
- package/lib/Recognizer/SpeechRecognizer.d.ts +8 -0
- package/lib/Recognizer/SpeechRecognizer.js +9 -0
- package/lib/Recognizer/methods.d.ts +9 -0
- package/lib/Recognizer/methods.js +33 -0
- package/lib/Recognizer/types.d.ts +6 -0
- package/lib/Recognizer/types.js +1 -0
- package/lib/Recognizer/useRecognizer.d.ts +16 -0
- package/lib/Recognizer/useRecognizer.js +71 -0
- package/lib/Recognizer/useRecognizerIsActive.d.ts +25 -0
- package/lib/Recognizer/useRecognizerIsActive.js +40 -0
- package/lib/Recognizer/useVoiceInputVolume.d.ts +25 -0
- package/lib/Recognizer/useVoiceInputVolume.js +52 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.js +7 -0
- package/lib/specs/NitroSpeech.nitro.d.ts +8 -0
- package/lib/specs/NitroSpeech.nitro.js +1 -0
- package/lib/specs/Recognizer.nitro.d.ts +97 -0
- package/lib/specs/Recognizer.nitro.js +1 -0
- package/lib/specs/SpeechRecognitionConfig.d.ts +162 -0
- package/lib/specs/SpeechRecognitionConfig.js +1 -0
- package/lib/specs/VolumeChangeEvent.d.ts +31 -0
- package/lib/specs/VolumeChangeEvent.js +1 -0
- package/nitro.json +0 -4
- package/nitrogen/generated/android/NitroSpeech+autolinking.cmake +2 -2
- package/nitrogen/generated/android/NitroSpeechOnLoad.cpp +4 -2
- package/nitrogen/generated/android/c++/JFunc_void_VolumeChangeEvent.hpp +78 -0
- package/nitrogen/generated/android/c++/JFunc_void_std__vector_std__string_.hpp +14 -14
- package/nitrogen/generated/android/c++/JHybridRecognizerSpec.cpp +73 -19
- package/nitrogen/generated/android/c++/JHybridRecognizerSpec.hpp +8 -4
- package/nitrogen/generated/android/c++/JIosPreset.hpp +58 -0
- package/nitrogen/generated/android/c++/JMutableSpeechRecognitionConfig.hpp +79 -0
- package/nitrogen/generated/android/c++/{JSpeechToTextParams.hpp → JSpeechRecognitionConfig.hpp} +48 -30
- package/nitrogen/generated/android/c++/JVolumeChangeEvent.hpp +65 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrospeech/Func_void_VolumeChangeEvent.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrospeech/HybridRecognizerSpec.kt +22 -5
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrospeech/IosPreset.kt +23 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrospeech/MutableSpeechRecognitionConfig.kt +76 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrospeech/SpeechRecognitionConfig.kt +121 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrospeech/VolumeChangeEvent.kt +61 -0
- package/nitrogen/generated/ios/NitroSpeech-Swift-Cxx-Bridge.cpp +46 -30
- package/nitrogen/generated/ios/NitroSpeech-Swift-Cxx-Bridge.hpp +211 -69
- package/nitrogen/generated/ios/NitroSpeech-Swift-Cxx-Umbrella.hpp +13 -3
- package/nitrogen/generated/ios/c++/HybridRecognizerSpecSwift.hpp +49 -9
- package/nitrogen/generated/ios/swift/Func_void_VolumeChangeEvent.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +46 -0
- package/nitrogen/generated/ios/swift/HybridRecognizerSpec.swift +7 -3
- package/nitrogen/generated/ios/swift/HybridRecognizerSpec_cxx.swift +78 -18
- package/nitrogen/generated/ios/swift/IosPreset.swift +40 -0
- package/nitrogen/generated/ios/swift/MutableSpeechRecognitionConfig.swift +118 -0
- package/nitrogen/generated/ios/swift/{SpeechToTextParams.swift → SpeechRecognitionConfig.swift} +108 -43
- package/nitrogen/generated/ios/swift/VolumeChangeEvent.swift +52 -0
- package/nitrogen/generated/shared/c++/HybridRecognizerSpec.cpp +5 -1
- package/nitrogen/generated/shared/c++/HybridRecognizerSpec.hpp +18 -7
- package/nitrogen/generated/shared/c++/IosPreset.hpp +76 -0
- package/nitrogen/generated/shared/c++/MutableSpeechRecognitionConfig.hpp +105 -0
- package/nitrogen/generated/shared/c++/{SpeechToTextParams.hpp → SpeechRecognitionConfig.hpp} +39 -20
- package/nitrogen/generated/shared/c++/VolumeChangeEvent.hpp +91 -0
- package/package.json +15 -16
- package/src/NitroSpeech.ts +5 -0
- package/src/Recognizer/RecognizerRef.ts +27 -0
- package/src/Recognizer/SpeechRecognizer.ts +10 -0
- package/src/Recognizer/methods.ts +45 -0
- package/src/Recognizer/types.ts +34 -0
- package/src/Recognizer/useRecognizer.ts +87 -0
- package/src/Recognizer/useRecognizerIsActive.ts +49 -0
- package/src/Recognizer/useVoiceInputVolume.ts +65 -0
- package/src/index.ts +13 -182
- package/src/specs/NitroSpeech.nitro.ts +2 -163
- package/src/specs/Recognizer.nitro.ts +113 -0
- package/src/specs/SpeechRecognitionConfig.ts +167 -0
- package/src/specs/VolumeChangeEvent.ts +31 -0
- package/android/proguard-rules.pro +0 -1
- package/ios/AnylyzerTranscriber.swift +0 -331
- package/ios/AutoStopper.swift +0 -69
- package/ios/HapticImpact.swift +0 -32
- package/ios/LegacySpeechRecognizer.swift +0 -161
- package/lib/commonjs/index.js +0 -145
- package/lib/commonjs/index.js.map +0 -1
- package/lib/commonjs/package.json +0 -1
- package/lib/commonjs/specs/NitroSpeech.nitro.js +0 -6
- package/lib/commonjs/specs/NitroSpeech.nitro.js.map +0 -1
- package/lib/module/index.js +0 -138
- package/lib/module/index.js.map +0 -1
- package/lib/module/package.json +0 -1
- package/lib/module/specs/NitroSpeech.nitro.js +0 -4
- package/lib/module/specs/NitroSpeech.nitro.js.map +0 -1
- package/lib/tsconfig.tsbuildinfo +0 -1
- package/lib/typescript/index.d.ts +0 -50
- package/lib/typescript/index.d.ts.map +0 -1
- package/lib/typescript/specs/NitroSpeech.nitro.d.ts +0 -162
- package/lib/typescript/specs/NitroSpeech.nitro.d.ts.map +0 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrospeech/SpeechToTextParams.kt +0 -68
package/nitrogen/generated/shared/c++/{SpeechToTextParams.hpp → SpeechRecognitionConfig.hpp}
RENAMED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
///
|
|
2
|
-
///
|
|
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 (
|
|
45
|
+
* A struct which can be represented as a JavaScript object (SpeechRecognitionConfig).
|
|
43
46
|
*/
|
|
44
|
-
struct
|
|
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
|
-
|
|
60
|
-
explicit
|
|
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
|
|
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++
|
|
77
|
+
// C++ SpeechRecognitionConfig <> JS SpeechRecognitionConfig (object)
|
|
71
78
|
template <>
|
|
72
|
-
struct JSIConverter<margelo::nitro::nitrospeech::
|
|
73
|
-
static inline margelo::nitro::nitrospeech::
|
|
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::
|
|
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::
|
|
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.
|
|
4
|
-
"description": "React Native Speech Recognition Library powered by Nitro Modules",
|
|
5
|
-
"main": "./lib/
|
|
6
|
-
"module": "./lib/
|
|
7
|
-
"types": "./lib/
|
|
8
|
-
"react-native": "./
|
|
9
|
-
"source": "src/index.ts",
|
|
3
|
+
"version": "0.4.1",
|
|
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
|
-
"
|
|
33
|
+
"build": "rm -rf lib && rm -f tsconfig.tsbuildinfo && bun tsc",
|
|
34
34
|
"lint": "eslint \"**/*.{js,ts,tsx}\" --fix",
|
|
35
|
-
"
|
|
36
|
-
"
|
|
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.
|
|
85
|
-
"nitrogen": "0.35.
|
|
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,27 @@
|
|
|
1
|
+
import type { RecognizerMethods } from './types'
|
|
2
|
+
import {
|
|
3
|
+
recognizerAddAutoFinishTime,
|
|
4
|
+
recognizerGetSupportedLocalesIOS,
|
|
5
|
+
recognizerGetIsActive,
|
|
6
|
+
recognizerResetAutoFinishTime,
|
|
7
|
+
recognizerStartListening,
|
|
8
|
+
recognizerStopListening,
|
|
9
|
+
recognizerUpdateConfig,
|
|
10
|
+
recognizerGetVoiceInputVolume,
|
|
11
|
+
} from './methods'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Safe cross-component reference to the Speech Recognizer methods.
|
|
15
|
+
*
|
|
16
|
+
* All methods support worklets and UI thread calls
|
|
17
|
+
*/
|
|
18
|
+
export const RecognizerRef: RecognizerMethods = {
|
|
19
|
+
startListening: recognizerStartListening,
|
|
20
|
+
stopListening: recognizerStopListening,
|
|
21
|
+
resetAutoFinishTime: recognizerResetAutoFinishTime,
|
|
22
|
+
addAutoFinishTime: recognizerAddAutoFinishTime,
|
|
23
|
+
updateConfig: recognizerUpdateConfig,
|
|
24
|
+
getIsActive: recognizerGetIsActive,
|
|
25
|
+
getVoiceInputVolume: recognizerGetVoiceInputVolume,
|
|
26
|
+
getSupportedLocalesIOS: recognizerGetSupportedLocalesIOS,
|
|
27
|
+
}
|
|
@@ -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,45 @@
|
|
|
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 recognizerGetVoiceInputVolume = () => {
|
|
38
|
+
'worklet'
|
|
39
|
+
return SpeechRecognizer.getVoiceInputVolume()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const recognizerGetSupportedLocalesIOS = () => {
|
|
43
|
+
'worklet'
|
|
44
|
+
return SpeechRecognizer.getSupportedLocalesIOS().sort()
|
|
45
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
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
|
+
| 'getVoiceInputVolume'
|
|
25
|
+
| 'getSupportedLocalesIOS'
|
|
26
|
+
>
|
|
27
|
+
|
|
28
|
+
export type {
|
|
29
|
+
RecognizerSpec,
|
|
30
|
+
SpeechRecognitionConfig,
|
|
31
|
+
VolumeChangeEvent,
|
|
32
|
+
RecognizerCallbacks,
|
|
33
|
+
RecognizerMethods,
|
|
34
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { useEffect, type DependencyList } from 'react'
|
|
2
|
+
import {
|
|
3
|
+
recognizerResetAutoFinishTime,
|
|
4
|
+
recognizerAddAutoFinishTime,
|
|
5
|
+
recognizerUpdateConfig,
|
|
6
|
+
recognizerGetIsActive,
|
|
7
|
+
recognizerGetSupportedLocalesIOS,
|
|
8
|
+
recognizerStartListening,
|
|
9
|
+
recognizerStopListening,
|
|
10
|
+
recognizerGetVoiceInputVolume,
|
|
11
|
+
} from './methods'
|
|
12
|
+
import type { RecognizerCallbacks, RecognizerMethods } from './types'
|
|
13
|
+
import { SpeechRecognizer } from './SpeechRecognizer'
|
|
14
|
+
import { speechRecognizerVolumeChangeHandler } from './useVoiceInputVolume'
|
|
15
|
+
import { speechRecognizerActiveStateHandler } from './useRecognizerIsActive'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Safe, lifecycle-aware hook to use the recognizer.
|
|
19
|
+
*
|
|
20
|
+
* @param callbacks - The callbacks to use for the recognizer.
|
|
21
|
+
* @param destroyDeps - The additional dependencies to use for the cleanup effect.
|
|
22
|
+
*
|
|
23
|
+
* Example: To cleanup when the screen is unfocused.
|
|
24
|
+
*
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const isFocused = useIsFocused()
|
|
27
|
+
* useRecognizer({ ... }, [isFocused])
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export const useRecognizer = (
|
|
31
|
+
callbacks: RecognizerCallbacks,
|
|
32
|
+
destroyDeps: DependencyList = []
|
|
33
|
+
): RecognizerMethods => {
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
SpeechRecognizer.onReadyForSpeech = () => {
|
|
36
|
+
speechRecognizerActiveStateHandler(true)
|
|
37
|
+
callbacks.onReadyForSpeech?.()
|
|
38
|
+
}
|
|
39
|
+
SpeechRecognizer.onRecordingStopped = () => {
|
|
40
|
+
speechRecognizerActiveStateHandler(false)
|
|
41
|
+
callbacks.onRecordingStopped?.()
|
|
42
|
+
}
|
|
43
|
+
SpeechRecognizer.onResult = (resultBatches: string[]) => {
|
|
44
|
+
callbacks.onResult?.(resultBatches)
|
|
45
|
+
}
|
|
46
|
+
SpeechRecognizer.onAutoFinishProgress = (timeLeftMs: number) => {
|
|
47
|
+
callbacks.onAutoFinishProgress?.(timeLeftMs)
|
|
48
|
+
}
|
|
49
|
+
SpeechRecognizer.onError = (message: string) => {
|
|
50
|
+
callbacks.onError?.(message)
|
|
51
|
+
}
|
|
52
|
+
SpeechRecognizer.onPermissionDenied = () => {
|
|
53
|
+
callbacks.onPermissionDenied?.()
|
|
54
|
+
}
|
|
55
|
+
SpeechRecognizer.onVolumeChange = (event) => {
|
|
56
|
+
speechRecognizerVolumeChangeHandler(event)
|
|
57
|
+
callbacks.onVolumeChange?.(event)
|
|
58
|
+
}
|
|
59
|
+
return () => {
|
|
60
|
+
SpeechRecognizer.onReadyForSpeech = undefined
|
|
61
|
+
SpeechRecognizer.onRecordingStopped = undefined
|
|
62
|
+
SpeechRecognizer.onResult = undefined
|
|
63
|
+
SpeechRecognizer.onAutoFinishProgress = undefined
|
|
64
|
+
SpeechRecognizer.onError = undefined
|
|
65
|
+
SpeechRecognizer.onPermissionDenied = undefined
|
|
66
|
+
SpeechRecognizer.onVolumeChange = undefined
|
|
67
|
+
}
|
|
68
|
+
}, [callbacks])
|
|
69
|
+
|
|
70
|
+
useEffect(() => {
|
|
71
|
+
return () => {
|
|
72
|
+
SpeechRecognizer.stopListening()
|
|
73
|
+
}
|
|
74
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
75
|
+
}, [...destroyDeps])
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
startListening: recognizerStartListening,
|
|
79
|
+
stopListening: recognizerStopListening,
|
|
80
|
+
resetAutoFinishTime: recognizerResetAutoFinishTime,
|
|
81
|
+
addAutoFinishTime: recognizerAddAutoFinishTime,
|
|
82
|
+
updateConfig: recognizerUpdateConfig,
|
|
83
|
+
getIsActive: recognizerGetIsActive,
|
|
84
|
+
getVoiceInputVolume: recognizerGetVoiceInputVolume,
|
|
85
|
+
getSupportedLocalesIOS: recognizerGetSupportedLocalesIOS,
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { useSyncExternalStore } from 'react'
|
|
2
|
+
|
|
3
|
+
type OnActiveStateChange = (isActive: boolean) => void
|
|
4
|
+
|
|
5
|
+
const subscribers = new Set<OnActiveStateChange>()
|
|
6
|
+
|
|
7
|
+
let recognizerIsActive = false
|
|
8
|
+
|
|
9
|
+
const getSnapshot = () => {
|
|
10
|
+
return recognizerIsActive
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Returns true if the speech recognition session is active.
|
|
15
|
+
*/
|
|
16
|
+
export const useRecognizerIsActive = () => {
|
|
17
|
+
return useSyncExternalStore((subscriber) => {
|
|
18
|
+
subscribers.add(subscriber)
|
|
19
|
+
return () => subscribers.delete(subscriber)
|
|
20
|
+
}, getSnapshot)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Direct access to default Speech Recognizer isActive state change handler.
|
|
25
|
+
*
|
|
26
|
+
* In case you use static Speech Recognizer:
|
|
27
|
+
*
|
|
28
|
+
* ```typescript
|
|
29
|
+
* import { speechRecognizerActiveStateHandler } from '@gmessier/nitro-speech'
|
|
30
|
+
*
|
|
31
|
+
* SpeechRecognizer.onReadyForSpeech = () => {
|
|
32
|
+
* speechRecognizerActiveStateHandler(true)
|
|
33
|
+
* }
|
|
34
|
+
* SpeechRecognizer.onRecordingStopped = () => {
|
|
35
|
+
* speechRecognizerActiveStateHandler(false)
|
|
36
|
+
* }
|
|
37
|
+
* ... // setup everything else
|
|
38
|
+
* SpeechRecognizer.startListening({ locale: 'en-US' })
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export const speechRecognizerActiveStateHandler: OnActiveStateChange = (
|
|
42
|
+
isActive
|
|
43
|
+
) => {
|
|
44
|
+
if (isActive === recognizerIsActive) {
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
recognizerIsActive = isActive
|
|
48
|
+
subscribers.forEach((subscriber) => subscriber?.(isActive))
|
|
49
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { useSyncExternalStore } from 'react'
|
|
2
|
+
import type { RecognizerSpec, VolumeChangeEvent } from './types'
|
|
3
|
+
|
|
4
|
+
type OnVolumeChange = Exclude<RecognizerSpec['onVolumeChange'], undefined>
|
|
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
|
+
}
|