4 from transitions
.extensions
import HierarchicalMachine
as Machine
9 from . import Config
, gain
10 from .mixer
import Mixer
12 file_lock
= Lock("file")
14 class MusicFile(Machine
):
15 def __init__(self
, filename
, mapping
, name
= None, gain
= 1):
20 { 'name': 'loaded', 'children': ['stopped', 'playing', 'paused', 'stopping'] }
23 { 'trigger': 'load', 'source': 'initial', 'dest': 'loading'}
,
24 { 'trigger': 'fail', 'source': 'loading', 'dest': 'failed'}
,
25 { 'trigger': 'success', 'source': 'loading', 'dest': 'loaded_stopped'}
,
26 { 'trigger': 'start_playing', 'source': 'loaded_stopped', 'dest': 'loaded_playing'}
,
27 { 'trigger': 'pause', 'source': 'loaded_playing', 'dest': 'loaded_paused'}
,
28 { 'trigger': 'unpause', 'source': 'loaded_paused', 'dest': 'loaded_playing'}
,
29 { 'trigger': 'stop_playing', 'source': ['loaded_playing','loaded_paused'], 'dest': 'loaded_stopping'}
,
30 { 'trigger': 'stopped', 'source': 'loaded_stopping', 'dest': 'loaded_stopped', 'after': 'trigger_stopped_events'}
33 Machine
.__init
__(self
, states
=states
, transitions
=transitions
, initial
='initial')
35 self
.mixer
= mapping
.mixer
or Mixer()
37 self
.mapping
= mapping
38 self
.filename
= filename
39 self
.name
= name
or filename
40 self
.audio_segment
= None
41 self
.audio_segment_frame_width
= 0
42 self
.volume_factor
= gain
43 self
.music_lock
= Lock("music__" + filename
)
44 self
.wait_event
= threading
.Event()
46 threading
.Thread(name
= "MSMusicLoad", target
= self
.load
).start()
48 def on_enter_loading(self
):
51 print("Loading « {} »".format(self
.name
))
52 db_gain
= gain(self
.volume_factor
* 100)
53 self
.audio_segment
= pydub
.AudioSegment
.from_file(self
.filename
).set_frame_rate(Config
.frame_rate
).set_channels(Config
.channels
).set_sample_width(Config
.sample_width
).apply_gain(db_gain
)
54 self
.audio_segment_frame_width
= self
.audio_segment
.frame_width
55 self
.sound_duration
= self
.audio_segment
.duration_seconds
56 except Exception as e
:
57 print("failed to load « {} »: {}".format(self
.name
, e
))
58 self
.loading_error
= e
62 print("Loaded « {} »".format(self
.name
))
64 def check_is_loaded(self
):
65 return self
.state
.startswith('loaded_')
67 def is_not_stopped(self
):
68 return self
.check_is_loaded() and not self
.is_loaded_stopped()
71 return self
.is_loaded_paused()
74 def sound_position(self
):
75 if self
.is_not_stopped():
76 return self
.current_frame
/ self
.current_audio_segment
.frame_rate
80 def play(self
, fade_in
= 0, volume
= 100, loop
= 0, start_at
= 0):
81 db_gain
= gain(volume
) + self
.mapping
.master_gain
85 ms
= int(start_at
* 1000)
86 ms_fi
= int(fade_in
* 1000)
88 self
.current_audio_segment
= (self
.audio_segment
+ db_gain
)
89 self
.current_frame
= int(start_at
* self
.audio_segment
.frame_rate
)
91 # FIXME: apply it to repeated when looping?
92 self
.a_s_with_effect
= self
.current_audio_segment
[ms
:ms
+ms_fi
].fade_in(ms_fi
)
93 self
.current_frame_with_effect
= 0
95 self
.a_s_with_effect
= None
99 def on_enter_loaded_playing(self
):
100 self
.mixer
.add_file(self
)
102 def finished_callback(self
):
103 if self
.is_loaded_playing():
105 if self
.is_loaded_stopping():
108 def trigger_stopped_events(self
):
109 self
.mixer
.remove_file(self
)
110 self
.wait_event
.set()
112 def play_callback(self
, out_data_length
, frame_count
):
113 if self
.is_loaded_paused():
114 return b
'\0' * out_data_length
116 with self
.music_lock
:
117 [data
, nb_frames
] = self
.get_next_sample(frame_count
)
118 if nb_frames
< frame_count
:
119 if self
.is_loaded_playing() and self
.loop
!= 0:
121 self
.current_frame
= 0
122 [new_data
, new_nb_frames
] = self
.get_next_sample(frame_count
- nb_frames
)
124 nb_frames
+= new_nb_frames
127 threading
.Thread(name
= "MSFinishedCallback", target
=self
.finished_callback
).start()
129 return data
.ljust(out_data_length
, b
'\0')
131 def get_next_sample(self
, frame_count
):
132 fw
= self
.audio_segment_frame_width
136 if self
.a_s_with_effect
is not None:
137 segment
= self
.a_s_with_effect
138 max_val
= int(segment
.frame_count())
140 start_i
= max(self
.current_frame_with_effect
, 0)
141 end_i
= min(self
.current_frame_with_effect
+ frame_count
, max_val
)
143 data
+= segment
._data
[(start_i
* fw
):(end_i
* fw
)]
145 frame_count
= max(0, self
.current_frame_with_effect
+ frame_count
- max_val
)
147 self
.current_frame_with_effect
+= end_i
- start_i
148 self
.current_frame
+= end_i
- start_i
149 nb_frames
+= end_i
- start_i
152 self
.a_s_with_effect
= None
154 segment
= self
.current_audio_segment
155 max_val
= int(segment
.frame_count())
157 start_i
= max(self
.current_frame
, 0)
158 end_i
= min(self
.current_frame
+ frame_count
, max_val
)
159 data
+= segment
._data
[(start_i
* fw
):(end_i
* fw
)]
160 nb_frames
+= end_i
- start_i
161 self
.current_frame
+= end_i
- start_i
163 return [data
, nb_frames
]
165 def seek(self
, value
= 0, delta
= False):
166 # We don't want to do that while stopping
167 if not (self
.is_loaded_playing() or self
.is_loaded_paused()):
169 with self
.music_lock
:
170 self
.a_s_with_effect
= None
171 self
.current_frame
= max(0, int(delta
) * self
.current_frame
+ int(value
* self
.audio_segment
.frame_rate
))
172 # FIXME: si on fait un seek + delta, adapter le "loop"
174 def stop(self
, fade_out
= 0, wait
= False):
175 if self
.is_loaded_playing():
176 ms
= int(self
.sound_position
* 1000)
177 ms_fo
= max(1, int(fade_out
* 1000))
179 # FIXME: stop fade_out puis seek -5 -> on abandonne le fade ? (cf
180 # commentaire dans fonction seek
181 new_audio_segment
= self
.current_audio_segment
[:ms
+ ms_fo
].fade_out(ms_fo
)
182 with self
.music_lock
:
183 self
.current_audio_segment
= new_audio_segment
191 def set_gain(self
, db_gain
):
192 if not self
.is_not_stopped():
195 new_audio_segment
= self
.current_audio_segment
+ db_gain
197 new_a_s_with_effect
= None
198 if self
.a_s_with_effect
is not None:
199 new_a_s_with_effect
= self
.a_s_with_effect
+ db_gain
201 with self
.music_lock
:
202 self
.current_audio_segment
= new_audio_segment
203 self
.a_s_with_effect
= new_a_s_with_effect
205 def set_volume(self
, value
, delta
= False):
206 [db_gain
, self
.volume
] = gain(value
+ int(delta
) * self
.volume
, self
.volume
)
208 self
.set_gain(db_gain
)
211 self
.wait_event
.clear()
212 self
.wait_event
.wait()
214 # Add some more functions to AudioSegments
215 def get_sample_slice_data(self
, start_sample
=0, end_sample
=float('inf')):
216 max_val
= int(self
.frame_count())
218 start_i
= max(start_sample
, 0) * self
.frame_width
219 end_i
= min(end_sample
, max_val
) * self
.frame_width
221 return self
._data
[start_i
:end_i
]
223 pydub
.AudioSegment
.get_sample_slice_data
= get_sample_slice_data