feat(ui): implement voice interaction and enhanced home page animations
Introduces a comprehensive voice interaction system and upgrades the home page experience with dynamic typing effects and improved layout transitions. Key changes: - Added `MicrophoneService` for managing media stream lifecycle and permissions. - Added `SpeechRecognitionService` for continuous speech-to-text processing. - Added `VoiceOrbService` using Three.js to render an animated audio-reactive orb. - Enhanced `HomePage` with a randomized typing effect for welcome titles and improved state management for message transitions. - Refactored `AiChatInputComponent` to support seamless switching between text composer and voice recording modes. - Improved `SidebarComponent` navigation logic and styling. - Updated global styles and component CSS for better theme consistency and smoother animations.
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
|
||||
export type MicrophoneErrorCode = 'unsupported' | 'denied' | 'unavailable' | 'unknown';
|
||||
|
||||
export class MicrophoneError extends Error {
|
||||
constructor(readonly code: MicrophoneErrorCode, message?: string) {
|
||||
super(message ?? code);
|
||||
this.name = 'MicrophoneError';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Owns the microphone MediaStream. It does nothing except capture:
|
||||
* the orb (audio level) and speech recognition consume it separately.
|
||||
*/
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class MicrophoneService {
|
||||
private stream: MediaStream | null = null;
|
||||
private endedCleanup: (() => void) | null = null;
|
||||
|
||||
/** `mediaDevices` is undefined on non-secure (http) origins other than localhost. */
|
||||
get isSupported(): boolean {
|
||||
return typeof navigator !== 'undefined' && !!navigator.mediaDevices?.getUserMedia;
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests microphone permission and returns the stream.
|
||||
* @param onEnded called if the capture track ends unexpectedly (e.g. device unplugged).
|
||||
*/
|
||||
async request(onEnded?: () => void): Promise<MediaStream> {
|
||||
if (!this.isSupported) {
|
||||
throw new MicrophoneError('unsupported');
|
||||
}
|
||||
|
||||
this.release();
|
||||
|
||||
let stream: MediaStream;
|
||||
try {
|
||||
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
||||
} catch (err) {
|
||||
throw this.mapError(err);
|
||||
}
|
||||
|
||||
this.stream = stream;
|
||||
|
||||
const track = stream.getAudioTracks()[0];
|
||||
if (track && onEnded) {
|
||||
const handler = (): void => onEnded();
|
||||
track.addEventListener('ended', handler);
|
||||
this.endedCleanup = () => track.removeEventListener('ended', handler);
|
||||
}
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
/** Stops every track of the given stream (defaults to the current one). */
|
||||
release(stream: MediaStream | null = this.stream): void {
|
||||
if (!stream) return;
|
||||
|
||||
if (stream === this.stream) {
|
||||
this.endedCleanup?.();
|
||||
this.endedCleanup = null;
|
||||
this.stream = null;
|
||||
}
|
||||
stream.getTracks().forEach((track) => track.stop());
|
||||
}
|
||||
|
||||
private mapError(err: unknown): MicrophoneError {
|
||||
const name = err instanceof DOMException ? err.name : '';
|
||||
switch (name) {
|
||||
case 'NotAllowedError':
|
||||
case 'SecurityError':
|
||||
case 'PermissionDeniedError':
|
||||
return new MicrophoneError('denied');
|
||||
case 'NotFoundError':
|
||||
case 'DevicesNotFoundError':
|
||||
case 'NotReadableError':
|
||||
case 'TrackStartError':
|
||||
case 'OverconstrainedError':
|
||||
return new MicrophoneError('unavailable');
|
||||
default:
|
||||
return new MicrophoneError('unknown');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user