From 2010311b76299bb3f0803a52510ab41d64d8e41a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Isma=C3=ABl=20Bouya?= Date: Thu, 28 Jul 2016 03:30:00 +0200 Subject: [PATCH] Improve error message and handling --- music_sampler/helpers.py | 33 +++++++++++++++++++++++++-------- music_sampler/mapping.py | 10 +++++----- music_sampler/mixer.py | 10 ++++++---- music_sampler/music_effect.py | 13 ++++++++++++- 4 files changed, 48 insertions(+), 18 deletions(-) diff --git a/music_sampler/helpers.py b/music_sampler/helpers.py index 68c1328..6acaba4 100644 --- a/music_sampler/helpers.py +++ b/music_sampler/helpers.py @@ -29,11 +29,11 @@ def register_fonts(): symbola = find_font("Symbola") if ubuntu_regular is None: - error_print("Font Ubuntu regular could not be found, please install it.") - sys.exit() + error_print("Font Ubuntu regular could not be found, " + "please install it.", exit=True) if symbola is None: - error_print("Font Symbola could not be found, please install it.") - sys.exit() + error_print("Font Symbola could not be found, please install it.", + exit=True) if ubuntu_bold is None: warn_print("Font Ubuntu Bold could not be found.") @@ -180,12 +180,29 @@ def gain(volume, old_volume=None): 20 * math.log10(max(volume, 0.1) / max(old_volume, 0.1)), max(volume, 0)] -def debug_print(message, with_trace=False): +def debug_print(message, with_trace=None): + if with_trace is None: + with_trace = (Logger.getEffectiveLevel() < logging.WARN) + with_trace &= (sys.exc_info()[0] is not None) + Logger.debug('MusicSampler: ' + message, exc_info=with_trace) -def error_print(message, with_trace=False): - Logger.error('MusicSampler: ' + message, exc_info=with_trace) +def error_print(message, exit=False, with_trace=None): + if with_trace is None: + with_trace = (Logger.getEffectiveLevel() < logging.WARN) + with_trace &= (sys.exc_info()[0] is not None) + + # FIXME: handle it correctly when in a thread + if exit: + Logger.critical('MusicSampler: ' + message, exc_info=with_trace) + sys.exit(1) + else: + Logger.error('MusicSampler: ' + message, exc_info=with_trace) + +def warn_print(message, with_trace=None): + if with_trace is None: + with_trace = (Logger.getEffectiveLevel() < logging.WARN) + with_trace &= (sys.exc_info()[0] is not None) -def warn_print(message, with_trace=False): Logger.warn('MusicSampler: ' + message, exc_info=with_trace) diff --git a/music_sampler/mapping.py b/music_sampler/mapping.py index ca471ef..0d58d30 100644 --- a/music_sampler/mapping.py +++ b/music_sampler/mapping.py @@ -93,8 +93,7 @@ class Mapping(RelativeLayout): self.key_config, self.open_files = self.parse_config() except Exception as e: error_print("Error while loading configuration: {}".format(e), - with_trace=True) - sys.exit() + with_trace=True, exit=True) else: self.success() @@ -306,12 +305,13 @@ class Mapping(RelativeLayout): try: config = yaml.safe_load(stream) except Exception as e: - error_print("Error while loading config file: {}".format(e)) - sys.exit() + error_print("Error while loading config file: {}".format(e), + exit=True) stream.close() if not isinstance(config, dict): - raise Exception("Top level config is supposed to be a hash") + error_print("Top level config is supposed to be a hash", + exit=True) if 'aliases' in config and isinstance(config['aliases'], dict): aliases = config['aliases'] diff --git a/music_sampler/mixer.py b/music_sampler/mixer.py index c8ec907..b290162 100644 --- a/music_sampler/mixer.py +++ b/music_sampler/mixer.py @@ -2,15 +2,17 @@ import sounddevice as sd import audioop import time -from .helpers import Config +from .helpers import Config, error_print sample_width = Config.sample_width -def sample_width_to_dtype(sample_width): +def sample_width_to_dtype(): if sample_width == 1 or sample_width == 2 or sample_width == 4: return 'int' + str(8*sample_width) else: - raise "Unknown sample width" + error_print("Unknown sample width, setting default value 2.") + Config.sample_width = 2 + return 'int16' def _latency(latency): if latency == "high" or latency == "low": @@ -23,7 +25,7 @@ class Mixer: self.stream = sd.RawOutputStream( samplerate=Config.frame_rate, channels=Config.channels, - dtype=sample_width_to_dtype(Config.sample_width), + dtype=sample_width_to_dtype(), latency=_latency(Config.latency), blocksize=Config.blocksize, callback=self.play_callback) diff --git a/music_sampler/music_effect.py b/music_sampler/music_effect.py index 4bdbb26..57825c8 100644 --- a/music_sampler/music_effect.py +++ b/music_sampler/music_effect.py @@ -1,5 +1,6 @@ class GainEffect: effect_types = [ + 'noop', 'fade' ] @@ -8,7 +9,7 @@ class GainEffect: if effect in self.effect_types: self.effect = effect else: - raise Exception("Unknown effect {}".format(effect)) + self.effect = 'noop' self.start = start self.end = end @@ -29,6 +30,16 @@ class GainEffect: current_loop, frame_count) + # Noop + def noop_init(self, **kwargs): + pass + + def noop_get_last_gain(self, **kwargs): + return 0 + + def noop_get_next_gain(self, **kwargs): + return [0, True] + # Fading def fade_init(self, gain=0, **kwargs): self.audio_segment_frame_count = self.audio_segment.frame_count() -- 2.41.0