aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--helpers/__init__.py22
-rw-r--r--helpers/action.py4
-rw-r--r--helpers/key.py3
-rw-r--r--helpers/lock.py6
-rw-r--r--helpers/music_file.py11
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
4import os 4import os
5import math 5import math
6import sounddevice as sd 6import sounddevice as sd
7import logging
7 8
8class Config: 9class Config:
9 pass 10 pass
@@ -32,6 +33,10 @@ def parse_args():
32 default="config.yml", 33 default="config.yml",
33 required=False, 34 required=False,
34 help="Config file to load") 35 help="Config file to load")
36 parser.add_argument("-d", "--debug",
37 nargs=0,
38 action=DebugModeAction,
39 help="Print messages in console")
35 parser.add_argument("-m", "--builtin-mixing", 40 parser.add_argument("-m", "--builtin-mixing",
36 action="store_true", 41 action="store_true",
37 help="Make the mixing of sounds manually (do it if the system cannot handle it correctly)") 42 help="Make the mixing of sounds manually (do it if the system cannot handle it correctly)")
@@ -75,9 +80,14 @@ def parse_args():
75 parser.add_argument('--', 80 parser.add_argument('--',
76 dest="args", 81 dest="args",
77 help="Kivy arguments. All arguments after this are interpreted by Kivy. Pass \"-- --help\" to get Kivy's usage.") 82 help="Kivy arguments. All arguments after this are interpreted by Kivy. Pass \"-- --help\" to get Kivy's usage.")
83
84 from kivy.logger import Logger
85 Logger.setLevel(logging.ERROR)
86
78 args = parser.parse_args(argv) 87 args = parser.parse_args(argv)
79 88
80 Config.yml_file = args.config 89 Config.yml_file = args.config
90
81 Config.latency = args.latency 91 Config.latency = args.latency
82 Config.blocksize = args.blocksize 92 Config.blocksize = args.blocksize
83 Config.frame_rate = args.frame_rate 93 Config.frame_rate = args.frame_rate
@@ -85,6 +95,11 @@ def parse_args():
85 Config.sample_width = args.sample_width 95 Config.sample_width = args.sample_width
86 Config.builtin_mixing = args.builtin_mixing 96 Config.builtin_mixing = args.builtin_mixing
87 97
98class DebugModeAction(argparse.Action):
99 def __call__(self, parser, namespace, values, option_string=None):
100 from kivy.logger import Logger
101 Logger.setLevel(logging.DEBUG)
102
88class SelectDeviceAction(argparse.Action): 103class SelectDeviceAction(argparse.Action):
89 def __call__(self, parser, namespace, values, option_string=None): 104 def __call__(self, parser, namespace, values, option_string=None):
90 sd.default.device = values 105 sd.default.device = values
@@ -116,3 +131,10 @@ def gain(volume, old_volume = None):
116 else: 131 else:
117 return [20 * math.log10(max(volume, 0.1) / max(old_volume, 0.1)), max(volume, 0)] 132 return [20 * math.log10(max(volume, 0.1) / max(old_volume, 0.1)), max(volume, 0)]
118 133
134def debug_print(message):
135 from kivy.logger import Logger
136 Logger.debug('MusicSampler: ' + message)
137
138def error_print(message):
139 from kivy.logger import Logger
140 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 @@
1import threading 1import threading
2import time 2import time
3 3
4from . import debug_print
5
4class Action: 6class Action:
5 action_types = [ 7 action_types = [
6 'command', 8 'command',
@@ -32,7 +34,7 @@ class Action:
32 return True 34 return True
33 35
34 def run(self): 36 def run(self):
35 print(self.description()) 37 debug_print(self.description())
36 getattr(self, self.action)(**self.arguments) 38 getattr(self, self.action)(**self.arguments)
37 39
38 def description(self): 40 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
4from kivy.uix.behaviors import ButtonBehavior 4from kivy.uix.behaviors import ButtonBehavior
5 5
6from .action import * 6from .action import *
7from . import debug_print
7import time 8import time
8 9
9class Key(ButtonBehavior, Widget): 10class Key(ButtonBehavior, Widget):
@@ -87,7 +88,7 @@ class Key(ButtonBehavior, Widget):
87 return None 88 return None
88 89
89 self.parent.parent.ids['KeyList'].append(self.key_sym) 90 self.parent.parent.ids['KeyList'].append(self.key_sym)
90 print("running actions for {}".format(self.key_sym)) 91 debug_print("running actions for {}".format(self.key_sym))
91 start_time = time.time() 92 start_time = time.time()
92 self.parent.start_running(self, start_time) 93 self.parent.start_running(self, start_time)
93 action_number = 0 94 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 @@
1import threading 1import threading
2 2
3from . import debug_print
4
3class Lock: 5class Lock:
4 def __init__(self, lock_type): 6 def __init__(self, lock_type):
5 self.type = lock_type 7 self.type = lock_type
@@ -12,10 +14,10 @@ class Lock:
12 self.release(*args, **kwargs) 14 self.release(*args, **kwargs)
13 15
14 def acquire(self, *args, **kwargs): 16 def acquire(self, *args, **kwargs):
15 #print("acquiring lock for {}".format(self.type)) 17 #debug_print("acquiring lock for {}".format(self.type))
16 self.lock.acquire(*args, **kwargs) 18 self.lock.acquire(*args, **kwargs)
17 19
18 def release(self, *args, **kwargs): 20 def release(self, *args, **kwargs):
19 #print("releasing lock for {}".format(self.type)) 21 #debug_print("releasing lock for {}".format(self.type))
20 self.lock.release(*args, **kwargs) 22 self.lock.release(*args, **kwargs)
21 23
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
6import os.path 6import os.path
7 7
8from .lock import Lock 8from .lock import Lock
9from . import Config, gain 9from . import Config, gain, debug_print, error_print
10from .mixer import Mixer 10from .mixer import Mixer
11 11
12file_lock = Lock("file") 12file_lock = Lock("file")
@@ -32,8 +32,6 @@ class MusicFile(Machine):
32 32
33 Machine.__init__(self, states=states, transitions=transitions, initial='initial') 33 Machine.__init__(self, states=states, transitions=transitions, initial='initial')
34 34
35 # FIXME: catch error here
36 self.mixer = mapping.mixer or Mixer()
37 self.volume = 100 35 self.volume = 100
38 self.mapping = mapping 36 self.mapping = mapping
39 self.filename = filename 37 self.filename = filename
@@ -49,18 +47,19 @@ class MusicFile(Machine):
49 def on_enter_loading(self): 47 def on_enter_loading(self):
50 with file_lock: 48 with file_lock:
51 try: 49 try:
52 print("Loading « {} »".format(self.name)) 50 debug_print("Loading « {} »".format(self.name))
51 self.mixer = self.mapping.mixer or Mixer()
53 db_gain = gain(self.volume_factor * 100) 52 db_gain = gain(self.volume_factor * 100)
54 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) 53 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)
55 self.audio_segment_frame_width = self.audio_segment.frame_width 54 self.audio_segment_frame_width = self.audio_segment.frame_width
56 self.sound_duration = self.audio_segment.duration_seconds 55 self.sound_duration = self.audio_segment.duration_seconds
57 except Exception as e: 56 except Exception as e:
58 print("failed to load « {} »: {}".format(self.name, e)) 57 error_print("failed to load « {} »: {}".format(self.name, e))
59 self.loading_error = e 58 self.loading_error = e
60 self.fail() 59 self.fail()
61 else: 60 else:
62 self.success() 61 self.success()
63 print("Loaded « {} »".format(self.name)) 62 debug_print("Loaded « {} »".format(self.name))
64 63
65 def check_is_loaded(self): 64 def check_is_loaded(self):
66 return self.state.startswith('loaded_') 65 return self.state.startswith('loaded_')