4 from transitions
.extensions
import HierarchicalMachine
as Machine
10 from .lock
import Lock
11 from . import Config
, gain
, debug_print
, error_print
12 from .mixer
import Mixer
13 from .music_effect
import GainEffect
15 file_lock
= Lock("file")
18 def __init__(self
, filename
, mapping
, name
=None, gain
=1):
48 'dest': 'loaded_stopped'
51 'trigger': 'start_playing',
52 'source': 'loaded_stopped',
53 'dest': 'loaded_playing'
57 'source': 'loaded_playing',
58 'dest': 'loaded_paused'
62 'source': 'loaded_paused',
63 'dest': 'loaded_playing'
66 'trigger': 'stop_playing',
67 'source': ['loaded_playing','loaded_paused'],
68 'dest': 'loaded_stopping'
72 'source': 'loaded_stopping',
73 'dest': 'loaded_stopped',
74 'after': 'trigger_stopped_events'
78 Machine(model
=self
, states
=states
,
79 transitions
=transitions
, initial
='initial',
80 ignore_invalid_triggers
=True)
83 self
.mapping
= mapping
84 self
.filename
= filename
85 self
.name
= name
or filename
86 self
.audio_segment
= None
87 self
.audio_segment_frame_width
= 0
88 self
.initial_volume_factor
= gain
89 self
.music_lock
= Lock("music__" + filename
)
90 self
.wait_event
= threading
.Event()
92 self
.gain_effects
= []
94 threading
.Thread(name
="MSMusicLoad", target
=self
.load
).start()
96 def on_enter_loading(self
):
99 debug_print("Loading « {} »".format(self
.name
))
100 self
.mixer
= self
.mapping
.mixer
or Mixer()
101 initial_db_gain
= gain(self
.initial_volume_factor
* 100)
102 self
.audio_segment
= pydub
.AudioSegment \
103 .from_file(self
.filename
) \
104 .set_frame_rate(Config
.frame_rate
) \
105 .set_channels(Config
.channels
) \
106 .set_sample_width(Config
.sample_width
) \
107 .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
110 except Exception as e
:
111 error_print("failed to load « {} »: {}".format(self
.name
, e
))
112 self
.loading_error
= e
116 debug_print("Loaded « {} »".format(self
.name
))
118 def check_is_loaded(self
):
119 return self
.state
.startswith('loaded_')
121 def is_not_stopped(self
):
122 return self
.check_is_loaded() and not self
.is_loaded_stopped()
125 return self
.is_loaded_paused()
128 def sound_position(self
):
129 if self
.is_not_stopped():
130 return self
.current_frame
/ self
.current_audio_segment
.frame_rate
134 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)
139 self
.current_loop
= 0
141 self
.last_loop
= float('inf')
143 self
.last_loop
= loop
145 with self
.music_lock
:
146 self
.current_audio_segment
= self
.audio_segment
147 self
.current_frame
= int(start_at
* self
.audio_segment
.frame_rate
)
149 db_gain
= gain(self
.volume
, 0)[0]
150 self
.set_gain(-db_gain
)
151 self
.gain_effects
.append(GainEffect(
153 self
.current_audio_segment
,
156 self
.sound_position
+ fade_in
,
161 def on_enter_loaded_playing(self
):
162 self
.mixer
.add_file(self
)
164 def finished_callback(self
):
165 if self
.is_loaded_playing():
167 if self
.is_loaded_stopping():
170 def trigger_stopped_events(self
):
171 self
.mixer
.remove_file(self
)
172 self
.wait_event
.set()
174 def play_callback(self
, out_data_length
, frame_count
):
175 if self
.is_loaded_paused():
176 return b
'\0' * out_data_length
178 with self
.music_lock
:
179 [data
, nb_frames
] = self
.get_next_sample(frame_count
)
180 if nb_frames
< frame_count
:
181 if self
.is_loaded_playing() and\
182 self
.current_loop
< self
.last_loop
:
183 self
.current_loop
+= 1
184 self
.current_frame
= 0
185 [new_data
, new_nb_frames
] = self
.get_next_sample(
186 frame_count
- nb_frames
)
188 nb_frames
+= new_nb_frames
190 # FIXME: too slow when mixing multiple streams
192 name
="MSFinishedCallback",
193 target
=self
.finished_callback
).start()
195 return data
.ljust(out_data_length
, b
'\0')
197 def get_next_sample(self
, frame_count
):
198 fw
= self
.audio_segment_frame_width
203 segment
= self
.current_audio_segment
204 max_val
= int(segment
.frame_count())
206 start_i
= max(self
.current_frame
, 0)
207 end_i
= min(self
.current_frame
+ frame_count
, max_val
)
208 data
+= segment
._data
[start_i
*fw
: end_i
*fw
]
209 nb_frames
+= end_i
- start_i
210 self
.current_frame
+= end_i
- start_i
212 volume_factor
= self
.volume_factor(self
.effects_next_gain(nb_frames
))
214 data
= audioop
.mul(data
, Config
.sample_width
, volume_factor
)
216 return [data
, nb_frames
]
218 def seek(self
, value
=0, delta
=False):
219 # We don't want to do that while stopping
220 if not (self
.is_loaded_playing() or self
.is_loaded_paused()):
222 with self
.music_lock
:
223 self
.abandon_all_effects()
225 frame_count
= int(self
.audio_segment
.frame_count())
226 frame_diff
= int(value
* self
.audio_segment
.frame_rate
)
227 self
.current_frame
+= frame_diff
228 while self
.current_frame
< 0:
229 self
.current_loop
-= 1
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
241 self
.current_frame
= max(
243 int(value
* self
.audio_segment
.frame_rate
))
245 def effects_next_gain(self
, frame_count
):
247 for gain_effect
in self
.gain_effects
:
248 [new_gain
, last_gain
] = gain_effect
.get_next_gain(
253 self
.set_gain(new_gain
)
254 self
.gain_effects
.remove(gain_effect
)
260 def abandon_all_effects(self
):
262 for gain_effect
in self
.gain_effects
:
263 db_gain
+= gain_effect
.get_last_gain()
265 self
.gain_effects
= []
266 self
.set_gain(db_gain
)
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))
273 new_audio_segment
= self
.current_audio_segment
[: ms
+ms_fo
] \
275 with self
.music_lock
:
276 self
.current_audio_segment
= new_audio_segment
279 if set_wait_id
is not None:
280 self
.mapping
.add_wait_id(set_wait_id
, self
.wait_event
)
286 def volume_factor(self
, additional_gain
):
287 return 10 ** ( (self
.db_gain
+ additional_gain
) / 20)
289 def set_gain(self
, db_gain
, absolute
=False):
291 self
.db_gain
= db_gain
293 self
.db_gain
+= db_gain
295 def set_volume(self
, value
, delta
=False, fade
=0):
296 [db_gain
, self
.volume
] = gain(
297 value
+ int(delta
) * self
.volume
,
300 if not (self
.is_loaded_playing() or self
.is_loaded_paused()):
304 self
.gain_effects
.append(GainEffect(
306 self
.current_audio_segment
,
309 self
.sound_position
+ fade
,
312 self
.set_gain(db_gain
)
315 self
.wait_event
.clear()
316 self
.wait_event
.wait()