karaoke-gen 0.75.54__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of karaoke-gen might be problematic. Click here for more details.
- karaoke_gen/__init__.py +38 -0
- karaoke_gen/audio_fetcher.py +1614 -0
- karaoke_gen/audio_processor.py +790 -0
- karaoke_gen/config.py +83 -0
- karaoke_gen/file_handler.py +387 -0
- karaoke_gen/instrumental_review/__init__.py +45 -0
- karaoke_gen/instrumental_review/analyzer.py +408 -0
- karaoke_gen/instrumental_review/editor.py +322 -0
- karaoke_gen/instrumental_review/models.py +171 -0
- karaoke_gen/instrumental_review/server.py +475 -0
- karaoke_gen/instrumental_review/static/index.html +1529 -0
- karaoke_gen/instrumental_review/waveform.py +409 -0
- karaoke_gen/karaoke_finalise/__init__.py +1 -0
- karaoke_gen/karaoke_finalise/karaoke_finalise.py +1833 -0
- karaoke_gen/karaoke_gen.py +1026 -0
- karaoke_gen/lyrics_processor.py +474 -0
- karaoke_gen/metadata.py +160 -0
- karaoke_gen/pipeline/__init__.py +87 -0
- karaoke_gen/pipeline/base.py +215 -0
- karaoke_gen/pipeline/context.py +230 -0
- karaoke_gen/pipeline/executors/__init__.py +21 -0
- karaoke_gen/pipeline/executors/local.py +159 -0
- karaoke_gen/pipeline/executors/remote.py +257 -0
- karaoke_gen/pipeline/stages/__init__.py +27 -0
- karaoke_gen/pipeline/stages/finalize.py +202 -0
- karaoke_gen/pipeline/stages/render.py +165 -0
- karaoke_gen/pipeline/stages/screens.py +139 -0
- karaoke_gen/pipeline/stages/separation.py +191 -0
- karaoke_gen/pipeline/stages/transcription.py +191 -0
- karaoke_gen/resources/AvenirNext-Bold.ttf +0 -0
- karaoke_gen/resources/Montserrat-Bold.ttf +0 -0
- karaoke_gen/resources/Oswald-Bold.ttf +0 -0
- karaoke_gen/resources/Oswald-SemiBold.ttf +0 -0
- karaoke_gen/resources/Zurich_Cn_BT_Bold.ttf +0 -0
- karaoke_gen/style_loader.py +531 -0
- karaoke_gen/utils/__init__.py +18 -0
- karaoke_gen/utils/bulk_cli.py +492 -0
- karaoke_gen/utils/cli_args.py +432 -0
- karaoke_gen/utils/gen_cli.py +978 -0
- karaoke_gen/utils/remote_cli.py +3268 -0
- karaoke_gen/video_background_processor.py +351 -0
- karaoke_gen/video_generator.py +424 -0
- karaoke_gen-0.75.54.dist-info/METADATA +718 -0
- karaoke_gen-0.75.54.dist-info/RECORD +287 -0
- karaoke_gen-0.75.54.dist-info/WHEEL +4 -0
- karaoke_gen-0.75.54.dist-info/entry_points.txt +5 -0
- karaoke_gen-0.75.54.dist-info/licenses/LICENSE +21 -0
- lyrics_transcriber/__init__.py +10 -0
- lyrics_transcriber/cli/__init__.py +0 -0
- lyrics_transcriber/cli/cli_main.py +285 -0
- lyrics_transcriber/core/__init__.py +0 -0
- lyrics_transcriber/core/config.py +50 -0
- lyrics_transcriber/core/controller.py +594 -0
- lyrics_transcriber/correction/__init__.py +0 -0
- lyrics_transcriber/correction/agentic/__init__.py +9 -0
- lyrics_transcriber/correction/agentic/adapter.py +71 -0
- lyrics_transcriber/correction/agentic/agent.py +313 -0
- lyrics_transcriber/correction/agentic/feedback/aggregator.py +12 -0
- lyrics_transcriber/correction/agentic/feedback/collector.py +17 -0
- lyrics_transcriber/correction/agentic/feedback/retention.py +24 -0
- lyrics_transcriber/correction/agentic/feedback/store.py +76 -0
- lyrics_transcriber/correction/agentic/handlers/__init__.py +24 -0
- lyrics_transcriber/correction/agentic/handlers/ambiguous.py +44 -0
- lyrics_transcriber/correction/agentic/handlers/background_vocals.py +68 -0
- lyrics_transcriber/correction/agentic/handlers/base.py +51 -0
- lyrics_transcriber/correction/agentic/handlers/complex_multi_error.py +46 -0
- lyrics_transcriber/correction/agentic/handlers/extra_words.py +74 -0
- lyrics_transcriber/correction/agentic/handlers/no_error.py +42 -0
- lyrics_transcriber/correction/agentic/handlers/punctuation.py +44 -0
- lyrics_transcriber/correction/agentic/handlers/registry.py +60 -0
- lyrics_transcriber/correction/agentic/handlers/repeated_section.py +44 -0
- lyrics_transcriber/correction/agentic/handlers/sound_alike.py +126 -0
- lyrics_transcriber/correction/agentic/models/__init__.py +5 -0
- lyrics_transcriber/correction/agentic/models/ai_correction.py +31 -0
- lyrics_transcriber/correction/agentic/models/correction_session.py +30 -0
- lyrics_transcriber/correction/agentic/models/enums.py +38 -0
- lyrics_transcriber/correction/agentic/models/human_feedback.py +30 -0
- lyrics_transcriber/correction/agentic/models/learning_data.py +26 -0
- lyrics_transcriber/correction/agentic/models/observability_metrics.py +28 -0
- lyrics_transcriber/correction/agentic/models/schemas.py +46 -0
- lyrics_transcriber/correction/agentic/models/utils.py +19 -0
- lyrics_transcriber/correction/agentic/observability/__init__.py +5 -0
- lyrics_transcriber/correction/agentic/observability/langfuse_integration.py +35 -0
- lyrics_transcriber/correction/agentic/observability/metrics.py +46 -0
- lyrics_transcriber/correction/agentic/observability/performance.py +19 -0
- lyrics_transcriber/correction/agentic/prompts/__init__.py +2 -0
- lyrics_transcriber/correction/agentic/prompts/classifier.py +227 -0
- lyrics_transcriber/correction/agentic/providers/__init__.py +6 -0
- lyrics_transcriber/correction/agentic/providers/base.py +36 -0
- lyrics_transcriber/correction/agentic/providers/circuit_breaker.py +145 -0
- lyrics_transcriber/correction/agentic/providers/config.py +73 -0
- lyrics_transcriber/correction/agentic/providers/constants.py +24 -0
- lyrics_transcriber/correction/agentic/providers/health.py +28 -0
- lyrics_transcriber/correction/agentic/providers/langchain_bridge.py +212 -0
- lyrics_transcriber/correction/agentic/providers/model_factory.py +209 -0
- lyrics_transcriber/correction/agentic/providers/response_cache.py +218 -0
- lyrics_transcriber/correction/agentic/providers/response_parser.py +111 -0
- lyrics_transcriber/correction/agentic/providers/retry_executor.py +127 -0
- lyrics_transcriber/correction/agentic/router.py +35 -0
- lyrics_transcriber/correction/agentic/workflows/__init__.py +5 -0
- lyrics_transcriber/correction/agentic/workflows/consensus_workflow.py +24 -0
- lyrics_transcriber/correction/agentic/workflows/correction_graph.py +59 -0
- lyrics_transcriber/correction/agentic/workflows/feedback_workflow.py +24 -0
- lyrics_transcriber/correction/anchor_sequence.py +919 -0
- lyrics_transcriber/correction/corrector.py +760 -0
- lyrics_transcriber/correction/feedback/__init__.py +2 -0
- lyrics_transcriber/correction/feedback/schemas.py +107 -0
- lyrics_transcriber/correction/feedback/store.py +236 -0
- lyrics_transcriber/correction/handlers/__init__.py +0 -0
- lyrics_transcriber/correction/handlers/base.py +52 -0
- lyrics_transcriber/correction/handlers/extend_anchor.py +149 -0
- lyrics_transcriber/correction/handlers/levenshtein.py +189 -0
- lyrics_transcriber/correction/handlers/llm.py +293 -0
- lyrics_transcriber/correction/handlers/llm_providers.py +60 -0
- lyrics_transcriber/correction/handlers/no_space_punct_match.py +154 -0
- lyrics_transcriber/correction/handlers/relaxed_word_count_match.py +85 -0
- lyrics_transcriber/correction/handlers/repeat.py +88 -0
- lyrics_transcriber/correction/handlers/sound_alike.py +259 -0
- lyrics_transcriber/correction/handlers/syllables_match.py +252 -0
- lyrics_transcriber/correction/handlers/word_count_match.py +80 -0
- lyrics_transcriber/correction/handlers/word_operations.py +187 -0
- lyrics_transcriber/correction/operations.py +352 -0
- lyrics_transcriber/correction/phrase_analyzer.py +435 -0
- lyrics_transcriber/correction/text_utils.py +30 -0
- lyrics_transcriber/frontend/.gitignore +23 -0
- lyrics_transcriber/frontend/.yarn/releases/yarn-4.7.0.cjs +935 -0
- lyrics_transcriber/frontend/.yarnrc.yml +3 -0
- lyrics_transcriber/frontend/README.md +50 -0
- lyrics_transcriber/frontend/REPLACE_ALL_FUNCTIONALITY.md +210 -0
- lyrics_transcriber/frontend/__init__.py +25 -0
- lyrics_transcriber/frontend/eslint.config.js +28 -0
- lyrics_transcriber/frontend/index.html +18 -0
- lyrics_transcriber/frontend/package.json +42 -0
- lyrics_transcriber/frontend/public/android-chrome-192x192.png +0 -0
- lyrics_transcriber/frontend/public/android-chrome-512x512.png +0 -0
- lyrics_transcriber/frontend/public/apple-touch-icon.png +0 -0
- lyrics_transcriber/frontend/public/favicon-16x16.png +0 -0
- lyrics_transcriber/frontend/public/favicon-32x32.png +0 -0
- lyrics_transcriber/frontend/public/favicon.ico +0 -0
- lyrics_transcriber/frontend/public/nomad-karaoke-logo.png +0 -0
- lyrics_transcriber/frontend/src/App.tsx +214 -0
- lyrics_transcriber/frontend/src/api.ts +254 -0
- lyrics_transcriber/frontend/src/components/AIFeedbackModal.tsx +77 -0
- lyrics_transcriber/frontend/src/components/AddLyricsModal.tsx +114 -0
- lyrics_transcriber/frontend/src/components/AgenticCorrectionMetrics.tsx +204 -0
- lyrics_transcriber/frontend/src/components/AudioPlayer.tsx +180 -0
- lyrics_transcriber/frontend/src/components/CorrectedWordWithActions.tsx +167 -0
- lyrics_transcriber/frontend/src/components/CorrectionAnnotationModal.tsx +359 -0
- lyrics_transcriber/frontend/src/components/CorrectionDetailCard.tsx +281 -0
- lyrics_transcriber/frontend/src/components/CorrectionMetrics.tsx +162 -0
- lyrics_transcriber/frontend/src/components/DurationTimelineView.tsx +257 -0
- lyrics_transcriber/frontend/src/components/EditActionBar.tsx +68 -0
- lyrics_transcriber/frontend/src/components/EditModal.tsx +702 -0
- lyrics_transcriber/frontend/src/components/EditTimelineSection.tsx +496 -0
- lyrics_transcriber/frontend/src/components/EditWordList.tsx +379 -0
- lyrics_transcriber/frontend/src/components/FileUpload.tsx +77 -0
- lyrics_transcriber/frontend/src/components/FindReplaceModal.tsx +467 -0
- lyrics_transcriber/frontend/src/components/Header.tsx +413 -0
- lyrics_transcriber/frontend/src/components/LyricsAnalyzer.tsx +1387 -0
- lyrics_transcriber/frontend/src/components/LyricsSynchronizer/SyncControls.tsx +185 -0
- lyrics_transcriber/frontend/src/components/LyricsSynchronizer/TimelineCanvas.tsx +704 -0
- lyrics_transcriber/frontend/src/components/LyricsSynchronizer/UpcomingWordsBar.tsx +80 -0
- lyrics_transcriber/frontend/src/components/LyricsSynchronizer/index.tsx +905 -0
- lyrics_transcriber/frontend/src/components/MetricsDashboard.tsx +51 -0
- lyrics_transcriber/frontend/src/components/ModeSelectionModal.tsx +127 -0
- lyrics_transcriber/frontend/src/components/ModeSelector.tsx +67 -0
- lyrics_transcriber/frontend/src/components/ModelSelector.tsx +23 -0
- lyrics_transcriber/frontend/src/components/PreviewVideoSection.tsx +144 -0
- lyrics_transcriber/frontend/src/components/ReferenceView.tsx +268 -0
- lyrics_transcriber/frontend/src/components/ReplaceAllLyricsModal.tsx +336 -0
- lyrics_transcriber/frontend/src/components/ReviewChangesModal.tsx +354 -0
- lyrics_transcriber/frontend/src/components/SegmentDetailsModal.tsx +64 -0
- lyrics_transcriber/frontend/src/components/TimelineEditor.tsx +376 -0
- lyrics_transcriber/frontend/src/components/TimingOffsetModal.tsx +131 -0
- lyrics_transcriber/frontend/src/components/TranscriptionView.tsx +256 -0
- lyrics_transcriber/frontend/src/components/WordDivider.tsx +187 -0
- lyrics_transcriber/frontend/src/components/shared/components/HighlightedText.tsx +379 -0
- lyrics_transcriber/frontend/src/components/shared/components/SourceSelector.tsx +56 -0
- lyrics_transcriber/frontend/src/components/shared/components/Word.tsx +87 -0
- lyrics_transcriber/frontend/src/components/shared/constants.ts +20 -0
- lyrics_transcriber/frontend/src/components/shared/hooks/useWordClick.ts +180 -0
- lyrics_transcriber/frontend/src/components/shared/styles.ts +13 -0
- lyrics_transcriber/frontend/src/components/shared/types.js +2 -0
- lyrics_transcriber/frontend/src/components/shared/types.ts +129 -0
- lyrics_transcriber/frontend/src/components/shared/utils/keyboardHandlers.ts +177 -0
- lyrics_transcriber/frontend/src/components/shared/utils/localStorage.ts +78 -0
- lyrics_transcriber/frontend/src/components/shared/utils/referenceLineCalculator.ts +75 -0
- lyrics_transcriber/frontend/src/components/shared/utils/segmentOperations.ts +360 -0
- lyrics_transcriber/frontend/src/components/shared/utils/timingUtils.ts +110 -0
- lyrics_transcriber/frontend/src/components/shared/utils/wordUtils.ts +22 -0
- lyrics_transcriber/frontend/src/hooks/useManualSync.ts +435 -0
- lyrics_transcriber/frontend/src/main.tsx +17 -0
- lyrics_transcriber/frontend/src/theme.ts +177 -0
- lyrics_transcriber/frontend/src/types/global.d.ts +9 -0
- lyrics_transcriber/frontend/src/types.js +2 -0
- lyrics_transcriber/frontend/src/types.ts +199 -0
- lyrics_transcriber/frontend/src/validation.ts +132 -0
- lyrics_transcriber/frontend/src/vite-env.d.ts +1 -0
- lyrics_transcriber/frontend/tsconfig.app.json +26 -0
- lyrics_transcriber/frontend/tsconfig.json +25 -0
- lyrics_transcriber/frontend/tsconfig.node.json +23 -0
- lyrics_transcriber/frontend/tsconfig.tsbuildinfo +1 -0
- lyrics_transcriber/frontend/update_version.js +11 -0
- lyrics_transcriber/frontend/vite.config.d.ts +2 -0
- lyrics_transcriber/frontend/vite.config.js +10 -0
- lyrics_transcriber/frontend/vite.config.ts +11 -0
- lyrics_transcriber/frontend/web_assets/android-chrome-192x192.png +0 -0
- lyrics_transcriber/frontend/web_assets/android-chrome-512x512.png +0 -0
- lyrics_transcriber/frontend/web_assets/apple-touch-icon.png +0 -0
- lyrics_transcriber/frontend/web_assets/assets/index-BECn1o8Q.js +43288 -0
- lyrics_transcriber/frontend/web_assets/assets/index-BECn1o8Q.js.map +1 -0
- lyrics_transcriber/frontend/web_assets/favicon-16x16.png +0 -0
- lyrics_transcriber/frontend/web_assets/favicon-32x32.png +0 -0
- lyrics_transcriber/frontend/web_assets/favicon.ico +0 -0
- lyrics_transcriber/frontend/web_assets/index.html +18 -0
- lyrics_transcriber/frontend/web_assets/nomad-karaoke-logo.png +0 -0
- lyrics_transcriber/frontend/yarn.lock +3752 -0
- lyrics_transcriber/lyrics/__init__.py +0 -0
- lyrics_transcriber/lyrics/base_lyrics_provider.py +211 -0
- lyrics_transcriber/lyrics/file_provider.py +95 -0
- lyrics_transcriber/lyrics/genius.py +384 -0
- lyrics_transcriber/lyrics/lrclib.py +231 -0
- lyrics_transcriber/lyrics/musixmatch.py +156 -0
- lyrics_transcriber/lyrics/spotify.py +290 -0
- lyrics_transcriber/lyrics/user_input_provider.py +44 -0
- lyrics_transcriber/output/__init__.py +0 -0
- lyrics_transcriber/output/ass/__init__.py +21 -0
- lyrics_transcriber/output/ass/ass.py +2088 -0
- lyrics_transcriber/output/ass/ass_specs.txt +732 -0
- lyrics_transcriber/output/ass/config.py +180 -0
- lyrics_transcriber/output/ass/constants.py +23 -0
- lyrics_transcriber/output/ass/event.py +94 -0
- lyrics_transcriber/output/ass/formatters.py +132 -0
- lyrics_transcriber/output/ass/lyrics_line.py +265 -0
- lyrics_transcriber/output/ass/lyrics_screen.py +252 -0
- lyrics_transcriber/output/ass/section_detector.py +89 -0
- lyrics_transcriber/output/ass/section_screen.py +106 -0
- lyrics_transcriber/output/ass/style.py +187 -0
- lyrics_transcriber/output/cdg.py +619 -0
- lyrics_transcriber/output/cdgmaker/__init__.py +0 -0
- lyrics_transcriber/output/cdgmaker/cdg.py +262 -0
- lyrics_transcriber/output/cdgmaker/composer.py +2260 -0
- lyrics_transcriber/output/cdgmaker/config.py +151 -0
- lyrics_transcriber/output/cdgmaker/images/instrumental.png +0 -0
- lyrics_transcriber/output/cdgmaker/images/intro.png +0 -0
- lyrics_transcriber/output/cdgmaker/pack.py +507 -0
- lyrics_transcriber/output/cdgmaker/render.py +346 -0
- lyrics_transcriber/output/cdgmaker/transitions/centertexttoplogobottomtext.png +0 -0
- lyrics_transcriber/output/cdgmaker/transitions/circlein.png +0 -0
- lyrics_transcriber/output/cdgmaker/transitions/circleout.png +0 -0
- lyrics_transcriber/output/cdgmaker/transitions/fizzle.png +0 -0
- lyrics_transcriber/output/cdgmaker/transitions/largecentertexttoplogo.png +0 -0
- lyrics_transcriber/output/cdgmaker/transitions/rectangle.png +0 -0
- lyrics_transcriber/output/cdgmaker/transitions/spiral.png +0 -0
- lyrics_transcriber/output/cdgmaker/transitions/topleftmusicalnotes.png +0 -0
- lyrics_transcriber/output/cdgmaker/transitions/wipein.png +0 -0
- lyrics_transcriber/output/cdgmaker/transitions/wipeleft.png +0 -0
- lyrics_transcriber/output/cdgmaker/transitions/wipeout.png +0 -0
- lyrics_transcriber/output/cdgmaker/transitions/wiperight.png +0 -0
- lyrics_transcriber/output/cdgmaker/utils.py +132 -0
- lyrics_transcriber/output/countdown_processor.py +306 -0
- lyrics_transcriber/output/fonts/AvenirNext-Bold.ttf +0 -0
- lyrics_transcriber/output/fonts/DMSans-VariableFont_opsz,wght.ttf +0 -0
- lyrics_transcriber/output/fonts/DMSerifDisplay-Regular.ttf +0 -0
- lyrics_transcriber/output/fonts/Oswald-SemiBold.ttf +0 -0
- lyrics_transcriber/output/fonts/Zurich_Cn_BT_Bold.ttf +0 -0
- lyrics_transcriber/output/fonts/arial.ttf +0 -0
- lyrics_transcriber/output/fonts/georgia.ttf +0 -0
- lyrics_transcriber/output/fonts/verdana.ttf +0 -0
- lyrics_transcriber/output/generator.py +257 -0
- lyrics_transcriber/output/lrc_to_cdg.py +61 -0
- lyrics_transcriber/output/lyrics_file.py +102 -0
- lyrics_transcriber/output/plain_text.py +96 -0
- lyrics_transcriber/output/segment_resizer.py +431 -0
- lyrics_transcriber/output/subtitles.py +397 -0
- lyrics_transcriber/output/video.py +544 -0
- lyrics_transcriber/review/__init__.py +0 -0
- lyrics_transcriber/review/server.py +676 -0
- lyrics_transcriber/storage/__init__.py +0 -0
- lyrics_transcriber/storage/dropbox.py +225 -0
- lyrics_transcriber/transcribers/__init__.py +0 -0
- lyrics_transcriber/transcribers/audioshake.py +379 -0
- lyrics_transcriber/transcribers/base_transcriber.py +157 -0
- lyrics_transcriber/transcribers/whisper.py +330 -0
- lyrics_transcriber/types.py +650 -0
- lyrics_transcriber/utils/__init__.py +0 -0
- lyrics_transcriber/utils/word_utils.py +27 -0
|
@@ -0,0 +1,704 @@
|
|
|
1
|
+
import { useRef, useEffect, useCallback, useState, memo } from 'react'
|
|
2
|
+
import { Box, IconButton, Tooltip } from '@mui/material'
|
|
3
|
+
import ArrowBackIcon from '@mui/icons-material/ArrowBack'
|
|
4
|
+
import ArrowForwardIcon from '@mui/icons-material/ArrowForward'
|
|
5
|
+
import { Word, LyricsSegment } from '../../types'
|
|
6
|
+
|
|
7
|
+
interface TimelineCanvasProps {
|
|
8
|
+
words: Word[]
|
|
9
|
+
segments: LyricsSegment[]
|
|
10
|
+
visibleStartTime: number
|
|
11
|
+
visibleEndTime: number
|
|
12
|
+
currentTime: number
|
|
13
|
+
selectedWordIds: Set<string>
|
|
14
|
+
onWordClick: (wordId: string, event: React.MouseEvent) => void
|
|
15
|
+
onBackgroundClick: () => void
|
|
16
|
+
onTimeBarClick: (time: number) => void
|
|
17
|
+
onSelectionComplete: (wordIds: string[]) => void
|
|
18
|
+
onWordTimingChange: (wordId: string, newStartTime: number, newEndTime: number) => void
|
|
19
|
+
onWordsMove: (updates: Array<{ wordId: string; newStartTime: number; newEndTime: number }>) => void
|
|
20
|
+
syncWordIndex: number
|
|
21
|
+
isManualSyncing: boolean
|
|
22
|
+
onScrollChange: (newStartTime: number) => void
|
|
23
|
+
audioDuration: number
|
|
24
|
+
zoomSeconds: number
|
|
25
|
+
height?: number
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Constants for rendering
|
|
29
|
+
const TIME_BAR_HEIGHT = 28
|
|
30
|
+
const WORD_BLOCK_HEIGHT = 24
|
|
31
|
+
const WORD_LEVEL_SPACING = 50
|
|
32
|
+
const CANVAS_PADDING = 8
|
|
33
|
+
const TEXT_ABOVE_BLOCK = 14
|
|
34
|
+
const RESIZE_HANDLE_SIZE = 8
|
|
35
|
+
const RESIZE_HANDLE_HITAREA = 12
|
|
36
|
+
const PLAYHEAD_COLOR = '#ffffff'
|
|
37
|
+
const WORD_BLOCK_COLOR = '#d32f2f'
|
|
38
|
+
const WORD_BLOCK_SELECTED_COLOR = '#b71c1c'
|
|
39
|
+
const WORD_BLOCK_CURRENT_COLOR = '#f44336'
|
|
40
|
+
const WORD_TEXT_CURRENT_COLOR = '#d32f2f'
|
|
41
|
+
const UPCOMING_WORD_BG = '#fff9c4'
|
|
42
|
+
const UPCOMING_WORD_TEXT = '#000000'
|
|
43
|
+
const TIME_BAR_BG = '#f5f5f5'
|
|
44
|
+
const TIME_BAR_TEXT = '#666666'
|
|
45
|
+
const TIMELINE_BG = '#e0e0e0'
|
|
46
|
+
|
|
47
|
+
// Drag modes
|
|
48
|
+
type DragMode = 'none' | 'selection' | 'resize' | 'move'
|
|
49
|
+
|
|
50
|
+
// Build a map of word ID to segment index
|
|
51
|
+
function buildWordToSegmentMap(segments: LyricsSegment[]): Map<string, number> {
|
|
52
|
+
const map = new Map<string, number>()
|
|
53
|
+
segments.forEach((segment, idx) => {
|
|
54
|
+
segment.words.forEach(word => {
|
|
55
|
+
map.set(word.id, idx)
|
|
56
|
+
})
|
|
57
|
+
})
|
|
58
|
+
return map
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Calculate which vertical level a word should be on
|
|
62
|
+
function calculateWordLevels(words: Word[], segments: LyricsSegment[]): Map<string, number> {
|
|
63
|
+
const levels = new Map<string, number>()
|
|
64
|
+
const wordToSegment = buildWordToSegmentMap(segments)
|
|
65
|
+
|
|
66
|
+
const segmentsWithTiming = segments
|
|
67
|
+
.map((segment, idx) => {
|
|
68
|
+
const timedWords = segment.words.filter(w => w.start_time !== null)
|
|
69
|
+
const minStart = timedWords.length > 0
|
|
70
|
+
? Math.min(...timedWords.map(w => w.start_time!))
|
|
71
|
+
: Infinity
|
|
72
|
+
return { idx, minStart }
|
|
73
|
+
})
|
|
74
|
+
.filter(s => s.minStart !== Infinity)
|
|
75
|
+
.sort((a, b) => a.minStart - b.minStart)
|
|
76
|
+
|
|
77
|
+
const segmentLevels = new Map<number, number>()
|
|
78
|
+
segmentsWithTiming.forEach(({ idx }, orderIndex) => {
|
|
79
|
+
segmentLevels.set(idx, orderIndex % 2)
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
for (const word of words) {
|
|
83
|
+
const segmentIdx = wordToSegment.get(word.id)
|
|
84
|
+
if (segmentIdx !== undefined && segmentLevels.has(segmentIdx)) {
|
|
85
|
+
levels.set(word.id, segmentLevels.get(segmentIdx)!)
|
|
86
|
+
} else {
|
|
87
|
+
levels.set(word.id, 0)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return levels
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function formatTime(seconds: number): string {
|
|
95
|
+
const mins = Math.floor(seconds / 60)
|
|
96
|
+
const secs = Math.floor(seconds % 60)
|
|
97
|
+
return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const TimelineCanvas = memo(function TimelineCanvas({
|
|
101
|
+
words,
|
|
102
|
+
segments,
|
|
103
|
+
visibleStartTime,
|
|
104
|
+
visibleEndTime,
|
|
105
|
+
currentTime,
|
|
106
|
+
selectedWordIds,
|
|
107
|
+
onWordClick,
|
|
108
|
+
onBackgroundClick,
|
|
109
|
+
onTimeBarClick,
|
|
110
|
+
onSelectionComplete,
|
|
111
|
+
onWordTimingChange,
|
|
112
|
+
onWordsMove,
|
|
113
|
+
syncWordIndex,
|
|
114
|
+
isManualSyncing,
|
|
115
|
+
onScrollChange,
|
|
116
|
+
audioDuration,
|
|
117
|
+
zoomSeconds,
|
|
118
|
+
height = 200
|
|
119
|
+
}: TimelineCanvasProps) {
|
|
120
|
+
const canvasRef = useRef<HTMLCanvasElement>(null)
|
|
121
|
+
const containerRef = useRef<HTMLDivElement>(null)
|
|
122
|
+
const [canvasWidth, setCanvasWidth] = useState(800)
|
|
123
|
+
const animationFrameRef = useRef<number>()
|
|
124
|
+
const wordLevelsRef = useRef<Map<string, number>>(new Map())
|
|
125
|
+
|
|
126
|
+
// Drag state
|
|
127
|
+
const [dragMode, setDragMode] = useState<DragMode>('none')
|
|
128
|
+
const dragStartRef = useRef<{ x: number; y: number; time: number } | null>(null)
|
|
129
|
+
const dragWordIdRef = useRef<string | null>(null)
|
|
130
|
+
const dragOriginalTimesRef = useRef<Map<string, { start: number; end: number }>>(new Map())
|
|
131
|
+
|
|
132
|
+
// Selection rectangle
|
|
133
|
+
const [selectionRect, setSelectionRect] = useState<{
|
|
134
|
+
startX: number; startY: number; endX: number; endY: number
|
|
135
|
+
} | null>(null)
|
|
136
|
+
|
|
137
|
+
// Hover state for showing resize handle
|
|
138
|
+
const [hoveredWordId, setHoveredWordId] = useState<string | null>(null)
|
|
139
|
+
const [cursorStyle, setCursorStyle] = useState<string>('default')
|
|
140
|
+
|
|
141
|
+
// Update canvas width on resize
|
|
142
|
+
useEffect(() => {
|
|
143
|
+
const updateWidth = () => {
|
|
144
|
+
if (containerRef.current) {
|
|
145
|
+
setCanvasWidth(containerRef.current.clientWidth)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
updateWidth()
|
|
150
|
+
const resizeObserver = new ResizeObserver(updateWidth)
|
|
151
|
+
if (containerRef.current) {
|
|
152
|
+
resizeObserver.observe(containerRef.current)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return () => resizeObserver.disconnect()
|
|
156
|
+
}, [])
|
|
157
|
+
|
|
158
|
+
// Calculate word levels when words or segments change
|
|
159
|
+
useEffect(() => {
|
|
160
|
+
wordLevelsRef.current = calculateWordLevels(words, segments)
|
|
161
|
+
}, [words, segments])
|
|
162
|
+
|
|
163
|
+
// Convert time to x position
|
|
164
|
+
const timeToX = useCallback((time: number): number => {
|
|
165
|
+
const duration = visibleEndTime - visibleStartTime
|
|
166
|
+
if (duration <= 0) return 0
|
|
167
|
+
return CANVAS_PADDING + ((time - visibleStartTime) / duration) * (canvasWidth - CANVAS_PADDING * 2)
|
|
168
|
+
}, [visibleStartTime, visibleEndTime, canvasWidth])
|
|
169
|
+
|
|
170
|
+
// Convert x position to time
|
|
171
|
+
const xToTime = useCallback((x: number): number => {
|
|
172
|
+
const duration = visibleEndTime - visibleStartTime
|
|
173
|
+
return visibleStartTime + ((x - CANVAS_PADDING) / (canvasWidth - CANVAS_PADDING * 2)) * duration
|
|
174
|
+
}, [visibleStartTime, visibleEndTime, canvasWidth])
|
|
175
|
+
|
|
176
|
+
// Get word bounds
|
|
177
|
+
const getWordBounds = useCallback((word: Word) => {
|
|
178
|
+
if (word.start_time === null || word.end_time === null) return null
|
|
179
|
+
|
|
180
|
+
const level = wordLevelsRef.current.get(word.id) || 0
|
|
181
|
+
const startX = timeToX(word.start_time)
|
|
182
|
+
const endX = timeToX(word.end_time)
|
|
183
|
+
const blockWidth = Math.max(endX - startX, 4)
|
|
184
|
+
const y = TIME_BAR_HEIGHT + CANVAS_PADDING + TEXT_ABOVE_BLOCK + level * WORD_LEVEL_SPACING
|
|
185
|
+
|
|
186
|
+
return { startX, endX, blockWidth, y, level }
|
|
187
|
+
}, [timeToX])
|
|
188
|
+
|
|
189
|
+
// Check if position is near resize handle
|
|
190
|
+
const isNearResizeHandlePos = useCallback((word: Word, x: number, y: number): boolean => {
|
|
191
|
+
const bounds = getWordBounds(word)
|
|
192
|
+
if (!bounds) return false
|
|
193
|
+
|
|
194
|
+
const handleX = bounds.startX + bounds.blockWidth - RESIZE_HANDLE_SIZE / 2
|
|
195
|
+
const handleY = bounds.y + WORD_BLOCK_HEIGHT / 2
|
|
196
|
+
|
|
197
|
+
return Math.abs(x - handleX) < RESIZE_HANDLE_HITAREA / 2 &&
|
|
198
|
+
Math.abs(y - handleY) < RESIZE_HANDLE_HITAREA / 2
|
|
199
|
+
}, [getWordBounds])
|
|
200
|
+
|
|
201
|
+
// Find word at position
|
|
202
|
+
const findWordAtPosition = useCallback((x: number, y: number): Word | null => {
|
|
203
|
+
for (const word of words) {
|
|
204
|
+
const bounds = getWordBounds(word)
|
|
205
|
+
if (!bounds) continue
|
|
206
|
+
|
|
207
|
+
if (x >= bounds.startX && x <= bounds.startX + bounds.blockWidth &&
|
|
208
|
+
y >= bounds.y && y <= bounds.y + WORD_BLOCK_HEIGHT) {
|
|
209
|
+
return word
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return null
|
|
213
|
+
}, [words, getWordBounds])
|
|
214
|
+
|
|
215
|
+
// Find words in selection rectangle
|
|
216
|
+
const findWordsInRect = useCallback((rect: { startX: number; startY: number; endX: number; endY: number }): string[] => {
|
|
217
|
+
const rectLeft = Math.min(rect.startX, rect.endX)
|
|
218
|
+
const rectRight = Math.max(rect.startX, rect.endX)
|
|
219
|
+
const rectTop = Math.min(rect.startY, rect.endY)
|
|
220
|
+
const rectBottom = Math.max(rect.startY, rect.endY)
|
|
221
|
+
|
|
222
|
+
const selectedIds: string[] = []
|
|
223
|
+
|
|
224
|
+
for (const word of words) {
|
|
225
|
+
const bounds = getWordBounds(word)
|
|
226
|
+
if (!bounds) continue
|
|
227
|
+
|
|
228
|
+
if (bounds.startX + bounds.blockWidth >= rectLeft && bounds.startX <= rectRight &&
|
|
229
|
+
bounds.y + WORD_BLOCK_HEIGHT >= rectTop && bounds.y <= rectBottom) {
|
|
230
|
+
selectedIds.push(word.id)
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return selectedIds
|
|
235
|
+
}, [words, getWordBounds])
|
|
236
|
+
|
|
237
|
+
// Draw the timeline
|
|
238
|
+
const draw = useCallback(() => {
|
|
239
|
+
const canvas = canvasRef.current
|
|
240
|
+
if (!canvas) return
|
|
241
|
+
|
|
242
|
+
const ctx = canvas.getContext('2d')
|
|
243
|
+
if (!ctx) return
|
|
244
|
+
|
|
245
|
+
const dpr = window.devicePixelRatio || 1
|
|
246
|
+
canvas.width = canvasWidth * dpr
|
|
247
|
+
canvas.height = height * dpr
|
|
248
|
+
ctx.scale(dpr, dpr)
|
|
249
|
+
|
|
250
|
+
// Clear canvas
|
|
251
|
+
ctx.fillStyle = TIMELINE_BG
|
|
252
|
+
ctx.fillRect(0, 0, canvasWidth, height)
|
|
253
|
+
|
|
254
|
+
// Draw time bar background
|
|
255
|
+
ctx.fillStyle = TIME_BAR_BG
|
|
256
|
+
ctx.fillRect(0, 0, canvasWidth, TIME_BAR_HEIGHT)
|
|
257
|
+
|
|
258
|
+
// Draw time markers
|
|
259
|
+
const duration = visibleEndTime - visibleStartTime
|
|
260
|
+
const secondsPerTick = duration > 15 ? 2 : duration > 8 ? 1 : 0.5
|
|
261
|
+
const startSecond = Math.ceil(visibleStartTime / secondsPerTick) * secondsPerTick
|
|
262
|
+
|
|
263
|
+
ctx.fillStyle = TIME_BAR_TEXT
|
|
264
|
+
ctx.font = '11px system-ui, -apple-system, sans-serif'
|
|
265
|
+
ctx.textAlign = 'center'
|
|
266
|
+
|
|
267
|
+
for (let t = startSecond; t <= visibleEndTime; t += secondsPerTick) {
|
|
268
|
+
const x = timeToX(t)
|
|
269
|
+
|
|
270
|
+
ctx.beginPath()
|
|
271
|
+
ctx.strokeStyle = '#999999'
|
|
272
|
+
ctx.lineWidth = 1
|
|
273
|
+
ctx.moveTo(x, TIME_BAR_HEIGHT - 6)
|
|
274
|
+
ctx.lineTo(x, TIME_BAR_HEIGHT)
|
|
275
|
+
ctx.stroke()
|
|
276
|
+
|
|
277
|
+
if (t % 1 === 0) {
|
|
278
|
+
ctx.fillText(formatTime(t), x, TIME_BAR_HEIGHT - 10)
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
ctx.beginPath()
|
|
283
|
+
ctx.strokeStyle = '#cccccc'
|
|
284
|
+
ctx.lineWidth = 1
|
|
285
|
+
ctx.moveTo(0, TIME_BAR_HEIGHT)
|
|
286
|
+
ctx.lineTo(canvasWidth, TIME_BAR_HEIGHT)
|
|
287
|
+
ctx.stroke()
|
|
288
|
+
|
|
289
|
+
const wordToSegment = buildWordToSegmentMap(segments)
|
|
290
|
+
const syncedWords = words.filter(w => w.start_time !== null && w.end_time !== null)
|
|
291
|
+
|
|
292
|
+
const currentWordId = syncedWords.find(w =>
|
|
293
|
+
currentTime >= w.start_time! && currentTime <= w.end_time!
|
|
294
|
+
)?.id || null
|
|
295
|
+
|
|
296
|
+
// First pass: draw all blocks
|
|
297
|
+
for (const word of syncedWords) {
|
|
298
|
+
const bounds = getWordBounds(word)
|
|
299
|
+
if (!bounds) continue
|
|
300
|
+
|
|
301
|
+
const isSelected = selectedWordIds.has(word.id)
|
|
302
|
+
const isCurrent = word.id === currentWordId
|
|
303
|
+
const isHovered = word.id === hoveredWordId
|
|
304
|
+
|
|
305
|
+
// Draw word block background
|
|
306
|
+
if (isSelected) {
|
|
307
|
+
ctx.fillStyle = WORD_BLOCK_SELECTED_COLOR
|
|
308
|
+
} else if (isCurrent) {
|
|
309
|
+
ctx.fillStyle = WORD_BLOCK_CURRENT_COLOR
|
|
310
|
+
} else {
|
|
311
|
+
ctx.fillStyle = WORD_BLOCK_COLOR
|
|
312
|
+
}
|
|
313
|
+
ctx.fillRect(bounds.startX, bounds.y, bounds.blockWidth, WORD_BLOCK_HEIGHT)
|
|
314
|
+
|
|
315
|
+
// Draw selection border
|
|
316
|
+
if (isSelected) {
|
|
317
|
+
ctx.strokeStyle = '#ffffff'
|
|
318
|
+
ctx.lineWidth = 2
|
|
319
|
+
ctx.strokeRect(bounds.startX, bounds.y, bounds.blockWidth, WORD_BLOCK_HEIGHT)
|
|
320
|
+
|
|
321
|
+
// Draw resize handle (white dot on right edge) for selected words when hovered
|
|
322
|
+
if (isHovered || selectedWordIds.size === 1) {
|
|
323
|
+
const handleX = bounds.startX + bounds.blockWidth - RESIZE_HANDLE_SIZE / 2
|
|
324
|
+
const handleY = bounds.y + WORD_BLOCK_HEIGHT / 2
|
|
325
|
+
|
|
326
|
+
ctx.beginPath()
|
|
327
|
+
ctx.fillStyle = '#ffffff'
|
|
328
|
+
ctx.arc(handleX, handleY, RESIZE_HANDLE_SIZE / 2, 0, Math.PI * 2)
|
|
329
|
+
ctx.fill()
|
|
330
|
+
ctx.strokeStyle = '#666666'
|
|
331
|
+
ctx.lineWidth = 1
|
|
332
|
+
ctx.stroke()
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// Second pass: draw text
|
|
338
|
+
const wordsBySegment = new Map<number, Word[]>()
|
|
339
|
+
for (const word of syncedWords) {
|
|
340
|
+
const segIdx = wordToSegment.get(word.id)
|
|
341
|
+
if (segIdx !== undefined) {
|
|
342
|
+
if (!wordsBySegment.has(segIdx)) {
|
|
343
|
+
wordsBySegment.set(segIdx, [])
|
|
344
|
+
}
|
|
345
|
+
wordsBySegment.get(segIdx)!.push(word)
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
ctx.font = '11px system-ui, -apple-system, sans-serif'
|
|
350
|
+
ctx.textAlign = 'left'
|
|
351
|
+
|
|
352
|
+
for (const [, segmentWords] of wordsBySegment) {
|
|
353
|
+
const sortedWords = [...segmentWords].sort((a, b) =>
|
|
354
|
+
(a.start_time || 0) - (b.start_time || 0)
|
|
355
|
+
)
|
|
356
|
+
|
|
357
|
+
if (sortedWords.length === 0) continue
|
|
358
|
+
|
|
359
|
+
const level = wordLevelsRef.current.get(sortedWords[0].id) || 0
|
|
360
|
+
const textY = TIME_BAR_HEIGHT + CANVAS_PADDING + TEXT_ABOVE_BLOCK + level * WORD_LEVEL_SPACING - 3
|
|
361
|
+
|
|
362
|
+
let rightmostTextEnd = -Infinity
|
|
363
|
+
|
|
364
|
+
for (const word of sortedWords) {
|
|
365
|
+
const blockStartX = timeToX(word.start_time!)
|
|
366
|
+
const textWidth = ctx.measureText(word.text).width
|
|
367
|
+
const textStartX = Math.max(blockStartX, rightmostTextEnd + 3)
|
|
368
|
+
|
|
369
|
+
if (textStartX < canvasWidth - 10) {
|
|
370
|
+
const isCurrent = word.id === currentWordId
|
|
371
|
+
ctx.fillStyle = isCurrent ? WORD_TEXT_CURRENT_COLOR : '#333333'
|
|
372
|
+
ctx.fillText(word.text, textStartX, textY)
|
|
373
|
+
rightmostTextEnd = textStartX + textWidth
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// Draw upcoming words during sync
|
|
379
|
+
if (isManualSyncing && syncWordIndex >= 0) {
|
|
380
|
+
const upcomingWords = words.slice(syncWordIndex).filter(w => w.start_time === null)
|
|
381
|
+
const playheadX = timeToX(currentTime)
|
|
382
|
+
let offsetX = playheadX + 10
|
|
383
|
+
|
|
384
|
+
ctx.font = '11px system-ui, -apple-system, sans-serif'
|
|
385
|
+
|
|
386
|
+
for (let i = 0; i < Math.min(upcomingWords.length, 12); i++) {
|
|
387
|
+
const word = upcomingWords[i]
|
|
388
|
+
const textWidth = ctx.measureText(word.text).width + 10
|
|
389
|
+
|
|
390
|
+
ctx.fillStyle = UPCOMING_WORD_BG
|
|
391
|
+
ctx.fillRect(offsetX, TIME_BAR_HEIGHT + CANVAS_PADDING + WORD_LEVEL_SPACING + 60, textWidth, 20)
|
|
392
|
+
|
|
393
|
+
ctx.fillStyle = UPCOMING_WORD_TEXT
|
|
394
|
+
ctx.textAlign = 'left'
|
|
395
|
+
ctx.fillText(word.text, offsetX + 5, TIME_BAR_HEIGHT + CANVAS_PADDING + WORD_LEVEL_SPACING + 74)
|
|
396
|
+
|
|
397
|
+
offsetX += textWidth + 3
|
|
398
|
+
if (offsetX > canvasWidth - 20) break
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// Draw playhead
|
|
403
|
+
if (currentTime >= visibleStartTime && currentTime <= visibleEndTime) {
|
|
404
|
+
const playheadX = timeToX(currentTime)
|
|
405
|
+
|
|
406
|
+
ctx.beginPath()
|
|
407
|
+
ctx.fillStyle = PLAYHEAD_COLOR
|
|
408
|
+
ctx.strokeStyle = '#333333'
|
|
409
|
+
ctx.lineWidth = 1
|
|
410
|
+
ctx.moveTo(playheadX - 6, 2)
|
|
411
|
+
ctx.lineTo(playheadX + 6, 2)
|
|
412
|
+
ctx.lineTo(playheadX, TIME_BAR_HEIGHT - 4)
|
|
413
|
+
ctx.closePath()
|
|
414
|
+
ctx.fill()
|
|
415
|
+
ctx.stroke()
|
|
416
|
+
|
|
417
|
+
ctx.beginPath()
|
|
418
|
+
ctx.strokeStyle = PLAYHEAD_COLOR
|
|
419
|
+
ctx.lineWidth = 2
|
|
420
|
+
ctx.moveTo(playheadX, TIME_BAR_HEIGHT)
|
|
421
|
+
ctx.lineTo(playheadX, height)
|
|
422
|
+
ctx.stroke()
|
|
423
|
+
|
|
424
|
+
ctx.beginPath()
|
|
425
|
+
ctx.strokeStyle = 'rgba(0,0,0,0.4)'
|
|
426
|
+
ctx.lineWidth = 1
|
|
427
|
+
ctx.moveTo(playheadX + 1, TIME_BAR_HEIGHT)
|
|
428
|
+
ctx.lineTo(playheadX + 1, height)
|
|
429
|
+
ctx.stroke()
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// Draw selection rectangle
|
|
433
|
+
if (selectionRect) {
|
|
434
|
+
ctx.fillStyle = 'rgba(25, 118, 210, 0.2)'
|
|
435
|
+
ctx.strokeStyle = 'rgba(25, 118, 210, 0.8)'
|
|
436
|
+
ctx.lineWidth = 1
|
|
437
|
+
|
|
438
|
+
const rectX = Math.min(selectionRect.startX, selectionRect.endX)
|
|
439
|
+
const rectY = Math.min(selectionRect.startY, selectionRect.endY)
|
|
440
|
+
const rectW = Math.abs(selectionRect.endX - selectionRect.startX)
|
|
441
|
+
const rectH = Math.abs(selectionRect.endY - selectionRect.startY)
|
|
442
|
+
|
|
443
|
+
ctx.fillRect(rectX, rectY, rectW, rectH)
|
|
444
|
+
ctx.strokeRect(rectX, rectY, rectW, rectH)
|
|
445
|
+
}
|
|
446
|
+
}, [
|
|
447
|
+
canvasWidth, height, visibleStartTime, visibleEndTime, currentTime,
|
|
448
|
+
words, segments, selectedWordIds, selectionRect, hoveredWordId,
|
|
449
|
+
syncWordIndex, isManualSyncing, timeToX, getWordBounds
|
|
450
|
+
])
|
|
451
|
+
|
|
452
|
+
// Animation frame
|
|
453
|
+
useEffect(() => {
|
|
454
|
+
const animate = () => {
|
|
455
|
+
draw()
|
|
456
|
+
animationFrameRef.current = requestAnimationFrame(animate)
|
|
457
|
+
}
|
|
458
|
+
animate()
|
|
459
|
+
|
|
460
|
+
return () => {
|
|
461
|
+
if (animationFrameRef.current) {
|
|
462
|
+
cancelAnimationFrame(animationFrameRef.current)
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}, [draw])
|
|
466
|
+
|
|
467
|
+
// Mouse handlers
|
|
468
|
+
const handleMouseDown = useCallback((e: React.MouseEvent<HTMLCanvasElement>) => {
|
|
469
|
+
const rect = canvasRef.current?.getBoundingClientRect()
|
|
470
|
+
if (!rect) return
|
|
471
|
+
|
|
472
|
+
const x = e.clientX - rect.left
|
|
473
|
+
const y = e.clientY - rect.top
|
|
474
|
+
const time = xToTime(x)
|
|
475
|
+
|
|
476
|
+
// Time bar click
|
|
477
|
+
if (y < TIME_BAR_HEIGHT) {
|
|
478
|
+
onTimeBarClick(Math.max(0, time))
|
|
479
|
+
return
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
const clickedWord = findWordAtPosition(x, y)
|
|
483
|
+
|
|
484
|
+
if (clickedWord && selectedWordIds.has(clickedWord.id)) {
|
|
485
|
+
// Check if clicking on resize handle
|
|
486
|
+
if (isNearResizeHandlePos(clickedWord, x, y)) {
|
|
487
|
+
// Start resize
|
|
488
|
+
setDragMode('resize')
|
|
489
|
+
dragStartRef.current = { x, y, time }
|
|
490
|
+
dragWordIdRef.current = clickedWord.id
|
|
491
|
+
dragOriginalTimesRef.current = new Map([[clickedWord.id, {
|
|
492
|
+
start: clickedWord.start_time!,
|
|
493
|
+
end: clickedWord.end_time!
|
|
494
|
+
}]])
|
|
495
|
+
return
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
// Start move (for all selected words)
|
|
499
|
+
setDragMode('move')
|
|
500
|
+
dragStartRef.current = { x, y, time }
|
|
501
|
+
dragWordIdRef.current = clickedWord.id
|
|
502
|
+
|
|
503
|
+
// Store original times for all selected words
|
|
504
|
+
const originalTimes = new Map<string, { start: number; end: number }>()
|
|
505
|
+
for (const wordId of selectedWordIds) {
|
|
506
|
+
const word = words.find(w => w.id === wordId)
|
|
507
|
+
if (word && word.start_time !== null && word.end_time !== null) {
|
|
508
|
+
originalTimes.set(wordId, { start: word.start_time, end: word.end_time })
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
dragOriginalTimesRef.current = originalTimes
|
|
512
|
+
return
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
if (clickedWord) {
|
|
516
|
+
// Click on unselected word - select it
|
|
517
|
+
onWordClick(clickedWord.id, e)
|
|
518
|
+
return
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// Background - start selection
|
|
522
|
+
setDragMode('selection')
|
|
523
|
+
dragStartRef.current = { x, y, time }
|
|
524
|
+
setSelectionRect({ startX: x, startY: y, endX: x, endY: y })
|
|
525
|
+
}, [xToTime, onTimeBarClick, findWordAtPosition, selectedWordIds, isNearResizeHandlePos, onWordClick, words])
|
|
526
|
+
|
|
527
|
+
const handleMouseMove = useCallback((e: React.MouseEvent<HTMLCanvasElement>) => {
|
|
528
|
+
const rect = canvasRef.current?.getBoundingClientRect()
|
|
529
|
+
if (!rect) return
|
|
530
|
+
|
|
531
|
+
const x = e.clientX - rect.left
|
|
532
|
+
const y = e.clientY - rect.top
|
|
533
|
+
const time = xToTime(x)
|
|
534
|
+
|
|
535
|
+
// Update hover state and cursor
|
|
536
|
+
if (dragMode === 'none') {
|
|
537
|
+
const hoveredWord = findWordAtPosition(x, y)
|
|
538
|
+
setHoveredWordId(hoveredWord?.id || null)
|
|
539
|
+
|
|
540
|
+
if (hoveredWord && selectedWordIds.has(hoveredWord.id)) {
|
|
541
|
+
const nearHandle = isNearResizeHandlePos(hoveredWord, x, y)
|
|
542
|
+
setCursorStyle(nearHandle ? 'ew-resize' : 'grab')
|
|
543
|
+
} else if (hoveredWord) {
|
|
544
|
+
setCursorStyle('pointer')
|
|
545
|
+
} else if (y < TIME_BAR_HEIGHT) {
|
|
546
|
+
setCursorStyle('pointer')
|
|
547
|
+
} else {
|
|
548
|
+
setCursorStyle('default')
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
if (!dragStartRef.current) return
|
|
553
|
+
|
|
554
|
+
if (dragMode === 'selection') {
|
|
555
|
+
setSelectionRect({
|
|
556
|
+
startX: dragStartRef.current.x,
|
|
557
|
+
startY: dragStartRef.current.y,
|
|
558
|
+
endX: x,
|
|
559
|
+
endY: y
|
|
560
|
+
})
|
|
561
|
+
} else if (dragMode === 'resize' && dragWordIdRef.current) {
|
|
562
|
+
// Resize the word
|
|
563
|
+
const originalTimes = dragOriginalTimesRef.current.get(dragWordIdRef.current)
|
|
564
|
+
if (originalTimes) {
|
|
565
|
+
const deltaTime = time - dragStartRef.current.time
|
|
566
|
+
const newEndTime = Math.max(originalTimes.start + 0.05, originalTimes.end + deltaTime)
|
|
567
|
+
onWordTimingChange(dragWordIdRef.current, originalTimes.start, newEndTime)
|
|
568
|
+
}
|
|
569
|
+
setCursorStyle('ew-resize')
|
|
570
|
+
} else if (dragMode === 'move') {
|
|
571
|
+
// Move all selected words
|
|
572
|
+
const deltaTime = time - dragStartRef.current.time
|
|
573
|
+
const updates: Array<{ wordId: string; newStartTime: number; newEndTime: number }> = []
|
|
574
|
+
|
|
575
|
+
for (const [wordId, originalTimes] of dragOriginalTimesRef.current) {
|
|
576
|
+
// Ensure end time is always after start time (at least 0.05s duration)
|
|
577
|
+
const newStartTime = Math.max(0, originalTimes.start + deltaTime)
|
|
578
|
+
const newEndTime = Math.max(newStartTime + 0.05, originalTimes.end + deltaTime)
|
|
579
|
+
updates.push({
|
|
580
|
+
wordId,
|
|
581
|
+
newStartTime,
|
|
582
|
+
newEndTime
|
|
583
|
+
})
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
if (updates.length > 0) {
|
|
587
|
+
onWordsMove(updates)
|
|
588
|
+
}
|
|
589
|
+
setCursorStyle('grabbing')
|
|
590
|
+
}
|
|
591
|
+
}, [dragMode, xToTime, findWordAtPosition, selectedWordIds, isNearResizeHandlePos, onWordTimingChange, onWordsMove])
|
|
592
|
+
|
|
593
|
+
const handleMouseUp = useCallback((e: React.MouseEvent<HTMLCanvasElement>) => {
|
|
594
|
+
const rect = canvasRef.current?.getBoundingClientRect()
|
|
595
|
+
|
|
596
|
+
if (dragMode === 'selection' && dragStartRef.current && rect) {
|
|
597
|
+
const endX = e.clientX - rect.left
|
|
598
|
+
const endY = e.clientY - rect.top
|
|
599
|
+
|
|
600
|
+
const dragDistance = Math.sqrt(
|
|
601
|
+
Math.pow(endX - dragStartRef.current.x, 2) +
|
|
602
|
+
Math.pow(endY - dragStartRef.current.y, 2)
|
|
603
|
+
)
|
|
604
|
+
|
|
605
|
+
if (dragDistance < 5) {
|
|
606
|
+
onBackgroundClick()
|
|
607
|
+
} else {
|
|
608
|
+
const finalRect = {
|
|
609
|
+
startX: dragStartRef.current.x,
|
|
610
|
+
startY: dragStartRef.current.y,
|
|
611
|
+
endX,
|
|
612
|
+
endY
|
|
613
|
+
}
|
|
614
|
+
const selectedIds = findWordsInRect(finalRect)
|
|
615
|
+
if (selectedIds.length > 0) {
|
|
616
|
+
onSelectionComplete(selectedIds)
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
// Reset drag state
|
|
622
|
+
setDragMode('none')
|
|
623
|
+
dragStartRef.current = null
|
|
624
|
+
dragWordIdRef.current = null
|
|
625
|
+
dragOriginalTimesRef.current = new Map()
|
|
626
|
+
setSelectionRect(null)
|
|
627
|
+
setCursorStyle('default')
|
|
628
|
+
}, [dragMode, onBackgroundClick, findWordsInRect, onSelectionComplete])
|
|
629
|
+
|
|
630
|
+
// Wheel handler
|
|
631
|
+
const handleWheel = useCallback((e: React.WheelEvent<HTMLCanvasElement>) => {
|
|
632
|
+
const delta = e.deltaX !== 0 ? e.deltaX : e.deltaY
|
|
633
|
+
const scrollAmount = (delta / 100) * (zoomSeconds / 4)
|
|
634
|
+
let newStart = Math.max(0, Math.min(audioDuration - zoomSeconds, visibleStartTime + scrollAmount))
|
|
635
|
+
|
|
636
|
+
if (newStart !== visibleStartTime) {
|
|
637
|
+
onScrollChange(newStart)
|
|
638
|
+
}
|
|
639
|
+
}, [visibleStartTime, zoomSeconds, audioDuration, onScrollChange])
|
|
640
|
+
|
|
641
|
+
const handleScrollLeft = useCallback(() => {
|
|
642
|
+
const newStart = Math.max(0, visibleStartTime - zoomSeconds * 0.25)
|
|
643
|
+
onScrollChange(newStart)
|
|
644
|
+
}, [visibleStartTime, zoomSeconds, onScrollChange])
|
|
645
|
+
|
|
646
|
+
const handleScrollRight = useCallback(() => {
|
|
647
|
+
const newStart = Math.min(audioDuration - zoomSeconds, visibleStartTime + zoomSeconds * 0.25)
|
|
648
|
+
onScrollChange(Math.max(0, newStart))
|
|
649
|
+
}, [visibleStartTime, zoomSeconds, audioDuration, onScrollChange])
|
|
650
|
+
|
|
651
|
+
return (
|
|
652
|
+
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 0.5 }}>
|
|
653
|
+
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
|
|
654
|
+
<Tooltip title="Scroll Left">
|
|
655
|
+
<IconButton
|
|
656
|
+
size="small"
|
|
657
|
+
onClick={handleScrollLeft}
|
|
658
|
+
disabled={visibleStartTime <= 0}
|
|
659
|
+
>
|
|
660
|
+
<ArrowBackIcon fontSize="small" />
|
|
661
|
+
</IconButton>
|
|
662
|
+
</Tooltip>
|
|
663
|
+
|
|
664
|
+
<Box
|
|
665
|
+
ref={containerRef}
|
|
666
|
+
sx={{
|
|
667
|
+
flexGrow: 1,
|
|
668
|
+
height,
|
|
669
|
+
cursor: cursorStyle,
|
|
670
|
+
borderRadius: 1,
|
|
671
|
+
overflow: 'hidden'
|
|
672
|
+
}}
|
|
673
|
+
>
|
|
674
|
+
<canvas
|
|
675
|
+
ref={canvasRef}
|
|
676
|
+
style={{
|
|
677
|
+
width: '100%',
|
|
678
|
+
height: '100%',
|
|
679
|
+
display: 'block',
|
|
680
|
+
cursor: cursorStyle
|
|
681
|
+
}}
|
|
682
|
+
onMouseDown={handleMouseDown}
|
|
683
|
+
onMouseMove={handleMouseMove}
|
|
684
|
+
onMouseUp={handleMouseUp}
|
|
685
|
+
onMouseLeave={handleMouseUp}
|
|
686
|
+
onWheel={handleWheel}
|
|
687
|
+
/>
|
|
688
|
+
</Box>
|
|
689
|
+
|
|
690
|
+
<Tooltip title="Scroll Right">
|
|
691
|
+
<IconButton
|
|
692
|
+
size="small"
|
|
693
|
+
onClick={handleScrollRight}
|
|
694
|
+
disabled={visibleStartTime >= audioDuration - zoomSeconds}
|
|
695
|
+
>
|
|
696
|
+
<ArrowForwardIcon fontSize="small" />
|
|
697
|
+
</IconButton>
|
|
698
|
+
</Tooltip>
|
|
699
|
+
</Box>
|
|
700
|
+
</Box>
|
|
701
|
+
)
|
|
702
|
+
})
|
|
703
|
+
|
|
704
|
+
export default TimelineCanvas
|