-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
151 lines (116 loc) · 4.41 KB
/
main.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
import pygame
import os
import time
import subprocess
import sys
import random
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
global keys_file
keys_file = None
global on
on = 0
class MyHandler(FileSystemEventHandler):
def __init__(self, sound_files_map, *args, **kwargs):
super().__init__(*args, **kwargs)
self.sound_files_map = sound_files_map
def on_modified(self, event):
global keys_file
global on
if keys_file is None and event.src_path.endswith(".keys.txt"):
keys_file = open(event.src_path, "r");
key = keys_file.readline().strip()
if key in self.sound_files_map:
if key == "V":
soundObj = pygame.mixer.Sound(self.sound_files_map[key][on])
on = (on + 1) % 2
else:
soundObj = pygame.mixer.Sound(self.sound_files_map[key])
soundObj.play()
#print(f'event type: {event.event_type} path : {event.src_path}')
cur_dir = os.path.dirname(os.path.abspath(__file__))
exec_file = "/build/bin/lmeca2710_project"
sound_dir = "sound"
# Choose the theme you prefer
theme = "synthwave.wav"
#theme = "chill.wav"
# Sound effects
sfm_default = dict(A=os.path.join(cur_dir, sound_dir, "pew.wav"),
D=os.path.join(cur_dir, sound_dir, "boum.wav"),
V=(os.path.join(cur_dir, sound_dir, "turn_on.wav"),
os.path.join(cur_dir, sound_dir, "turn_off.wav"))
)
sfm_youpidou = dict(A=os.path.join(cur_dir, sound_dir, "piou_piou.wav"),
D=os.path.join(cur_dir, sound_dir, "on_aime_pas_bis.wav"),
V=(os.path.join(cur_dir, sound_dir, "ohoui.wav"),
os.path.join(cur_dir, sound_dir, "il_est_mort.wav")),
QUIT=os.path.join(cur_dir, sound_dir, "on_va_manger.wav"),
ENTER=os.path.join(cur_dir, sound_dir, "it_must_work.wav")
)
random_talks = [
os.path.join(cur_dir, sound_dir, "aucune_chance.wav"),
os.path.join(cur_dir, sound_dir, "complique.wav"),
os.path.join(cur_dir, sound_dir, "piece_of_cake_inc.wav"),
os.path.join(cur_dir, sound_dir, "puree.wav"),
os.path.join(cur_dir, sound_dir, "vilain_vilain.wav"),
os.path.join(cur_dir, sound_dir, "youpidou.wav"),
os.path.join(cur_dir, sound_dir, "youtubepointcomhein.wav"),
os.path.join(cur_dir, sound_dir, "voila_voila.wav"),
os.path.join(cur_dir, sound_dir, "ca_va_prendre_du_temps.wav"),
os.path.join(cur_dir, sound_dir, "aiou.wav"),
os.path.join(cur_dir, sound_dir, "biiiiiiiyoup.wav"),
]
pygame.mixer.init()
if __name__ == "__main__":
if "-y" in sys.argv:
mode = "YOUPIDOU"
sfm = sfm_youpidou
else:
mode = "default"
sfm = sfm_default
event_handler = MyHandler(sfm)
observer = Observer()
observer.schedule(event_handler, path=os.path.join(cur_dir, "data"), recursive=False)
observer.start()
args = ["." + os.path.join(cur_dir, exec_file)]
args.extend(sys.argv[1:])
process = subprocess.Popen(args)
file = os.path.join(cur_dir, sound_dir, theme)
soundObj = pygame.mixer.Sound(file)
time.sleep(1)
if mode == "YOUPIDOU":
soundObj_enter = pygame.mixer.Sound(sfm["ENTER"])
channel = soundObj_enter.play()
while channel.get_busy():
time.sleep(0.1)
soundObj_enter.stop()
soundObj.set_volume(0.5)
soundObj.play(-1)
soundObj_random = None
channel_random = None
last_sound = None
while process.poll() is None:
time.sleep(0.5)
if mode == "YOUPIDOU":
if channel_random is None or not channel_random.get_busy():
if channel_random:
soundObj_random.stop()
will_play = random.random() > 0.8
if will_play:
r_sound = random.choice(random_talks)
while r_sound == last_sound:
r_sound = random.choice(random_talks)
last_sound = r_sound
soundObj_random = pygame.mixer.Sound(r_sound)
channel_random = soundObj_random.play()
soundObj.stop()
if soundObj_random:
soundObj_random.stop()
if keys_file is not None:
keys_file.close()
if mode == "YOUPIDOU":
soundObj = pygame.mixer.Sound(sfm["QUIT"])
channel = soundObj.play(fade_ms=300)
while channel.get_busy():
time.sleep(0.1)
soundObj.stop()