Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ function App() {

const scribearStatus = useSelector((state: RootState) => state.APIStatusReducer?.scribearServerStatus as number);
const scribearMessage = useSelector((state: RootState) => (state.APIStatusReducer as any)?.scribearServerMessage as string | undefined);
const listening = useSelector((state: RootState) => (state as any).ControlReducer?.listening === true);
const micNoAudio = useSelector((state: RootState) => (state as any).ControlReducer?.micNoAudio === true);

const [snackbarOpen, setSnackbarOpen] = useState(false);
const [snackbarMsg, setSnackbarMsg] = useState('');
Expand All @@ -39,6 +41,15 @@ function App() {
}
}, [scribearStatus]);

useEffect(() => {
// Show a snackbar if we expect mic audio but none is coming through
if (listening && micNoAudio) {
setSnackbarMsg('No microphone audio detected. Please check permissions, input device, or mic level.');
setSnackbarSeverity('warning');
setSnackbarOpen(true);
}
}, [listening, micNoAudio]);

const handleClose = (_event?: React.SyntheticEvent | Event, reason?: string) => {
if (reason === 'clickaway') return;
setSnackbarOpen(false);
Expand Down
61 changes: 60 additions & 1 deletion src/components/api/scribearServer/scribearRecognizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export class ScribearRecognizer implements Recognizer {
private language: string
private recorder?: RecordRTC;
private kSampleRate = 16000;
private lastAudioTimestamp: number | null = null;
private inactivityInterval: any = null;

urlParams = new URLSearchParams(window.location.search);
mode = this.urlParams.get('mode');
Expand All @@ -50,21 +52,69 @@ export class ScribearRecognizer implements Recognizer {
}

private async _startRecording() {
let mic_stream = await navigator.mediaDevices.getUserMedia({ audio: true, video: false });
let mic_stream: MediaStream;
try {
mic_stream = await navigator.mediaDevices.getUserMedia({ audio: true, video: false });
} catch (e) {
console.error('Failed to access microphone', e);
try { store.dispatch({ type: 'SET_MIC_INACTIVITY', payload: true }); } catch (_) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try/catch here seems unnecessary, since dispatch shouldn't throw an error when used correctly. If it is, could you put some error handling in the catch?

// Surface an API status error to prompt UI; recognizer encapsulates flag setting here
try { store.dispatch({ type: 'CHANGE_API_STATUS', payload: { scribearServerStatus: STATUS.ERROR, scribearServerMessage: 'Microphone permission denied or unavailable' } }); } catch (_) {}
throw e;
}

this.recorder = new RecordRTC(mic_stream, {
type: 'audio',
mimeType: 'audio/wav',
desiredSampRate: this.kSampleRate,
timeSlice: 50,
ondataavailable: async (blob: Blob) => {
// update last audio timestamp and mark that we've received at least one audio chunk
this.lastAudioTimestamp = Date.now();
try { (window as any).__lastAudioTimestamp = this.lastAudioTimestamp; } catch (e) {}
try { (window as any).__hasReceivedAudio = true; if ((window as any).__initialAudioTimer) { clearTimeout((window as any).__initialAudioTimer); (window as any).__initialAudioTimer = null; } } catch (e) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__lastAudioTimestamp, __hasReceivedAudio, __initialAudioTimer appear to be unused. Can we remove these setters here?

try {
const controlState = (store.getState() as any).ControlReducer;
if (controlState?.micNoAudio === true) {
store.dispatch({ type: 'SET_MIC_INACTIVITY', payload: false });
}
} catch (e) {
console.warn('Failed to clear mic inactivity', e);
}
this.socket?.send(blob);
},
recorderType: StereoAudioRecorder,
numberOfAudioChannels: 1,
});

this.recorder.startRecording();

// start inactivity monitor
const thresholdMs = 3000;
if (this.inactivityInterval == null) {
this.inactivityInterval = setInterval(() => {
try {
const state: any = store.getState();
const listening = state.ControlReducer?.listening === true;
const micNoAudio = state.ControlReducer?.micNoAudio === true;
if (listening) {
if (!this.lastAudioTimestamp || (Date.now() - this.lastAudioTimestamp > thresholdMs)) {
if (!micNoAudio) {
store.dispatch({ type: 'SET_MIC_INACTIVITY', payload: true });
}
} else {
if (micNoAudio) {
store.dispatch({ type: 'SET_MIC_INACTIVITY', payload: false });
}
}
} else {
if (micNoAudio) store.dispatch({ type: 'SET_MIC_INACTIVITY', payload: false });
}
} catch (e) {
console.warn('Error in mic inactivity interval', e);
}
}, 1000);
}
}

/**
Expand Down Expand Up @@ -179,6 +229,15 @@ export class ScribearRecognizer implements Recognizer {
if (!this.socket) { return; }
this.socket.close();
this.socket = null;
if (this.inactivityInterval) {
clearInterval(this.inactivityInterval);
this.inactivityInterval = null;
}
try {
store.dispatch({ type: 'SET_MIC_INACTIVITY', payload: false });
} catch (e) {
console.warn('Failed to clear mic inactivity on stop', e);
}
}

/**
Expand Down