-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathambient_sound.py
285 lines (235 loc) · 10.7 KB
/
ambient_sound.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
import logging
import random
import subprocess
import os
import sys
import psutil
import glob
import threading
from time import sleep
from kalliope.core.Utils import Utils
from kalliope.core.NeuronModule import NeuronModule, InvalidParameterException
logging.basicConfig()
logger = logging.getLogger("kalliope")
pid_file_path = "pid.txt"
sound_path = "sounds/"
class AmbientSound:
def __init__(self, name=None, file_extension=None):
self.name = name
self.file_extension = file_extension
def __str__(self):
return "name: %s, file_extension: %s" % (self.name, self.file_extension)
class SoundDatabase:
def __init__(self):
self.available_sounds = self.load_available_sounds()
@staticmethod
def load_available_sounds():
"""
Check all file in the sub folder "sounds" and return a list of AmbientSound object
:return: list of AmbientSound object
"""
list_ambient_sounds = list()
list_sound_name_with_extension = \
[os.path.basename(x) for x in glob.glob(SoundDatabase.get_sound_folder_path() + '/*')]
logger.debug("[Ambient_sounds] file in sound folder: %s" % list_sound_name_with_extension)
for sound_with_extension in list_sound_name_with_extension:
tuple_name_extension = os.path.splitext(sound_with_extension)
# print tuple_name_extension
new_ambient_sound = AmbientSound(name=tuple_name_extension[0], file_extension=tuple_name_extension[1])
list_ambient_sounds.append(new_ambient_sound)
return list_ambient_sounds
def get_sound_by_name(self, sound_name_to_find):
"""
Return an AmbientSound object from a given name if exist in list_ambient_sounds of the sound database
:param sound_name_to_find: name of the sound to find
:return:
"""
for ambient_sound in self.available_sounds:
if ambient_sound.name == sound_name_to_find:
return ambient_sound
return None
def get_random_ambient_sound(self):
"""
return an AmbientSound object randomly from available sounds
:return: AmbientSound object
"""
return random.choice(self.available_sounds)
@classmethod
def get_neuron_path(cls):
"""
Return the absolute path of the current neuron folder
:return: absolute path of the current neuron folder
"""
current_script_path = os.path.abspath(__file__)
return os.path.normpath(current_script_path + os.sep + os.pardir)
@classmethod
def get_sound_folder_path(cls):
"""
Return the absolute path of the current sound folder in the neuron folder
:return: absolute path of the current sound folder in the neuron folder
"""
absolute_sound_file_path = cls.get_neuron_path() + os.sep + sound_path
logger.debug("[Ambient_sounds] absolute_sound_folder_path: %s" % absolute_sound_file_path)
return absolute_sound_file_path
class Ambient_sound(NeuronModule):
"""
Ambient sound neuron
Play a sound from the list of sound available in the sound folder
"""
def __init__(self, **kwargs):
super(Ambient_sound, self).__init__(**kwargs)
self.state = kwargs.get('state', None)
self.sound_name = kwargs.get('sound_name', None)
self.mplayer_path = kwargs.get('mplayer_path', "/usr/bin/mplayer")
self.auto_stop_minutes = kwargs.get('auto_stop_minutes', None)
# this is the target AmbientSound object if the user gave a sound_name to play.
# this object will be loaded by the _is_parameters_ok function durring the check if the sound exist
self.target_ambient_sound = None
# sound database
self.sdb = SoundDatabase()
# message dict that will be passed to the neuron template
self.message = dict()
# add the list of available sounds
self.message["available_sounds"] = list()
for sound in self.sdb.available_sounds:
self.message["available_sounds"].append(sound.name)
# check if sent parameters are in good state
if self._is_parameters_ok():
if self.state == "off":
self.stop_last_process()
self.clean_pid_file()
else:
# we stop the last process if exist
self.stop_last_process()
# if the user haven't given a sound name
if self.target_ambient_sound is None:
# then we load one randomly
self.target_ambient_sound = self.sdb.get_random_ambient_sound()
logger.debug("[Ambient_sounds] Random ambient sound selected: %s" % self.target_ambient_sound)
# then we can start a new process
if self.target_ambient_sound is not None:
self.start_new_process(self.target_ambient_sound)
# give the current file name played to the neuron template
self.message["playing_sound"] = self.target_ambient_sound.name
# run auto stop thread
if self.auto_stop_minutes:
thread_auto_stop = threading.Thread(target=self.wait_before_stop)
thread_auto_stop.start()
# give the message dict to the neuron template
self.say(self.message)
def wait_before_stop(self):
logger.debug("[Ambient_sounds] Wait %s minutes before checking if the thread is alive" % self.auto_stop_minutes)
Utils.print_info("[Ambient_sounds] Wait %s minutes before stopping the ambient sound" % self.auto_stop_minutes)
sleep(self.auto_stop_minutes*60) # *60 to convert received minutes into seconds
logger.debug("[Ambient_sounds] Time is over, Stop player")
Utils.print_info("[Ambient_sounds] Time is over, stopping the ambient sound")
self.stop_last_process()
def _is_parameters_ok(self):
"""
Check that all given parameter are valid
:return: True if all given parameter are ok
"""
if self.state not in ["on", "off"]:
raise InvalidParameterException("[Ambient_sounds] State must be 'on' or 'off'")
# check that the given sound name exist
if self.sound_name is not None:
self.target_ambient_sound = self.sdb.get_sound_by_name(self.sound_name)
if self.target_ambient_sound is None:
raise InvalidParameterException("[Ambient_sounds] Sound name %s does not exist" % self.sound_name)
# if wait auto_stop_minutes is set, mut be an integer or string convertible to integer
if self.auto_stop_minutes is not None:
if not isinstance(self.auto_stop_minutes, int):
try:
self.auto_stop_minutes = int(self.auto_stop_minutes)
except ValueError:
raise InvalidParameterException("[Ambient_sounds] auto_stop_minutes must be an integer")
# check auto_stop_minutes is positive
if self.auto_stop_minutes < 1:
raise InvalidParameterException("[Ambient_sounds] auto_stop_minutes must be set at least to 1 minute")
return True
@staticmethod
def store_pid(pid):
"""
Store a PID number into a file
:param pid: pid number to save
:return:
"""
content = str(pid)
absolute_pid_file_path = SoundDatabase.get_neuron_path() + os.sep + pid_file_path
try:
with open(absolute_pid_file_path, "wb") as file_open:
if sys.version_info[0] == 2:
file_open.write(content)
else:
file_open.write(content.encode())
file_open.close()
except IOError as e:
logger.error("[Ambient_sounds] I/O error(%s): %s", e.errno, e.strerror)
return False
@staticmethod
def load_pid():
"""
Load a PID number from the pid.txt file
:return:
"""
absolute_pid_file_path = SoundDatabase.get_neuron_path() + os.sep + pid_file_path
if os.path.isfile(absolute_pid_file_path):
try:
with open(absolute_pid_file_path, "r") as file_open:
pid_str = file_open.readline()
if pid_str:
return int(pid_str)
except IOError as e:
logger.debug("[Ambient_sounds] I/O error(%s): %s", e.errno, e.strerror)
return False
return False
def stop_last_process(self):
"""
stop the last mplayer process launched by this neuron
:return:
"""
pid = self.load_pid()
if pid is not None:
logger.debug("[Ambient_sounds] loaded pid: %s" % pid)
try:
p = psutil.Process(pid)
p.kill()
logger.debug("[Ambient_sounds] mplayer process with pid %s killed" % pid)
except psutil.NoSuchProcess:
logger.debug("[Ambient_sounds] the process PID %s does not exist" % pid)
else:
logger.debug("[Ambient_sounds] pid is null. Process already stopped")
def start_new_process(self, target_ambient_sound):
"""
Start mplayer process with the given AmbientSound
:param target_ambient_sound:
:type target_ambient_sound: AmbientSound
:return:
"""
mplayer_exec_path = [self.mplayer_path]
mplayer_options = ['-slave', '-quiet', '-loop', '0']
mplayer_command = list()
mplayer_command.extend(mplayer_exec_path)
mplayer_command.extend(mplayer_options)
mplayer_command.append(SoundDatabase.get_sound_folder_path() + os.sep +
target_ambient_sound.name + target_ambient_sound.file_extension)
logger.debug("[Ambient_sounds] Mplayer cmd: %s" % str(mplayer_command))
# run mplayer in background inside a new process
fnull = open(os.devnull, 'w')
pid = subprocess.Popen(mplayer_command, stdout=fnull, stderr=fnull).pid
# store the pid in a file to be killed later
self.store_pid(pid)
logger.debug("[Ambient_sounds] Mplayer started, pid: %s" % pid)
@staticmethod
def clean_pid_file():
"""
Clean up all data stored in the pid.txt file
"""
absolute_pid_file_path = SoundDatabase.get_neuron_path() + os.sep + pid_file_path
try:
with open(absolute_pid_file_path, "w") as file_open:
file_open.close()
logger.debug("[Ambient_sounds] pid file cleaned")
except IOError as e:
logger.error("I/O error(%s): %s", e.errno, e.strerror)
return False