-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynthesize.py
56 lines (40 loc) · 1.53 KB
/
synthesize.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
import sys
import tempfile
from melody import melodies
from xml_building import build_xml_file_content
from syllable_counter import get_number_of_syllables
from filters import filter_digits_to_text, filter_remove_nonletters
def print_festival_singing_command(xml_file_content):
filename = tempfile.mktemp()
with open(filename, 'w') as f:
f.write(xml_file_content)
print('(tts \"%s\" \'singing)' % filename)
def synthesize(message, melody):
message = message.lower()
message = filter_digits_to_text(message)
message = filter_remove_nonletters(message)
words = message.split(' ')
words = [word for word in words if word != '']
notes_iterator = melody.get_notes_iterator()
pitches = []
for word in words:
num_syllabes = get_number_of_syllables(word)
notes = []
beats = []
for unused_i in range(num_syllabes):
try:
note, beat = next(notes_iterator)
except StopIteration:
notes_iterator = melody.get_notes_iterator()
note, beat = next(notes_iterator)
notes.append(note)
beats.append(beat)
pitches.append((notes, beats, word))
xml_file_content = build_xml_file_content(melody.get_bpm(), pitches)
print_festival_singing_command(xml_file_content)
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage: %s melody" % sys.argv[0])
print("Text is read from stdin")
sys.exit(0)
synthesize(sys.stdin.read(), melodies[sys.argv[1]])