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.

29 lines
685 B

11 months ago
  1. class OpenAiTTS {
  2. constructor() {
  3. if (!process.env.TTS_OPEN_AI_KEY)
  4. throw new Error("No OpenAI API key was set.");
  5. const { OpenAI: OpenAIApi } = require("openai");
  6. this.openai = new OpenAIApi({
  7. apiKey: process.env.TTS_OPEN_AI_KEY,
  8. });
  9. this.voice = process.env.TTS_OPEN_AI_VOICE_MODEL ?? "alloy";
  10. }
  11. async ttsBuffer(textInput) {
  12. try {
  13. const result = await this.openai.audio.speech.create({
  14. model: "tts-1",
  15. voice: this.voice,
  16. input: textInput,
  17. });
  18. return Buffer.from(await result.arrayBuffer());
  19. } catch (e) {
  20. console.error(e);
  21. }
  22. return null;
  23. }
  24. }
  25. module.exports = {
  26. OpenAiTTS,
  27. };