Files
Andrew Beveridge ☄️andClaude Opus 4.6 26297fe7c4 feat: Add Edit Input Audio feature (trim, cut, mute, join) (#514)
## Summary

@coderabbitai ignore

Adds a complete "Edit Input Audio" feature that lets users optionally
trim, cut, mute, or join input audio before processing begins. This is
especially useful for live recordings from YouTube where users need to
skip talking/intros.

**5 phases implemented:**
- **Phase 1 (Backend):** 3 new job states, FFmpeg audio operations
service, 6 API endpoints, edit stack undo/redo
- **Phase 2 (Sessions):** Audio edit session persistence with Firestore
+ GCS, SHA256 deduplication
- **Phase 3 (Creation Flow):** Opt-in toggle in job creation,
requires_audio_edit flag across all 3 creation paths
- **Phase 4 (Editor UI):** Full audio editor with waveform
visualization, selection-based editing, playback controls, submit flow
- **Phase 5 (Restore):** Auto-save hook, session restore dialog, manual
save progress

**Key changes:**
- 3 new job states: `awaiting_audio_edit`, `in_audio_edit`,
`audio_edit_complete`
- Server-side FFmpeg operations (lossless FLAC internally, OGG Opus for
browser)
- Collapsible audio edit toggle in VisibilityStep (defaults to off)
- "Edit Audio" action button in JobCard for awaiting_audio_edit status
- Session auto-save every 3 edits + on page hide + before submit
- Session restore dialog when returning to an interrupted edit

## Test plan

- [x] All 706 frontend tests pass (38 suites)
- [x] All 2491 backend tests pass (1 pre-existing failure in unrelated
test)
- [x] 23 AudioEditor component tests (loading, rendering, undo/redo,
submit, restore, save)
- [x] 25 audio edit route tests
- [x] 18 audio edit service tests
- [x] 13 audio edit session tests
- [x] 5 VisibilityStep toggle tests
- [ ] Manual E2E test with real audio file (user to verify)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 20:31:13 -04:00

117 lines
4.4 KiB
TypeScript

"use client"
import { useEffect, useCallback, useRef } from "react"
import { api, Job } from "@/lib/api"
interface AutoProcessorProps {
/** List of jobs to monitor */
jobs: Job[]
/** Callback to refresh jobs list after auto-processing */
onJobsChanged: () => void
}
/**
* AutoProcessor component that automatically handles interactive states
* for jobs that have non_interactive mode enabled.
*
* This is equivalent to the -y flag in karaoke-gen-remote CLI:
* - Auto-completes lyrics review (accepts auto-corrected lyrics)
* - Auto-selects instrumental based on backing vocals analyzer recommendation
* - Auto-selects first/best audio search result
*/
export function AutoProcessor({ jobs, onJobsChanged }: AutoProcessorProps) {
const processedRef = useRef<Set<string>>(new Set())
const processingRef = useRef<Set<string>>(new Set())
const processJob = useCallback(async (job: Job, action: string) => {
const processingKey = `${job.job_id}-${action}`
// Skip if already being processed or already processed
if (processingRef.current.has(processingKey) || processedRef.current.has(processingKey)) {
return
}
// Mark as processing
processingRef.current.add(processingKey)
processedRef.current.add(processingKey)
console.log(`[AutoProcessor] Auto-processing ${action} for job ${job.job_id}`)
try {
switch (action) {
case 'review': {
// Auto-complete lyrics review with instrumental selection
// Use backing vocals analyzer recommendation (default to clean if review needed or missing)
const analysis = job.state_data?.backing_vocals_analysis
const recommendation = analysis?.recommended_selection
// Only use 'with_backing' if explicitly recommended by analyzer, otherwise clean (safe default)
const selection = recommendation === 'with_backing' ? 'with_backing' : 'clean'
await api.completeReview(job.job_id, selection)
console.log(`[AutoProcessor] Auto-completed review for job ${job.job_id} with ${selection} instrumental (recommendation: ${recommendation || 'none'})`)
break
}
case 'instrumental': {
// For finalise-only jobs that still use the separate instrumental selection flow
const analysis = job.state_data?.backing_vocals_analysis
const recommendation = analysis?.recommended_selection
// Only use 'with_backing' if explicitly recommended by analyzer, otherwise clean (safe default)
const selection = recommendation === 'with_backing' ? 'with_backing' : 'clean'
await api.selectInstrumental(job.job_id, selection)
console.log(`[AutoProcessor] Auto-selected ${selection} instrumental for job ${job.job_id} (analyzer recommendation: ${recommendation || 'none'})`)
break
}
case 'audio':
// Auto-select first audio search result (index 0)
await api.selectAudioResult(job.job_id, 0)
console.log(`[AutoProcessor] Auto-selected first audio result for job ${job.job_id}`)
break
case 'audio_edit':
// Auto-submit audio edit with no changes (skip editing)
await api.submitAudioEdit(job.job_id)
console.log(`[AutoProcessor] Auto-submitted audio edit (no changes) for job ${job.job_id}`)
break
}
// Refresh jobs to reflect the change
onJobsChanged()
} catch (err) {
console.error(`[AutoProcessor] Failed to auto-process ${action} for job ${job.job_id}:`, err)
// Remove from processed so it can be retried
processedRef.current.delete(processingKey)
} finally {
processingRef.current.delete(processingKey)
}
}, [onJobsChanged])
useEffect(() => {
// Find jobs that need auto-processing (only jobs with non_interactive flag)
for (const job of jobs) {
// Only process jobs with non_interactive mode enabled
if (!job.non_interactive) {
continue
}
switch (job.status) {
case 'awaiting_review':
processJob(job, 'review')
break
case 'awaiting_instrumental_selection':
processJob(job, 'instrumental')
break
case 'awaiting_audio_selection':
processJob(job, 'audio')
break
case 'awaiting_audio_edit':
processJob(job, 'audio_edit')
break
}
}
}, [jobs, processJob])
// This component doesn't render anything - it's purely for side effects
return null
}