]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/commitdiff
Add debugger
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Sun, 17 Jul 2016 15:31:07 +0000 (17:31 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Sun, 17 Jul 2016 15:31:07 +0000 (17:31 +0200)
helpers/__init__.py
helpers/action.py
helpers/key.py
helpers/lock.py
helpers/music_file.py

index ce8f04bf026a55840f2a4b095f9954484c9ef6ea..da447d8c589a417efb85ab92218326a21339936e 100644 (file)
@@ -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)
index 1b8fc5ff53381f60fcf6bb0d0e590af3777819d0..1a2abe203567a496802824c68c1ae3113b57eb47 100644 (file)
@@ -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):
index 5eaf481bea86a4d2aa962be2b4bd035a0692bc8e..fe82d5bea396a9a4d31b412a483f80b29fb2d810 100644 (file)
@@ -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
index 85d281ac8e576731e0362e67a36ad19d15c508da..9beafcde60c80b789b46839ae462c71245727593 100644 (file)
@@ -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)
 
index 7e5f978855c8b6c8dc4b4b47b9d22ef5ae3177d3..5b0d0dffdf2205d90e5c7fddbb3a2723860db659 100644 (file)
@@ -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_')