29 lines
694 B
TypeScript
29 lines
694 B
TypeScript
|
|
import type { Context } from 'koa'
|
||
|
|
import { textToSpeech } from '../../services/hermes/tts'
|
||
|
|
|
||
|
|
export async function generate(ctx: Context) {
|
||
|
|
const { text, lang } = ctx.request.body as {
|
||
|
|
text?: string
|
||
|
|
lang?: string
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!text || typeof text !== 'string') {
|
||
|
|
ctx.status = 400
|
||
|
|
ctx.body = { error: 'text is required' }
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
if (text.length > 5000) {
|
||
|
|
ctx.status = 400
|
||
|
|
ctx.body = { error: 'text is too long (max 5000 characters)' }
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
const { audio, engine } = await textToSpeech({ text, lang })
|
||
|
|
|
||
|
|
ctx.set('Content-Type', 'audio/mpeg')
|
||
|
|
ctx.set('Content-Length', String(audio.length))
|
||
|
|
ctx.set('X-TTS-Engine', engine)
|
||
|
|
ctx.body = audio
|
||
|
|
}
|