@lakshmiprasanth/react-voice-to-text
Version:
A modern React package for voice-to-text conversion with real-time speech recognition and file upload support
143 lines (140 loc) • 4.14 kB
JavaScript
;
/**
* Check browser support for speech recognition and related APIs
*/
function getBrowserSupport() {
if (typeof window === 'undefined') {
return {
webSpeechAPI: false,
mediaRecorder: false,
getUserMedia: false,
browser: {
name: 'unknown',
version: 'unknown'
}
};
}
const browserInfo = getBrowserInfo();
return {
webSpeechAPI: 'SpeechRecognition' in window || 'webkitSpeechRecognition' in window,
mediaRecorder: 'MediaRecorder' in window,
getUserMedia: 'mediaDevices' in navigator && 'getUserMedia' in navigator.mediaDevices,
browser: browserInfo
};
}
/**
* Get the SpeechRecognition constructor for the current browser
*/
function getSpeechRecognition() {
if (typeof window === 'undefined') {
return null;
}
return (window.SpeechRecognition ||
window.webkitSpeechRecognition ||
null);
}
/**
* Check if speech recognition is supported in the current browser
*/
function isSpeechRecognitionSupported() {
const support = getBrowserSupport();
return support.webSpeechAPI;
}
/**
* Get available audio input devices
*/
async function getAudioDevices() {
if (typeof navigator === 'undefined' || !navigator.mediaDevices) {
return [];
}
try {
const devices = await navigator.mediaDevices.enumerateDevices();
return devices.filter(device => device.kind === 'audioinput');
}
catch (error) {
console.error('Failed to enumerate audio devices:', error);
return [];
}
}
/**
* Request microphone permission
*/
async function requestMicrophonePermission() {
if (typeof navigator === 'undefined' || !navigator.mediaDevices) {
return false;
}
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
// Stop the stream immediately as we only needed permission
stream.getTracks().forEach(track => track.stop());
return true;
}
catch (error) {
console.error('Microphone permission denied:', error);
return false;
}
}
/**
* Check if the current environment is a browser
*/
function isBrowser() {
return typeof window !== 'undefined' && typeof document !== 'undefined';
}
/**
* Check if the current environment is a secure context (HTTPS)
*/
function isSecureContext() {
if (typeof window === 'undefined') {
return false;
}
return window.isSecureContext || false;
}
/**
* Get browser name and version
*/
function getBrowserInfo() {
if (typeof navigator === 'undefined') {
return { name: 'unknown', version: 'unknown' };
}
const userAgent = navigator.userAgent;
let name = 'unknown';
let version = 'unknown';
// Chrome
if (userAgent.includes('Chrome') && !userAgent.includes('Edg')) {
name = 'Chrome';
const match = userAgent.match(/Chrome\/(\d+)/);
if (match)
version = match[1];
}
// Safari
else if (userAgent.includes('Safari') && !userAgent.includes('Chrome')) {
name = 'Safari';
const match = userAgent.match(/Version\/(\d+)/);
if (match)
version = match[1];
}
// Firefox
else if (userAgent.includes('Firefox')) {
name = 'Firefox';
const match = userAgent.match(/Firefox\/(\d+)/);
if (match)
version = match[1];
}
// Edge
else if (userAgent.includes('Edg')) {
name = 'Edge';
const match = userAgent.match(/Edg\/(\d+)/);
if (match)
version = match[1];
}
return { name, version };
}
exports.getAudioDevices = getAudioDevices;
exports.getBrowserInfo = getBrowserInfo;
exports.getBrowserSupport = getBrowserSupport;
exports.getSpeechRecognition = getSpeechRecognition;
exports.isBrowser = isBrowser;
exports.isSecureContext = isSecureContext;
exports.isSpeechRecognitionSupported = isSpeechRecognitionSupported;
exports.requestMicrophonePermission = requestMicrophonePermission;
//# sourceMappingURL=browser.js.map