feat: TTS服务 + 数字人面试组件 (P1)
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import { Controller, Get, Post, Body, Param, Res, HttpException, HttpStatus } from '@nestjs/common'
|
||||
import { Response } from 'express'
|
||||
import * as fs from 'fs'
|
||||
import { TtsService } from './tts.service'
|
||||
import { Public } from '../../common/decorators/public.decorator'
|
||||
|
||||
@Controller('tts')
|
||||
export class TtsController {
|
||||
constructor(private ttsService: TtsService) {}
|
||||
|
||||
@Public()
|
||||
@Post('synthesize')
|
||||
async synthesize(@Body('text') text: string, @Body('voice') voice?: string) {
|
||||
if (!text || text.length > 500) {
|
||||
throw new HttpException('文本不能为空且不超过500字', HttpStatus.BAD_REQUEST)
|
||||
}
|
||||
const result = await this.ttsService.synthesize(text, voice)
|
||||
return { hash: result.hash, durationMs: result.durationMs }
|
||||
}
|
||||
|
||||
@Public()
|
||||
@Get('audio/:hash')
|
||||
async getAudio(@Param('hash') hash: string, @Res() res: Response) {
|
||||
const filePath = this.ttsService.getCachedPath(hash)
|
||||
if (!filePath) {
|
||||
throw new HttpException('音频不存在', HttpStatus.NOT_FOUND)
|
||||
}
|
||||
const stream = fs.createReadStream(filePath)
|
||||
res.setHeader('Content-Type', 'audio/mpeg')
|
||||
res.setHeader('Cache-Control', 'public, max-age=31536000')
|
||||
stream.pipe(res)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user