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")
17 class MusicFile(Machine
):
18 def __init__(self
, filename
, mapping
, name
=None, gain
=1):
25 'children': ['stopped', 'playing', 'paused', 'stopping']
42 'dest': 'loaded_stopped'
45 'trigger': 'start_playing',
46 'source': 'loaded_stopped',
47 'dest': 'loaded_playing'
51 'source': 'loaded_playing',
52 'dest': 'loaded_paused'
56 'source': 'loaded_paused',
57 'dest': 'loaded_playing'
60 'trigger': 'stop_playing',
61 'source': ['loaded_playing','loaded_paused'],
62 'dest': 'loaded_stopping'
66 'source': 'loaded_stopping',
67 'dest': 'loaded_stopped',
68 'after': 'trigger_stopped_events'
72 Machine
.__init
__(self
, states
=states
,
73 transitions
=transitions
, initial
='initial')
76 self
.mapping
= mapping
77 self
.filename
= filename
78 self
.name
= name
or filename
79 self
.audio_segment
= None
80 self
.audio_segment_frame_width
= 0
81 self
.initial_volume_factor
= gain
82 self
.music_lock
= Lock("music__" + filename
)
83 self
.wait_event
= threading
.Event()
85 self
.gain_effects
= []
87 threading
.Thread(name
="MSMusicLoad", target
=self
.load
).start()
89 def on_enter_loading(self
):
92 debug_print("Loading « {} »".format(self
.name
))
93 self
.mixer
= self
.mapping
.mixer
or Mixer()
94 initial_db_gain
= gain(self
.initial_volume_factor
* 100)
95 self
.audio_segment
= pydub
.AudioSegment \
96 .from_file(self
.filename
) \
97 .set_frame_rate(Config
.frame_rate
) \
98 .set_channels(Config
.channels
) \
99 .set_sample_width(Config
.sample_width
) \
100 .apply_gain(initial_db_gain
)
101 self
.audio_segment_frame_width
= self
.audio_segment
.frame_width
102 self
.sound_duration
= self
.audio_segment
.duration_seconds
103 except Exception as e
:
104 error_print("failed to load « {} »: {}".format(self
.name
, e
))
105 self
.loading_error
= e
109 debug_print("Loaded « {} »".format(self
.name
))
111 def check_is_loaded(self
):
112 return self
.state
.startswith('loaded_')
114 def is_not_stopped(self
):
115 return self
.check_is_loaded() and not self
.is_loaded_stopped()
118 return self
.is_loaded_paused()
121 def sound_position(self
):
122 if self
.is_not_stopped():
123 return self
.current_frame
/ self
.current_audio_segment
.frame_rate
127 def play(self
, fade_in
=0, volume
=100, loop
=0, start_at
=0):
128 # FIXME: create a "reinitialize" method
129 self
.gain_effects
= []
130 self
.set_gain(gain(volume
) + self
.mapping
.master_gain
, absolute
=True)
132 self
.current_loop
= 0
134 self
.last_loop
= float('inf')
136 self
.last_loop
= loop
138 with self
.music_lock
:
139 self
.current_audio_segment
= self
.audio_segment
140 self
.current_frame
= int(start_at
* self
.audio_segment
.frame_rate
)
142 db_gain
= gain(self
.volume
, 0)[0]
143 self
.set_gain(-db_gain
)
144 self
.gain_effects
.append(GainEffect(
146 self
.current_audio_segment
,
149 self
.sound_position
+ fade_in
,
154 def on_enter_loaded_playing(self
):
155 self
.mixer
.add_file(self
)
157 def finished_callback(self
):
158 if self
.is_loaded_playing():
160 if self
.is_loaded_stopping():
163 def trigger_stopped_events(self
):
164 self
.mixer
.remove_file(self
)
165 self
.wait_event
.set()
167 def play_callback(self
, out_data_length
, frame_count
):
168 if self
.is_loaded_paused():
169 return b
'\0' * out_data_length
171 with self
.music_lock
:
172 [data
, nb_frames
] = self
.get_next_sample(frame_count
)
173 if nb_frames
< frame_count
:
174 if self
.is_loaded_playing() and\
175 self
.current_loop
< self
.last_loop
:
176 self
.current_loop
+= 1
177 self
.current_frame
= 0
178 [new_data
, new_nb_frames
] = self
.get_next_sample(
179 frame_count
- nb_frames
)
181 nb_frames
+= new_nb_frames
183 # FIXME: too slow when mixing multiple streams
185 name
="MSFinishedCallback",
186 target
=self
.finished_callback
).start()
188 return data
.ljust(out_data_length
, b
'\0')
190 def get_next_sample(self
, frame_count
):
191 fw
= self
.audio_segment_frame_width
196 segment
= self
.current_audio_segment
197 max_val
= int(segment
.frame_count())
199 start_i
= max(self
.current_frame
, 0)
200 end_i
= min(self
.current_frame
+ frame_count
, max_val
)
201 data
+= segment
._data
[start_i
*fw
: end_i
*fw
]
202 nb_frames
+= end_i
- start_i
203 self
.current_frame
+= end_i
- start_i
205 volume_factor
= self
.volume_factor(self
.effects_next_gain(nb_frames
))
207 data
= audioop
.mul(data
, Config
.sample_width
, volume_factor
)
209 return [data
, nb_frames
]
211 def seek(self
, value
=0, delta
=False):
212 # We don't want to do that while stopping
213 if not (self
.is_loaded_playing() or self
.is_loaded_paused()):
215 with self
.music_lock
:
216 self
.abandon_all_effects()
218 frame_count
= int(self
.audio_segment
.frame_count())
219 frame_diff
= int(value
* self
.audio_segment
.frame_rate
)
220 self
.current_frame
+= frame_diff
221 while self
.current_frame
< 0:
222 self
.current_loop
-= 1
223 self
.current_frame
+= frame_count
224 while self
.current_frame
> frame_count
:
225 self
.current_loop
+= 1
226 self
.current_frame
-= frame_count
227 if self
.current_loop
< 0:
228 self
.current_loop
= 0
229 self
.current_frame
= 0
230 if self
.current_loop
> self
.last_loop
:
231 self
.current_loop
= self
.last_loop
232 self
.current_frame
= frame_count
234 self
.current_frame
= max(
236 int(value
* self
.audio_segment
.frame_rate
))
238 def effects_next_gain(self
, frame_count
):
240 for gain_effect
in self
.gain_effects
:
241 [new_gain
, last_gain
] = gain_effect
.get_next_gain(
246 self
.set_gain(new_gain
)
247 self
.gain_effects
.remove(gain_effect
)
253 def abandon_all_effects(self
):
255 for gain_effect
in self
.gain_effects
:
256 db_gain
+= gain_effect
.get_last_gain()
258 self
.gain_effects
= []
259 self
.set_gain(db_gain
)
261 def stop(self
, fade_out
=0, wait
=False):
262 if self
.is_loaded_playing():
263 ms
= int(self
.sound_position
* 1000)
264 ms_fo
= max(1, int(fade_out
* 1000))
266 new_audio_segment
= self
.current_audio_segment
[: ms
+ms_fo
] \
268 with self
.music_lock
:
269 self
.current_audio_segment
= new_audio_segment
277 def volume_factor(self
, additional_gain
):
278 return 10 ** ( (self
.db_gain
+ additional_gain
) / 20)
280 def set_gain(self
, db_gain
, absolute
=False):
282 self
.db_gain
= db_gain
284 self
.db_gain
+= db_gain
286 def set_volume(self
, value
, delta
=False, fade
=0):
287 [db_gain
, self
.volume
] = gain(
288 value
+ int(delta
) * self
.volume
,
292 self
.gain_effects
.append(GainEffect(
294 self
.current_audio_segment
,
297 self
.sound_position
+ fade
,
300 self
.set_gain(db_gain
)
303 self
.wait_event
.clear()
304 self
.wait_event
.wait()