Spaces:
Sleeping
Sleeping
File size: 14,587 Bytes
5008b66 03fe1d8 5008b66 03fe1d8 5008b66 03fe1d8 5008b66 03fe1d8 5008b66 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
import { useState, useEffect } from 'react';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Textarea } from '@/components/ui/textarea';
import { Label } from '@/components/ui/label';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
import { Badge } from '@/components/ui/badge';
import { Play, Pause, Download, Volume2, Activity } from 'lucide-react';
import { useToast } from '@/hooks/use-toast';
import api from '@/services/api';
import SpeakerScene from '../three/SpeakerScene';
import AudioWaveform from '../audio/AudioWaveform';
import MelSpectrogramVisualizer from '../audio/MelSpectrogramVisualizer';
import ProcessingPipeline from '../audio/ProcessingPipeline';
import FFTVisualizer from '../audio/FFTVisualizer';
import RealTimeStatsDashboard from '../audio/RealTimeStatsDashboard';
interface Voice {
id: string;
name: string;
audioUrl?: string;
}
interface SpeechSynthesisProps {
voices?: Voice[];
language?: 'english' | 'hindi';
onLanguageChange?: (language: 'english' | 'hindi') => void;
onSynthesisComplete?: (audioUrl: string) => void;
className?: string;
}
// Sample texts for different languages
const sampleTexts = {
english: "Hello, this is a sample text for speech synthesis. The technology can convert this text into natural-sounding speech.",
hindi: "नमस्ते, यह स्पीच सिंथेसिस के लिए एक नमूना टेक्स्ट है। यह तकनीक इस टेक्स्ट को प्राकृतिक आवाज़ में बदल सकती है।",
mixed: "Hello दोस्तों, this is a mixed language example. आज हम speech synthesis के बारे में बात करेंगे।"
};
export default function SpeechSynthesis({
voices: propVoices,
language = 'english',
onLanguageChange,
onSynthesisComplete,
className = ""
}: SpeechSynthesisProps) {
const [inputText, setInputText] = useState('');
const [selectedVoice, setSelectedVoice] = useState<string>('');
const [isSynthesizing, setIsSynthesizing] = useState(false);
const [isPlaying, setIsPlaying] = useState(false);
const [synthesizedAudio, setSynthesizedAudio] = useState<string>('');
const [audioElement, setAudioElement] = useState<HTMLAudioElement | null>(null);
const [voices, setVoices] = useState<Voice[]>(propVoices || []);
const [isLoadingVoices, setIsLoadingVoices] = useState(false);
const [spectrogramData, setSpectrogramData] = useState<number[][]>([]);
const [audioFilename, setAudioFilename] = useState<string>('');
const [showStatsDashboard, setShowStatsDashboard] = useState(false);
const [synthesizerStartTime, setSynthesizerStartTime] = useState<number | null>(null);
const { toast } = useToast();
useEffect(() => {
// Don't set default text - let user type their own
// Load voices from backend
loadVoices();
}, []);
const loadVoices = async () => {
setIsLoadingVoices(true);
try {
const data = await api.fetchVoices();
const loadedVoices = data.voices.map((v: any) => ({
id: v.id,
name: v.name,
audioUrl: v.path
}));
setVoices(loadedVoices);
console.log('Loaded voices:', loadedVoices);
} catch (error) {
console.error('Failed to load voices:', error);
toast({
title: "Failed to load voices",
description: "Could not fetch voices from backend",
variant: "destructive"
});
} finally {
setIsLoadingVoices(false);
}
};
const handleSampleTextSelect = (type: keyof typeof sampleTexts) => {
setInputText(sampleTexts[type]);
};
const handleSynthesize = async () => {
console.log('Synthesize clicked - Voice:', selectedVoice, 'Text:', inputText); // Debug log
if (!inputText.trim()) {
toast({
title: "No text provided",
description: "Please enter some text to synthesize",
variant: "destructive"
});
return;
}
if (!selectedVoice) {
toast({
title: "No voice selected",
description: "Please select a voice for synthesis",
variant: "destructive"
});
return;
}
setIsSynthesizing(true);
setSpectrogramData([]); // Reset spectrogram
setSynthesizerStartTime(Date.now()); // Record synthesis start time
try {
// Call backend API for synthesis with language support
const result = await api.synthesize(selectedVoice, inputText, language);
// Get the audio file URL from backend with cache busting
const audioUrl = api.getAudioUrl(result.audio_url) + `?t=${Date.now()}`;
// Extract filename from audio_url (e.g., "/api/audio/synthesis_abc123.wav" -> "synthesis_abc123.wav")
const filename = result.audio_url.split('/').pop() || '';
setAudioFilename(filename); // Store filename for mel-spectrogram real-time fetching
// Fetch mel-spectrogram data after synthesis
if (filename) {
try {
const spectrogramResult = await api.getSpectrogram(filename);
setSpectrogramData(spectrogramResult.spectrogram);
console.log('Spectrogram data loaded:', spectrogramResult);
} catch (err) {
console.warn('Could not load spectrogram data:', err);
// Continue without spectrogram data
}
}
// Reset audio element to force reload
if (audioElement) {
audioElement.pause();
audioElement.src = '';
setAudioElement(null);
}
setSynthesizedAudio(audioUrl);
setIsPlaying(false);
onSynthesisComplete?.(audioUrl);
toast({
title: "Synthesis complete!",
description: "Your text has been converted to speech"
});
} catch (error) {
console.error('Synthesis error:', error);
toast({
title: "Synthesis failed",
description: error instanceof Error ? error.message : "There was an error generating the speech. Please try again.",
variant: "destructive"
});
} finally {
setIsSynthesizing(false);
}
};
const handlePlay = () => {
if (!synthesizedAudio) return;
if (audioElement) {
if (isPlaying) {
audioElement.pause();
setIsPlaying(false);
} else {
audioElement.play();
setIsPlaying(true);
}
} else {
const audio = new Audio(synthesizedAudio);
audio.onended = () => setIsPlaying(false);
audio.onpause = () => setIsPlaying(false);
audio.play();
setIsPlaying(true);
setAudioElement(audio);
}
};
const handleDownload = () => {
if (synthesizedAudio) {
const a = document.createElement('a');
a.href = synthesizedAudio;
a.download = `synthesis-${Date.now()}.wav`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
};
const detectLanguage = (text: string) => {
const hindiRegex = /[\u0900-\u097F]/;
const hasHindi = hindiRegex.test(text);
const hasEnglish = /[a-zA-Z]/.test(text);
if (hasHindi && hasEnglish) return 'Mixed (English + Hindi)';
if (hasHindi) return 'Hindi';
if (hasEnglish) return 'English';
return 'Unknown';
};
return (
<>
<Card className={`glass-effect ${className}`}>
<CardHeader className="flex flex-row items-center justify-between">
<div>
<CardTitle className="gradient-text">Speech Synthesis</CardTitle>
<CardDescription>
Convert text to speech using your enrolled voices
</CardDescription>
</div>
<Button
onClick={() => setShowStatsDashboard(true)}
variant="outline"
size="sm"
className="gap-2"
title="Show real-time synthesis dashboard"
>
<Activity className="w-4 h-4" />
Dashboard
</Button>
</CardHeader>
<CardContent className="space-y-6">
{/* Language Selector */}
<div className="flex gap-2">
<button
onClick={() => onLanguageChange?.('english')}
className={`flex-1 py-2 px-4 rounded-lg font-medium transition-all ${
language === 'english'
? 'bg-blue-600 text-white shadow-lg'
: 'bg-gray-200 text-gray-700 hover:bg-gray-300'
}`}
>
🇬🇧 English
</button>
<button
onClick={() => onLanguageChange?.('hindi')}
className={`flex-1 py-2 px-4 rounded-lg font-medium transition-all ${
language === 'hindi'
? 'bg-orange-600 text-white shadow-lg'
: 'bg-gray-200 text-gray-700 hover:bg-gray-300'
}`}
>
🇮🇳 हिन्दी
</button>
</div>
{/* Voice Selection */}
<div className="space-y-2">
<Label htmlFor="voice-select">Select Voice</Label>
<Select value={selectedVoice} onValueChange={(value) => {
console.log('Voice selected:', value); // Debug log
setSelectedVoice(value);
}}>
<SelectTrigger className="bg-surface border-border">
<SelectValue placeholder="Choose a voice" />
</SelectTrigger>
<SelectContent>
{voices.map((voice) => (
<SelectItem key={voice.id} value={voice.id}>
<div className="flex items-center space-x-2">
<Volume2 className="w-4 h-4" />
<span>{voice.name}</span>
</div>
</SelectItem>
))}
</SelectContent>
</Select>
</div>
{/* Text Input */}
<div className="space-y-2">
<div className="flex items-center justify-between">
<Label htmlFor="input-text">Text to Synthesize</Label>
<Badge variant="outline">
{detectLanguage(inputText)}
</Badge>
</div>
<Textarea
id="input-text"
placeholder="Enter your text here... (English and Hindi supported)"
value={inputText}
onChange={(e) => setInputText(e.target.value)}
className="min-h-[120px] bg-surface border-border hindi-text"
maxLength={1000}
autoComplete="off"
spellCheck={false}
/>
<div className="text-sm text-muted-foreground text-right">
{inputText.length}/1000 characters
</div>
</div>
{/* Sample Text Buttons */}
<div className="space-y-2">
<Label>Sample Texts</Label>
<div className="flex flex-wrap gap-2">
<Button
variant="outline"
size="sm"
onClick={() => handleSampleTextSelect('english')}
>
English Sample
</Button>
<Button
variant="outline"
size="sm"
onClick={() => handleSampleTextSelect('hindi')}
className="hindi-text"
>
हिंदी नमूना
</Button>
<Button
variant="outline"
size="sm"
onClick={() => handleSampleTextSelect('mixed')}
>
Mixed Language
</Button>
</div>
</div>
{/* 3D Speaker and Synthesis */}
<div className="grid grid-cols-1 md:grid-cols-2 gap-6 items-center">
<div className="w-full h-48 rounded-xl overflow-hidden">
<SpeakerScene isPlaying={isSynthesizing || isPlaying} />
</div>
<div className="space-y-4">
{/* Mel-Spectrogram Visualization */}
<MelSpectrogramVisualizer
isActive={isSynthesizing || isPlaying}
spectrogramData={spectrogramData}
audioFilename={audioFilename}
/>
{/* FFT Spectrum Analyzer */}
<FFTVisualizer
isActive={isSynthesizing || isPlaying}
audioFilename={audioFilename}
synthesizerStartTime={synthesizerStartTime}
/>
{/* Processing Pipeline */}
<ProcessingPipeline
isActive={isSynthesizing}
synthesizerStartTime={synthesizerStartTime}
/>
{/* Waveform Visualization */}
<div className="h-16 flex items-center justify-center">
<AudioWaveform
isPlaying={isSynthesizing || isPlaying}
bars={15}
/>
</div>
{/* Synthesis Button */}
<Button
onClick={handleSynthesize}
disabled={isSynthesizing || !inputText.trim() || !selectedVoice}
size="lg"
className="w-full bg-accent hover:bg-accent/90 glow-accent"
>
{isSynthesizing ? 'Synthesizing...' : 'Generate Speech'}
</Button>
{/* Debug info */}
<div className="text-xs text-muted-foreground text-center">
{!selectedVoice && <span>⚠ No voice selected</span>}
{!inputText.trim() && <span>⚠ No text entered</span>}
</div>
</div>
</div>
{/* Audio Controls */}
{synthesizedAudio && !isSynthesizing && (
<div className="flex items-center justify-center space-x-4 p-4 bg-surface rounded-lg">
<Button onClick={handlePlay} size="lg">
{isPlaying ? <Pause className="w-5 h-5 mr-2" /> : <Play className="w-5 h-5 mr-2" />}
{isPlaying ? 'Pause' : 'Play'}
</Button>
<Button onClick={handleDownload} variant="outline">
<Download className="w-4 h-4 mr-2" />
Download
</Button>
</div>
)}
</CardContent>
</Card>
<RealTimeStatsDashboard
isOpen={showStatsDashboard}
onOpenChange={setShowStatsDashboard}
synthesizerStartTime={synthesizerStartTime}
isSynthesizing={isSynthesizing}
currentVoiceName={voices.find(v => v.id === selectedVoice)?.name || 'Current Voice'}
enrolledVoiceCount={voices.length}
/>
</>
);
} |