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
14 file_lock
= Lock("file")
16 class MusicFile(Machine
):
17 def __init__(self
, filename
, mapping
, name
=None, gain
=1):
24 'children': ['stopped', 'playing', 'paused', 'stopping']
41 'dest': 'loaded_stopped'
44 'trigger': 'start_playing',
45 'source': 'loaded_stopped',
46 'dest': 'loaded_playing'
50 'source': 'loaded_playing',
51 'dest': 'loaded_paused'
55 'source': 'loaded_paused',
56 'dest': 'loaded_playing'
59 'trigger': 'stop_playing',
60 'source': ['loaded_playing','loaded_paused'],
61 'dest': 'loaded_stopping'
65 'source': 'loaded_stopping',
66 'dest': 'loaded_stopped',
67 'after': 'trigger_stopped_events'
71 Machine
.__init
__(self
, states
=states
,
72 transitions
=transitions
, initial
='initial')
75 self
.mapping
= mapping
76 self
.filename
= filename
77 self
.name
= name
or filename
78 self
.audio_segment
= None
79 self
.audio_segment_frame_width
= 0
80 self
.initial_volume_factor
= gain
81 self
.music_lock
= Lock("music__" + filename
)
82 self
.wait_event
= threading
.Event()
84 self
.volume_factor
= 1
86 threading
.Thread(name
="MSMusicLoad", target
=self
.load
).start()
88 def on_enter_loading(self
):
91 debug_print("Loading « {} »".format(self
.name
))
92 self
.mixer
= self
.mapping
.mixer
or Mixer()
93 initial_db_gain
= gain(self
.initial_volume_factor
* 100)
94 self
.audio_segment
= pydub
.AudioSegment \
95 .from_file(self
.filename
) \
96 .set_frame_rate(Config
.frame_rate
) \
97 .set_channels(Config
.channels
) \
98 .set_sample_width(Config
.sample_width
) \
99 .apply_gain(initial_db_gain
)
100 self
.audio_segment_frame_width
= self
.audio_segment
.frame_width
101 self
.sound_duration
= self
.audio_segment
.duration_seconds
102 except Exception as e
:
103 error_print("failed to load « {} »: {}".format(self
.name
, e
))
104 self
.loading_error
= e
108 debug_print("Loaded « {} »".format(self
.name
))
110 def check_is_loaded(self
):
111 return self
.state
.startswith('loaded_')
113 def is_not_stopped(self
):
114 return self
.check_is_loaded() and not self
.is_loaded_stopped()
117 return self
.is_loaded_paused()
120 def sound_position(self
):
121 if self
.is_not_stopped():
122 return self
.current_frame
/ self
.current_audio_segment
.frame_rate
126 def play(self
, fade_in
=0, volume
=100, loop
=0, start_at
=0):
127 self
.set_gain(gain(volume
) + self
.mapping
.master_gain
, absolute
=True)
131 ms
= int(start_at
* 1000)
132 ms_fi
= int(fade_in
* 1000)
133 with self
.music_lock
:
134 self
.current_audio_segment
= self
.audio_segment
135 self
.current_frame
= int(start_at
* self
.audio_segment
.frame_rate
)
137 # FIXME: apply it to repeated when looping?
138 self
.a_s_with_effect
= self \
139 .current_audio_segment
[ms
: ms
+ms_fi
] \
141 self
.current_frame_with_effect
= 0
143 self
.a_s_with_effect
= None
147 def on_enter_loaded_playing(self
):
148 self
.mixer
.add_file(self
)
150 def finished_callback(self
):
151 if self
.is_loaded_playing():
153 if self
.is_loaded_stopping():
156 def trigger_stopped_events(self
):
157 self
.mixer
.remove_file(self
)
158 self
.wait_event
.set()
160 def play_callback(self
, out_data_length
, frame_count
):
161 if self
.is_loaded_paused():
162 return b
'\0' * out_data_length
164 with self
.music_lock
:
165 [data
, nb_frames
] = self
.get_next_sample(frame_count
)
166 if nb_frames
< frame_count
:
167 if self
.is_loaded_playing() and self
.loop
!= 0:
169 self
.current_frame
= 0
170 [new_data
, new_nb_frames
] = self
.get_next_sample(
171 frame_count
- nb_frames
)
173 nb_frames
+= new_nb_frames
177 name
="MSFinishedCallback",
178 target
=self
.finished_callback
).start()
180 return data
.ljust(out_data_length
, b
'\0')
182 def get_next_sample(self
, frame_count
):
183 fw
= self
.audio_segment_frame_width
187 if self
.a_s_with_effect
is not None:
188 segment
= self
.a_s_with_effect
189 max_val
= int(segment
.frame_count())
191 start_i
= max(self
.current_frame_with_effect
, 0)
192 end_i
= min(self
.current_frame_with_effect
+ frame_count
, max_val
)
194 data
+= segment
._data
[start_i
*fw
: end_i
*fw
]
198 self
.current_frame_with_effect
+ frame_count
- max_val
)
200 self
.current_frame_with_effect
+= end_i
- start_i
201 self
.current_frame
+= end_i
- start_i
202 nb_frames
+= end_i
- start_i
205 self
.a_s_with_effect
= None
207 segment
= self
.current_audio_segment
208 max_val
= int(segment
.frame_count())
210 start_i
= max(self
.current_frame
, 0)
211 end_i
= min(self
.current_frame
+ frame_count
, max_val
)
212 data
+= segment
._data
[start_i
*fw
: end_i
*fw
]
213 nb_frames
+= end_i
- start_i
214 self
.current_frame
+= end_i
- start_i
216 data
= audioop
.mul(data
, Config
.sample_width
, self
.volume_factor
)
218 return [data
, nb_frames
]
220 def seek(self
, value
=0, delta
=False):
221 # We don't want to do that while stopping
222 if not (self
.is_loaded_playing() or self
.is_loaded_paused()):
224 with self
.music_lock
:
225 self
.a_s_with_effect
= None
226 self
.current_frame
= max(
228 int(delta
) * self
.current_frame
229 + int(value
* self
.audio_segment
.frame_rate
))
230 # FIXME: si on fait un seek + delta, adapter le "loop"
232 def stop(self
, fade_out
=0, wait
=False):
233 if self
.is_loaded_playing():
234 ms
= int(self
.sound_position
* 1000)
235 ms_fo
= max(1, int(fade_out
* 1000))
237 new_audio_segment
= self
.current_audio_segment
[: ms
+ms_fo
] \
239 with self
.music_lock
:
240 self
.current_audio_segment
= new_audio_segment
248 def set_gain(self
, db_gain
, absolute
=False):
250 self
.db_gain
= db_gain
252 self
.db_gain
+= db_gain
253 self
.volume_factor
= 10 ** (self
.db_gain
/ 20)
255 def set_volume(self
, value
, delta
=False):
256 [db_gain
, self
.volume
] = gain(
257 value
+ int(delta
) * self
.volume
,
260 self
.set_gain(db_gain
)
263 self
.wait_event
.clear()
264 self
.wait_event
.wait()