| import { Request, Response } from "express"; | |
| import { VoiceService } from "./voice.service"; | |
| export class VoiceController { | |
| constructor(private readonly voiceService: VoiceService) { } | |
| async transcribe(req: Request, res: Response): Promise<void> { | |
| try { | |
| if (!req.file) { | |
| res.error(400, "NO_AUDIO_FILE", "No audio file provided"); | |
| return; | |
| } | |
| const result = await this.voiceService.transcribeAudio(req.file.buffer); | |
| if (result.error) { | |
| res.error(result.status || 500, "STT_ERROR", result.error); | |
| return; | |
| } | |
| res.success(result, "Transcription successful"); | |
| } catch (error) { | |
| console.error("Transcribe Controller Error:", error); | |
| res.error(500, "STT_SERVER_ERROR", "Internal Server Error", error); | |
| } | |
| } | |
| async tts(req: Request, res: Response): Promise<void> { | |
| try { | |
| const { text } = req.body; | |
| if (!text) { | |
| res.error(400, "NO_TEXT_PROVIDED", "No text provided for TTS"); | |
| return; | |
| } | |
| console.log(`\n\x1b[36m========== AI RESPONSE ==========\x1b[0m`); | |
| console.log(`\x1b[36m[AI]: ${text}\x1b[0m`); | |
| console.log(`\x1b[36m=================================\x1b[0m\n`); | |
| const audioBuffer = await this.voiceService.generateTTSAudio(text); | |
| const base64Audio = audioBuffer.toString('base64'); | |
| res.success({ audioBase64: base64Audio }, "TTS generation successful"); | |
| } catch (error) { | |
| console.error("TTS Controller Error:", error); | |
| res.error(500, "TTS_SERVER_ERROR", "Internal Server Error", error); | |
| } | |
| } | |
| } | |
| // import { Request, Response } from "express"; | |
| // import { VoiceService } from "./voice.service"; | |
| // export class VoiceController { | |
| // constructor(private readonly voiceService: VoiceService) { } | |
| // async transcribe(req: Request, res: Response): Promise<void> { | |
| // try { | |
| // if (!req.file) { | |
| // res.error(400, "NO_AUDIO_FILE", "No audio file provided"); | |
| // return; | |
| // } | |
| // const result = await this.voiceService.transcribeAudio(req.file.buffer); | |
| // if (result.error) { | |
| // res.error(result.status || 500, "STT_ERROR", result.error); | |
| // return; | |
| // } | |
| // res.success(result, "Transcription successful"); | |
| // } catch (error) { | |
| // console.error("Transcribe Controller Error:", error); | |
| // res.error(500, "STT_SERVER_ERROR", "Internal Server Error", error); | |
| // } | |
| // } | |
| // async tts(req: Request, res: Response): Promise<void> { | |
| // try { | |
| // const { text, language } = req.body; | |
| // if (!text) { | |
| // res.error(400, "NO_TEXT_PROVIDED", "No text provided for TTS"); | |
| // return; | |
| // } | |
| // console.log(`\n\x1b[36m========== AI RESPONSE ==========\x1b[0m`); | |
| // console.log(`\x1b[36m[AI]: ${text}\x1b[0m`); | |
| // console.log(`\x1b[36m=================================\x1b[0m\n`); | |
| // const audioBuffer = await this.voiceService.generateTTSAudio(text, language || 'en'); | |
| // const base64Audio = audioBuffer.toString('base64'); | |
| // res.success({ audioBase64: base64Audio }, "TTS generation successful"); | |
| // } catch (error) { | |
| // console.error("TTS Controller Error:", error); | |
| // res.error(500, "TTS_SERVER_ERROR", "Internal Server Error", error); | |
| // } | |
| // } | |
| // } | |