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 # FIXME: catch error here
36 self
.mixer
= mapping
.mixer
or Mixer()
38 self
.mapping
= mapping
39 self
.filename
= filename
40 self
.name
= name
or filename
41 self
.audio_segment
= None
42 self
.audio_segment_frame_width
= 0
43 self
.volume_factor
= gain
44 self
.music_lock
= Lock("music__" + filename
)
45 self
.wait_event
= threading
.Event()
47 threading
.Thread(name
= "MSMusicLoad", target
= self
.load
).start()
49 def on_enter_loading(self
):
52 print("Loading « {} »".format(self
.name
))
53 db_gain
= gain(self
.volume_factor
* 100)
54 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
)
55 self
.audio_segment_frame_width
= self
.audio_segment
.frame_width
56 self
.sound_duration
= self
.audio_segment
.duration_seconds
57 except Exception as e
:
58 print("failed to load « {} »: {}".format(self
.name
, e
))
59 self
.loading_error
= e
63 print("Loaded « {} »".format(self
.name
))
65 def check_is_loaded(self
):
66 return self
.state
.startswith('loaded_')
68 def is_not_stopped(self
):
69 return self
.check_is_loaded() and not self
.is_loaded_stopped()
72 return self
.is_loaded_paused()
75 def sound_position(self
):
76 if self
.is_not_stopped():
77 return self
.current_frame
/ self
.current_audio_segment
.frame_rate
81 def play(self
, fade_in
= 0, volume
= 100, loop
= 0, start_at
= 0):
82 db_gain
= gain(volume
) + self
.mapping
.master_gain
86 ms
= int(start_at
* 1000)
87 ms_fi
= int(fade_in
* 1000)
89 self
.current_audio_segment
= (self
.audio_segment
+ db_gain
)
90 self
.current_frame
= int(start_at
* self
.audio_segment
.frame_rate
)
92 # FIXME: apply it to repeated when looping?
93 self
.a_s_with_effect
= self
.current_audio_segment
[ms
:ms
+ms_fi
].fade_in(ms_fi
)
94 self
.current_frame_with_effect
= 0
96 self
.a_s_with_effect
= None
100 def on_enter_loaded_playing(self
):
101 self
.mixer
.add_file(self
)
103 def finished_callback(self
):
104 if self
.is_loaded_playing():
106 if self
.is_loaded_stopping():
109 def trigger_stopped_events(self
):
110 self
.mixer
.remove_file(self
)
111 self
.wait_event
.set()
113 def play_callback(self
, out_data_length
, frame_count
):
114 if self
.is_loaded_paused():
115 return b
'\0' * out_data_length
117 with self
.music_lock
:
118 [data
, nb_frames
] = self
.get_next_sample(frame_count
)
119 if nb_frames
< frame_count
:
120 if self
.is_loaded_playing() and self
.loop
!= 0:
122 self
.current_frame
= 0
123 [new_data
, new_nb_frames
] = self
.get_next_sample(frame_count
- nb_frames
)
125 nb_frames
+= new_nb_frames
128 threading
.Thread(name
= "MSFinishedCallback", target
=self
.finished_callback
).start()
130 return data
.ljust(out_data_length
, b
'\0')
132 def get_next_sample(self
, frame_count
):
133 fw
= self
.audio_segment_frame_width
137 if self
.a_s_with_effect
is not None:
138 segment
= self
.a_s_with_effect
139 max_val
= int(segment
.frame_count())
141 start_i
= max(self
.current_frame_with_effect
, 0)
142 end_i
= min(self
.current_frame_with_effect
+ frame_count
, max_val
)
144 data
+= segment
._data
[(start_i
* fw
):(end_i
* fw
)]
146 frame_count
= max(0, self
.current_frame_with_effect
+ frame_count
- max_val
)
148 self
.current_frame_with_effect
+= end_i
- start_i
149 self
.current_frame
+= end_i
- start_i
150 nb_frames
+= end_i
- start_i
153 self
.a_s_with_effect
= None
155 segment
= self
.current_audio_segment
156 max_val
= int(segment
.frame_count())
158 start_i
= max(self
.current_frame
, 0)
159 end_i
= min(self
.current_frame
+ frame_count
, max_val
)
160 data
+= segment
._data
[(start_i
* fw
):(end_i
* fw
)]
161 nb_frames
+= end_i
- start_i
162 self
.current_frame
+= end_i
- start_i
164 return [data
, nb_frames
]
166 def seek(self
, value
= 0, delta
= False):
167 # We don't want to do that while stopping
168 if not (self
.is_loaded_playing() or self
.is_loaded_paused()):
170 with self
.music_lock
:
171 self
.a_s_with_effect
= None
172 self
.current_frame
= max(0, int(delta
) * self
.current_frame
+ int(value
* self
.audio_segment
.frame_rate
))
173 # FIXME: si on fait un seek + delta, adapter le "loop"
175 def stop(self
, fade_out
= 0, wait
= False):
176 if self
.is_loaded_playing():
177 ms
= int(self
.sound_position
* 1000)
178 ms_fo
= max(1, int(fade_out
* 1000))
180 new_audio_segment
= self
.current_audio_segment
[:ms
+ ms_fo
].fade_out(ms_fo
)
181 with self
.music_lock
:
182 self
.current_audio_segment
= new_audio_segment
190 def set_gain(self
, db_gain
):
191 if not self
.is_not_stopped():
194 new_audio_segment
= self
.current_audio_segment
+ db_gain
196 new_a_s_with_effect
= None
197 if self
.a_s_with_effect
is not None:
198 new_a_s_with_effect
= self
.a_s_with_effect
+ db_gain
200 with self
.music_lock
:
201 self
.current_audio_segment
= new_audio_segment
202 self
.a_s_with_effect
= new_a_s_with_effect
204 def set_volume(self
, value
, delta
= False):
205 [db_gain
, self
.volume
] = gain(value
+ int(delta
) * self
.volume
, self
.volume
)
207 self
.set_gain(db_gain
)
210 self
.wait_event
.clear()
211 self
.wait_event
.wait()