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