sto-k-odnomu/backend/src/voice/voice.service.ts

103 lines
3.6 KiB
TypeScript
Raw Normal View History

2026-01-05 00:57:10 +00:00
import { Injectable, HttpException, HttpStatus, Logger } from '@nestjs/common';
2026-01-05 00:25:02 +00:00
import { ConfigService } from '@nestjs/config';
@Injectable()
export class VoiceService {
2026-01-05 00:57:10 +00:00
private readonly logger = new Logger(VoiceService.name);
2026-01-05 00:25:02 +00:00
private readonly voiceServiceUrl: string;
constructor(private configService: ConfigService) {
2026-01-05 00:57:10 +00:00
this.logger.log('Initializing VoiceService...');
2026-01-05 01:09:13 +00:00
var voiceServiceHost = this.configService.get<string>('VOICE_SERVICE_HOST');
2026-01-05 00:25:02 +00:00
2026-01-05 00:30:35 +00:00
if (!voiceServiceHost) {
2026-01-05 00:57:10 +00:00
const error = 'VOICE_SERVICE_HOST environment variable is not set';
this.logger.error(error);
throw new Error(error);
2026-01-05 00:25:02 +00:00
}
2026-01-05 01:09:13 +00:00
while (voiceServiceHost.endsWith('/')) {
voiceServiceHost = voiceServiceHost.slice(0, -1);
}
2026-01-05 00:30:35 +00:00
this.voiceServiceUrl = voiceServiceHost;
2026-01-05 00:57:10 +00:00
this.logger.log(`VoiceService initialized with URL: ${this.voiceServiceUrl}`);
2026-01-05 00:25:02 +00:00
}
async generateTTS(text: string, voice: string = 'sarah'): Promise<Buffer> {
2026-01-05 00:57:10 +00:00
this.logger.log(`Generating TTS for text: "${text.substring(0, 50)}..." with voice: ${voice}`);
2026-01-05 00:25:02 +00:00
try {
const url = `${this.voiceServiceUrl}/api/voice/tts`;
2026-01-05 00:57:10 +00:00
this.logger.debug(`Making request to: ${url}`);
2026-01-05 00:25:02 +00:00
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ text, voice }),
});
2026-01-05 00:57:10 +00:00
this.logger.debug(`Response status: ${response.status} ${response.statusText}`);
2026-01-05 00:25:02 +00:00
if (!response.ok) {
2026-01-05 00:57:10 +00:00
this.logger.error(`Voice service error: ${response.status} ${response.statusText}`);
2026-01-05 00:25:02 +00:00
throw new HttpException(
`Voice service error: ${response.statusText}`,
response.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
const arrayBuffer = await response.arrayBuffer();
2026-01-05 00:57:10 +00:00
const buffer = Buffer.from(arrayBuffer);
this.logger.log(`TTS generated successfully, size: ${buffer.length} bytes`);
return buffer;
2026-01-05 00:25:02 +00:00
} catch (error) {
if (error instanceof HttpException) {
2026-01-05 00:57:10 +00:00
this.logger.error(`HttpException in generateTTS: ${error.message}`);
2026-01-05 00:25:02 +00:00
throw error;
}
2026-01-05 00:57:10 +00:00
this.logger.error(`Error in generateTTS: ${error.message}`, error.stack);
2026-01-05 00:25:02 +00:00
throw new HttpException(
`Failed to generate speech: ${error.message}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getEffect(effectType: string): Promise<Buffer> {
2026-01-05 00:57:10 +00:00
this.logger.log(`Getting effect: ${effectType}`);
2026-01-05 00:25:02 +00:00
try {
const url = `${this.voiceServiceUrl}/api/voice/effects/${effectType}`;
2026-01-05 00:57:10 +00:00
this.logger.debug(`Making request to: ${url}`);
2026-01-05 00:25:02 +00:00
const response = await fetch(url, {
method: 'GET',
});
2026-01-05 00:57:10 +00:00
this.logger.debug(`Response status: ${response.status} ${response.statusText}`);
2026-01-05 00:25:02 +00:00
if (!response.ok) {
2026-01-05 00:57:10 +00:00
this.logger.error(`Voice service error: ${response.status} ${response.statusText}`);
2026-01-05 00:25:02 +00:00
throw new HttpException(
`Voice service error: ${response.statusText}`,
response.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
const arrayBuffer = await response.arrayBuffer();
2026-01-05 00:57:10 +00:00
const buffer = Buffer.from(arrayBuffer);
this.logger.log(`Effect retrieved successfully, size: ${buffer.length} bytes`);
return buffer;
2026-01-05 00:25:02 +00:00
} catch (error) {
if (error instanceof HttpException) {
2026-01-05 00:57:10 +00:00
this.logger.error(`HttpException in getEffect: ${error.message}`);
2026-01-05 00:25:02 +00:00
throw error;
}
2026-01-05 00:57:10 +00:00
this.logger.error(`Error in getEffect: ${error.message}`, error.stack);
2026-01-05 00:25:02 +00:00
throw new HttpException(
`Failed to get sound effect: ${error.message}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}