File size: 2,244 Bytes
1905805
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import wave
from io import BytesIO
from openai import AsyncOpenAI

from utils.config import Config

from .base import TTSOperation

class OpenAITTS(TTSOperation):
    def __init__(self):
        super().__init__("openai")
        self.client = None
        
        self.base_url = "https://api.openai.com/v1/"
        self.voice = "nova"
        self.model = "tts-1"
        
    async def start(self) -> None:
        '''General setup needed to start generated'''
        await super().start()
        self.client = AsyncOpenAI(base_url=self.base_url)
    
    async def close(self) -> None:
        '''Clean up resources before unloading'''
        await super().close()
        await self.client.close()
        self.client = None

    async def configure(self, config_d):
        '''Configure and validate operation-specific configuration'''
        if "base_url" in config_d: self.base_url = str(config_d["base_url"])
        if "voice" in config_d: self.voice = str(config_d["voice"])
        if "model" in config_d: self.model = str(config_d["model"])
        
        assert self.base_url is not None and len(self.base_url) > 0
        assert self.voice is not None and len(self.voice) > 0
        assert self.model is not None and len(self.model) > 0
        
    async def get_configuration(self):
        '''Returns values of configurable fields'''
        return {
            "base_url": self.base_url,
            "voice": self.voice,
            "model": self.model
        }

    async def _generate(self, content: str = None, **kwargs):
        '''Generate a output stream'''
        async with self.client.audio.speech.with_streaming_response.create(
            model=self.model,
            voice=self.voice,
            input=content,
            response_format="wav",
        ) as response:
            output_b = BytesIO(await response.read())
    
            with wave.open(output_b, "r") as f:
                sr = f.getframerate()
                sw = f.getsampwidth()
                ch = f.getnchannels()
                ab = f.readframes(f.getnframes())
            
            yield {
                "audio_bytes": ab,
                "sr": sr,
                "sw": sw,
                "ch": ch
            }