You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.6 KiB

11 months ago
  1. class GenericOpenAiTTS {
  2. constructor() {
  3. if (!process.env.TTS_OPEN_AI_COMPATIBLE_KEY)
  4. this.#log(
  5. "No OpenAI compatible API key was set. You might need to set this to use your OpenAI compatible TTS service."
  6. );
  7. if (!process.env.TTS_OPEN_AI_COMPATIBLE_VOICE_MODEL)
  8. this.#log(
  9. "No OpenAI compatible voice model was set. We will use the default voice model 'alloy'. This may not exist for your selected endpoint."
  10. );
  11. if (!process.env.TTS_OPEN_AI_COMPATIBLE_ENDPOINT)
  12. throw new Error(
  13. "No OpenAI compatible endpoint was set. Please set this to use your OpenAI compatible TTS service."
  14. );
  15. const { OpenAI: OpenAIApi } = require("openai");
  16. this.openai = new OpenAIApi({
  17. apiKey: process.env.TTS_OPEN_AI_COMPATIBLE_KEY || null,
  18. baseURL: process.env.TTS_OPEN_AI_COMPATIBLE_ENDPOINT,
  19. });
  20. this.voice = process.env.TTS_OPEN_AI_COMPATIBLE_VOICE_MODEL ?? "alloy";
  21. }
  22. #log(text, ...args) {
  23. console.log(`\x1b[32m[OpenAiGenericTTS]\x1b[0m ${text}`, ...args);
  24. }
  25. /**
  26. * Generates a buffer from the given text input using the OpenAI compatible TTS service.
  27. * @param {string} textInput - The text to be converted to audio.
  28. * @returns {Promise<Buffer>} A buffer containing the audio data.
  29. */
  30. async ttsBuffer(textInput) {
  31. try {
  32. const result = await this.openai.audio.speech.create({
  33. model: "tts-1",
  34. voice: this.voice,
  35. input: textInput,
  36. });
  37. return Buffer.from(await result.arrayBuffer());
  38. } catch (e) {
  39. console.error(e);
  40. }
  41. return null;
  42. }
  43. }
  44. module.exports = {
  45. GenericOpenAiTTS,
  46. };