]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blame - helpers/music_file.py
Put music effect in separate file
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / music_file.py
CommitLineData
be27763f
IB
1import threading
2import pydub
98ff4305 3import time
60979de4 4from transitions.extensions import HierarchicalMachine as Machine
be27763f 5
29597680
IB
6import pyaudio as pa
7import sounddevice as sd
8import os.path
9
10from .lock import Lock
1b4b78f5
IB
11from . import gain
12
29597680
IB
13file_lock = Lock("file")
14
15pyaudio = pa.PyAudio()
16
60979de4 17class MusicFile(Machine):
1b4b78f5 18 def __init__(self, filename, mapping, name = None, gain = 1):
60979de4
IB
19 states = [
20 'initial',
21 'loading',
22 'failed',
29597680 23 { 'name': 'loaded', 'children': ['stopped', 'playing', 'paused', 'stopping'] }
60979de4
IB
24 ]
25 transitions = [
26 { 'trigger': 'load', 'source': 'initial', 'dest': 'loading'},
27 { 'trigger': 'fail', 'source': 'loading', 'dest': 'failed'},
28 { 'trigger': 'success', 'source': 'loading', 'dest': 'loaded_stopped'},
29597680
IB
29 { 'trigger': 'start_playing', 'source': 'loaded_stopped', 'dest': 'loaded_playing'},
30 { 'trigger': 'pause', 'source': 'loaded_playing', 'dest': 'loaded_paused'},
31 { 'trigger': 'unpause', 'source': 'loaded_paused', 'dest': 'loaded_playing'},
32 { 'trigger': 'stop_playing', 'source': ['loaded_playing','loaded_paused'], 'dest': 'loaded_stopping'},
33 { 'trigger': 'stopped', 'source': 'loaded_stopping', 'dest': 'loaded_stopped'}
60979de4
IB
34 ]
35
36 Machine.__init__(self, states=states, transitions=transitions, initial='initial')
37
1b4b78f5
IB
38 self.volume = 100
39 self.mapping = mapping
be27763f 40 self.filename = filename
29597680 41 self.stream = None
9de92b6d 42 self.name = name or filename
29597680 43 self.audio_segment = None
ccc8b4f2 44 self.audio_segment_frame_width = 0
1b4b78f5 45 self.volume_factor = gain
29597680 46 self.music_lock = Lock("music__" + filename)
0deb82a5 47 self.wait_event = threading.Event()
be27763f 48
29597680
IB
49 threading.Thread(name = "MSMusicLoad", target = self.load).start()
50
51 def on_enter_loading(self):
52 with file_lock:
53 try:
54 print("Loading « {} »".format(self.name))
1b4b78f5
IB
55 db_gain = gain(self.volume_factor * 100)
56 self.audio_segment = pydub.AudioSegment.from_file(self.filename).set_frame_rate(44100).apply_gain(db_gain)
ccc8b4f2 57 self.audio_segment_frame_width = self.audio_segment.frame_width
29597680
IB
58 self.sound_duration = self.audio_segment.duration_seconds
59 except Exception as e:
60 print("failed to load « {} »: {}".format(self.name, e))
61 self.loading_error = e
62 self.fail()
63 else:
64 self.success()
65 print("Loaded « {} »".format(self.name))
60979de4
IB
66
67 def check_is_loaded(self):
68 return self.state.startswith('loaded_')
be27763f 69
29597680
IB
70 def is_not_stopped(self):
71 return self.check_is_loaded() and not self.is_loaded_stopped()
0e5d59f7 72
9de92b6d 73 def is_paused(self):
29597680 74 return self.is_loaded_paused()
9de92b6d 75
98ff4305
IB
76 @property
77 def sound_position(self):
29597680
IB
78 if self.is_not_stopped():
79 return self.current_frame / self.current_audio_segment.frame_rate
98ff4305
IB
80 else:
81 return 0
82
6f4944c1 83 def play(self, fade_in = 0, volume = 100, loop = 0, start_at = 0):
1b4b78f5
IB
84 db_gain = gain(volume) + self.mapping.master_gain
85 self.volume = volume
6f4944c1 86 self.loop = loop
1b4b78f5 87
29597680 88 ms = int(start_at * 1000)
ccc8b4f2 89 ms_fi = int(fade_in * 1000)
29597680 90 with self.music_lock:
ccc8b4f2
IB
91 self.current_audio_segment = (self.audio_segment + db_gain)
92 self.current_frame = int(start_at * self.audio_segment.frame_rate)
93 if ms_fi > 0:
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
97 else:
98 self.a_s_with_effect = None
99
6f4944c1 100 self.before_loaded_playing()
29597680
IB
101 self.start_playing()
102
6f4944c1 103 def before_loaded_playing(self):
29597680
IB
104 with self.music_lock:
105 segment = self.current_audio_segment
106
107 self.stream = sd.RawOutputStream(samplerate=segment.frame_rate,
108 channels=segment.channels,
109 dtype='int' + str(8*segment.sample_width), # FIXME: ?
110 latency=1.,
111 callback=self.play_callback,
112 finished_callback=self.finished_callback
113 )
114
115 def on_enter_loaded_playing(self):
116 self.stream.start()
117
118 def on_enter_loaded_paused(self):
119 self.stream.stop()
120
121 def finished_callback(self):
122 if self.is_loaded_playing():
123 self.stop_playing()
124 if self.is_loaded_stopping():
125 self.stopped()
126
0deb82a5
IB
127 def on_enter_loaded_stopped(self):
128 self.wait_event.set()
129
29597680
IB
130 def play_callback(self, out_data, frame_count, time_info, status_flags):
131 with self.music_lock:
ccc8b4f2
IB
132 [data, nb_frames] = self.get_next_sample(frame_count)
133 if nb_frames < frame_count:
6f4944c1
IB
134 if self.is_loaded_playing() and self.loop != 0:
135 self.loop -= 1
ccc8b4f2
IB
136 self.current_frame = 0
137 [new_data, new_nb_frames] = self.get_next_sample(frame_count - nb_frames)
138 data += new_data
139 nb_frames += new_nb_frames
140 elif nb_frames == 0:
6f4944c1 141 raise sd.CallbackStop
29597680 142
ccc8b4f2
IB
143 out_data[:] = data.ljust(len(out_data), b'\0')
144
145 def get_next_sample(self, frame_count):
146 fw = self.audio_segment_frame_width
147
148 data = b""
149 nb_frames = 0
150 if self.a_s_with_effect is not None:
151 segment = self.a_s_with_effect
152 max_val = int(segment.frame_count())
153
154 start_i = max(self.current_frame_with_effect, 0)
155 end_i = min(self.current_frame_with_effect + frame_count, max_val)
156
157 data += segment._data[(start_i * fw):(end_i * fw)]
158
159 frame_count = max(0, self.current_frame_with_effect + frame_count - max_val)
160
161 self.current_frame_with_effect += end_i - start_i
162 self.current_frame += end_i - start_i
163 nb_frames += end_i - start_i
164
165 if frame_count > 0:
166 self.a_s_with_effect = None
167
168 segment = self.current_audio_segment
169 max_val = int(segment.frame_count())
170
171 start_i = max(self.current_frame, 0)
172 end_i = min(self.current_frame + frame_count, max_val)
173 data += segment._data[(start_i * fw):(end_i * fw)]
174 nb_frames += end_i - start_i
175 self.current_frame += end_i - start_i
176
177 return [data, nb_frames]
be27763f 178
52d58baf
IB
179 def seek(self, value = 0, delta = False):
180 # We don't want to do that while stopping
181 if not (self.is_loaded_playing() or self.is_loaded_paused()):
182 return
183 with self.music_lock:
ccc8b4f2 184 self.a_s_with_effect = None
52d58baf 185 self.current_frame = max(0, int(delta) * self.current_frame + int(value * self.audio_segment.frame_rate))
ccc8b4f2 186 # FIXME: si on fait un seek + delta, adapter le "loop"
52d58baf 187
1b4b78f5 188 def stop(self, fade_out = 0, wait = False):
29597680
IB
189 if self.is_loaded_playing():
190 ms = int(self.sound_position * 1000)
191 ms_fo = max(1, int(fade_out * 1000))
192
ccc8b4f2
IB
193 # FIXME: stop fade_out puis seek -5 -> on abandonne le fade ? (cf
194 # commentaire dans fonction seek
29597680
IB
195 with self.music_lock:
196 self.current_audio_segment = self.current_audio_segment[:ms + ms_fo].fade_out(ms_fo)
197 self.stop_playing()
1b4b78f5
IB
198 if wait:
199 self.wait_end()
0e5d59f7 200 else:
29597680
IB
201 self.stop_playing()
202 self.stopped()
0e5d59f7 203
1b4b78f5
IB
204 def set_gain(self, db_gain):
205 if not self.is_not_stopped():
29597680
IB
206 return
207
1b4b78f5
IB
208 new_audio_segment = self.current_audio_segment + db_gain
209
ccc8b4f2
IB
210 new_a_s_with_effect = None
211 if self.a_s_with_effect is not None:
212 new_a_s_with_effect = self.a_s_with_effect + db_gain
213
29597680
IB
214 with self.music_lock:
215 self.current_audio_segment = new_audio_segment
ccc8b4f2 216 self.a_s_with_effect = new_a_s_with_effect
29597680 217
8e50011c
IB
218 def set_volume(self, value, delta = False):
219 [db_gain, self.volume] = gain(value + int(delta) * self.volume, self.volume)
1b4b78f5
IB
220
221 self.set_gain(db_gain)
be27763f 222
b86db9f1 223 def wait_end(self):
0deb82a5
IB
224 self.wait_event.clear()
225 self.wait_event.wait()
d479af33 226
29597680
IB
227# Add some more functions to AudioSegments
228def get_sample_slice_data(self, start_sample=0, end_sample=float('inf')):
229 max_val = int(self.frame_count())
230
231 start_i = max(start_sample, 0) * self.frame_width
232 end_i = min(end_sample, max_val) * self.frame_width
233
234 return self._data[start_i:end_i]
235
236pydub.AudioSegment.get_sample_slice_data = get_sample_slice_data