X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2FPython%2FMusicSampler.git;a=blobdiff_plain;f=helpers%2Fmusic_file.py;h=54a3fdcbb65614187f5ff5c75b399bbbca239944;hp=56060bd8d9368ab8e9e08d7f2cd9a4f7b37f0c1d;hb=2e4049036ec4d90a9daeff606d821d2ac2d023ce;hpb=ccda4cb91686d4c2b70f0c26d21c26ac3d03c3b9 diff --git a/helpers/music_file.py b/helpers/music_file.py index 56060bd..54a3fdc 100644 --- a/helpers/music_file.py +++ b/helpers/music_file.py @@ -14,25 +14,62 @@ from .mixer import Mixer file_lock = Lock("file") class MusicFile(Machine): - def __init__(self, filename, mapping, name = None, gain = 1): + def __init__(self, filename, mapping, name=None, gain=1): states = [ 'initial', 'loading', 'failed', - { 'name': 'loaded', 'children': ['stopped', 'playing', 'paused', 'stopping'] } + { + 'name': 'loaded', + 'children': ['stopped', 'playing', 'paused', 'stopping'] + } ] transitions = [ - { 'trigger': 'load', 'source': 'initial', 'dest': 'loading'}, - { 'trigger': 'fail', 'source': 'loading', 'dest': 'failed'}, - { 'trigger': 'success', 'source': 'loading', 'dest': 'loaded_stopped'}, - { 'trigger': 'start_playing', 'source': 'loaded_stopped', 'dest': 'loaded_playing'}, - { 'trigger': 'pause', 'source': 'loaded_playing', 'dest': 'loaded_paused'}, - { 'trigger': 'unpause', 'source': 'loaded_paused', 'dest': 'loaded_playing'}, - { 'trigger': 'stop_playing', 'source': ['loaded_playing','loaded_paused'], 'dest': 'loaded_stopping'}, - { 'trigger': 'stopped', 'source': 'loaded_stopping', 'dest': 'loaded_stopped', 'after': 'trigger_stopped_events'} + { + 'trigger': 'load', + 'source': 'initial', + 'dest': 'loading' + }, + { + 'trigger': 'fail', + 'source': 'loading', + 'dest': 'failed' + }, + { + 'trigger': 'success', + 'source': 'loading', + 'dest': 'loaded_stopped' + }, + { + 'trigger': 'start_playing', + 'source': 'loaded_stopped', + 'dest': 'loaded_playing' + }, + { + 'trigger': 'pause', + 'source': 'loaded_playing', + 'dest': 'loaded_paused' + }, + { + 'trigger': 'unpause', + 'source': 'loaded_paused', + 'dest': 'loaded_playing' + }, + { + 'trigger': 'stop_playing', + 'source': ['loaded_playing','loaded_paused'], + 'dest': 'loaded_stopping' + }, + { + 'trigger': 'stopped', + 'source': 'loaded_stopping', + 'dest': 'loaded_stopped', + 'after': 'trigger_stopped_events' + } ] - Machine.__init__(self, states=states, transitions=transitions, initial='initial') + Machine.__init__(self, states=states, + transitions=transitions, initial='initial') self.volume = 100 self.mapping = mapping @@ -45,7 +82,7 @@ class MusicFile(Machine): self.wait_event = threading.Event() self.db_gain = 0 - threading.Thread(name = "MSMusicLoad", target = self.load).start() + threading.Thread(name="MSMusicLoad", target=self.load).start() def on_enter_loading(self): with file_lock: @@ -53,7 +90,12 @@ class MusicFile(Machine): debug_print("Loading « {} »".format(self.name)) self.mixer = self.mapping.mixer or Mixer() initial_db_gain = gain(self.initial_volume_factor * 100) - 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) + 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) self.audio_segment_frame_width = self.audio_segment.frame_width self.sound_duration = self.audio_segment.duration_seconds except Exception as e: @@ -80,7 +122,7 @@ class MusicFile(Machine): else: return 0 - def play(self, fade_in = 0, volume = 100, loop = 0, start_at = 0): + def play(self, fade_in=0, volume=100, loop=0, start_at=0): self.db_gain = gain(volume) + self.mapping.master_gain self.volume = volume self.loop = loop @@ -92,7 +134,9 @@ class MusicFile(Machine): self.current_frame = int(start_at * self.audio_segment.frame_rate) if ms_fi > 0: # FIXME: apply it to repeated when looping? - self.a_s_with_effect = self.current_audio_segment[ms:ms+ms_fi].fade_in(ms_fi) + self.a_s_with_effect = self \ + .current_audio_segment[ms : ms+ms_fi] \ + .fade_in(ms_fi) self.current_frame_with_effect = 0 else: self.a_s_with_effect = None @@ -122,12 +166,15 @@ class MusicFile(Machine): if self.is_loaded_playing() and self.loop != 0: self.loop -= 1 self.current_frame = 0 - [new_data, new_nb_frames] = self.get_next_sample(frame_count - nb_frames) + [new_data, new_nb_frames] = self.get_next_sample( + frame_count - nb_frames) data += new_data nb_frames += new_nb_frames elif nb_frames == 0: # FIXME: too slow - threading.Thread(name = "MSFinishedCallback", target=self.finished_callback).start() + threading.Thread( + name="MSFinishedCallback", + target=self.finished_callback).start() return data.ljust(out_data_length, b'\0') @@ -141,11 +188,13 @@ class MusicFile(Machine): max_val = int(segment.frame_count()) start_i = max(self.current_frame_with_effect, 0) - end_i = min(self.current_frame_with_effect + frame_count, max_val) + end_i = min(self.current_frame_with_effect + frame_count, max_val) - data += segment._data[(start_i * fw):(end_i * fw)] + data += segment._data[start_i*fw : end_i*fw] - frame_count = max(0, self.current_frame_with_effect + frame_count - max_val) + frame_count = max( + 0, + self.current_frame_with_effect + frame_count - max_val) self.current_frame_with_effect += end_i - start_i self.current_frame += end_i - start_i @@ -159,7 +208,7 @@ class MusicFile(Machine): start_i = max(self.current_frame, 0) end_i = min(self.current_frame + frame_count, max_val) - data += segment._data[(start_i * fw):(end_i * fw)] + data += segment._data[start_i*fw : end_i*fw] nb_frames += end_i - start_i self.current_frame += end_i - start_i @@ -167,21 +216,25 @@ class MusicFile(Machine): return [data, nb_frames] - def seek(self, value = 0, delta = False): + def seek(self, value=0, delta=False): # We don't want to do that while stopping if not (self.is_loaded_playing() or self.is_loaded_paused()): return with self.music_lock: self.a_s_with_effect = None - self.current_frame = max(0, int(delta) * self.current_frame + int(value * self.audio_segment.frame_rate)) + self.current_frame = max( + 0, + int(delta) * self.current_frame + + int(value * self.audio_segment.frame_rate)) # FIXME: si on fait un seek + delta, adapter le "loop" - def stop(self, fade_out = 0, wait = False): + def stop(self, fade_out=0, wait=False): if self.is_loaded_playing(): ms = int(self.sound_position * 1000) ms_fo = max(1, int(fade_out * 1000)) - new_audio_segment = self.current_audio_segment[:ms + ms_fo].fade_out(ms_fo) + new_audio_segment = self.current_audio_segment[: ms+ms_fo] \ + .fade_out(ms_fo) with self.music_lock: self.current_audio_segment = new_audio_segment self.stop_playing() @@ -195,8 +248,10 @@ class MusicFile(Machine): self.db_gain += db_gain self.volume_factor = 10 ** (self.db_gain / 20) - def set_volume(self, value, delta = False): - [db_gain, self.volume] = gain(value + int(delta) * self.volume, self.volume) + def set_volume(self, value, delta=False): + [db_gain, self.volume] = gain( + value + int(delta) * self.volume, + self.volume) self.set_gain(db_gain)