Skip to content

Commit

Permalink
feat: added silenceTimeoutMs option
Browse files Browse the repository at this point in the history
  • Loading branch information
mfkrause committed Nov 22, 2024
1 parent 23a9404 commit ed585d4
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion android/src/main/java/com/voicekit/VoiceKitService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class VoiceKitService(private val context: ReactApplicationContext) {
if (options.getString("mode") == "continuous-and-stop") {
stopListening()
}
}, 1000)
}, if (options.hasKey("silenceTimeoutMs")) options.getDouble("silenceTimeoutMs").toLong() else 1000L)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,7 @@ PODS:
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- react-native-voicekit (0.1.0):
- react-native-voicekit (0.1.3):
- DoubleConversion
- glog
- hermes-engine
Expand Down Expand Up @@ -1757,7 +1757,7 @@ SPEC CHECKSUMS:
React-logger: d79b704bf215af194f5213a6b7deec50ba8e6a9b
React-Mapbuffer: b982d5bba94a8bc073bda48f0d27c9b28417fae3
React-microtasksnativemodule: 2cec1d6e126598df0f165268afa231174dd1a611
react-native-voicekit: 40c19dab626b0cf00b7befe521e3f54e3998184a
react-native-voicekit: e16ce16d802c3b61e2ae309fe1fa9b6637b99a9a
React-nativeconfig: 8c83d992b9cc7d75b5abe262069eaeea4349f794
React-NativeModulesApple: 9f7920224a3b0c7d04d77990067ded14cee3c614
React-perflogger: 59e1a3182dca2cee7b9f1f7aab204018d46d1914
Expand Down
1 change: 1 addition & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default function App() {
locale: 'en-US',
enablePartialResults: true,
mode: VoiceMode.Continuous,
silenceTimeoutMs: 1000,
});

return (
Expand Down
2 changes: 1 addition & 1 deletion ios/Core/VoiceKitService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class VoiceKitService: NSObject, SFSpeechRecognizerDelegate {

// Reset the timer
lastResultTimer?.invalidate()
lastResultTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { [weak self] _ in
lastResultTimer = Timer.scheduledTimer(withTimeInterval: TimeInterval((options["silenceTimeoutMs"] as? Double ?? 1000.0) / 1000.0), repeats: false) { [weak self] _ in
guard let self else { return }
Logger.log(level: .debug, message: "Final result timer fired")
if let finalTranscription = lastTranscription {
Expand Down
9 changes: 7 additions & 2 deletions src/RNVoiceKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type NativeRNVoiceKit from './types/native';
import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
import RNVoiceError from './utils/VoiceError';
import { VoiceErrorCode } from './types/native';
import { VoiceEvent } from './types';
import { VoiceEvent, VoiceMode } from './types';
import type { VoiceEventMap, VoiceStartListeningOptions } from './types';

const LINKING_ERROR =
Expand Down Expand Up @@ -58,7 +58,12 @@ class RNVoiceKit {
}

async startListening(options?: VoiceStartListeningOptions): Promise<void> {
await nativeInstance.startListening(options ?? {});
await nativeInstance.startListening({
locale: options?.locale ?? 'en-US',
mode: options?.mode ?? VoiceMode.Continuous,
silenceTimeoutMs: options?.silenceTimeoutMs ?? 1000,
muteAndroidBeep: options?.muteAndroidBeep ?? false,
});
}

async stopListening(): Promise<void> {
Expand Down
18 changes: 5 additions & 13 deletions src/hooks/useVoice.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import { useCallback, useEffect, useState } from 'react';
import RNVoiceKit from '../RNVoiceKit';
import { VoiceEvent, VoiceMode } from '../types';
import { VoiceEvent, type VoiceStartListeningOptions } from '../types';

interface UseVoiceProps {
interface UseVoiceProps extends VoiceStartListeningOptions {
/** Whether to update the transcript on partial results. Defaults to false. */
enablePartialResults?: boolean;
/** The locale to use for speech recognition. Defaults to 'en-US'. */
locale?: string;
/**
* The mode to use for speech recognition. When 'continuous', the speech recognition will continue until it is stopped
* or an error occurs. When 'single', the speech recognition will stop after the first final speech result is
* returned. Defaults to 'single'.
*/
mode?: VoiceMode;
}

export function useVoice(props?: UseVoiceProps) {
const { enablePartialResults = false, locale = 'en-US', mode = VoiceMode.Continuous } = props ?? {};
const { enablePartialResults = false, ...listeningOptions } = props ?? {};

const [available, setAvailable] = useState(false);
const [listening, setListening] = useState(false);
Expand All @@ -43,8 +35,8 @@ export function useVoice(props?: UseVoiceProps) {
}, []);

const startListening = useCallback(() => {
return RNVoiceKit.startListening({ locale, mode });
}, [locale, mode]);
return RNVoiceKit.startListening(listeningOptions);
}, [listeningOptions]);

const stopListening = useCallback(() => {
return RNVoiceKit.stopListening();
Expand Down
7 changes: 7 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ export interface VoiceStartListeningOptions {
* Defaults to `single`.
*/
mode?: VoiceMode;
/**
* The period of silence after the user stops speaking before the speech recognizer emits a `result` event.
* Provided in milliseconds.
*
* Defaults to 1000.
*/
silenceTimeoutMs?: number;
/**
* Whether to try and mute the beep sound Android makes when starting and stopping the speech recognizer. This will
* mute the device's music and notification audio streams when starting to listen and unmute them when stopping. This
Expand Down
2 changes: 1 addition & 1 deletion src/types/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export enum VoiceErrorCode {
}

export default interface NativeRNVoiceKit {
startListening: (options: VoiceStartListeningOptions) => Promise<void>;
startListening: (options: Required<VoiceStartListeningOptions>) => Promise<void>;
stopListening: () => Promise<void>;
isSpeechRecognitionAvailable: () => Promise<boolean>;
}

0 comments on commit ed585d4

Please sign in to comment.