-
Notifications
You must be signed in to change notification settings - Fork 2
/
freeswitch_nl_translations.py
executable file
·133 lines (111 loc) · 5.05 KB
/
freeswitch_nl_translations.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
124
125
126
127
128
129
130
131
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import csv
import os
import requests
import config
import json
import base64
import sys
import getopt
class GoogleTTS:
def __init__(self):
self.url = "https://texttospeech.googleapis.com/v1/text:synthesize"
self.apiKey = config.config['googleAPIkey']
self.voice = {'name': None, 'languageCode': None}
self.httpSession = requests.session()
self.audioConfig = {'audioEncoding': config.config['audioEncoding'],
'speakingRate': config.config['speakingRate'],
'pitch': config.config['pitch'],
'volumeGainDb': config.config['volumeGainDb'],
'sampleRateHertz': config.config['sampleRateHertz'],
'effectsProfileId': config.config['effectsProfileId']
}
def setVoice(self, name, languageCode):
self.voice['name'] = name
self.voice['languageCode'] = languageCode
def produce(self, text=None, ssml=None):
if text is None and ssml is None:
print("No text or ssml to produce")
return
data = {
"input": {},
"voice": {"name": self.voice['name'], "languageCode": self.voice['languageCode']},
"audioConfig": self.audioConfig
}
if text is not None:
data['input']['text'] = text
if ssml is not None:
data['input']['ssml'] = ssml
headers = {"content-type": "application/json", "X-Goog-Api-Key": self.apiKey }
r = self.httpSession.post(url=self.url, json=data, headers=headers)
content = json.loads(r.content)
if 'audioContent' not in content:
print(headers)
print(content)
sys.exit(0)
return base64.b64decode(content['audioContent'])
reDownload = None
opts, args = getopt.getopt(sys.argv[1:],"n")
for opt, arg in opts:
if opt == '-n':
print ('Not redownloading existing files')
reDownload = False
if reDownload is None:
reDownload = input("Download existing audiofiles again? [y/N] ")
if reDownload == 'Y' or reDownload == 'y':
reDownload = True
else:
reDownload = False
tts = GoogleTTS()
tts.setVoice(name='nl-NL-Wavenet-E', languageCode='nl-NL')
#audio = tts.produce(text="Hallo")
fileExt = "wav"
if config.config['audioEncoding'] == 'LINEAR16':
fileExt = 'wav'
elif config.config['audioEncoding'] == 'MP3':
fileExt = 'mp3'
#with open('test.'+fileExt, 'wb') as audioFile:
# audioFile.write(audio)
processLanguages = ['nl-nl'] #columns from csv to process
with open(os.path.join(os.path.abspath( os.path.dirname( __file__ ) ), 'freeswitch_nl_translations.csv'), 'r') as csvfile:
csvreader = csv.DictReader(csvfile, delimiter=';', quotechar='"')
languages = list( next(csvreader).keys() )
languages.remove('folder')
languages.remove('filename')
#print(languages)
csvfile.seek(0)
_ = next(csvreader)
for lang in languages:
if lang not in processLanguages:
print("Skipping language %s"%(lang))
continue
tts.setVoice(name=config.languages[lang]['name'], languageCode=config.languages[lang]['languageCode'])
for row in csvreader:
colLang, colDialect = lang.split('-')
targetDir = os.path.join(os.path.abspath( os.path.dirname( __file__ ) ), 'google', colLang, colDialect, config.languages[lang]['name'], row['folder'].strip('/') )
#print(targetDir)
try:
os.stat(targetDir)
except:
print('Creating folder %s'%(targetDir) )
os.makedirs(targetDir)
#import os.path
file_exists = os.path.isfile( os.path.join(targetDir, row['filename']+'.'+fileExt) )
if (reDownload and file_exists) or not file_exists:
if row[lang] is not None and row[lang] != '':
audio = tts.produce(text=row[lang])
with open(os.path.join(targetDir, row['filename']+'.'+fileExt), 'wb') as audioFile:
audioFile.write(audio)
else:
print("skipping download: %s"%( os.path.join(targetDir, row['filename']+'.'+fileExt) ) )
for samplerate in [8000,16000,32000,48000]:
targetDir2 = os.path.join(os.path.abspath( os.path.dirname( __file__ ) ), 'output', colLang, colDialect, config.languages[lang]['name'], row['folder'].strip('/'), str(samplerate) )
try:
os.stat(targetDir2)
except:
print('Creating folder %s'%(targetDir2) )
os.makedirs(targetDir2)
#print 'Converting %s/%s to %s/%s'%(targetdir,wavfile,targetdir2,wavfile)
os.system( 'sox %s -r %d -c 1 -e signed-integer %s'%(os.path.join(targetDir, row['filename']+'.'+fileExt),samplerate,os.path.join(targetDir2, row['filename']+'.'+fileExt)) )
continue