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):
22 { 'name': 'loaded', 'children': ['stopped', 'playing', 'paused', 'stopping'] }
25 { 'trigger': 'load', 'source': 'initial', 'dest': 'loading'}
,
26 { 'trigger': 'fail', 'source': 'loading', 'dest': 'failed'}
,
27 { 'trigger': 'success', 'source': 'loading', 'dest': 'loaded_stopped'}
,
28 { 'trigger': 'start_playing', 'source': 'loaded_stopped', 'dest': 'loaded_playing'}
,
29 { 'trigger': 'pause', 'source': 'loaded_playing', 'dest': 'loaded_paused'}
,
30 { 'trigger': 'unpause', 'source': 'loaded_paused', 'dest': 'loaded_playing'}
,
31 { 'trigger': 'stop_playing', 'source': ['loaded_playing','loaded_paused'], 'dest': 'loaded_stopping'}
,
32 { 'trigger': 'stopped', 'source': 'loaded_stopping', 'dest': 'loaded_stopped', 'after': 'trigger_stopped_events'}
35 Machine
.__init
__(self
, states
=states
, transitions
=transitions
, initial
='initial')
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
.initial_volume_factor
= gain
44 self
.music_lock
= Lock("music__" + filename
)
45 self
.wait_event
= threading
.Event()
48 threading
.Thread(name
= "MSMusicLoad", target
= self
.load
).start()
50 def on_enter_loading(self
):
53 debug_print("Loading « {} »".format(self
.name
))
54 self
.mixer
= self
.mapping
.mixer
or Mixer()
55 initial_db_gain
= gain(self
.initial_volume_factor
* 100)
56 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(initial_db_gain
)
57 self
.audio_segment_frame_width
= self
.audio_segment
.frame_width
58 self
.sound_duration
= self
.audio_segment
.duration_seconds
59 except Exception as e
:
60 error_print("failed to load « {} »: {}".format(self
.name
, e
))
61 self
.loading_error
= e
65 debug_print("Loaded « {} »".format(self
.name
))
67 def check_is_loaded(self
):
68 return self
.state
.startswith('loaded_')
70 def is_not_stopped(self
):
71 return self
.check_is_loaded() and not self
.is_loaded_stopped()
74 return self
.is_loaded_paused()
77 def sound_position(self
):
78 if self
.is_not_stopped():
79 return self
.current_frame
/ self
.current_audio_segment
.frame_rate
83 def play(self
, fade_in
= 0, volume
= 100, loop
= 0, start_at
= 0):
84 self
.db_gain
= gain(volume
) + self
.mapping
.master_gain
88 ms
= int(start_at
* 1000)
89 ms_fi
= int(fade_in
* 1000)
91 self
.current_audio_segment
= self
.audio_segment
92 self
.current_frame
= int(start_at
* self
.audio_segment
.frame_rate
)
94 # FIXME: apply it to repeated when looping?
95 self
.a_s_with_effect
= self
.current_audio_segment
[ms
:ms
+ms_fi
].fade_in(ms_fi
)
96 self
.current_frame_with_effect
= 0
98 self
.a_s_with_effect
= None
102 def on_enter_loaded_playing(self
):
103 self
.mixer
.add_file(self
)
105 def finished_callback(self
):
106 if self
.is_loaded_playing():
108 if self
.is_loaded_stopping():
111 def trigger_stopped_events(self
):
112 self
.mixer
.remove_file(self
)
113 self
.wait_event
.set()
115 def play_callback(self
, out_data_length
, frame_count
):
116 if self
.is_loaded_paused():
117 return b
'\0' * out_data_length
119 with self
.music_lock
:
120 [data
, nb_frames
] = self
.get_next_sample(frame_count
)
121 if nb_frames
< frame_count
:
122 if self
.is_loaded_playing() and self
.loop
!= 0:
124 self
.current_frame
= 0
125 [new_data
, new_nb_frames
] = self
.get_next_sample(frame_count
- nb_frames
)
127 nb_frames
+= new_nb_frames
130 threading
.Thread(name
= "MSFinishedCallback", target
=self
.finished_callback
).start()
132 return data
.ljust(out_data_length
, b
'\0')
134 def get_next_sample(self
, frame_count
):
135 fw
= self
.audio_segment_frame_width
139 if self
.a_s_with_effect
is not None:
140 segment
= self
.a_s_with_effect
141 max_val
= int(segment
.frame_count())
143 start_i
= max(self
.current_frame_with_effect
, 0)
144 end_i
= min(self
.current_frame_with_effect
+ frame_count
, max_val
)
146 data
+= segment
._data
[(start_i
* fw
):(end_i
* fw
)]
148 frame_count
= max(0, self
.current_frame_with_effect
+ frame_count
- max_val
)
150 self
.current_frame_with_effect
+= end_i
- start_i
151 self
.current_frame
+= end_i
- start_i
152 nb_frames
+= end_i
- start_i
155 self
.a_s_with_effect
= None
157 segment
= self
.current_audio_segment
158 max_val
= int(segment
.frame_count())
160 start_i
= max(self
.current_frame
, 0)
161 end_i
= min(self
.current_frame
+ frame_count
, max_val
)
162 data
+= segment
._data
[(start_i
* fw
):(end_i
* fw
)]
163 nb_frames
+= end_i
- start_i
164 self
.current_frame
+= end_i
- start_i
166 data
= audioop
.mul(data
, Config
.sample_width
, self
.volume_factor
)
168 return [data
, nb_frames
]
170 def seek(self
, value
= 0, delta
= False):
171 # We don't want to do that while stopping
172 if not (self
.is_loaded_playing() or self
.is_loaded_paused()):
174 with self
.music_lock
:
175 self
.a_s_with_effect
= None
176 self
.current_frame
= max(0, int(delta
) * self
.current_frame
+ int(value
* self
.audio_segment
.frame_rate
))
177 # FIXME: si on fait un seek + delta, adapter le "loop"
179 def stop(self
, fade_out
= 0, wait
= False):
180 if self
.is_loaded_playing():
181 ms
= int(self
.sound_position
* 1000)
182 ms_fo
= max(1, int(fade_out
* 1000))
184 new_audio_segment
= self
.current_audio_segment
[:ms
+ ms_fo
].fade_out(ms_fo
)
185 with self
.music_lock
:
186 self
.current_audio_segment
= new_audio_segment
194 def set_gain(self
, db_gain
):
195 self
.db_gain
+= db_gain
196 self
.volume_factor
= 10 ** (self
.db_gain
/ 20)
198 def set_volume(self
, value
, delta
= False):
199 [db_gain
, self
.volume
] = gain(value
+ int(delta
) * self
.volume
, self
.volume
)
201 self
.set_gain(db_gain
)
204 self
.wait_event
.clear()
205 self
.wait_event
.wait()