Re-add AbortController API, fixes duplicate playback
This commit is contained in:
parent
cd00ad2b9b
commit
0be34a09fe
2 changed files with 67 additions and 36 deletions
|
|
@ -17,26 +17,33 @@ export async function POST(req: NextRequest) {
|
||||||
return NextResponse.json({ error: 'Missing required parameters' }, { status: 400 });
|
return NextResponse.json({ error: 'Missing required parameters' }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize OpenAI client
|
// Initialize OpenAI client with abort signal
|
||||||
const openai = new OpenAI({
|
const openai = new OpenAI({
|
||||||
apiKey: openApiKey,
|
apiKey: openApiKey,
|
||||||
baseURL: openApiBaseUrl,
|
baseURL: openApiBaseUrl,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Request audio from OpenAI
|
// Request audio from OpenAI and pass along the abort signal
|
||||||
const response = await openai.audio.speech.create({
|
const response = await openai.audio.speech.create({
|
||||||
model: 'tts-1',
|
model: 'tts-1',
|
||||||
voice: voice as "alloy",
|
voice: voice as "alloy",
|
||||||
input: text,
|
input: text,
|
||||||
speed: speed,
|
speed: speed,
|
||||||
});
|
}, { 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 arrayBuffer = await response.arrayBuffer();
|
const arrayBuffer = await response.arrayBuffer();
|
||||||
|
|
||||||
// Return audio data with appropriate headers
|
// Return audio data with appropriate headers
|
||||||
return new NextResponse(arrayBuffer);
|
return new NextResponse(arrayBuffer);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
// Check if this was an abort error
|
||||||
|
if (error instanceof Error && error.name === 'AbortError') {
|
||||||
|
console.log('TTS request aborted by client');
|
||||||
|
return new Response(null, { status: 499 }); // Use 499 status for client closed request
|
||||||
|
}
|
||||||
|
|
||||||
console.error('Error generating TTS:', error);
|
console.error('Error generating TTS:', error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: 'Failed to generate audio' },
|
{ error: 'Failed to generate audio' },
|
||||||
|
|
|
||||||
|
|
@ -139,6 +139,8 @@ export function TTSProvider({ children }: { children: ReactNode }) {
|
||||||
|
|
||||||
// Track pending preload requests
|
// Track pending preload requests
|
||||||
const preloadRequests = useRef<Map<string, Promise<string>>>(new Map());
|
const preloadRequests = useRef<Map<string, Promise<string>>>(new Map());
|
||||||
|
// Track active abort controllers for TTS requests
|
||||||
|
const activeAbortControllers = useRef<Set<AbortController>>(new Set());
|
||||||
|
|
||||||
//console.log('page:', currDocPage, 'pages:', currDocPages);
|
//console.log('page:', currDocPage, 'pages:', currDocPages);
|
||||||
|
|
||||||
|
|
@ -177,7 +179,15 @@ export function TTSProvider({ children }: { children: ReactNode }) {
|
||||||
activeHowl.unload(); // Ensure Howl instance is fully cleaned up
|
activeHowl.unload(); // Ensure Howl instance is fully cleaned up
|
||||||
setActiveHowl(null);
|
setActiveHowl(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (clearPending) {
|
if (clearPending) {
|
||||||
|
// Abort all active TTS requests
|
||||||
|
console.log('Aborting active TTS requests');
|
||||||
|
activeAbortControllers.current.forEach(controller => {
|
||||||
|
controller.abort();
|
||||||
|
});
|
||||||
|
activeAbortControllers.current.clear();
|
||||||
|
// Clear any pending preload requests
|
||||||
preloadRequests.current.clear();
|
preloadRequests.current.clear();
|
||||||
}
|
}
|
||||||
}, [activeHowl]);
|
}, [activeHowl]);
|
||||||
|
|
@ -222,7 +232,7 @@ export function TTSProvider({ children }: { children: ReactNode }) {
|
||||||
if (!isEPUB) {
|
if (!isEPUB) {
|
||||||
// Handle next/previous page transitions
|
// Handle next/previous page transitions
|
||||||
if ((nextIndex >= sentences.length && currDocPageNumber < currDocPages!) ||
|
if ((nextIndex >= sentences.length && currDocPageNumber < currDocPages!) ||
|
||||||
(nextIndex < 0 && currDocPageNumber > 1)) {
|
(nextIndex < 0 && currDocPageNumber > 1)) {
|
||||||
// Pass wasPlaying to maintain playback state during page turn
|
// Pass wasPlaying to maintain playback state during page turn
|
||||||
skipToLocation(currDocPageNumber + (nextIndex >= sentences.length ? 1 : -1));
|
skipToLocation(currDocPageNumber + (nextIndex >= sentences.length ? 1 : -1));
|
||||||
return;
|
return;
|
||||||
|
|
@ -396,6 +406,10 @@ export function TTSProvider({ children }: { children: ReactNode }) {
|
||||||
try {
|
try {
|
||||||
console.log('Requesting audio for sentence:', sentence);
|
console.log('Requesting audio for sentence:', sentence);
|
||||||
|
|
||||||
|
// Create an AbortController for this request
|
||||||
|
const controller = new AbortController();
|
||||||
|
activeAbortControllers.current.add(controller);
|
||||||
|
|
||||||
const response = await fetch('/api/tts', {
|
const response = await fetch('/api/tts', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
|
|
@ -408,8 +422,12 @@ export function TTSProvider({ children }: { children: ReactNode }) {
|
||||||
voice: voice,
|
voice: voice,
|
||||||
speed: speed,
|
speed: speed,
|
||||||
}),
|
}),
|
||||||
|
signal: controller.signal,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Remove the controller once the request is complete
|
||||||
|
activeAbortControllers.current.delete(controller);
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error('Failed to generate audio');
|
throw new Error('Failed to generate audio');
|
||||||
}
|
}
|
||||||
|
|
@ -422,6 +440,12 @@ export function TTSProvider({ children }: { children: ReactNode }) {
|
||||||
|
|
||||||
return arrayBuffer;
|
return arrayBuffer;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
// Check if this was an abort error
|
||||||
|
if (error instanceof Error && error.name === 'AbortError') {
|
||||||
|
console.log('TTS request aborted:', sentence.substring(0, 20));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
setIsPlaying(false);
|
setIsPlaying(false);
|
||||||
toast.error('Failed to generate audio. Server not responding.', {
|
toast.error('Failed to generate audio. Server not responding.', {
|
||||||
id: 'tts-api-error',
|
id: 'tts-api-error',
|
||||||
|
|
@ -532,7 +556,7 @@ export function TTSProvider({ children }: { children: ReactNode }) {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onloaderror: (id, error) => {
|
onloaderror: (id, error) => {
|
||||||
console.error('Error loading audio:', error);
|
console.warn('Error loading audio:', error);
|
||||||
setIsProcessing(false);
|
setIsProcessing(false);
|
||||||
setActiveHowl(null);
|
setActiveHowl(null);
|
||||||
URL.revokeObjectURL(audioUrl);
|
URL.revokeObjectURL(audioUrl);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue