fix: voice input recording format + ASR logging

- Change recorder format from mp3 (8kHz) to aac (22kHz) for better
  whisper ASR accuracy
- Move recorder.onStop binding before recorder.stop() to prevent
  race condition (onStop may fire before handler is registered)
- Backend: add Logger, log ASR errors instead of silent catch {}
- Backend: change default extension from .mp3 to .aac
This commit is contained in:
yuzhiran
2026-06-25 08:49:55 +08:00
parent 9b1c92464e
commit b8aaa51eb1
2 changed files with 21 additions and 16 deletions
+8 -3
View File
@@ -1,4 +1,4 @@
import { Controller, Get, Post, Body, Param, Res, HttpException, HttpStatus, UseGuards, UploadedFile, UseInterceptors } from '@nestjs/common'
import { Controller, Get, Post, Body, Param, Res, HttpException, HttpStatus, UseGuards, UploadedFile, UseInterceptors, Logger } from '@nestjs/common'
import { FileInterceptor } from '@nestjs/platform-express'
import { Response } from 'express'
import * as fs from 'fs'
@@ -10,6 +10,7 @@ import { Public } from '../../common/decorators/public.decorator'
@Controller('tts')
export class TtsController {
private readonly logger = new Logger(TtsController.name)
constructor(private ttsService: TtsService) {}
@UseGuards(JwtAuthGuard)
@@ -42,7 +43,7 @@ export class TtsController {
if (!file) throw new HttpException('请上传音频文件', HttpStatus.BAD_REQUEST)
const uploadDir = '/tmp/asr_uploads'
if (!fs.existsSync(uploadDir)) fs.mkdirSync(uploadDir, { recursive: true })
const ext = path.extname(file.originalname) || '.mp3'
const ext = file.originalname ? path.extname(file.originalname) || '.aac' : '.aac'
const dest = path.join(uploadDir, file.filename + ext)
fs.renameSync(file.path, dest)
try {
@@ -63,7 +64,11 @@ export class TtsController {
if (whisperResult && whisperResult.trim()) {
return { text: whisperResult.trim() }
}
} catch {}
} catch (e: any) {
this.logger.error(`ASR failed: ${e?.message || e}`)
}
// 清理临时文件
try { fs.unlinkSync(dest) } catch {}
return { text: '' }
}
}