From a24c34bc1458c4b0962773d804fac4d325632ee8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Isma=C3=ABl=20Bouya?= Date: Sun, 17 Jul 2016 17:31:07 +0200 Subject: [PATCH] Add debugger --- helpers/__init__.py | 22 ++++++++++++++++++++++ helpers/action.py | 4 +++- helpers/key.py | 3 ++- helpers/lock.py | 6 ++++-- helpers/music_file.py | 11 +++++------ 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/helpers/__init__.py b/helpers/__init__.py index ce8f04b..da447d8 100644 --- a/helpers/__init__.py +++ b/helpers/__init__.py @@ -4,6 +4,7 @@ import sys import os import math import sounddevice as sd +import logging class Config: pass @@ -32,6 +33,10 @@ def parse_args(): default="config.yml", required=False, help="Config file to load") + parser.add_argument("-d", "--debug", + nargs=0, + action=DebugModeAction, + 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)") @@ -75,9 +80,14 @@ 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.") + + from kivy.logger import Logger + Logger.setLevel(logging.ERROR) + args = parser.parse_args(argv) Config.yml_file = args.config + Config.latency = args.latency Config.blocksize = args.blocksize Config.frame_rate = args.frame_rate @@ -85,6 +95,11 @@ def parse_args(): Config.sample_width = args.sample_width Config.builtin_mixing = args.builtin_mixing +class DebugModeAction(argparse.Action): + def __call__(self, parser, namespace, values, option_string=None): + from kivy.logger import Logger + Logger.setLevel(logging.DEBUG) + class SelectDeviceAction(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): sd.default.device = values @@ -116,3 +131,10 @@ def gain(volume, old_volume = None): else: 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 + Logger.debug('MusicSampler: ' + message) + +def error_print(message): + from kivy.logger import Logger + Logger.error('MusicSampler: ' + message) diff --git a/helpers/action.py b/helpers/action.py index 1b8fc5f..1a2abe2 100644 --- a/helpers/action.py +++ b/helpers/action.py @@ -1,6 +1,8 @@ import threading import time +from . import debug_print + class Action: action_types = [ 'command', @@ -32,7 +34,7 @@ class Action: return True def run(self): - print(self.description()) + debug_print(self.description()) getattr(self, self.action)(**self.arguments) def description(self): diff --git a/helpers/key.py b/helpers/key.py index 5eaf481..fe82d5b 100644 --- a/helpers/key.py +++ b/helpers/key.py @@ -4,6 +4,7 @@ from kivy.clock import Clock from kivy.uix.behaviors import ButtonBehavior from .action import * +from . import debug_print import time class Key(ButtonBehavior, Widget): @@ -87,7 +88,7 @@ class Key(ButtonBehavior, Widget): return None self.parent.parent.ids['KeyList'].append(self.key_sym) - print("running actions for {}".format(self.key_sym)) + debug_print("running actions for {}".format(self.key_sym)) start_time = time.time() self.parent.start_running(self, start_time) action_number = 0 diff --git a/helpers/lock.py b/helpers/lock.py index 85d281a..9beafcd 100644 --- a/helpers/lock.py +++ b/helpers/lock.py @@ -1,5 +1,7 @@ import threading +from . import debug_print + class Lock: def __init__(self, lock_type): self.type = lock_type @@ -12,10 +14,10 @@ class Lock: self.release(*args, **kwargs) def acquire(self, *args, **kwargs): - #print("acquiring lock for {}".format(self.type)) + #debug_print("acquiring lock for {}".format(self.type)) self.lock.acquire(*args, **kwargs) def release(self, *args, **kwargs): - #print("releasing lock for {}".format(self.type)) + #debug_print("releasing lock for {}".format(self.type)) self.lock.release(*args, **kwargs) diff --git a/helpers/music_file.py b/helpers/music_file.py index 7e5f978..5b0d0df 100644 --- a/helpers/music_file.py +++ b/helpers/music_file.py @@ -6,7 +6,7 @@ from transitions.extensions import HierarchicalMachine as Machine import os.path from .lock import Lock -from . import Config, gain +from . import Config, gain, debug_print, error_print from .mixer import Mixer file_lock = Lock("file") @@ -32,8 +32,6 @@ class MusicFile(Machine): Machine.__init__(self, states=states, transitions=transitions, initial='initial') - # FIXME: catch error here - self.mixer = mapping.mixer or Mixer() self.volume = 100 self.mapping = mapping self.filename = filename @@ -49,18 +47,19 @@ class MusicFile(Machine): def on_enter_loading(self): with file_lock: try: - print("Loading « {} »".format(self.name)) + debug_print("Loading « {} »".format(self.name)) + self.mixer = self.mapping.mixer or Mixer() db_gain = gain(self.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(db_gain) self.audio_segment_frame_width = self.audio_segment.frame_width self.sound_duration = self.audio_segment.duration_seconds except Exception as e: - print("failed to load « {} »: {}".format(self.name, e)) + error_print("failed to load « {} »: {}".format(self.name, e)) self.loading_error = e self.fail() else: self.success() - print("Loaded « {} »".format(self.name)) + debug_print("Loaded « {} »".format(self.name)) def check_is_loaded(self): return self.state.startswith('loaded_') -- 2.41.0