-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtts.py
123 lines (98 loc) · 3.58 KB
/
tts.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# requires: pydub requests gtts hachoir
import io
import os
import requests
from gtts import gTTS
from pydub import AudioSegment
from .. import loader, utils
def register(cb):
cb(DttsMod())
class DttsMod(loader.Module):
"""Text to speech module"""
strings = {
"name": "DTTS",
"no_text": "I can't say nothing",
"tts_lang_cfg": "Set your language code for the TTS here.",
}
def __init__(self):
self.config = loader.ModuleConfig(
"TTS_LANG", "en", lambda m: self.strings("tts_lang_cfg", m)
)
self.is_ffmpeg = os.system("ffmpeg -version") == 0
async def say(self, message, speaker, text, file=".dtts.mp3"):
reply = await message.get_reply_message()
if not text:
if not reply:
return await utils.answer(message, self.strings["no_text"])
text = reply.raw_text # use text from reply
if not text:
return await utils.answer(message, self.strings["no_text"])
if message.out:
await message.delete() # Delete message only one is user's
data = {"text": text}
if speaker:
data["speaker"] = speaker
# creating file in memory
f = io.BytesIO(
requests.get("https://station.aimylogic.com/generate", data=data).content
)
f.name = file
if self.is_ffmpeg:
f, duration = to_voice(f)
else:
duration = None
await message.client.send_file(
message.chat_id, f, voice_note=True, reply_to=reply, duration=duration
)
@loader.unrestricted
@loader.ratelimit
async def levitancmd(self, message):
"""Convert text to speech with levitan voice"""
await self.say(message, "levitan", utils.get_args_raw(message))
@loader.unrestricted
@loader.ratelimit
async def oksanacmd(self, message):
"""Convert text to speech with oksana voice"""
await self.say(message, "oksana", utils.get_args_raw(message))
@loader.unrestricted
@loader.ratelimit
async def yandexcmd(self, message):
"""Convert text to speech with yandex voice"""
await self.say(message, None, utils.get_args_raw(message))
@loader.unrestricted
@loader.ratelimit
async def ttscmd(self, message):
"""Convert text to speech with Google APIs"""
reply = await message.get_reply_message()
text = utils.get_args_raw(message.message)
if not text:
if message.is_reply:
text = (await message.get_reply_message()).message
else:
return await utils.answer(message, self.strings("no_text", message))
if message.out:
await message.delete()
tts = await utils.run_sync(gTTS, text, lang=self.config["TTS_LANG"])
voice = io.BytesIO()
await utils.run_sync(tts.write_to_fp, voice)
voice.seek(0)
voice.name = "voice.mp3"
if self.is_ffmpeg:
voice, duration = to_voice(voice)
else:
duration = None
await message.client.send_file(
message.chat_id, voice, voice_note=True, reply_to=reply, duration=duration
)
def to_voice(item):
"""Returns audio in opus format and it's duration"""
item.seek(0)
item = AudioSegment.from_file(item)
m = io.BytesIO()
m.name = "voice.ogg"
item.split_to_mono()
dur = len(item) / 1000
item.export(m, format="ogg", bitrate="64k", codec="libopus")
m.seek(0)
return m, dur
# By @vreply @pernel_kanic @nim1love @db0mb3r and add @tshipenchko some geyporn