-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_script.py
377 lines (277 loc) · 10.5 KB
/
read_script.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
"""Produced for the 7th edition of Rhizome's 7on7: sevenonseven.rhizome.org
Copyright 2016, Tracy Chou and Claire Evans.
"""
import argparse
import os
import random
import re
import subprocess
from pyfiglet import figlet_format
MALE_VOICES = [
"Alex",
"Daniel",
"Fred",
"Junior",
"Lee",
"Oliver",
"Ralph"
]
FEMALE_VOICES = [
"Agnes",
"Fiona",
"Karen",
"Kathy",
# "Mei-Jia",
"Princess",
"Tessa",
# "Ting-Ting",
"Vicki",
"Victoria"
]
def mic_check():
for voice in MALE_VOICES + FEMALE_VOICES:
print("Next up: {voice}".format(voice=voice))
say_cmd = "say -v {voice} {dialog}".format(voice=voice, dialog="Test test 1 2 3")
subprocess.call(say_cmd.split())
class Gender:
MALE = 1
FEMALE = 2
class Character(object):
def __init__(self, number, gender=None, voice=None, is_ai=None, color=None):
self.placeholder = "X{number}".format(number=number)
self.color = color
self.gender = gender
self.voice = voice
self.is_ai = is_ai
def set_gender(self, gender):
self.gender = gender
def set_voice(self, voice):
self.voice = voice
def set_is_ai(self, is_ai):
self.is_ai = is_ai
def set_color(self, color):
self.color = color
@property
def name(self):
return self.voice
def say(self, dialog):
# Default speed is 200 wpm for humans, 250 pm for AIs. Override to a
# slow speed of 130pm if there"s a directive in the script as such.
wpm = 250 if self.is_ai else 200
if should_read_slowly(dialog):
wpm = 130
dialog = replace_pauses(dialog)
dialog = remove_parentheticals(dialog)
say_cmd = "say -v {voice} -r {wpm} {dialog}".format(
voice=self.voice, wpm=wpm, dialog=dialog)
subprocess.call(say_cmd.split())
class Colors:
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
MAGENTA = "\033[35m"
CYAN = "\033[36m"
GRAY = "\033[37m"
WHITE = "\033[38m"
@classmethod
def all(cls):
return [
cls.RED,
cls.GREEN,
cls.YELLOW,
cls.BLUE,
cls.MAGENTA,
cls.CYAN,
cls.GRAY,
cls.WHITE
]
class Formatting:
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
END = "\033[0m"
def batch_set_voices(characters, gender):
if gender == Gender.MALE:
voices = MALE_VOICES
else:
voices = FEMALE_VOICES
random.shuffle(voices)
for character, voice in zip(characters, voices[:len(characters)]):
character.set_voice(voice)
def construct_characters(genders, is_ais, colors):
assert len(genders) == len(is_ais) == len(colors)
num_characters = len(genders)
characters = [Character(i) for i in range(1, num_characters+1)]
male_characters = []
female_characters = []
for character, gender, is_ai, color in zip(characters, genders, is_ais, colors):
character.set_gender(gender)
character.set_is_ai(is_ai)
character.set_color(color)
if gender == Gender.MALE:
male_characters.append(character)
else:
female_characters.append(character)
# Batch set the voices by gender so we don't randomly select the same voice
# for multiple characters.
batch_set_voices(male_characters, Gender.MALE)
batch_set_voices(female_characters, Gender.FEMALE)
return dict((character.placeholder, character) for character in characters)
class Acts:
I = "ACT I"
II = "ACT II"
III = "ACT III"
@classmethod
def all(cls):
return [cls.I, cls.II, cls.III]
CHARACTER_PROG = re.compile("(?P<placeholder>X[1-4])[^:]*:(?P<dialog>.*)")
def get_character_placeholder_and_dialog(line):
match = CHARACTER_PROG.match(line)
if match:
return match.group("placeholder"), match.group("dialog")
else:
return None, None
HE_SHE_PROG = re.compile("\((?P<placeholder>X[1-4]) (?P<pronoun>he/she)\)", flags=re.IGNORECASE)
HIS_HER_PROG = re.compile("\((?P<placeholder>X[1-4]) (?P<pronoun>his/her)\)", flags=re.IGNORECASE)
HIM_HER_PROG = re.compile("\((?P<placeholder>X[1-4]) (?P<pronoun>him/her)\)", flags=re.IGNORECASE)
def replace_gender_pronoun(prog, line, characters, female_pronoun, male_pronoun):
for match in re.finditer(prog, line):
placeholder = match.group("placeholder")
character = characters.get(placeholder)
pronoun_capitalized = match.group("pronoun")[0].isupper()
if character.gender == Gender.MALE:
pronoun = male_pronoun
else:
pronoun = female_pronoun
if pronoun_capitalized:
pronoun = pronoun.capitalize()
line = re.sub(prog, pronoun, line)
return line
def replace_gender_pronouns(line, characters):
line = replace_gender_pronoun(HE_SHE_PROG, line, characters, "she", "he")
line = replace_gender_pronoun(HIS_HER_PROG, line, characters, "her", "his")
line = replace_gender_pronoun(HIM_HER_PROG, line, characters, "her", "him")
return line
CHARACTER_PLACEHOLDER_PROG = re.compile("(?P<placeholder>X[1-4])", flags=re.IGNORECASE)
def replace_placeholder_names(line, characters):
prog = CHARACTER_PLACEHOLDER_PROG
for match in re.finditer(prog, line):
placeholder = match.group("placeholder")
character = characters.get(placeholder)
name = character.name
line = re.sub(prog, name, line, count=1)
return line
SLOW_SPEED_DIRECTIVE_PROG = re.compile("\(Slowly.*\) ")
def should_read_slowly(line):
match = SLOW_SPEED_DIRECTIVE_PROG.search(line)
return bool(match)
BEAT_PROG = re.compile("\(Beat.\) ")
MILLISECOND_WAIT_PROG = re.compile("\(After a (?P<ms>\d+) millisecond pause\.\)")
def replace_pauses(line):
line = re.sub(BEAT_PROG, "[[slnc 500]]", line)
match = MILLISECOND_WAIT_PROG.search(line)
if match:
ms = match.group("ms")
line = re.sub(MILLISECOND_WAIT_PROG, "[[slnc {ms}]]".format(ms=ms), line)
return line
PARENTHETICAL_PROG = re.compile("\([^\)]*\)")
def remove_parentheticals(dialog):
return re.sub(PARENTHETICAL_PROG, "", dialog)
def print_line_in_color(line, color, bold=False, remove_trailing_newline=False):
bold_formatting = Formatting.BOLD if bold else ""
if remove_trailing_newline:
print("%s%s%s%s" % (bold_formatting, color, line, Formatting.END),)
else:
print("%s%s%s%s" % (bold_formatting, color, line, Formatting.END))
def clear_screen():
os.system("cls||clear")
def read_script(filename, characters, start, end):
with open(filename) as f:
started = False
ended = False
for line in f:
line = line.strip()
if not started and line == "<{start}>".format(start=start):
started = True
elif started and line == "</{end}>".format(end=end):
ended = True
if not started:
continue
if any(line == "<{act}>".format(act=act) for act in Acts.all()) or \
any(line == "</{act}>".format(act=act) for act in Acts.all()):
# Either the beginning or end of an act, so clear the screen.
clear_screen()
print_line_in_color(figlet_format(line, font="starwars"), Colors.YELLOW)
print_line_in_color(line, Colors.YELLOW, bold=True)
# Press [Enter] to continue.
input()
clear_screen()
continue
# Figure out if this is a line of dialog.
placeholder, dialog = get_character_placeholder_and_dialog(line)
# Do name and pronoun replacements for line to be printed on screen.
line = replace_gender_pronouns(line, characters)
line = replace_placeholder_names(line, characters)
if not line:
print()
elif line and not dialog:
# Stage direction. Press [Enter] to continue.
print_line_in_color(line, Colors.YELLOW, bold=True, remove_trailing_newline=True)
input()
else:
# Dialog!
character = characters.get(placeholder)
print_line_in_color(line, character.color)
# Do name and pronoun replacements for the dialog to be spoken.
dialog = replace_gender_pronouns(dialog, characters)
dialog = replace_placeholder_names(dialog, characters)
character.say(dialog)
if ended:
break
def main(genders=None, start=None, end=None):
clear_screen()
num_characters = 4
if not genders:
genders = [Gender.MALE if random.random() < 0.5 else Gender.FEMALE
for i in range(num_characters)]
colors = [Colors.CYAN, Colors.GREEN, Colors.WHITE, Colors.BLUE]
is_ais = [False, False, True, False]
characters = construct_characters(genders=genders, is_ais=is_ais, colors=colors)
print_line_in_color("Reading script with characters:", Colors.YELLOW)
for placeholder in sorted(characters.keys()):
character = characters.get(placeholder)
if character.gender == Gender.FEMALE:
gender_for_print = "f"
else:
gender_for_print = "m"
print_line_in_color(" {placeholder} in the voice of {name} ({gender})".format(
placeholder=placeholder, name=character.name, gender=gender_for_print),
Colors.YELLOW)
input()
if not start:
start = Acts.I
if not end:
end = Acts.III
assert start <= end
filename = "svs.txt"
read_script(filename, characters, start, end)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--genders", type=str, help="""
a four-letter string of f's and m's to indicate the genders of the four
characters, in order; e.g., FMFF""", required=False)
parser.add_argument("--start", type=str, help="""
the act to start reading; e.g., ACT I""", required=False)
parser.add_argument("--end", type=str, help="""the act to stop reading;
e.g., ACT II""", required=False)
args = parser.parse_args()
genders = args.genders
if genders:
assert len(genders) == 4
genders = [gender.lower() == "f" and Gender.FEMALE or
gender.lower() == "m" and Gender.MALE
for gender in list(genders)]
start = args.start
end = args.end
# mic_check()
main(genders=genders, start=start, end=end)