@lobehub/tts
Version:
A high-quality & reliable TTS React Hooks library
63 lines • 4.5 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var _excluded = ["onStart", "onStop", "onBlobAvailable", "onRecognitionFinish"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import { useCallback } from 'react';
import { useAudioRecorder } from "../useAudioRecorder";
import { useSpeechRecognitionCore } from "./useSpeechRecognitionCore";
export var useSpeechRecognitionAutoStop = function useSpeechRecognitionAutoStop(locale) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
onStart = _ref.onStart,
onStop = _ref.onStop,
onBlobAvailable = _ref.onBlobAvailable,
_onRecognitionFinish = _ref.onRecognitionFinish,
rest = _objectWithoutProperties(_ref, _excluded);
var _useAudioRecorder = useAudioRecorder(onBlobAvailable),
time = _useAudioRecorder.time,
formattedTime = _useAudioRecorder.formattedTime,
startRecord = _useAudioRecorder.start,
stopRecord = _useAudioRecorder.stop,
blob = _useAudioRecorder.blob,
url = _useAudioRecorder.url;
var _useSpeechRecognition = useSpeechRecognitionCore(locale, _objectSpread({
onRecognitionFinish: function onRecognitionFinish(data) {
_onRecognitionFinish === null || _onRecognitionFinish === void 0 || _onRecognitionFinish(data);
stopRecord();
}
}, rest)),
isLoading = _useSpeechRecognition.isLoading,
start = _useSpeechRecognition.start,
stop = _useSpeechRecognition.stop,
text = _useSpeechRecognition.text;
var handleStart = useCallback(function () {
onStart === null || onStart === void 0 || onStart();
start();
startRecord();
}, [start, startRecord]);
var handleStop = useCallback(function () {
onStop === null || onStop === void 0 || onStop();
stop();
stopRecord();
}, [stop, stopRecord]);
return {
blob: blob,
formattedTime: formattedTime,
isLoading: isLoading,
isRecording: isLoading,
response: new Response(JSON.stringify({
text: text
}), {
status: 200
}),
start: handleStart,
stop: handleStop,
text: text,
time: time,
url: url
};
};