File size: 13,667 Bytes
b0b150b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState, useRef, useEffect, useCallback } from 'react';
import { Box, IconButton, Slider, Typography, Tooltip, CircularProgress } from '@mui/material';
import {
    VolumeUp as SpeakerIcon,
    PlayArrow as PlayIcon,
    Pause as PauseIcon,
    Stop as StopIcon,
    VolumeOff as MuteIcon
} from '@mui/icons-material';
import { generateTTS, getTTSAudioURL } from '../api/client';

/**
 * TTSPlayer Component
 * Plays text-to-speech audio with playback controls and word highlighting
 */
function TTSPlayer({ text, provider = 'elevenlabs', autoPlay = false, onError, onWordChange }) {
    const [isPlaying, setIsPlaying] = useState(false);
    const [isLoading, setIsLoading] = useState(false);
    const [progress, setProgress] = useState(0);
    const [volume, setVolume] = useState(1);
    const [audioURL, setAudioURL] = useState(null);
    const [duration, setDuration] = useState(0);
    const [currentWordIndex, setCurrentWordIndex] = useState(-1);
    const [words, setWords] = useState([]);

    const audioRef = useRef(null);
    const utteranceRef = useRef(null);
    const wordTimerRef = useRef(null);

    // Parse text into words on mount
    useEffect(() => {
        if (text) {
            // Split text into words while preserving punctuation
            const wordList = text.split(/(\s+)/).filter(w => w.trim().length > 0);
            setWords(wordList);
        }
    }, [text]);

    useEffect(() => {
        if (autoPlay && text) {
            handlePlay();
        }

        return () => {
            cleanup();
        };
    }, []);

    const cleanup = useCallback(() => {
        if (audioRef.current) {
            audioRef.current.pause();
            audioRef.current = null;
        }
        if (window.speechSynthesis) {
            window.speechSynthesis.cancel();
        }
        if (wordTimerRef.current) {
            clearInterval(wordTimerRef.current);
            wordTimerRef.current = null;
        }
        setCurrentWordIndex(-1);
    }, []);

    const handlePlay = async () => {
        if (!audioURL) {
            // Generate TTS first
            setIsLoading(true);

            try {
                const result = await generateTTS(text, provider);

                if (result.success) {
                    if (provider === 'web_speech' || result.client_side) {
                        // Use Web Speech API with word highlighting
                        speakWithWebSpeech(text);
                    } else {
                        // Use ElevenLabs audio file
                        const url = getTTSAudioURL(result.audio_url.split('/').pop());
                        setAudioURL(url);
                        playAudio(url);
                    }
                } else {
                    // Fallback to Web Speech API
                    if (result.fallback === 'web_speech') {
                        speakWithWebSpeech(text);
                    } else {
                        throw new Error(result.error || 'TTS generation failed');
                    }
                }
            } catch (error) {
                console.error('TTS error:', error);
                if (onError) onError(error);

                // Final fallback to Web Speech API
                speakWithWebSpeech(text);
            } finally {
                setIsLoading(false);
            }
        } else {
            // Resume existing audio
            if (audioRef.current) {
                audioRef.current.play();
                setIsPlaying(true);
            }
        }
    };

    const playAudio = (url) => {
        const audio = new Audio(url);
        audioRef.current = audio;

        audio.volume = volume;

        audio.addEventListener('loadedmetadata', () => {
            setDuration(audio.duration);
        });

        audio.addEventListener('timeupdate', () => {
            const progressPercent = (audio.currentTime / audio.duration) * 100;
            setProgress(progressPercent);

            // Estimate word highlighting for audio playback
            if (words.length > 0) {
                const wordIndex = Math.floor((progressPercent / 100) * words.length);
                if (wordIndex !== currentWordIndex && wordIndex < words.length) {
                    setCurrentWordIndex(wordIndex);
                    if (onWordChange) onWordChange(wordIndex, words[wordIndex]);
                }
            }
        });

        audio.addEventListener('ended', () => {
            setIsPlaying(false);
            setProgress(0);
            setCurrentWordIndex(-1);
            if (onWordChange) onWordChange(-1, null);
        });

        audio.addEventListener('error', (e) => {
            console.error('Audio playback error:', e);
            // Fallback to Web Speech
            speakWithWebSpeech(text);
        });

        audio.play().catch(err => {
            console.error('Audio play failed:', err);
            speakWithWebSpeech(text);
        });
        setIsPlaying(true);
    };

    const speakWithWebSpeech = (text) => {
        if ('speechSynthesis' in window) {
            // Cancel any ongoing speech
            window.speechSynthesis.cancel();

            const utterance = new SpeechSynthesisUtterance(text);
            utteranceRef.current = utterance;
            utterance.volume = volume;
            utterance.rate = 1.0;
            utterance.pitch = 1.0;

            // Word boundary event for real-time word highlighting
            let currentCharIndex = 0;

            utterance.onboundary = (event) => {
                if (event.name === 'word') {
                    // Find which word is being spoken based on character index
                    const charIndex = event.charIndex;
                    let wordIdx = 0;
                    let charCount = 0;

                    for (let i = 0; i < words.length; i++) {
                        charCount += words[i].length + 1; // +1 for space
                        if (charCount > charIndex) {
                            wordIdx = i;
                            break;
                        }
                    }

                    setCurrentWordIndex(wordIdx);
                    if (onWordChange) onWordChange(wordIdx, words[wordIdx]);

                    // Update progress
                    const progressPercent = ((wordIdx + 1) / words.length) * 100;
                    setProgress(progressPercent);
                }
            };

            utterance.onstart = () => {
                setIsPlaying(true);
                setCurrentWordIndex(0);
                if (onWordChange && words.length > 0) onWordChange(0, words[0]);
            };

            utterance.onend = () => {
                setIsPlaying(false);
                setProgress(100);
                setCurrentWordIndex(-1);
                if (onWordChange) onWordChange(-1, null);

                // Reset progress after a short delay
                setTimeout(() => setProgress(0), 500);
            };

            utterance.onerror = (error) => {
                console.error('Web Speech error:', error);
                setIsPlaying(false);
                setCurrentWordIndex(-1);
                if (onError) onError(error);
            };

            window.speechSynthesis.speak(utterance);
        } else {
            alert('Text-to-speech is not supported in your browser');
        }
    };

    const handlePause = () => {
        if (audioRef.current) {
            audioRef.current.pause();
            setIsPlaying(false);
        } else if (window.speechSynthesis) {
            window.speechSynthesis.pause();
            setIsPlaying(false);
        }
    };

    const handleResume = () => {
        if (audioRef.current) {
            audioRef.current.play();
            setIsPlaying(true);
        } else if (window.speechSynthesis) {
            window.speechSynthesis.resume();
            setIsPlaying(true);
        }
    };

    const handleStop = () => {
        cleanup();
        setIsPlaying(false);
        setProgress(0);
        setAudioURL(null);
    };

    const handleVolumeChange = (event, newValue) => {
        setVolume(newValue);
        if (audioRef.current) {
            audioRef.current.volume = newValue;
        }
    };

    const handleProgressChange = (event, newValue) => {
        if (audioRef.current && duration) {
            audioRef.current.currentTime = (newValue / 100) * duration;
            setProgress(newValue);
        }
    };

    // Render highlighted text
    const renderHighlightedText = () => {
        if (!isPlaying || words.length === 0) return null;

        return (
            <Box sx={{
                mt: 1,
                p: 1.5,
                bgcolor: 'rgba(139, 92, 246, 0.05)',
                borderRadius: 2,
                border: '1px solid rgba(139, 92, 246, 0.1)',
                maxHeight: 100,
                overflowY: 'auto',
                fontSize: '0.9rem',
                lineHeight: 1.8
            }}>
                {words.map((word, idx) => (
                    <span
                        key={idx}
                        style={{
                            padding: '2px 4px',
                            margin: '0 1px',
                            borderRadius: '4px',
                            backgroundColor: idx === currentWordIndex
                                ? 'rgba(139, 92, 246, 0.4)'
                                : idx < currentWordIndex
                                    ? 'rgba(139, 92, 246, 0.1)'
                                    : 'transparent',
                            color: idx === currentWordIndex
                                ? 'white'
                                : 'inherit',
                            fontWeight: idx === currentWordIndex ? 600 : 400,
                            transition: 'all 0.15s ease'
                        }}
                    >
                        {word}
                    </span>
                ))}
            </Box>
        );
    };

    return (
        <Box sx={{ width: '100%' }}>
            <Box sx={{
                display: 'flex',
                alignItems: 'center',
                gap: 1,
                p: 1,
                bgcolor: 'rgba(139, 92, 246, 0.05)',
                borderRadius: 2,
                border: '1px solid rgba(139, 92, 246, 0.2)',
                minWidth: 200
            }}>
                {/* Play/Pause Button */}
                <Tooltip title={isPlaying ? 'Pause' : 'Play'}>
                    <IconButton
                        onClick={isPlaying ? handlePause : handlePlay}
                        disabled={isLoading}
                        size="small"
                        sx={{
                            color: 'var(--primary)',
                            '&:hover': { bgcolor: 'rgba(139, 92, 246, 0.1)' }
                        }}
                    >
                        {isLoading ? (
                            <CircularProgress size={20} />
                        ) : isPlaying ? (
                            <PauseIcon fontSize="small" />
                        ) : (
                            <PlayIcon fontSize="small" />
                        )}
                    </IconButton>
                </Tooltip>

                {/* Progress Bar */}
                <Box sx={{ flexGrow: 1, mx: 1 }}>
                    <Slider
                        value={progress}
                        onChange={handleProgressChange}
                        disabled={!audioRef.current}
                        size="small"
                        sx={{
                            color: 'var(--primary)',
                            '& .MuiSlider-thumb': {
                                width: 12,
                                height: 12
                            }
                        }}
                    />
                </Box>

                {/* Stop Button */}
                {isPlaying && (
                    <Tooltip title="Stop">
                        <IconButton
                            onClick={handleStop}
                            size="small"
                            sx={{
                                color: 'text.secondary',
                                '&:hover': { bgcolor: 'rgba(255, 255, 255, 0.05)' }
                            }}
                        >
                            <StopIcon fontSize="small" />
                        </IconButton>
                    </Tooltip>
                )}

                {/* Volume Control */}
                <Box sx={{ display: 'flex', alignItems: 'center', width: 80 }}>
                    <IconButton size="small" sx={{ color: 'text.secondary' }}>
                        {volume === 0 ? <MuteIcon fontSize="small" /> : <SpeakerIcon fontSize="small" />}
                    </IconButton>
                    <Slider
                        value={volume}
                        onChange={handleVolumeChange}
                        min={0}
                        max={1}
                        step={0.1}
                        size="small"
                        sx={{
                            color: 'var(--primary)',
                            ml: 0.5,
                            '& .MuiSlider-thumb': {
                                width: 10,
                                height: 10
                            }
                        }}
                    />
                </Box>
            </Box>

            {/* Word Highlighting Display */}
            {renderHighlightedText()}
        </Box>
    );
}

export default TTSPlayer;