Refactor audio conversion API to improve memory management and add streaming response
This commit is contained in:
parent
4a2d75e155
commit
b67aea6b27
6 changed files with 424 additions and 138 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
import { NextRequest, NextResponse } from 'next/server';
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
import { spawn } from 'child_process';
|
import { spawn } from 'child_process';
|
||||||
import { writeFile, readFile, mkdir, unlink, rmdir } from 'fs/promises';
|
import { writeFile, mkdir, unlink, rmdir } from 'fs/promises';
|
||||||
|
import { createReadStream } from 'fs';
|
||||||
import { existsSync } from 'fs';
|
import { existsSync } from 'fs';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import { randomUUID } from 'crypto';
|
import { randomUUID } from 'crypto';
|
||||||
|
|
@ -65,9 +66,19 @@ async function runFFmpeg(args: string[]): Promise<void> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function cleanup(files: string[], directories: string[]) {
|
||||||
|
await Promise.all([
|
||||||
|
...files.map(f => unlink(f).catch(console.error)),
|
||||||
|
...directories.map(d => rmdir(d).catch(console.error))
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
export async function POST(request: NextRequest) {
|
export async function POST(request: NextRequest) {
|
||||||
|
const tempFiles: string[] = [];
|
||||||
|
const tempDirs: string[] = [];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Parse the request body
|
// Parse the request body as a stream
|
||||||
const data: ConversionRequest = await request.json();
|
const data: ConversionRequest = await request.json();
|
||||||
|
|
||||||
// Create temp directory if it doesn't exist
|
// Create temp directory if it doesn't exist
|
||||||
|
|
@ -82,23 +93,46 @@ export async function POST(request: NextRequest) {
|
||||||
const metadataPath = join(tempDir, `${id}.txt`);
|
const metadataPath = join(tempDir, `${id}.txt`);
|
||||||
const intermediateDir = join(tempDir, `${id}-intermediate`);
|
const intermediateDir = join(tempDir, `${id}-intermediate`);
|
||||||
|
|
||||||
|
tempFiles.push(outputPath, metadataPath);
|
||||||
|
tempDirs.push(intermediateDir);
|
||||||
|
|
||||||
// Create intermediate directory
|
// Create intermediate directory
|
||||||
if (!existsSync(intermediateDir)) {
|
if (!existsSync(intermediateDir)) {
|
||||||
await mkdir(intermediateDir);
|
await mkdir(intermediateDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process each chapter - no need for initial conversion since input is WAV
|
// Process chapters sequentially to avoid memory issues
|
||||||
const chapterFiles: { path: string; title: string; duration: number }[] = [];
|
const chapterFiles: { path: string; title: string; duration: number }[] = [];
|
||||||
let currentTime = 0;
|
let currentTime = 0;
|
||||||
|
|
||||||
for (let i = 0; i < data.chapters.length; i++) {
|
for (let i = 0; i < data.chapters.length; i++) {
|
||||||
const chapter = data.chapters[i];
|
const chapter = data.chapters[i];
|
||||||
|
const inputPath = join(intermediateDir, `${i}-input.mp3`);
|
||||||
const outputPath = join(intermediateDir, `${i}.wav`);
|
const outputPath = join(intermediateDir, `${i}.wav`);
|
||||||
|
|
||||||
// Write the chapter audio directly since it's already WAV
|
tempFiles.push(inputPath, outputPath);
|
||||||
await writeFile(outputPath, Buffer.from(new Uint8Array(chapter.buffer)));
|
|
||||||
|
// Write the chapter audio to a temp file using a Buffer chunk size of 64KB
|
||||||
|
const chunkSize = 64 * 1024; // 64KB chunks
|
||||||
|
const buffer = Buffer.from(new Uint8Array(chapter.buffer));
|
||||||
|
const chunks: Buffer[] = [];
|
||||||
|
|
||||||
|
for (let offset = 0; offset < buffer.length; offset += chunkSize) {
|
||||||
|
chunks.push(buffer.slice(offset, offset + chunkSize));
|
||||||
|
}
|
||||||
|
|
||||||
|
await writeFile(inputPath, Buffer.concat(chunks));
|
||||||
|
chunks.length = 0; // Clear chunks array
|
||||||
|
|
||||||
|
// Convert to WAV with consistent format
|
||||||
|
await runFFmpeg([
|
||||||
|
'-i', inputPath,
|
||||||
|
'-acodec', 'pcm_s16le',
|
||||||
|
'-ar', '44100',
|
||||||
|
'-ac', '2',
|
||||||
|
outputPath
|
||||||
|
]);
|
||||||
|
|
||||||
// Get the duration of this chapter
|
|
||||||
const duration = await getAudioDuration(outputPath);
|
const duration = await getAudioDuration(outputPath);
|
||||||
|
|
||||||
chapterFiles.push({
|
chapterFiles.push({
|
||||||
|
|
@ -106,16 +140,18 @@ export async function POST(request: NextRequest) {
|
||||||
title: chapter.title,
|
title: chapter.title,
|
||||||
duration
|
duration
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Clean up input file early
|
||||||
|
await unlink(inputPath).catch(console.error);
|
||||||
|
const index = tempFiles.indexOf(inputPath);
|
||||||
|
if (index > -1) {
|
||||||
|
tempFiles.splice(index, 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create chapter metadata file
|
// Create chapter metadata file
|
||||||
const metadata: string[] = [];
|
const metadata: string[] = [];
|
||||||
metadata.push(
|
|
||||||
`title=Kokoro Audiobook`,
|
|
||||||
`artist=KokoroTTS`,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Calculate chapter timings based on actual durations
|
|
||||||
chapterFiles.forEach((chapter) => {
|
chapterFiles.forEach((chapter) => {
|
||||||
const startMs = Math.floor(currentTime * 1000);
|
const startMs = Math.floor(currentTime * 1000);
|
||||||
currentTime += chapter.duration;
|
currentTime += chapter.duration;
|
||||||
|
|
@ -134,6 +170,8 @@ export async function POST(request: NextRequest) {
|
||||||
|
|
||||||
// Create list file for concat
|
// Create list file for concat
|
||||||
const listPath = join(tempDir, `${id}-list.txt`);
|
const listPath = join(tempDir, `${id}-list.txt`);
|
||||||
|
tempFiles.push(listPath);
|
||||||
|
|
||||||
await writeFile(
|
await writeFile(
|
||||||
listPath,
|
listPath,
|
||||||
chapterFiles.map(f => `file '${f.path}'`).join('\n')
|
chapterFiles.map(f => `file '${f.path}'`).join('\n')
|
||||||
|
|
@ -152,24 +190,46 @@ export async function POST(request: NextRequest) {
|
||||||
outputPath
|
outputPath
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Read the converted file
|
// Create a readable stream from the output file
|
||||||
const m4bData = await readFile(outputPath);
|
const fileStream = createReadStream(outputPath);
|
||||||
|
|
||||||
// Clean up temp files
|
// Create a web-compatible ReadableStream from the Node.js stream
|
||||||
await Promise.all([
|
const webStream = new ReadableStream({
|
||||||
...chapterFiles.map(f => unlink(f.path)),
|
start(controller) {
|
||||||
unlink(metadataPath),
|
fileStream.on('data', (chunk) => {
|
||||||
unlink(listPath),
|
controller.enqueue(chunk);
|
||||||
unlink(outputPath),
|
});
|
||||||
rmdir(intermediateDir)
|
|
||||||
].map(p => p.catch(console.error)));
|
fileStream.on('end', () => {
|
||||||
|
controller.close();
|
||||||
|
// Clean up only after the stream has been fully sent
|
||||||
|
cleanup(tempFiles, tempDirs).catch(console.error);
|
||||||
|
});
|
||||||
|
|
||||||
|
fileStream.on('error', (error) => {
|
||||||
|
console.error('Stream error:', error);
|
||||||
|
controller.error(error);
|
||||||
|
cleanup(tempFiles, tempDirs).catch(console.error);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
cancel() {
|
||||||
|
fileStream.destroy();
|
||||||
|
cleanup(tempFiles, tempDirs).catch(console.error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return new NextResponse(m4bData, {
|
// Return the streaming response
|
||||||
|
return new NextResponse(webStream, {
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'audio/mp4',
|
'Content-Type': 'audio/mp4',
|
||||||
|
'Transfer-Encoding': 'chunked'
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
// Clean up in case of error
|
||||||
|
await cleanup(tempFiles, tempDirs).catch(console.error);
|
||||||
|
|
||||||
console.error('Error converting audio:', error);
|
console.error('Error converting audio:', error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: 'Failed to convert audio format' },
|
{ error: 'Failed to convert audio format' },
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,8 @@ export async function POST(req: NextRequest) {
|
||||||
// Get API credentials from headers or fall back to environment variables
|
// Get API credentials from headers or fall back to environment variables
|
||||||
const openApiKey = req.headers.get('x-openai-key') || process.env.API_KEY || 'none';
|
const openApiKey = req.headers.get('x-openai-key') || process.env.API_KEY || 'none';
|
||||||
const openApiBaseUrl = req.headers.get('x-openai-base-url') || process.env.API_BASE;
|
const openApiBaseUrl = req.headers.get('x-openai-base-url') || process.env.API_BASE;
|
||||||
const { text, voice, speed, format } = await req.json();
|
const { text, voice, speed } = await req.json();
|
||||||
console.log('Received TTS request:', text, voice, speed, format);
|
console.log('Received TTS request:', text, voice, speed);
|
||||||
|
|
||||||
if (!openApiKey) {
|
if (!openApiKey) {
|
||||||
return NextResponse.json({ error: 'Missing OpenAI API key' }, { status: 401 });
|
return NextResponse.json({ error: 'Missing OpenAI API key' }, { status: 401 });
|
||||||
|
|
@ -29,19 +29,16 @@ export async function POST(req: NextRequest) {
|
||||||
voice: voice as "alloy",
|
voice: voice as "alloy",
|
||||||
input: text,
|
input: text,
|
||||||
speed: speed,
|
speed: speed,
|
||||||
// Use wav format for audiobook generation to avoid initial conversion
|
response_format: 'mp3', // Always use mp3 since we convert to WAV later if needed
|
||||||
response_format: format === 'audiobook' ? 'wav' : (format === 'aac' ? 'aac' : 'mp3'),
|
}, { signal: req.signal });
|
||||||
}, { signal: req.signal }); // Pass the abort signal to OpenAI client
|
|
||||||
|
|
||||||
// Get the audio data as array buffer
|
// Get the audio data as array buffer
|
||||||
// This will also be aborted if the client cancels
|
|
||||||
const stream = response.body;
|
const stream = response.body;
|
||||||
|
|
||||||
// Return audio data with appropriate headers
|
// Return audio data with appropriate headers
|
||||||
const contentType = format === 'audiobook' ? 'audio/wav' : (format === 'aac' ? 'audio/aac' : 'audio/mpeg');
|
|
||||||
return new NextResponse(stream, {
|
return new NextResponse(stream, {
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': contentType
|
'Content-Type': 'audio/mpeg'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ import { useConfig, ViewType } from '@/contexts/ConfigContext';
|
||||||
import { ChevronUpDownIcon, CheckIcon } from '@/components/icons/Icons';
|
import { ChevronUpDownIcon, CheckIcon } from '@/components/icons/Icons';
|
||||||
import { useEPUB } from '@/contexts/EPUBContext';
|
import { useEPUB } from '@/contexts/EPUBContext';
|
||||||
import { usePDF } from '@/contexts/PDFContext';
|
import { usePDF } from '@/contexts/PDFContext';
|
||||||
|
import { useTimeEstimation } from '@/hooks/useTimeEstimation';
|
||||||
|
import { LoadingSpinner } from './Spinner';
|
||||||
|
|
||||||
const isDev = process.env.NEXT_PUBLIC_NODE_ENV !== 'production' || process.env.NODE_ENV == null;
|
const isDev = process.env.NEXT_PUBLIC_NODE_ENV !== 'production' || process.env.NODE_ENV == null;
|
||||||
|
|
||||||
|
|
@ -37,9 +39,9 @@ export function DocumentSettings({ isOpen, setIsOpen, epub }: DocViewSettingsPro
|
||||||
rightMargin,
|
rightMargin,
|
||||||
updateConfigKey
|
updateConfigKey
|
||||||
} = useConfig();
|
} = useConfig();
|
||||||
const { createFullAudioBook } = useEPUB();
|
const { createFullAudioBook, isAudioCombining } = useEPUB();
|
||||||
const { createFullAudioBook: createPDFAudioBook } = usePDF();
|
const { createFullAudioBook: createPDFAudioBook, isAudioCombining: isPDFAudioCombining } = usePDF();
|
||||||
const [progress, setProgress] = useState(0);
|
const { progress, setProgress, estimatedTimeRemaining } = useTimeEstimation();
|
||||||
const [isGenerating, setIsGenerating] = useState(false);
|
const [isGenerating, setIsGenerating] = useState(false);
|
||||||
const [audioFormat, setAudioFormat] = useState<'mp3' | 'm4b'>('mp3');
|
const [audioFormat, setAudioFormat] = useState<'mp3' | 'm4b'>('mp3');
|
||||||
const [localMargins, setLocalMargins] = useState({
|
const [localMargins, setLocalMargins] = useState({
|
||||||
|
|
@ -116,7 +118,7 @@ export function DocumentSettings({ isOpen, setIsOpen, epub }: DocViewSettingsPro
|
||||||
setProgress(0);
|
setProgress(0);
|
||||||
abortControllerRef.current = null;
|
abortControllerRef.current = null;
|
||||||
}
|
}
|
||||||
}, [createFullAudioBook, createPDFAudioBook, epub, audioFormat]);
|
}, [createFullAudioBook, createPDFAudioBook, epub, audioFormat, setProgress]);
|
||||||
|
|
||||||
const handleCancel = () => {
|
const handleCancel = () => {
|
||||||
if (abortControllerRef.current) {
|
if (abortControllerRef.current) {
|
||||||
|
|
@ -195,7 +197,10 @@ export function DocumentSettings({ isOpen, setIsOpen, epub }: DocViewSettingsPro
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex justify-between items-center text-sm text-muted">
|
<div className="flex justify-between items-center text-sm text-muted">
|
||||||
<span>{Math.round(progress)}% complete</span>
|
<span>
|
||||||
|
{Math.round(progress)}% complete
|
||||||
|
{estimatedTimeRemaining && ` • ${estimatedTimeRemaining} remaining`}
|
||||||
|
</span>
|
||||||
<Button
|
<Button
|
||||||
type="button"
|
type="button"
|
||||||
className="inline-flex justify-center rounded-lg px-2.5 py-1 text-sm
|
className="inline-flex justify-center rounded-lg px-2.5 py-1 text-sm
|
||||||
|
|
@ -204,7 +209,13 @@ export function DocumentSettings({ isOpen, setIsOpen, epub }: DocViewSettingsPro
|
||||||
transform transition-transform duration-200 ease-in-out hover:scale-[1.02]"
|
transform transition-transform duration-200 ease-in-out hover:scale-[1.02]"
|
||||||
onClick={handleCancel}
|
onClick={handleCancel}
|
||||||
>
|
>
|
||||||
Cancel and download
|
{(epub ? isAudioCombining : isPDFAudioCombining) ? (
|
||||||
|
<div className="w-full h-full flex items-center justify-end">
|
||||||
|
<LoadingSpinner />
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
'Cancel and download'
|
||||||
|
)}
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@ interface EPUBContextType {
|
||||||
locationRef: RefObject<string | number>;
|
locationRef: RefObject<string | number>;
|
||||||
handleLocationChanged: (location: string | number) => void;
|
handleLocationChanged: (location: string | number) => void;
|
||||||
setRendition: (rendition: Rendition) => void;
|
setRendition: (rendition: Rendition) => void;
|
||||||
|
isAudioCombining: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const EPUBContext = createContext<EPUBContextType | undefined>(undefined);
|
const EPUBContext = createContext<EPUBContextType | undefined>(undefined);
|
||||||
|
|
@ -60,6 +61,7 @@ export function EPUBProvider({ children }: { children: ReactNode }) {
|
||||||
const [currDocData, setCurrDocData] = useState<ArrayBuffer>();
|
const [currDocData, setCurrDocData] = useState<ArrayBuffer>();
|
||||||
const [currDocName, setCurrDocName] = useState<string>();
|
const [currDocName, setCurrDocName] = useState<string>();
|
||||||
const [currDocText, setCurrDocText] = useState<string>();
|
const [currDocText, setCurrDocText] = useState<string>();
|
||||||
|
const [isAudioCombining, setIsAudioCombining] = useState(false);
|
||||||
|
|
||||||
// Add new refs
|
// Add new refs
|
||||||
const bookRef = useRef<Book | null>(null);
|
const bookRef = useRef<Book | null>(null);
|
||||||
|
|
@ -180,9 +182,6 @@ export function EPUBProvider({ children }: { children: ReactNode }) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a complete audiobook by processing all text through NLP and TTS
|
* Creates a complete audiobook by processing all text through NLP and TTS
|
||||||
* @param {string} voice - The voice to use for TTS
|
|
||||||
* @param {number} speed - The speed to use for TTS
|
|
||||||
* @returns {Promise<ArrayBuffer>} The complete audiobook as an ArrayBuffer
|
|
||||||
*/
|
*/
|
||||||
const createFullAudioBook = useCallback(async (
|
const createFullAudioBook = useCallback(async (
|
||||||
onProgress: (progress: number) => void,
|
onProgress: (progress: number) => void,
|
||||||
|
|
@ -193,9 +192,10 @@ export function EPUBProvider({ children }: { children: ReactNode }) {
|
||||||
const textArray = await extractBookText();
|
const textArray = await extractBookText();
|
||||||
if (!textArray.length) throw new Error('No text content found in book');
|
if (!textArray.length) throw new Error('No text content found in book');
|
||||||
|
|
||||||
|
// Calculate total text length for accurate progress tracking
|
||||||
|
const totalLength = textArray.reduce((sum, text) => sum + text.trim().length, 0);
|
||||||
const audioChunks: { buffer: ArrayBuffer; title?: string; startTime: number }[] = [];
|
const audioChunks: { buffer: ArrayBuffer; title?: string; startTime: number }[] = [];
|
||||||
let processedSections = 0;
|
let processedLength = 0;
|
||||||
const totalSections = textArray.length;
|
|
||||||
let currentTime = 0;
|
let currentTime = 0;
|
||||||
|
|
||||||
// Get TOC for chapter titles if available
|
// Get TOC for chapter titles if available
|
||||||
|
|
@ -209,10 +209,8 @@ export function EPUBProvider({ children }: { children: ReactNode }) {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!text.trim()) {
|
const trimmedText = text.trim();
|
||||||
processedSections++;
|
if (!trimmedText) continue;
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const ttsResponse = await fetch('/api/tts', {
|
const ttsResponse = await fetch('/api/tts', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
|
@ -221,10 +219,10 @@ export function EPUBProvider({ children }: { children: ReactNode }) {
|
||||||
'x-openai-base-url': baseUrl,
|
'x-openai-base-url': baseUrl,
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
text: text.trim(),
|
text: trimmedText,
|
||||||
voice: voice,
|
voice: voice,
|
||||||
speed: voiceSpeed,
|
speed: voiceSpeed,
|
||||||
format: 'audiobook' // Request WAV format directly
|
format: 'audiobook'
|
||||||
}),
|
}),
|
||||||
signal
|
signal
|
||||||
});
|
});
|
||||||
|
|
@ -241,7 +239,7 @@ export function EPUBProvider({ children }: { children: ReactNode }) {
|
||||||
// Find matching chapter title from TOC if available
|
// Find matching chapter title from TOC if available
|
||||||
let chapterTitle;
|
let chapterTitle;
|
||||||
if (spine && chapters.length > 0) {
|
if (spine && chapters.length > 0) {
|
||||||
let spineIndex = processedSections;
|
let spineIndex = processedLength;
|
||||||
let currentSpineHref: string | undefined;
|
let currentSpineHref: string | undefined;
|
||||||
|
|
||||||
spine.each((item: SpineItem) => {
|
spine.each((item: SpineItem) => {
|
||||||
|
|
@ -254,9 +252,9 @@ export function EPUBProvider({ children }: { children: ReactNode }) {
|
||||||
const matchingChapter = chapters.find(chapter =>
|
const matchingChapter = chapters.find(chapter =>
|
||||||
chapter.href && currentSpineHref?.includes(chapter.href)
|
chapter.href && currentSpineHref?.includes(chapter.href)
|
||||||
);
|
);
|
||||||
chapterTitle = matchingChapter?.label || `Chapter ${processedSections + 1}`;
|
chapterTitle = matchingChapter?.label || `Section ${processedLength + 1}`;
|
||||||
} else {
|
} else {
|
||||||
chapterTitle = `Chapter ${processedSections + 1}`;
|
chapterTitle = `Section ${processedLength + 1}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
audioChunks.push({
|
audioChunks.push({
|
||||||
|
|
@ -266,13 +264,17 @@ export function EPUBProvider({ children }: { children: ReactNode }) {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add silence between sections
|
// Add silence between sections
|
||||||
const silenceBuffer = new ArrayBuffer(48000); // 1 second of silence
|
const silenceBuffer = new ArrayBuffer(48000);
|
||||||
audioChunks.push({
|
audioChunks.push({
|
||||||
buffer: silenceBuffer,
|
buffer: silenceBuffer,
|
||||||
startTime: currentTime + (audioBuffer.byteLength / 48000)
|
startTime: currentTime + (audioBuffer.byteLength / 48000)
|
||||||
});
|
});
|
||||||
|
|
||||||
currentTime += (audioBuffer.byteLength + 48000) / 48000; // Update time including silence
|
currentTime += (audioBuffer.byteLength + 48000) / 48000;
|
||||||
|
|
||||||
|
// Update progress based on processed text length
|
||||||
|
processedLength += trimmedText.length;
|
||||||
|
onProgress((processedLength / totalLength) * 100);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof Error && error.name === 'AbortError') {
|
if (error instanceof Error && error.name === 'AbortError') {
|
||||||
|
|
@ -282,9 +284,6 @@ export function EPUBProvider({ children }: { children: ReactNode }) {
|
||||||
}
|
}
|
||||||
console.error('Error processing section:', error);
|
console.error('Error processing section:', error);
|
||||||
}
|
}
|
||||||
|
|
||||||
processedSections++;
|
|
||||||
onProgress((processedSections / totalSections) * 100);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (audioChunks.length === 0) {
|
if (audioChunks.length === 0) {
|
||||||
|
|
@ -302,41 +301,46 @@ export function EPUBProvider({ children }: { children: ReactNode }) {
|
||||||
audioChunks: { buffer: ArrayBuffer; title?: string; startTime: number }[],
|
audioChunks: { buffer: ArrayBuffer; title?: string; startTime: number }[],
|
||||||
format: 'mp3' | 'm4b'
|
format: 'mp3' | 'm4b'
|
||||||
): Promise<ArrayBuffer> => {
|
): Promise<ArrayBuffer> => {
|
||||||
if (format === 'm4b') {
|
setIsAudioCombining(true);
|
||||||
// Convert to M4B format using the audio conversion API
|
try {
|
||||||
const response = await fetch('/api/audio/convert', {
|
if (format === 'm4b') {
|
||||||
method: 'POST',
|
// Convert to M4B format using the audio conversion API
|
||||||
headers: {
|
const response = await fetch('/api/audio/convert', {
|
||||||
'Content-Type': 'application/json',
|
method: 'POST',
|
||||||
},
|
headers: {
|
||||||
body: JSON.stringify({
|
'Content-Type': 'application/json',
|
||||||
chapters: audioChunks
|
},
|
||||||
.filter(chunk => chunk.title) // Only include chunks with titles
|
body: JSON.stringify({
|
||||||
.map(chunk => ({
|
chapters: audioChunks
|
||||||
title: chunk.title,
|
.filter(chunk => chunk.title) // Only include chunks with titles
|
||||||
buffer: Array.from(new Uint8Array(chunk.buffer))
|
.map(chunk => ({
|
||||||
}))
|
title: chunk.title,
|
||||||
}),
|
buffer: Array.from(new Uint8Array(chunk.buffer))
|
||||||
});
|
}))
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error('Failed to convert audio to M4B format');
|
throw new Error('Failed to convert audio to M4B format');
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.arrayBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.arrayBuffer();
|
// For MP3, just concatenate the buffers
|
||||||
|
const totalLength = audioChunks.reduce((acc, chunk) => acc + chunk.buffer.byteLength, 0);
|
||||||
|
const combinedBuffer = new Uint8Array(totalLength);
|
||||||
|
|
||||||
|
let offset = 0;
|
||||||
|
for (const chunk of audioChunks) {
|
||||||
|
combinedBuffer.set(new Uint8Array(chunk.buffer), offset);
|
||||||
|
offset += chunk.buffer.byteLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
return combinedBuffer.buffer;
|
||||||
|
} finally {
|
||||||
|
setIsAudioCombining(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// For MP3, just concatenate the buffers
|
|
||||||
const totalLength = audioChunks.reduce((acc, chunk) => acc + chunk.buffer.byteLength, 0);
|
|
||||||
const combinedBuffer = new Uint8Array(totalLength);
|
|
||||||
|
|
||||||
let offset = 0;
|
|
||||||
for (const chunk of audioChunks) {
|
|
||||||
combinedBuffer.set(new Uint8Array(chunk.buffer), offset);
|
|
||||||
offset += chunk.buffer.byteLength;
|
|
||||||
}
|
|
||||||
|
|
||||||
return combinedBuffer.buffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const setRendition = useCallback((rendition: Rendition) => {
|
const setRendition = useCallback((rendition: Rendition) => {
|
||||||
|
|
@ -398,6 +402,7 @@ export function EPUBProvider({ children }: { children: ReactNode }) {
|
||||||
locationRef,
|
locationRef,
|
||||||
handleLocationChanged,
|
handleLocationChanged,
|
||||||
setRendition,
|
setRendition,
|
||||||
|
isAudioCombining,
|
||||||
}),
|
}),
|
||||||
[
|
[
|
||||||
setCurrentDocument,
|
setCurrentDocument,
|
||||||
|
|
@ -411,6 +416,7 @@ export function EPUBProvider({ children }: { children: ReactNode }) {
|
||||||
createFullAudioBook,
|
createFullAudioBook,
|
||||||
handleLocationChanged,
|
handleLocationChanged,
|
||||||
setRendition,
|
setRendition,
|
||||||
|
isAudioCombining,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -63,6 +63,7 @@ interface PDFContextType {
|
||||||
isProcessing: boolean
|
isProcessing: boolean
|
||||||
) => void;
|
) => void;
|
||||||
createFullAudioBook: (onProgress: (progress: number) => void, signal?: AbortSignal, format?: 'mp3' | 'm4b') => Promise<ArrayBuffer>;
|
createFullAudioBook: (onProgress: (progress: number) => void, signal?: AbortSignal, format?: 'mp3' | 'm4b') => Promise<ArrayBuffer>;
|
||||||
|
isAudioCombining: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the context
|
// Create the context
|
||||||
|
|
@ -101,6 +102,7 @@ export function PDFProvider({ children }: { children: ReactNode }) {
|
||||||
const [currDocName, setCurrDocName] = useState<string>();
|
const [currDocName, setCurrDocName] = useState<string>();
|
||||||
const [currDocText, setCurrDocText] = useState<string>();
|
const [currDocText, setCurrDocText] = useState<string>();
|
||||||
const [pdfDocument, setPdfDocument] = useState<PDFDocumentProxy>();
|
const [pdfDocument, setPdfDocument] = useState<PDFDocumentProxy>();
|
||||||
|
const [isAudioCombining, setIsAudioCombining] = useState(false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles successful PDF document load
|
* Handles successful PDF document load
|
||||||
|
|
@ -199,29 +201,40 @@ export function PDFProvider({ children }: { children: ReactNode }) {
|
||||||
throw new Error('No PDF document loaded');
|
throw new Error('No PDF document loaded');
|
||||||
}
|
}
|
||||||
|
|
||||||
const audioChunks: { buffer: ArrayBuffer; title?: string; startTime: number }[] = [];
|
// First pass: extract and measure all text
|
||||||
const totalPages = pdfDocument.numPages;
|
const textPerPage: string[] = [];
|
||||||
let processedPages = 0;
|
let totalLength = 0;
|
||||||
let currentTime = 0;
|
|
||||||
|
for (let pageNum = 1; pageNum <= pdfDocument.numPages; pageNum++) {
|
||||||
for (let pageNum = 1; pageNum <= totalPages; pageNum++) {
|
|
||||||
if (signal?.aborted) {
|
|
||||||
const partialBuffer = await combineAudioChunks(audioChunks, format);
|
|
||||||
return partialBuffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
const text = await extractTextFromPDF(pdfDocument, pageNum, {
|
const text = await extractTextFromPDF(pdfDocument, pageNum, {
|
||||||
header: headerMargin,
|
header: headerMargin,
|
||||||
footer: footerMargin,
|
footer: footerMargin,
|
||||||
left: leftMargin,
|
left: leftMargin,
|
||||||
right: rightMargin
|
right: rightMargin
|
||||||
});
|
});
|
||||||
|
const trimmedText = text.trim();
|
||||||
|
if (trimmedText) {
|
||||||
|
textPerPage.push(trimmedText);
|
||||||
|
totalLength += trimmedText.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!text.trim()) {
|
if (totalLength === 0) {
|
||||||
processedPages++;
|
throw new Error('No text content found in PDF');
|
||||||
continue;
|
}
|
||||||
|
|
||||||
|
const audioChunks: { buffer: ArrayBuffer; title?: string; startTime: number }[] = [];
|
||||||
|
let processedLength = 0;
|
||||||
|
let currentTime = 0;
|
||||||
|
|
||||||
|
// Second pass: process text into audio
|
||||||
|
for (let i = 0; i < textPerPage.length; i++) {
|
||||||
|
if (signal?.aborted) {
|
||||||
|
const partialBuffer = await combineAudioChunks(audioChunks, format);
|
||||||
|
return partialBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const text = textPerPage[i];
|
||||||
try {
|
try {
|
||||||
const ttsResponse = await fetch('/api/tts', {
|
const ttsResponse = await fetch('/api/tts', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
|
@ -230,10 +243,10 @@ export function PDFProvider({ children }: { children: ReactNode }) {
|
||||||
'x-openai-base-url': baseUrl,
|
'x-openai-base-url': baseUrl,
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
text: text.trim(),
|
text,
|
||||||
voice: voice,
|
voice: voice,
|
||||||
speed: voiceSpeed,
|
speed: voiceSpeed,
|
||||||
format: 'audiobook' // Request WAV format directly
|
format: 'audiobook'
|
||||||
}),
|
}),
|
||||||
signal
|
signal
|
||||||
});
|
});
|
||||||
|
|
@ -247,10 +260,9 @@ export function PDFProvider({ children }: { children: ReactNode }) {
|
||||||
throw new Error('Received empty audio buffer from TTS');
|
throw new Error('Received empty audio buffer from TTS');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add chapter metadata for each page
|
|
||||||
audioChunks.push({
|
audioChunks.push({
|
||||||
buffer: audioBuffer,
|
buffer: audioBuffer,
|
||||||
title: `Page ${pageNum}`,
|
title: `Page ${i + 1}`,
|
||||||
startTime: currentTime
|
startTime: currentTime
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -263,6 +275,10 @@ export function PDFProvider({ children }: { children: ReactNode }) {
|
||||||
|
|
||||||
currentTime += (audioBuffer.byteLength + 48000) / 48000;
|
currentTime += (audioBuffer.byteLength + 48000) / 48000;
|
||||||
|
|
||||||
|
// Update progress based on processed text length
|
||||||
|
processedLength += text.length;
|
||||||
|
onProgress((processedLength / totalLength) * 100);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof Error && error.name === 'AbortError') {
|
if (error instanceof Error && error.name === 'AbortError') {
|
||||||
console.log('TTS request aborted');
|
console.log('TTS request aborted');
|
||||||
|
|
@ -271,9 +287,6 @@ export function PDFProvider({ children }: { children: ReactNode }) {
|
||||||
}
|
}
|
||||||
console.error('Error processing page:', error);
|
console.error('Error processing page:', error);
|
||||||
}
|
}
|
||||||
|
|
||||||
processedPages++;
|
|
||||||
onProgress((processedPages / totalPages) * 100);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (audioChunks.length === 0) {
|
if (audioChunks.length === 0) {
|
||||||
|
|
@ -291,41 +304,46 @@ export function PDFProvider({ children }: { children: ReactNode }) {
|
||||||
audioChunks: { buffer: ArrayBuffer; title?: string; startTime: number }[],
|
audioChunks: { buffer: ArrayBuffer; title?: string; startTime: number }[],
|
||||||
format: 'mp3' | 'm4b'
|
format: 'mp3' | 'm4b'
|
||||||
): Promise<ArrayBuffer> => {
|
): Promise<ArrayBuffer> => {
|
||||||
if (format === 'm4b') {
|
setIsAudioCombining(true);
|
||||||
// Convert to M4B format using the audio conversion API
|
try {
|
||||||
const response = await fetch('/api/audio/convert', {
|
if (format === 'm4b') {
|
||||||
method: 'POST',
|
// Convert to M4B format using the audio conversion API
|
||||||
headers: {
|
const response = await fetch('/api/audio/convert', {
|
||||||
'Content-Type': 'application/json',
|
method: 'POST',
|
||||||
},
|
headers: {
|
||||||
body: JSON.stringify({
|
'Content-Type': 'application/json',
|
||||||
chapters: audioChunks
|
},
|
||||||
.filter(chunk => chunk.title) // Only include chunks with titles
|
body: JSON.stringify({
|
||||||
.map(chunk => ({
|
chapters: audioChunks
|
||||||
title: chunk.title,
|
.filter(chunk => chunk.title) // Only include chunks with titles
|
||||||
buffer: Array.from(new Uint8Array(chunk.buffer))
|
.map(chunk => ({
|
||||||
}))
|
title: chunk.title,
|
||||||
}),
|
buffer: Array.from(new Uint8Array(chunk.buffer))
|
||||||
});
|
}))
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error('Failed to convert audio to M4B format');
|
throw new Error('Failed to convert audio to M4B format');
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.arrayBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.arrayBuffer();
|
// For MP3, just concatenate the buffers
|
||||||
|
const totalLength = audioChunks.reduce((acc, chunk) => acc + chunk.buffer.byteLength, 0);
|
||||||
|
const combinedBuffer = new Uint8Array(totalLength);
|
||||||
|
|
||||||
|
let offset = 0;
|
||||||
|
for (const chunk of audioChunks) {
|
||||||
|
combinedBuffer.set(new Uint8Array(chunk.buffer), offset);
|
||||||
|
offset += chunk.buffer.byteLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
return combinedBuffer.buffer;
|
||||||
|
} finally {
|
||||||
|
setIsAudioCombining(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// For MP3, just concatenate the buffers
|
|
||||||
const totalLength = audioChunks.reduce((acc, chunk) => acc + chunk.buffer.byteLength, 0);
|
|
||||||
const combinedBuffer = new Uint8Array(totalLength);
|
|
||||||
|
|
||||||
let offset = 0;
|
|
||||||
for (const chunk of audioChunks) {
|
|
||||||
combinedBuffer.set(new Uint8Array(chunk.buffer), offset);
|
|
||||||
offset += chunk.buffer.byteLength;
|
|
||||||
}
|
|
||||||
|
|
||||||
return combinedBuffer.buffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Context value memoization
|
// Context value memoization
|
||||||
|
|
@ -344,6 +362,7 @@ export function PDFProvider({ children }: { children: ReactNode }) {
|
||||||
handleTextClick,
|
handleTextClick,
|
||||||
pdfDocument,
|
pdfDocument,
|
||||||
createFullAudioBook,
|
createFullAudioBook,
|
||||||
|
isAudioCombining,
|
||||||
}),
|
}),
|
||||||
[
|
[
|
||||||
onDocumentLoadSuccess,
|
onDocumentLoadSuccess,
|
||||||
|
|
@ -356,6 +375,7 @@ export function PDFProvider({ children }: { children: ReactNode }) {
|
||||||
clearCurrDoc,
|
clearCurrDoc,
|
||||||
pdfDocument,
|
pdfDocument,
|
||||||
createFullAudioBook,
|
createFullAudioBook,
|
||||||
|
isAudioCombining,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
||||||
192
src/hooks/useTimeEstimation.ts
Normal file
192
src/hooks/useTimeEstimation.ts
Normal file
|
|
@ -0,0 +1,192 @@
|
||||||
|
import { useState, useEffect, useCallback, useRef } from 'react';
|
||||||
|
|
||||||
|
export interface TimeEstimation {
|
||||||
|
progress: number;
|
||||||
|
setProgress: (progress: number) => void;
|
||||||
|
estimatedTimeRemaining: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useTimeEstimation(): TimeEstimation {
|
||||||
|
const [progress, setProgressState] = useState(0);
|
||||||
|
const [estimatedTimeRemaining, setEstimatedTimeRemaining] = useState<string | null>(null);
|
||||||
|
|
||||||
|
// Store timing data to avoid dependency cycles
|
||||||
|
const startTimeRef = useRef<number | null>(null);
|
||||||
|
const progressHistoryRef = useRef<Array<{time: number, progress: number, textLength?: number}>>([]);
|
||||||
|
const lastProgressUpdateRef = useRef<number>(0);
|
||||||
|
const lastEstimateUpdateRef = useRef<number>(0);
|
||||||
|
|
||||||
|
const resetState = useCallback(() => {
|
||||||
|
startTimeRef.current = null;
|
||||||
|
progressHistoryRef.current = [];
|
||||||
|
setEstimatedTimeRemaining(null);
|
||||||
|
lastEstimateUpdateRef.current = 0;
|
||||||
|
lastProgressUpdateRef.current = 0;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const formatTimeRemaining = useCallback((seconds: number): string => {
|
||||||
|
if (seconds < 30) {
|
||||||
|
return '1m';
|
||||||
|
} else if (seconds < 3600) {
|
||||||
|
const minutes = Math.round(seconds / 60);
|
||||||
|
return `${minutes}m`;
|
||||||
|
} else {
|
||||||
|
const totalMinutes = Math.round(seconds / 60);
|
||||||
|
const hours = Math.floor(totalMinutes / 60);
|
||||||
|
const minutes = totalMinutes % 60;
|
||||||
|
return minutes > 0
|
||||||
|
? `${hours}h ${minutes}m`
|
||||||
|
: `${hours}h`;
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const calculateMovingAverage = useCallback((data: number[], windowSize: number) => {
|
||||||
|
const result = [];
|
||||||
|
for (let i = 0; i < data.length; i++) {
|
||||||
|
const start = Math.max(0, i - windowSize + 1);
|
||||||
|
const window = data.slice(start, i + 1);
|
||||||
|
const average = window.reduce((sum, val) => sum + val, 0) / window.length;
|
||||||
|
result.push(average);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const calculateTimeRemaining = useCallback((newProgress: number, currentTime: number) => {
|
||||||
|
// Initialize start time if this is the first meaningful update
|
||||||
|
if (startTimeRef.current === null && newProgress > 0) {
|
||||||
|
startTimeRef.current = currentTime;
|
||||||
|
progressHistoryRef.current = [{ time: currentTime, progress: newProgress }];
|
||||||
|
lastProgressUpdateRef.current = currentTime;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip if no progress
|
||||||
|
if (newProgress <= 0) {
|
||||||
|
resetState();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if we should update the estimate
|
||||||
|
const timeSinceLastUpdate = currentTime - lastProgressUpdateRef.current;
|
||||||
|
const shouldSkipUpdate = timeSinceLastUpdate < 3000; // Reduced from 10s to 3s for more responsive updates
|
||||||
|
|
||||||
|
const lastDataPoint = progressHistoryRef.current[progressHistoryRef.current.length - 1];
|
||||||
|
const timeSinceLastDataPoint = lastDataPoint ? currentTime - lastDataPoint.time : Infinity;
|
||||||
|
const progressDifference = lastDataPoint ? newProgress - lastDataPoint.progress : Infinity;
|
||||||
|
|
||||||
|
// Store data point if significant time or progress change
|
||||||
|
if (timeSinceLastDataPoint > 2000 || Math.abs(progressDifference) > 1) {
|
||||||
|
progressHistoryRef.current.push({ time: currentTime, progress: newProgress });
|
||||||
|
|
||||||
|
// Keep a sliding window of data points
|
||||||
|
const maxDataPoints = 20; // Increased from 5 for smoother averaging
|
||||||
|
if (progressHistoryRef.current.length > maxDataPoints) {
|
||||||
|
progressHistoryRef.current = [
|
||||||
|
progressHistoryRef.current[0],
|
||||||
|
...progressHistoryRef.current.slice(-maxDataPoints + 1)
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lastProgressUpdateRef.current = currentTime;
|
||||||
|
|
||||||
|
if (shouldSkipUpdate) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const history = progressHistoryRef.current;
|
||||||
|
|
||||||
|
if (history.length < 2) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate progress rates for smoothing
|
||||||
|
const rates: number[] = [];
|
||||||
|
for (let i = 1; i < history.length; i++) {
|
||||||
|
const timeSpan = (history[i].time - history[i-1].time) / 1000; // Convert to seconds
|
||||||
|
const progressSpan = history[i].progress - history[i-1].progress;
|
||||||
|
if (timeSpan > 0) {
|
||||||
|
rates.push(progressSpan / timeSpan);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rates.length === 0) return;
|
||||||
|
|
||||||
|
// Apply moving average smoothing to the rates
|
||||||
|
const smoothedRates = calculateMovingAverage(rates, Math.min(5, rates.length));
|
||||||
|
const currentRate = smoothedRates[smoothedRates.length - 1];
|
||||||
|
|
||||||
|
if (currentRate > 0) {
|
||||||
|
const remainingProgress = 100 - newProgress;
|
||||||
|
const estimatedSeconds = remainingProgress / currentRate;
|
||||||
|
|
||||||
|
if (isFinite(estimatedSeconds) && estimatedSeconds > 0) {
|
||||||
|
// Apply adaptive dampening based on progress
|
||||||
|
const dampingFactor = Math.max(0.2, Math.min(0.8, newProgress / 100));
|
||||||
|
const previousEstimate = getSecondsFromEstimate(estimatedTimeRemaining);
|
||||||
|
|
||||||
|
const smoothedSeconds = previousEstimate
|
||||||
|
? (estimatedSeconds * dampingFactor) + (previousEstimate * (1 - dampingFactor))
|
||||||
|
: estimatedSeconds;
|
||||||
|
|
||||||
|
setEstimatedTimeRemaining(formatTimeRemaining(smoothedSeconds));
|
||||||
|
lastEstimateUpdateRef.current = currentTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [estimatedTimeRemaining, formatTimeRemaining, resetState, calculateMovingAverage]);
|
||||||
|
|
||||||
|
const getSecondsFromEstimate = (estimate: string | null): number | null => {
|
||||||
|
if (!estimate) return null;
|
||||||
|
|
||||||
|
let seconds = 0;
|
||||||
|
|
||||||
|
if (estimate.includes('h')) {
|
||||||
|
const hours = parseInt(estimate.split('h')[0], 10);
|
||||||
|
seconds += hours * 3600;
|
||||||
|
estimate = estimate.split('h')[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (estimate?.includes('m')) {
|
||||||
|
const minutes = parseInt(estimate.split('m')[0], 10);
|
||||||
|
seconds += minutes * 60;
|
||||||
|
estimate = estimate.split('m')[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (estimate?.includes('s')) {
|
||||||
|
const secs = parseInt(estimate.split('s')[0], 10);
|
||||||
|
seconds += secs;
|
||||||
|
}
|
||||||
|
|
||||||
|
return seconds;
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateProgress = useCallback((newProgress: number) => {
|
||||||
|
const currentTime = Date.now();
|
||||||
|
const clampedProgress = Math.max(0, Math.min(100, newProgress));
|
||||||
|
|
||||||
|
setProgressState(clampedProgress);
|
||||||
|
|
||||||
|
if (clampedProgress === 0) {
|
||||||
|
resetState();
|
||||||
|
} else if (clampedProgress === 100) {
|
||||||
|
setEstimatedTimeRemaining(null);
|
||||||
|
lastEstimateUpdateRef.current = currentTime;
|
||||||
|
lastProgressUpdateRef.current = currentTime;
|
||||||
|
} else {
|
||||||
|
calculateTimeRemaining(clampedProgress, currentTime);
|
||||||
|
}
|
||||||
|
}, [calculateTimeRemaining, resetState]);
|
||||||
|
|
||||||
|
// Reset time estimation when component unmounts
|
||||||
|
useEffect(() => {
|
||||||
|
return () => {
|
||||||
|
resetState();
|
||||||
|
};
|
||||||
|
}, [resetState]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
progress,
|
||||||
|
setProgress: updateProgress,
|
||||||
|
estimatedTimeRemaining
|
||||||
|
};
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue