]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/commitdiff
Improve error message and handling
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Thu, 28 Jul 2016 01:30:00 +0000 (03:30 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Thu, 28 Jul 2016 01:30:00 +0000 (03:30 +0200)
music_sampler/helpers.py
music_sampler/mapping.py
music_sampler/mixer.py
music_sampler/music_effect.py

index 68c1328998379a9d5f2c1deb61f5174d0359741a..6acaba4f7e9300523ef6c9a20c389b2eb9d79484 100644 (file)
@@ -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)
 
index ca471ef7115a3762870c3e5177e996a9c56273af..0d58d3024fc51fb731ce7f39a4bc0432cd410ad3 100644 (file)
@@ -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']
index c8ec907230712ca99a7a6e02e4ee3514ed664a9c..b290162a1fe7b22ff45e8b2c30c17c619bac936a 100644 (file)
@@ -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)
index 4bdbb262ecd17d89d643f07eff1d0a9df3f4dccd..57825c85972f190cfe905c8a19282d7c44273516 100644 (file)
@@ -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()