diff options
Diffstat (limited to 'helpers')
-rw-r--r-- | helpers/action.py | 6 | ||||
-rw-r--r-- | helpers/mapping.py | 15 | ||||
-rw-r--r-- | helpers/mixer.py | 3 | ||||
-rw-r--r-- | helpers/music_file.py | 352 |
4 files changed, 182 insertions, 194 deletions
diff --git a/helpers/action.py b/helpers/action.py index 99cd399..ec8fcb6 100644 --- a/helpers/action.py +++ b/helpers/action.py | |||
@@ -30,7 +30,7 @@ class Action: | |||
30 | 30 | ||
31 | def ready(self): | 31 | def ready(self): |
32 | if 'music' in self.arguments: | 32 | if 'music' in self.arguments: |
33 | return self.arguments['music'].check_is_loaded() | 33 | return self.arguments['music'].is_loaded(allow_substates=True) |
34 | else: | 34 | else: |
35 | return True | 35 | return True |
36 | 36 | ||
@@ -71,14 +71,14 @@ class Action: | |||
71 | loop=0, **kwargs): | 71 | loop=0, **kwargs): |
72 | for music in self.music_list(music): | 72 | for music in self.music_list(music): |
73 | if restart_if_running: | 73 | if restart_if_running: |
74 | if music.is_not_stopped(): | 74 | if music.is_in_use(): |
75 | music.stop() | 75 | music.stop() |
76 | music.play( | 76 | music.play( |
77 | volume=volume, | 77 | volume=volume, |
78 | fade_in=fade_in, | 78 | fade_in=fade_in, |
79 | start_at=start_at, | 79 | start_at=start_at, |
80 | loop=loop) | 80 | loop=loop) |
81 | elif not music.is_not_stopped(): | 81 | elif not music.is_in_use(): |
82 | music.play( | 82 | music.play( |
83 | volume=volume, | 83 | volume=volume, |
84 | fade_in=fade_in, | 84 | fade_in=fade_in, |
diff --git a/helpers/mapping.py b/helpers/mapping.py index d60e709..c2a94e6 100644 --- a/helpers/mapping.py +++ b/helpers/mapping.py | |||
@@ -10,7 +10,6 @@ import sys | |||
10 | from .music_file import * | 10 | from .music_file import * |
11 | from .mixer import Mixer | 11 | from .mixer import Mixer |
12 | from . import Config, gain, error_print | 12 | from . import Config, gain, error_print |
13 | from .music_effect import GainEffect | ||
14 | from .action import Action | 13 | from .action import Action |
15 | 14 | ||
16 | class Mapping(RelativeLayout): | 15 | class Mapping(RelativeLayout): |
@@ -47,19 +46,7 @@ class Mapping(RelativeLayout): | |||
47 | self.master_volume) | 46 | self.master_volume) |
48 | 47 | ||
49 | for music in self.open_files.values(): | 48 | for music in self.open_files.values(): |
50 | if not (music.is_loaded_playing() or music.is_loaded_paused()): | 49 | music.set_gain_with_effect(db_gain, fade=fade) |
51 | continue | ||
52 | |||
53 | if fade > 0: | ||
54 | music.gain_effects.append(GainEffect( | ||
55 | "fade", | ||
56 | music.current_audio_segment, | ||
57 | music.current_loop, | ||
58 | music.sound_position, | ||
59 | music.sound_position + fade, | ||
60 | gain=db_gain)) | ||
61 | else: | ||
62 | music.set_gain(db_gain) | ||
63 | 50 | ||
64 | def add_wait_id(self, wait_id, action_or_wait): | 51 | def add_wait_id(self, wait_id, action_or_wait): |
65 | self.wait_ids[wait_id] = action_or_wait | 52 | self.wait_ids[wait_id] = action_or_wait |
diff --git a/helpers/mixer.py b/helpers/mixer.py index 1d3f28f..9242b61 100644 --- a/helpers/mixer.py +++ b/helpers/mixer.py | |||
@@ -35,7 +35,8 @@ class Mixer: | |||
35 | self.start() | 35 | self.start() |
36 | 36 | ||
37 | def remove_file(self, music_file): | 37 | def remove_file(self, music_file): |
38 | self.open_files.remove(music_file) | 38 | if music_file in self.open_files: |
39 | self.open_files.remove(music_file) | ||
39 | if len(self.open_files) == 0: | 40 | if len(self.open_files) == 0: |
40 | self.stop() | 41 | self.stop() |
41 | 42 | ||
diff --git a/helpers/music_file.py b/helpers/music_file.py index 017fc59..ccf60ce 100644 --- a/helpers/music_file.py +++ b/helpers/music_file.py | |||
@@ -15,84 +15,78 @@ from .music_effect import GainEffect | |||
15 | file_lock = Lock("file") | 15 | file_lock = Lock("file") |
16 | 16 | ||
17 | class MusicFile: | 17 | class MusicFile: |
18 | STATES = [ | ||
19 | 'initial', | ||
20 | 'loading', | ||
21 | 'failed', | ||
22 | { | ||
23 | 'name': 'loaded', | ||
24 | 'children': [ | ||
25 | 'playing', | ||
26 | 'paused', | ||
27 | 'stopping' | ||
28 | ] | ||
29 | } | ||
30 | ] | ||
31 | TRANSITIONS = [ | ||
32 | { | ||
33 | 'trigger': 'load', | ||
34 | 'source': 'initial', | ||
35 | 'dest': 'loading' | ||
36 | }, | ||
37 | { | ||
38 | 'trigger': 'fail', | ||
39 | 'source': 'loading', | ||
40 | 'dest': 'failed' | ||
41 | }, | ||
42 | { | ||
43 | 'trigger': 'success', | ||
44 | 'source': 'loading', | ||
45 | 'dest': 'loaded' | ||
46 | }, | ||
47 | { | ||
48 | 'trigger': 'start_playing', | ||
49 | 'source': 'loaded', | ||
50 | 'dest': 'loaded_playing' | ||
51 | }, | ||
52 | { | ||
53 | 'trigger': 'pause', | ||
54 | 'source': 'loaded_playing', | ||
55 | 'dest': 'loaded_paused' | ||
56 | }, | ||
57 | { | ||
58 | 'trigger': 'unpause', | ||
59 | 'source': 'loaded_paused', | ||
60 | 'dest': 'loaded_playing' | ||
61 | }, | ||
62 | { | ||
63 | 'trigger': 'stop_playing', | ||
64 | 'source': ['loaded_playing','loaded_paused'], | ||
65 | 'dest': 'loaded_stopping' | ||
66 | }, | ||
67 | { | ||
68 | 'trigger': 'stopped', | ||
69 | 'source': '*', | ||
70 | 'dest': 'loaded', | ||
71 | 'before': 'trigger_stopped_events' | ||
72 | } | ||
73 | ] | ||
74 | |||
18 | def __init__(self, filename, mapping, name=None, gain=1): | 75 | def __init__(self, filename, mapping, name=None, gain=1): |
19 | states = [ | 76 | Machine(model=self, states=self.STATES, |
20 | 'initial', | 77 | transitions=self.TRANSITIONS, initial='initial', |
21 | 'loading', | ||
22 | 'failed', | ||
23 | { | ||
24 | 'name': 'loaded', | ||
25 | 'children': [ | ||
26 | 'stopped', | ||
27 | 'playing', | ||
28 | 'paused', | ||
29 | 'stopping', | ||
30 | 'stopped' | ||
31 | ] | ||
32 | } | ||
33 | ] | ||
34 | transitions = [ | ||
35 | { | ||
36 | 'trigger': 'load', | ||
37 | 'source': 'initial', | ||
38 | 'dest': 'loading' | ||
39 | }, | ||
40 | { | ||
41 | 'trigger': 'fail', | ||
42 | 'source': 'loading', | ||
43 | 'dest': 'failed' | ||
44 | }, | ||
45 | { | ||
46 | 'trigger': 'success', | ||
47 | 'source': 'loading', | ||
48 | 'dest': 'loaded_stopped' | ||
49 | }, | ||
50 | { | ||
51 | 'trigger': 'start_playing', | ||
52 | 'source': 'loaded_stopped', | ||
53 | 'dest': 'loaded_playing' | ||
54 | }, | ||
55 | { | ||
56 | 'trigger': 'pause', | ||
57 | 'source': 'loaded_playing', | ||
58 | 'dest': 'loaded_paused' | ||
59 | }, | ||
60 | { | ||
61 | 'trigger': 'unpause', | ||
62 | 'source': 'loaded_paused', | ||
63 | 'dest': 'loaded_playing' | ||
64 | }, | ||
65 | { | ||
66 | 'trigger': 'stop_playing', | ||
67 | 'source': ['loaded_playing','loaded_paused'], | ||
68 | 'dest': 'loaded_stopping' | ||
69 | }, | ||
70 | { | ||
71 | 'trigger': 'stopped', | ||
72 | 'source': 'loaded_stopping', | ||
73 | 'dest': 'loaded_stopped', | ||
74 | 'after': 'trigger_stopped_events' | ||
75 | } | ||
76 | ] | ||
77 | |||
78 | Machine(model=self, states=states, | ||
79 | transitions=transitions, initial='initial', | ||
80 | ignore_invalid_triggers=True) | 78 | ignore_invalid_triggers=True) |
81 | 79 | ||
82 | self.volume = 100 | ||
83 | self.mapping = mapping | 80 | self.mapping = mapping |
84 | self.filename = filename | 81 | self.filename = filename |
85 | self.name = name or filename | 82 | self.name = name or filename |
86 | self.audio_segment = None | 83 | self.audio_segment = None |
87 | self.audio_segment_frame_width = 0 | ||
88 | self.initial_volume_factor = gain | 84 | self.initial_volume_factor = gain |
89 | self.music_lock = Lock("music__" + filename) | 85 | self.music_lock = Lock("music__" + filename) |
90 | self.wait_event = threading.Event() | ||
91 | self.db_gain = 0 | ||
92 | self.gain_effects = [] | ||
93 | 86 | ||
94 | threading.Thread(name="MSMusicLoad", target=self.load).start() | 87 | threading.Thread(name="MSMusicLoad", target=self.load).start() |
95 | 88 | ||
89 | # Machine related events | ||
96 | def on_enter_loading(self): | 90 | def on_enter_loading(self): |
97 | with file_lock: | 91 | with file_lock: |
98 | try: | 92 | try: |
@@ -105,7 +99,6 @@ class MusicFile: | |||
105 | .set_channels(Config.channels) \ | 99 | .set_channels(Config.channels) \ |
106 | .set_sample_width(Config.sample_width) \ | 100 | .set_sample_width(Config.sample_width) \ |
107 | .apply_gain(initial_db_gain) | 101 | .apply_gain(initial_db_gain) |
108 | self.audio_segment_frame_width = self.audio_segment.frame_width | ||
109 | self.sound_duration = self.audio_segment.duration_seconds | 102 | self.sound_duration = self.audio_segment.duration_seconds |
110 | except Exception as e: | 103 | except Exception as e: |
111 | error_print("failed to load « {} »: {}".format(self.name, e)) | 104 | error_print("failed to load « {} »: {}".format(self.name, e)) |
@@ -115,28 +108,40 @@ class MusicFile: | |||
115 | self.success() | 108 | self.success() |
116 | debug_print("Loaded « {} »".format(self.name)) | 109 | debug_print("Loaded « {} »".format(self.name)) |
117 | 110 | ||
118 | def check_is_loaded(self): | 111 | def on_enter_loaded(self): |
119 | return self.state.startswith('loaded_') | 112 | self.gain_effects = [] |
113 | self.set_gain(0, absolute=True) | ||
114 | self.current_audio_segment = None | ||
115 | self.volume = 100 | ||
116 | self.wait_event = threading.Event() | ||
117 | self.current_loop = 0 | ||
118 | |||
119 | def on_enter_loaded_playing(self): | ||
120 | self.mixer.add_file(self) | ||
120 | 121 | ||
121 | def is_not_stopped(self): | 122 | # Machine related states |
122 | return self.check_is_loaded() and not self.is_loaded_stopped() | 123 | def is_in_use(self): |
124 | return self.is_loaded(allow_substates=True) and not self.is_loaded() | ||
123 | 125 | ||
124 | def is_paused(self): | 126 | def is_in_use_not_stopping(self): |
125 | return self.is_loaded_paused() | 127 | return self.is_loaded_playing() or self.is_loaded_paused() |
126 | 128 | ||
129 | # Machine related triggers | ||
130 | def trigger_stopped_events(self): | ||
131 | self.mixer.remove_file(self) | ||
132 | self.wait_event.set() | ||
133 | |||
134 | # Actions and properties called externally | ||
127 | @property | 135 | @property |
128 | def sound_position(self): | 136 | def sound_position(self): |
129 | if self.is_not_stopped(): | 137 | if self.is_in_use(): |
130 | return self.current_frame / self.current_audio_segment.frame_rate | 138 | return self.current_frame / self.current_audio_segment.frame_rate |
131 | else: | 139 | else: |
132 | return 0 | 140 | return 0 |
133 | 141 | ||
134 | def play(self, fade_in=0, volume=100, loop=0, start_at=0): | 142 | def play(self, fade_in=0, volume=100, loop=0, start_at=0): |
135 | # FIXME: create a "reinitialize" method | ||
136 | self.gain_effects = [] | ||
137 | self.set_gain(gain(volume) + self.mapping.master_gain, absolute=True) | 143 | self.set_gain(gain(volume) + self.mapping.master_gain, absolute=True) |
138 | self.volume = volume | 144 | self.volume = volume |
139 | self.current_loop = 0 | ||
140 | if loop < 0: | 145 | if loop < 0: |
141 | self.last_loop = float('inf') | 146 | self.last_loop = float('inf') |
142 | else: | 147 | else: |
@@ -145,31 +150,89 @@ class MusicFile: | |||
145 | with self.music_lock: | 150 | with self.music_lock: |
146 | self.current_audio_segment = self.audio_segment | 151 | self.current_audio_segment = self.audio_segment |
147 | self.current_frame = int(start_at * self.audio_segment.frame_rate) | 152 | self.current_frame = int(start_at * self.audio_segment.frame_rate) |
148 | if fade_in > 0: | ||
149 | db_gain = gain(self.volume, 0)[0] | ||
150 | self.set_gain(-db_gain) | ||
151 | self.gain_effects.append(GainEffect( | ||
152 | "fade", | ||
153 | self.current_audio_segment, | ||
154 | self.current_loop, | ||
155 | self.sound_position, | ||
156 | self.sound_position + fade_in, | ||
157 | gain=db_gain)) | ||
158 | 153 | ||
159 | self.start_playing() | 154 | self.start_playing() |
160 | 155 | ||
161 | def on_enter_loaded_playing(self): | 156 | if fade_in > 0: |
162 | self.mixer.add_file(self) | 157 | db_gain = gain(self.volume, 0)[0] |
158 | self.set_gain(-db_gain) | ||
159 | self.add_fade_effect(db_gain, fade_in) | ||
163 | 160 | ||
164 | def finished_callback(self): | 161 | def seek(self, value=0, delta=False): |
162 | if not self.is_in_use_not_stopping(): | ||
163 | return | ||
164 | |||
165 | with self.music_lock: | ||
166 | self.abandon_all_effects() | ||
167 | if delta: | ||
168 | frame_count = int(self.audio_segment.frame_count()) | ||
169 | frame_diff = int(value * self.audio_segment.frame_rate) | ||
170 | self.current_frame += frame_diff | ||
171 | while self.current_frame < 0: | ||
172 | self.current_loop -= 1 | ||
173 | self.current_frame += frame_count | ||
174 | while self.current_frame > frame_count: | ||
175 | self.current_loop += 1 | ||
176 | self.current_frame -= frame_count | ||
177 | if self.current_loop < 0: | ||
178 | self.current_loop = 0 | ||
179 | self.current_frame = 0 | ||
180 | if self.current_loop > self.last_loop: | ||
181 | self.current_loop = self.last_loop | ||
182 | self.current_frame = frame_count | ||
183 | else: | ||
184 | self.current_frame = max( | ||
185 | 0, | ||
186 | int(value * self.audio_segment.frame_rate)) | ||
187 | |||
188 | def stop(self, fade_out=0, wait=False, set_wait_id=None): | ||
165 | if self.is_loaded_playing(): | 189 | if self.is_loaded_playing(): |
190 | ms = int(self.sound_position * 1000) | ||
191 | ms_fo = max(1, int(fade_out * 1000)) | ||
192 | |||
193 | new_audio_segment = self.current_audio_segment[: ms+ms_fo] \ | ||
194 | .fade_out(ms_fo) | ||
195 | with self.music_lock: | ||
196 | self.current_audio_segment = new_audio_segment | ||
166 | self.stop_playing() | 197 | self.stop_playing() |
167 | if self.is_loaded_stopping(): | 198 | if wait: |
199 | if set_wait_id is not None: | ||
200 | self.mapping.add_wait_id(set_wait_id, self.wait_event) | ||
201 | self.wait_end() | ||
202 | else: | ||
168 | self.stopped() | 203 | self.stopped() |
169 | 204 | ||
170 | def trigger_stopped_events(self): | 205 | def abandon_all_effects(self): |
171 | self.mixer.remove_file(self) | 206 | db_gain = 0 |
172 | self.wait_event.set() | 207 | for gain_effect in self.gain_effects: |
208 | db_gain += gain_effect.get_last_gain() | ||
209 | |||
210 | self.gain_effects = [] | ||
211 | self.set_gain(db_gain) | ||
212 | |||
213 | def set_volume(self, value, delta=False, fade=0): | ||
214 | [db_gain, self.volume] = gain( | ||
215 | value + int(delta) * self.volume, | ||
216 | self.volume) | ||
217 | |||
218 | self.set_gain_with_effect(db_gain, fade=fade) | ||
219 | |||
220 | def set_gain_with_effect(self, db_gain, fade=0): | ||
221 | if not self.is_in_use(): | ||
222 | return | ||
223 | |||
224 | if fade > 0: | ||
225 | self.add_fade_effect(db_gain, fade) | ||
226 | else: | ||
227 | self.set_gain(db_gain) | ||
228 | |||
229 | def wait_end(self): | ||
230 | self.wait_event.clear() | ||
231 | self.wait_event.wait() | ||
232 | |||
233 | # Callbacks | ||
234 | def finished_callback(self): | ||
235 | self.stopped() | ||
173 | 236 | ||
174 | def play_callback(self, out_data_length, frame_count): | 237 | def play_callback(self, out_data_length, frame_count): |
175 | if self.is_loaded_paused(): | 238 | if self.is_loaded_paused(): |
@@ -194,8 +257,15 @@ class MusicFile: | |||
194 | 257 | ||
195 | return data.ljust(out_data_length, b'\0') | 258 | return data.ljust(out_data_length, b'\0') |
196 | 259 | ||
260 | # Helpers | ||
261 | def set_gain(self, db_gain, absolute=False): | ||
262 | if absolute: | ||
263 | self.db_gain = db_gain | ||
264 | else: | ||
265 | self.db_gain += db_gain | ||
266 | |||
197 | def get_next_sample(self, frame_count): | 267 | def get_next_sample(self, frame_count): |
198 | fw = self.audio_segment_frame_width | 268 | fw = self.audio_segment.frame_width |
199 | 269 | ||
200 | data = b"" | 270 | data = b"" |
201 | nb_frames = 0 | 271 | nb_frames = 0 |
@@ -215,32 +285,17 @@ class MusicFile: | |||
215 | 285 | ||
216 | return [data, nb_frames] | 286 | return [data, nb_frames] |
217 | 287 | ||
218 | def seek(self, value=0, delta=False): | 288 | def add_fade_effect(self, db_gain, fade_duration): |
219 | # We don't want to do that while stopping | 289 | if not self.is_in_use(): |
220 | if not (self.is_loaded_playing() or self.is_loaded_paused()): | ||
221 | return | 290 | return |
222 | with self.music_lock: | 291 | |
223 | self.abandon_all_effects() | 292 | self.gain_effects.append(GainEffect( |
224 | if delta: | 293 | "fade", |
225 | frame_count = int(self.audio_segment.frame_count()) | 294 | self.current_audio_segment, |
226 | frame_diff = int(value * self.audio_segment.frame_rate) | 295 | self.current_loop, |
227 | self.current_frame += frame_diff | 296 | self.sound_position, |
228 | while self.current_frame < 0: | 297 | self.sound_position + fade_duration, |
229 | self.current_loop -= 1 | 298 | gain=db_gain)) |
230 | self.current_frame += frame_count | ||
231 | while self.current_frame > frame_count: | ||
232 | self.current_loop += 1 | ||
233 | self.current_frame -= frame_count | ||
234 | if self.current_loop < 0: | ||
235 | self.current_loop = 0 | ||
236 | self.current_frame = 0 | ||
237 | if self.current_loop > self.last_loop: | ||
238 | self.current_loop = self.last_loop | ||
239 | self.current_frame = frame_count | ||
240 | else: | ||
241 | self.current_frame = max( | ||
242 | 0, | ||
243 | int(value * self.audio_segment.frame_rate)) | ||
244 | 299 | ||
245 | def effects_next_gain(self, frame_count): | 300 | def effects_next_gain(self, frame_count): |
246 | db_gain = 0 | 301 | db_gain = 0 |
@@ -257,61 +312,6 @@ class MusicFile: | |||
257 | return db_gain | 312 | return db_gain |
258 | 313 | ||
259 | 314 | ||
260 | def abandon_all_effects(self): | 315 | def volume_factor(self, additional_gain=0): |
261 | db_gain = 0 | ||
262 | for gain_effect in self.gain_effects: | ||
263 | db_gain += gain_effect.get_last_gain() | ||
264 | |||
265 | self.gain_effects = [] | ||
266 | self.set_gain(db_gain) | ||
267 | |||
268 | def stop(self, fade_out=0, wait=False, set_wait_id=None): | ||
269 | if self.is_loaded_playing(): | ||
270 | ms = int(self.sound_position * 1000) | ||
271 | ms_fo = max(1, int(fade_out * 1000)) | ||
272 | |||
273 | new_audio_segment = self.current_audio_segment[: ms+ms_fo] \ | ||
274 | .fade_out(ms_fo) | ||
275 | with self.music_lock: | ||
276 | self.current_audio_segment = new_audio_segment | ||
277 | self.stop_playing() | ||
278 | if wait: | ||
279 | if set_wait_id is not None: | ||
280 | self.mapping.add_wait_id(set_wait_id, self.wait_event) | ||
281 | self.wait_end() | ||
282 | else: | ||
283 | self.stop_playing() | ||
284 | self.stopped() | ||
285 | |||
286 | def volume_factor(self, additional_gain): | ||
287 | return 10 ** ( (self.db_gain + additional_gain) / 20) | 316 | return 10 ** ( (self.db_gain + additional_gain) / 20) |
288 | 317 | ||
289 | def set_gain(self, db_gain, absolute=False): | ||
290 | if absolute: | ||
291 | self.db_gain = db_gain | ||
292 | else: | ||
293 | self.db_gain += db_gain | ||
294 | |||
295 | def set_volume(self, value, delta=False, fade=0): | ||
296 | [db_gain, self.volume] = gain( | ||
297 | value + int(delta) * self.volume, | ||
298 | self.volume) | ||
299 | |||
300 | if not (self.is_loaded_playing() or self.is_loaded_paused()): | ||
301 | return | ||
302 | |||
303 | if fade > 0: | ||
304 | self.gain_effects.append(GainEffect( | ||
305 | "fade", | ||
306 | self.current_audio_segment, | ||
307 | self.current_loop, | ||
308 | self.sound_position, | ||
309 | self.sound_position + fade, | ||
310 | gain=db_gain)) | ||
311 | else: | ||
312 | self.set_gain(db_gain) | ||
313 | |||
314 | def wait_end(self): | ||
315 | self.wait_event.clear() | ||
316 | self.wait_event.wait() | ||
317 | |||