-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtts.py
More file actions
122 lines (100 loc) · 3.71 KB
/
tts.py
File metadata and controls
122 lines (100 loc) · 3.71 KB
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
"""
TTS idea for ALPACA
How to use Google Cloud:
1. Create a project in Google Cloud Console
https://console.cloud.google.com/
2. On your PCs terminal install gcloud cli and run this commands:
gcloud init
gcloud auth application-default login
gcloud auth application-default set-quota-project XYZ-Project-ID
"""
from pathlib import Path
import subprocess
from google.cloud import texttospeech
import json
SRC_PATH = "data-src/dialog/2080.schnack"
# Instantiates a client
client = texttospeech.TextToSpeechClient()
# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.VoiceSelectionParams(
language_code="de-DE",
# ssml_gender=texttospeech.SsmlVoiceGender.FEMALE,
name="de-DE-Chirp3-HD-Algenib",
)
# Select the type of audio file you want returned
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3,
# speaking_rate=1.23,
speaking_rate=1.00,
# effects_profile_id=["handset-class-device"],
)
def get_voice_by_localization_id(dialogs, characters, localization_id) -> texttospeech.VoiceSelectionParams:
character: str = ""
ai_voice: str = "de-DE-Chirp3-HD-Algenib"
for dialog in dialogs:
for node in dialog["nodes"]:
if node["id"] == localization_id:
if "character" in node:
character = node["character"]
for chara in characters:
if str(chara["canonicalName"]).lower() == character.lower():
if "ai_voice" in chara["properties"]:
ai_voice = chara["properties"]["ai_voice"]
return texttospeech.VoiceSelectionParams(
language_code="de-DE",
name=ai_voice,
)
with Path(SRC_PATH).open() as f:
data = f.read()
parsed = json.loads(data)
localizations = parsed["localization"]
dialogs = parsed["dialogs"]
characters = parsed["characters"]
for localization in localizations:
out_path_temp: Path = Path(f"./data-src/audio/voice/de_{localization}.ogg_temp")
out_path: Path = Path(f"./data-src/audio/voice/de_{localization}.ogg")
if localizations[localization][0] == "":
continue
# Only new dialogs
if out_path.exists():
continue
text = str(localizations[localization][0]).replace("\n", " ")
if not out_path_temp.exists():
print(f"{localization}.ogg")
# Set the text input to be synthesized
synthesis_input = texttospeech.SynthesisInput(
text=text
)
voice = get_voice_by_localization_id(dialogs, characters, localization)
# Perform the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = client.synthesize_speech(
input=synthesis_input, voice=voice, audio_config=audio_config
)
# The response's audio_content is binary.
with out_path_temp.open("wb") as out:
# Write the response to the output file.
out.write(response.audio_content)
print(f'Audio content written to file "{out_path_temp}"')
command = [
"ffmpeg",
"-i",
str(out_path_temp),
"-c:a",
"vorbis",
"-ac",
"2",
"-strict",
"experimental",
str(out_path),
"-y",
]
p = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
output = p.communicate()[0]
out_path_temp.unlink(missing_ok=True)