From 2e4049036ec4d90a9daeff606d821d2ac2d023ce Mon Sep 17 00:00:00 2001 From: =?utf8?q?Isma=C3=ABl=20Bouya?= Date: Mon, 18 Jul 2016 00:15:15 +0200 Subject: [PATCH] Coding styles --- helpers/__init__.py | 26 ++++++---- helpers/action.py | 101 ++++++++++++++++++++++---------------- helpers/key.py | 12 ++--- helpers/mapping.py | 23 ++++++--- helpers/mixer.py | 15 +++--- helpers/music_file.py | 109 +++++++++++++++++++++++++++++++----------- music_sampler.py | 3 +- music_sampler.spec | 34 ++++++++++--- 8 files changed, 216 insertions(+), 107 deletions(-) diff --git a/helpers/__init__.py b/helpers/__init__.py index da447d8..9d66638 100644 --- a/helpers/__init__.py +++ b/helpers/__init__.py @@ -17,12 +17,12 @@ def path(): return path + "/../" def parse_args(): - argv = sys.argv[1:] - sys.argv = sys.argv[:1] + argv = sys.argv[1 :] + sys.argv = sys.argv[: 1] if "--" in argv: index = argv.index("--") - kivy_args = argv[index+1:] - argv = argv[:index] + kivy_args = argv[index+1 :] + argv = argv[: index] sys.argv.extend(kivy_args) @@ -39,7 +39,8 @@ def parse_args(): help="Print messages in console") parser.add_argument("-m", "--builtin-mixing", action="store_true", - help="Make the mixing of sounds manually (do it if the system cannot handle it correctly)") + help="Make the mixing of sounds manually\ + (do it if the system cannot handle it correctly)") parser.add_argument("-l", "--latency", default="high", required=False, @@ -48,7 +49,8 @@ def parse_args(): default=0, type=int, required=False, - help="Blocksize: If not 0, the numbe of frames to take at each step for the mixer") + help="Blocksize: If not 0, the number of frames to take\ + at each step for the mixer") parser.add_argument("-f", "--frame-rate", default=44100, type=int, @@ -66,7 +68,8 @@ def parse_args(): help="Sample width (number of bytes for each frame)") parser.add_argument("-V", "--version", action="version", - help="Displays the current version and exits. Only use in bundled package", + help="Displays the current version and exits. Only use\ + in bundled package", version=show_version()) parser.add_argument("--device", action=SelectDeviceAction, @@ -79,7 +82,8 @@ def parse_args(): ) parser.add_argument('--', dest="args", - help="Kivy arguments. All arguments after this are interpreted by Kivy. Pass \"-- --help\" to get Kivy's usage.") + help="Kivy arguments. All arguments after this are interpreted\ + by Kivy. Pass \"-- --help\" to get Kivy's usage.") from kivy.logger import Logger Logger.setLevel(logging.ERROR) @@ -125,11 +129,13 @@ def duration_to_min_sec(duration): else: return "{}:{:0>2}".format(minutes, seconds) -def gain(volume, old_volume = None): +def gain(volume, old_volume=None): if old_volume is None: return 20 * math.log10(volume / 100) else: - return [20 * math.log10(max(volume, 0.1) / max(old_volume, 0.1)), max(volume, 0)] + return [ + 20 * math.log10(max(volume, 0.1) / max(old_volume, 0.1)), + max(volume, 0)] def debug_print(message): from kivy.logger import Logger diff --git a/helpers/action.py b/helpers/action.py index 1a2abe2..218c316 100644 --- a/helpers/action.py +++ b/helpers/action.py @@ -51,58 +51,64 @@ class Action: return self.mapping.open_files.values() # Actions - def command(self, command = "", **kwargs): + def command(self, command="", **kwargs): # FIXME: todo pass - def pause(self, music = None, **kwargs): + def pause(self, music=None, **kwargs): for music in self.music_list(music): if music.is_loaded_playing(): music.pause() - def unpause(self, music = None, **kwargs): + def unpause(self, music=None, **kwargs): for music in self.music_list(music): if music.is_loaded_paused(): music.unpause() - def play(self, music = None, fade_in = 0, start_at = 0, - restart_if_running = False, volume = 100, - loop = 0, - **kwargs): + def play(self, music=None, fade_in=0, start_at=0, + restart_if_running=False, volume=100, + loop=0, **kwargs): for music in self.music_list(music): if restart_if_running: if music.is_not_stopped(): music.stop() - music.play(volume = volume, fade_in = fade_in, start_at = start_at, loop = loop) - else: - if not music.is_not_stopped(): - music.play(volume = volume, fade_in = fade_in, start_at = start_at, loop = loop) - - def seek(self, music = None, value = 0, delta = False, **kwargs): + music.play( + volume=volume, + fade_in=fade_in, + start_at=start_at, + loop=loop) + elif not music.is_not_stopped(): + music.play( + volume=volume, + fade_in=fade_in, + start_at=start_at, + loop=loop) + + def seek(self, music=None, value=0, delta=False, **kwargs): for music in self.music_list(music): - music.seek(value = value, delta = delta) + music.seek(value=value, delta=delta) - def stop(self, music = None, fade_out = 0, wait = False, **kwargs): + def stop(self, music=None, fade_out=0, wait=False, **kwargs): previous = None for music in self.music_list(music): if music.is_loaded_paused() or music.is_loaded_playing(): if previous is not None: - previous.stop(fade_out = fade_out) + previous.stop(fade_out=fade_out) previous = music if previous is not None: - previous.stop(fade_out = fade_out, wait = wait) + previous.stop(fade_out=fade_out, wait=wait) def stop_all_actions(self, **kwargs): self.mapping.stop_all_running() - def volume(self, music = None, value = 100, delta = False, **kwargs): + def volume(self, music=None, value=100, delta=False, **kwargs): if music is not None: - music.set_volume(value, delta = delta) + music.set_volume(value, delta=delta) else: - self.mapping.set_master_volume(value, delta = delta) + self.mapping.set_master_volume(value, delta=delta) - def wait(self, duration = 0, music = None, **kwargs): + def wait(self, duration=0, music=None, **kwargs): self.sleep_event = threading.Event() if music is not None: @@ -112,23 +118,23 @@ class Action: self.sleep_event.wait() # Action messages - def command_print(self, command = "", **kwargs): + def command_print(self, command="", **kwargs): return "running command {}".format(command) - def pause_print(self, music = None, **kwargs): + def pause_print(self, music=None, **kwargs): if music is not None: return "pausing « {} »".format(music.name) else: return "pausing all musics" - def unpause_print(self, music = None, **kwargs): + def unpause_print(self, music=None, **kwargs): if music is not None: return "unpausing « {} »".format(music.name) else: return "unpausing all musics" - def play_print(self, music = None, fade_in = 0, start_at = 0, - restart_if_running = False, volume = 100, loop = 0, **kwargs): + def play_print(self, music=None, fade_in=0, start_at=0, + restart_if_running=False, volume=100, loop=0, **kwargs): message = "starting " if music is not None: message += "« {} »".format(music.name) @@ -153,7 +159,7 @@ class Action: return message - def stop_print(self, music = None, fade_out = 0, wait = False, **kwargs): + def stop_print(self, music=None, fade_out=0, wait=False, **kwargs): message = "stopping " if music is not None: message += "music « {} »".format(music.name) @@ -170,41 +176,52 @@ class Action: def stop_all_actions_print(self, **kwargs): return "stopping all actions" - def seek_print(self, music = None, value = 0, delta = False, **kwargs): + def seek_print(self, music=None, value=0, delta=False, **kwargs): if delta: if music is not None: - return "moving music « {} » by {:+d}s".format(music.name, value) + return "moving music « {} » by {:+d}s" \ + .format(music.name, value) else: - return "moving all musics by {:+d}s".format(value) + return "moving all musics by {:+d}s" \ + .format(value) else: if music is not None: - return "moving music « {} » to position {}s".format(music.name, value) + return "moving music « {} » to position {}s" \ + .format(music.name, value) else: - return "moving all musics to position {}s".format(value) + return "moving all musics to position {}s" \ + .format(value) - def volume_print(self, music = None, value = 100, delta = False, **kwargs): + def volume_print(self, music=None, value=100, delta=False, **kwargs): if delta: if music is not None: - return "{:+d}% to volume of « {} »".format(value, music.name) + return "{:+d}% to volume of « {} »" \ + .format(value, music.name) else: - return "{:+d}% to volume".format(value) + return "{:+d}% to volume" \ + .format(value) else: if music is not None: - return "setting volume of « {} » to {}%".format(music.name, value) + return "setting volume of « {} » to {}%" \ + .format(music.name, value) else: - return "setting volume to {}%".format(value) + return "setting volume to {}%" \ + .format(value) - def wait_print(self, duration = 0, music = None, **kwargs): + def wait_print(self, duration=0, music=None, **kwargs): if music is None: - return "waiting {}s".format(duration) + return "waiting {}s" \ + .format(duration) elif duration == 0: - return "waiting the end of « {} »".format(music.name) + return "waiting the end of « {} »" \ + .format(music.name) else: - return "waiting the end of « {} » + {}s".format(music.name, duration) + return "waiting the end of « {} » + {}s" \ + .format(music.name, duration) # Interruptions - def wait_interrupt(self, duration = 0, music = None, **kwargs): + def wait_interrupt(self, duration=0, music=None, **kwargs): if self.sleep_event is not None: self.sleep_event.set() if music is not None: diff --git a/helpers/key.py b/helpers/key.py index fe82d5b..34c5140 100644 --- a/helpers/key.py +++ b/helpers/key.py @@ -1,5 +1,6 @@ from kivy.uix.widget import Widget -from kivy.properties import AliasProperty, BooleanProperty, ListProperty, StringProperty +from kivy.properties import AliasProperty, BooleanProperty, \ + ListProperty, StringProperty from kivy.clock import Clock from kivy.uix.behaviors import ButtonBehavior @@ -56,7 +57,7 @@ class Key(ButtonBehavior, Widget): def set_description(self, description): if description[0] is not None: self.description_title = str(description[0]) - for desc in description[1:]: + for desc in description[1 :]: if desc is None: self.description.append("") else: @@ -94,16 +95,15 @@ class Key(ButtonBehavior, Widget): action_number = 0 for self.current_action in self.actions: if self.parent.keep_running(self, start_time): - self.list_actions(action_number = action_number + 0.5) + self.list_actions(action_number=action_number + 0.5) self.current_action.run() action_number += 1 - self.list_actions(action_number = action_number) + self.list_actions(action_number=action_number) self.parent.finished_running(self, start_time) - def list_actions(self, action_number = 0): + def list_actions(self, action_number=0): self.parent.parent.ids['ActionList'].update_list(self, action_number) def on_press(self): self.list_actions() - pass diff --git a/helpers/mapping.py b/helpers/mapping.py index bc8ccba..858fe77 100644 --- a/helpers/mapping.py +++ b/helpers/mapping.py @@ -32,8 +32,11 @@ class Mapping(RelativeLayout): def master_gain(self): return gain(self.master_volume) - def set_master_volume(self, value, delta = False): - [db_gain, self.master_volume] = gain(value + int(delta) * self.master_volume, self.master_volume) + def set_master_volume(self, value, delta=False): + [db_gain, self.master_volume] = gain( + value + int(delta) * self.master_volume, + self.master_volume) + for music in self.open_files.values(): music.set_gain(db_gain) @@ -44,7 +47,7 @@ class Mapping(RelativeLayout): def _on_keyboard_down(self, keyboard, keycode, text, modifiers): key = self.find_by_key_code(keycode) if len(modifiers) == 0 and key is not None: - threading.Thread(name = "MSKeyAction", target=key.do_actions).start() + threading.Thread(name="MSKeyAction", target=key.do_actions).start() elif 'ctrl' in modifiers and (keycode[0] == 113 or keycode[0] == '99'): for thread in threading.enumerate(): if thread.getName()[0:2] != "MS": @@ -131,10 +134,14 @@ class Mapping(RelativeLayout): del(action[action_name]['include']) if isinstance(included, str): - action[action_name].update(aliases[included], **action[action_name]) + action[action_name].update( + aliases[included], + **action[action_name]) else: for included_ in included: - action[action_name].update(aliases[included_], **action[action_name]) + action[action_name].update( + aliases[included_], + **action[action_name]) for argument in action[action_name]: if argument == 'file': @@ -151,14 +158,16 @@ class Mapping(RelativeLayout): filename) if filename not in key_properties[mapped_key]['files']: - key_properties[mapped_key]['files'].append(seen_files[filename]) + key_properties[mapped_key]['files'] \ + .append(seen_files[filename]) action_args['music'] = seen_files[filename] else: action_args[argument] = action[action_name][argument] - key_properties[mapped_key]['actions'].append([action_name, action_args]) + key_properties[mapped_key]['actions'] \ + .append([action_name, action_args]) return (key_properties, seen_files) diff --git a/helpers/mixer.py b/helpers/mixer.py index d08520a..1d3f28f 100644 --- a/helpers/mixer.py +++ b/helpers/mixer.py @@ -5,6 +5,7 @@ import time from . import Config sample_width = Config.sample_width + def sample_width_to_dtype(sample_width): if sample_width == 1 or sample_width == 2 or sample_width == 4: return 'int' + str(8*sample_width) @@ -19,13 +20,13 @@ def _latency(latency): class Mixer: def __init__(self): - self.stream = sd.RawOutputStream(samplerate=Config.frame_rate, - channels=Config.channels, - dtype=sample_width_to_dtype(Config.sample_width), - latency=_latency(Config.latency), - blocksize=Config.blocksize, - callback=self.play_callback, - ) + self.stream = sd.RawOutputStream( + samplerate=Config.frame_rate, + channels=Config.channels, + dtype=sample_width_to_dtype(Config.sample_width), + latency=_latency(Config.latency), + blocksize=Config.blocksize, + callback=self.play_callback) self.open_files = [] def add_file(self, music_file): 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) diff --git a/music_sampler.py b/music_sampler.py index 5613fdf..d91e150 100644 --- a/music_sampler.py +++ b/music_sampler.py @@ -50,8 +50,7 @@ class PlayList(RelativeLayout): text = "{}/{}".format( helpers.duration_to_min_sec(music_file.sound_position), - helpers.duration_to_min_sec(music_file.sound_duration) - ) + helpers.duration_to_min_sec(music_file.sound_duration)) if music_file.is_paused(): self.playlist.append(["⏸", music_file.name, text, False]) diff --git a/music_sampler.spec b/music_sampler.spec index 5bae714..591a6cf 100644 --- a/music_sampler.spec +++ b/music_sampler.spec @@ -1,9 +1,21 @@ # -*- mode: python -*- import os -from kivy.tools.packaging.pyinstaller_hooks import get_deps_minimal, hookspath, runtime_hooks +from kivy.tools.packaging.pyinstaller_hooks import get_deps_minimal,\ + hookspath, runtime_hooks -excluded_and_hidden_modules = get_deps_minimal(video=None, camera=None, audio=None, clipboard=None, spelling=None) -excluded_and_hidden_modules['hiddenimports'] += [ 'six', 'packaging', 'packaging.version', 'packaging.specifiers', 'packaging.requirements'] +excluded_and_hidden_modules = get_deps_minimal( + video=None, + camera=None, + audio=None, + clipboard=None, + spelling=None) + +excluded_and_hidden_modules['hiddenimports'] += [ + 'six', + 'packaging', + 'packaging.version', + 'packaging.specifiers', + 'packaging.requirements' ] commit_message = os.popen('git log -1 --format="%h %ci"').read() pyinstaller_file = open(".pyinstaller_commit", "w") @@ -23,8 +35,18 @@ a = Analysis(['music_sampler.py'], pyz = PYZ(a.pure, a.zipped_data) # Single file -exe = EXE(pyz, a.scripts, a.binaries, a.zipfiles, a.datas, name='music_sampler') +exe = EXE(pyz, a.scripts, a.binaries, a.zipfiles, a.datas, + name='music_sampler') # Directory -# exe = EXE(pyz, a.scripts, exclude_binaries=True, name='music_sampler_dir', debug=False, strip=False, upx=True, console=True ) -# coll = COLLECT(exe, a.binaries, a.zipfiles, a.datas, strip=False, upx=True, name='music_sampler_dir') +# exe = EXE(pyz, a.scripts, +# exclude_binaries=True, +# name='music_sampler_dir', +# debug=False, +# strip=False, +# upx=True, +# console=True) +# coll = COLLECT(exe, a.binaries, a.zipfiles, a.datas, +# strip=False, +# upx=True, +# name='music_sampler_dir') -- 2.41.0