zedai-voice
Version:
Siri-like voice banking SDK for React Native - Complete voice-powered fintech solution with wake word detection and beautiful UI
719 lines (715 loc) • 30.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _reactNative = require("react-native");
var _voice = _interopRequireDefault(require("@react-native-voice/voice"));
var _reactNativeTts = _interopRequireDefault(require("react-native-tts"));
var _reactNativeSound = _interopRequireDefault(require("react-native-sound"));
var _reactNativeFs = _interopRequireDefault(require("react-native-fs"));
var _Logger = _interopRequireDefault(require("../utils/Logger"));
var _Constants = require("../utils/Constants");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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); }
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(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { if (r) i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n;else { var o = function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); }; o("next", 0), o("throw", 1), o("return", 2); } }, _regeneratorDefine2(e, r, n, t); }
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : 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); } // src/core/AudioManager.js
var AudioManager = /*#__PURE__*/function () {
function AudioManager(config) {
_classCallCheck(this, AudioManager);
this.config = config;
this.isRecording = false;
this.isPlaying = false;
this.currentRecording = null;
this.recordingPath = null;
this.state = _Constants.VOICE_STATES.IDLE;
this.listeners = new Map();
this.initializeAudio();
}
return _createClass(AudioManager, [{
key: "initializeAudio",
value: function () {
var _initializeAudio = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
var _t;
return _regenerator().w(function (_context) {
while (1) switch (_context.n) {
case 0:
_context.p = 0;
_context.n = 1;
return this.requestAudioPermissions();
case 1:
_context.n = 2;
return this.initializeVoiceRecognition();
case 2:
_context.n = 3;
return this.initializeTTS();
case 3:
_Logger["default"].info('Audio Manager initialized successfully');
_context.n = 5;
break;
case 4:
_context.p = 4;
_t = _context.v;
_Logger["default"].error('Audio Manager initialization failed', {
error: _t.message
});
throw new Error(_Constants.ERROR_CODES.AUDIO_PERMISSION_DENIED);
case 5:
return _context.a(2);
}
}, _callee, this, [[0, 4]]);
}));
function initializeAudio() {
return _initializeAudio.apply(this, arguments);
}
return initializeAudio;
}()
}, {
key: "requestAudioPermissions",
value: function () {
var _requestAudioPermissions = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
var granted, allGranted;
return _regenerator().w(function (_context2) {
while (1) switch (_context2.n) {
case 0:
if (!(_reactNative.Platform.OS === 'android')) {
_context2.n = 2;
break;
}
_context2.n = 1;
return _reactNative.PermissionsAndroid.requestMultiple([_reactNative.PermissionsAndroid.PERMISSIONS.RECORD_AUDIO, _reactNative.PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, _reactNative.PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE]);
case 1:
granted = _context2.v;
allGranted = Object.values(granted).every(function (permission) {
return permission === _reactNative.PermissionsAndroid.RESULTS.GRANTED;
});
if (allGranted) {
_context2.n = 2;
break;
}
throw new Error('Audio permissions not granted');
case 2:
return _context2.a(2);
}
}, _callee2);
}));
function requestAudioPermissions() {
return _requestAudioPermissions.apply(this, arguments);
}
return requestAudioPermissions;
}()
}, {
key: "initializeVoiceRecognition",
value: function () {
var _initializeVoiceRecognition = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() {
return _regenerator().w(function (_context3) {
while (1) switch (_context3.n) {
case 0:
_voice["default"].onSpeechStart = this.onSpeechStart.bind(this);
_voice["default"].onSpeechRecognized = this.onSpeechRecognized.bind(this);
_voice["default"].onSpeechEnd = this.onSpeechEnd.bind(this);
_voice["default"].onSpeechError = this.onSpeechError.bind(this);
_voice["default"].onSpeechResults = this.onSpeechResults.bind(this);
_voice["default"].onSpeechPartialResults = this.onSpeechPartialResults.bind(this);
_voice["default"].onSpeechVolumeChanged = this.onSpeechVolumeChanged.bind(this);
_Logger["default"].info('Voice recognition initialized');
case 1:
return _context3.a(2);
}
}, _callee3, this);
}));
function initializeVoiceRecognition() {
return _initializeVoiceRecognition.apply(this, arguments);
}
return initializeVoiceRecognition;
}()
}, {
key: "initializeTTS",
value: function () {
var _initializeTTS = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4() {
return _regenerator().w(function (_context4) {
while (1) switch (_context4.n) {
case 0:
_reactNativeTts["default"].setDefaultLanguage('en-US');
_reactNativeTts["default"].setDefaultRate(0.5);
_reactNativeTts["default"].setDefaultPitch(1.0);
_reactNativeTts["default"].addEventListener('tts-start', this.onTTSStart.bind(this));
_reactNativeTts["default"].addEventListener('tts-finish', this.onTTSFinish.bind(this));
_reactNativeTts["default"].addEventListener('tts-cancel', this.onTTSCancel.bind(this));
_Logger["default"].info('Text-to-Speech initialized');
case 1:
return _context4.a(2);
}
}, _callee4, this);
}));
function initializeTTS() {
return _initializeTTS.apply(this, arguments);
}
return initializeTTS;
}() // Voice Recognition Event Handlers
}, {
key: "onSpeechStart",
value: function onSpeechStart(event) {
_Logger["default"].debug('Speech started', event);
this.setState(_Constants.VOICE_STATES.LISTENING);
this.emit('speechStart', event);
}
}, {
key: "onSpeechRecognized",
value: function onSpeechRecognized(event) {
_Logger["default"].debug('Speech recognized', event);
this.emit('speechRecognized', event);
}
}, {
key: "onSpeechEnd",
value: function onSpeechEnd(event) {
_Logger["default"].debug('Speech ended', event);
this.setState(_Constants.VOICE_STATES.PROCESSING);
this.isRecording = false;
this.emit('speechEnd', event);
}
}, {
key: "onSpeechError",
value: function onSpeechError(event) {
_Logger["default"].error('Speech error', event);
this.setState(_Constants.VOICE_STATES.ERROR);
this.isRecording = false;
this.emit('speechError', event);
}
}, {
key: "onSpeechResults",
value: function onSpeechResults(event) {
_Logger["default"].debug('Speech results', event);
var results = event.value || [];
if (results.length > 0) {
this.emit('speechResults', {
text: results[0],
allResults: results
});
}
}
}, {
key: "onSpeechPartialResults",
value: function onSpeechPartialResults(event) {
_Logger["default"].debug('Speech partial results', event);
var partialResults = event.value || [];
if (partialResults.length > 0) {
this.emit('speechPartialResults', {
text: partialResults[0]
});
}
}
}, {
key: "onSpeechVolumeChanged",
value: function onSpeechVolumeChanged(event) {
this.emit('volumeChanged', {
volume: event.value
});
}
// TTS Event Handlers
}, {
key: "onTTSStart",
value: function onTTSStart(event) {
_Logger["default"].debug('TTS started', event);
this.setState(_Constants.VOICE_STATES.SPEAKING);
this.isPlaying = true;
this.emit('ttsStart', event);
}
}, {
key: "onTTSFinish",
value: function onTTSFinish(event) {
_Logger["default"].debug('TTS finished', event);
this.setState(_Constants.VOICE_STATES.IDLE);
this.isPlaying = false;
this.emit('ttsFinish', event);
}
}, {
key: "onTTSCancel",
value: function onTTSCancel(event) {
_Logger["default"].debug('TTS cancelled', event);
this.setState(_Constants.VOICE_STATES.IDLE);
this.isPlaying = false;
this.emit('ttsCancel', event);
}
// Public Methods
}, {
key: "startListening",
value: function () {
var _startListening = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5() {
var options,
voiceOptions,
_args5 = arguments,
_t2;
return _regenerator().w(function (_context5) {
while (1) switch (_context5.n) {
case 0:
options = _args5.length > 0 && _args5[0] !== undefined ? _args5[0] : {};
_context5.p = 1;
if (!this.isRecording) {
_context5.n = 2;
break;
}
_Logger["default"].warn('Already recording');
return _context5.a(2);
case 2:
if (!this.isPlaying) {
_context5.n = 3;
break;
}
_context5.n = 3;
return this.stopSpeaking();
case 3:
voiceOptions = _objectSpread({
language: options.language || 'en-US',
timeout: options.timeout || _Constants.AUDIO_CONFIG.MAX_RECORDING_TIME,
speechTimeout: options.speechTimeout || 2000
}, options);
_context5.n = 4;
return _voice["default"].start(voiceOptions.language, voiceOptions);
case 4:
this.isRecording = true;
this.setState(_Constants.VOICE_STATES.LISTENING);
_Logger["default"].info('Started listening', voiceOptions);
_context5.n = 6;
break;
case 5:
_context5.p = 5;
_t2 = _context5.v;
_Logger["default"].error('Failed to start listening', {
error: _t2.message
});
this.setState(_Constants.VOICE_STATES.ERROR);
throw new Error(_Constants.ERROR_CODES.SPEECH_RECOGNITION_FAILED);
case 6:
return _context5.a(2);
}
}, _callee5, this, [[1, 5]]);
}));
function startListening() {
return _startListening.apply(this, arguments);
}
return startListening;
}()
}, {
key: "stopListening",
value: function () {
var _stopListening = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6() {
var _t3;
return _regenerator().w(function (_context6) {
while (1) switch (_context6.n) {
case 0:
_context6.p = 0;
if (this.isRecording) {
_context6.n = 1;
break;
}
_Logger["default"].warn('Not currently recording');
return _context6.a(2);
case 1:
_context6.n = 2;
return _voice["default"].stop();
case 2:
this.isRecording = false;
this.setState(_Constants.VOICE_STATES.PROCESSING);
_Logger["default"].info('Stopped listening');
_context6.n = 4;
break;
case 3:
_context6.p = 3;
_t3 = _context6.v;
_Logger["default"].error('Failed to stop listening', {
error: _t3.message
});
throw new Error(_Constants.ERROR_CODES.SPEECH_RECOGNITION_FAILED);
case 4:
return _context6.a(2);
}
}, _callee6, this, [[0, 3]]);
}));
function stopListening() {
return _stopListening.apply(this, arguments);
}
return stopListening;
}()
}, {
key: "cancelListening",
value: function () {
var _cancelListening = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7() {
var _t4;
return _regenerator().w(function (_context7) {
while (1) switch (_context7.n) {
case 0:
_context7.p = 0;
_context7.n = 1;
return _voice["default"].cancel();
case 1:
this.isRecording = false;
this.setState(_Constants.VOICE_STATES.IDLE);
_Logger["default"].info('Cancelled listening');
_context7.n = 3;
break;
case 2:
_context7.p = 2;
_t4 = _context7.v;
_Logger["default"].error('Failed to cancel listening', {
error: _t4.message
});
case 3:
return _context7.a(2);
}
}, _callee7, this, [[0, 2]]);
}));
function cancelListening() {
return _cancelListening.apply(this, arguments);
}
return cancelListening;
}()
}, {
key: "speak",
value: function () {
var _speak = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(text) {
var options,
ttsOptions,
_args8 = arguments,
_t5;
return _regenerator().w(function (_context8) {
while (1) switch (_context8.n) {
case 0:
options = _args8.length > 1 && _args8[1] !== undefined ? _args8[1] : {};
_context8.p = 1;
if (!this.isPlaying) {
_context8.n = 2;
break;
}
_context8.n = 2;
return this.stopSpeaking();
case 2:
if (!this.isRecording) {
_context8.n = 3;
break;
}
_context8.n = 3;
return this.stopListening();
case 3:
ttsOptions = _objectSpread({
language: options.language || 'en-US',
rate: options.rate || 0.5,
pitch: options.pitch || 1.0
}, options);
if (ttsOptions.language) {
_reactNativeTts["default"].setDefaultLanguage(ttsOptions.language);
}
if (ttsOptions.rate) {
_reactNativeTts["default"].setDefaultRate(ttsOptions.rate);
}
if (ttsOptions.pitch) {
_reactNativeTts["default"].setDefaultPitch(ttsOptions.pitch);
}
_context8.n = 4;
return _reactNativeTts["default"].speak(text);
case 4:
_Logger["default"].info('Started speaking', {
text: text.substring(0, 50) + '...',
options: ttsOptions
});
_context8.n = 6;
break;
case 5:
_context8.p = 5;
_t5 = _context8.v;
_Logger["default"].error('Failed to speak', {
error: _t5.message,
text: text
});
this.setState(_Constants.VOICE_STATES.ERROR);
throw new Error('Failed to speak text');
case 6:
return _context8.a(2);
}
}, _callee8, this, [[1, 5]]);
}));
function speak(_x) {
return _speak.apply(this, arguments);
}
return speak;
}()
}, {
key: "stopSpeaking",
value: function () {
var _stopSpeaking = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9() {
var _t6;
return _regenerator().w(function (_context9) {
while (1) switch (_context9.n) {
case 0:
_context9.p = 0;
_context9.n = 1;
return _reactNativeTts["default"].stop();
case 1:
this.isPlaying = false;
this.setState(_Constants.VOICE_STATES.IDLE);
_Logger["default"].info('Stopped speaking');
_context9.n = 3;
break;
case 2:
_context9.p = 2;
_t6 = _context9.v;
_Logger["default"].error('Failed to stop speaking', {
error: _t6.message
});
case 3:
return _context9.a(2);
}
}, _callee9, this, [[0, 2]]);
}));
function stopSpeaking() {
return _stopSpeaking.apply(this, arguments);
}
return stopSpeaking;
}()
}, {
key: "recordAudio",
value: function () {
var _recordAudio = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee0() {
var _this = this;
var duration,
audioPath,
_args0 = arguments,
_t7;
return _regenerator().w(function (_context0) {
while (1) switch (_context0.n) {
case 0:
duration = _args0.length > 0 && _args0[0] !== undefined ? _args0[0] : _Constants.AUDIO_CONFIG.MAX_RECORDING_TIME;
_context0.p = 1;
audioPath = "".concat(_reactNativeFs["default"].CachesDirectoryPath, "/voice_recording_").concat(Date.now(), ".wav"); // This would integrate with a native audio recording module
// For now, we'll use the Voice module for simplicity
_context0.n = 2;
return this.startListening({
timeout: duration
});
case 2:
return _context0.a(2, new Promise(function (resolve, reject) {
var timeout = setTimeout(function () {
_this.stopListening();
reject(new Error('Recording timeout'));
}, duration);
_this.once('speechResults', function (result) {
clearTimeout(timeout);
resolve({
path: audioPath,
text: result.text,
duration: duration
});
});
_this.once('speechError', function (error) {
clearTimeout(timeout);
reject(new Error(error.message || 'Recording failed'));
});
}));
case 3:
_context0.p = 3;
_t7 = _context0.v;
_Logger["default"].error('Failed to record audio', {
error: _t7.message
});
throw new Error(_Constants.ERROR_CODES.SPEECH_RECOGNITION_FAILED);
case 4:
return _context0.a(2);
}
}, _callee0, this, [[1, 3]]);
}));
function recordAudio() {
return _recordAudio.apply(this, arguments);
}
return recordAudio;
}()
}, {
key: "playAudioFile",
value: function () {
var _playAudioFile = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee1(filePath) {
var _t8;
return _regenerator().w(function (_context1) {
while (1) switch (_context1.n) {
case 0:
_context1.p = 0;
return _context1.a(2, new Promise(function (resolve, reject) {
var sound = new _reactNativeSound["default"](filePath, '', function (error) {
if (error) {
_Logger["default"].error('Failed to load audio file', {
error: error.message,
filePath: filePath
});
reject(new Error('Failed to load audio file'));
return;
}
sound.play(function (success) {
if (success) {
_Logger["default"].info('Audio file played successfully', {
filePath: filePath
});
resolve();
} else {
_Logger["default"].error('Failed to play audio file', {
filePath: filePath
});
reject(new Error('Failed to play audio file'));
}
sound.release();
});
});
}));
case 1:
_context1.p = 1;
_t8 = _context1.v;
_Logger["default"].error('Audio playback error', {
error: _t8.message,
filePath: filePath
});
throw _t8;
case 2:
return _context1.a(2);
}
}, _callee1, null, [[0, 1]]);
}));
function playAudioFile(_x2) {
return _playAudioFile.apply(this, arguments);
}
return playAudioFile;
}() // Event System
}, {
key: "on",
value: function on(event, callback) {
if (!this.listeners.has(event)) {
this.listeners.set(event, []);
}
this.listeners.get(event).push(callback);
}
}, {
key: "once",
value: function once(event, callback) {
var _this2 = this;
var _wrapper = function wrapper() {
callback.apply(void 0, arguments);
_this2.off(event, _wrapper);
};
this.on(event, _wrapper);
}
}, {
key: "off",
value: function off(event, callback) {
if (this.listeners.has(event)) {
var callbacks = this.listeners.get(event);
var index = callbacks.indexOf(callback);
if (index > -1) {
callbacks.splice(index, 1);
}
}
}
}, {
key: "emit",
value: function emit(event, data) {
if (this.listeners.has(event)) {
this.listeners.get(event).forEach(function (callback) {
try {
callback(data);
} catch (error) {
_Logger["default"].error('Event callback error', {
event: event,
error: error.message
});
}
});
}
}
}, {
key: "setState",
value: function setState(newState) {
if (this.state !== newState) {
var oldState = this.state;
this.state = newState;
this.emit('stateChanged', {
oldState: oldState,
newState: newState
});
_Logger["default"].debug('Audio state changed', {
oldState: oldState,
newState: newState
});
}
}
}, {
key: "getState",
value: function getState() {
return this.state;
}
}, {
key: "isListening",
value: function isListening() {
return this.isRecording;
}
}, {
key: "isSpeaking",
value: function isSpeaking() {
return this.isPlaying;
}
}, {
key: "cleanup",
value: function () {
var _cleanup = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee10() {
var _t9;
return _regenerator().w(function (_context10) {
while (1) switch (_context10.n) {
case 0:
_context10.p = 0;
if (!this.isRecording) {
_context10.n = 1;
break;
}
_context10.n = 1;
return this.cancelListening();
case 1:
if (!this.isPlaying) {
_context10.n = 2;
break;
}
_context10.n = 2;
return this.stopSpeaking();
case 2:
// Remove TTS listeners
_reactNativeTts["default"].removeAllListeners('tts-start');
_reactNativeTts["default"].removeAllListeners('tts-finish');
_reactNativeTts["default"].removeAllListeners('tts-cancel');
// Clean up Voice
_voice["default"].destroy().then(_voice["default"].removeAllListeners);
this.listeners.clear();
_Logger["default"].info('Audio Manager cleaned up');
_context10.n = 4;
break;
case 3:
_context10.p = 3;
_t9 = _context10.v;
_Logger["default"].error('Audio cleanup error', {
error: _t9.message
});
case 4:
return _context10.a(2);
}
}, _callee10, this, [[0, 3]]);
}));
function cleanup() {
return _cleanup.apply(this, arguments);
}
return cleanup;
}()
}]);
}();
var _default = exports["default"] = AudioManager;