aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2016-07-28 03:30:00 +0200
committerIsmaël Bouya <ismael.bouya@normalesup.org>2016-07-28 03:30:00 +0200
commit2010311b76299bb3f0803a52510ab41d64d8e41a (patch)
treebc6dd00aa99c38a7dcffcc381e50af6394aa4896
parent023d9381fb532d09f1e08df65ee545da64222caa (diff)
downloadMusicSampler-2010311b76299bb3f0803a52510ab41d64d8e41a.tar.gz
MusicSampler-2010311b76299bb3f0803a52510ab41d64d8e41a.tar.zst
MusicSampler-2010311b76299bb3f0803a52510ab41d64d8e41a.zip
Improve error message and handling
-rw-r--r--music_sampler/helpers.py33
-rw-r--r--music_sampler/mapping.py10
-rw-r--r--music_sampler/mixer.py10
-rw-r--r--music_sampler/music_effect.py13
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():
29 symbola = find_font("Symbola") 29 symbola = find_font("Symbola")
30 30
31 if ubuntu_regular is None: 31 if ubuntu_regular is None:
32 error_print("Font Ubuntu regular could not be found, please install it.") 32 error_print("Font Ubuntu regular could not be found, "
33 sys.exit() 33 "please install it.", exit=True)
34 if symbola is None: 34 if symbola is None:
35 error_print("Font Symbola could not be found, please install it.") 35 error_print("Font Symbola could not be found, please install it.",
36 sys.exit() 36 exit=True)
37 if ubuntu_bold is None: 37 if ubuntu_bold is None:
38 warn_print("Font Ubuntu Bold could not be found.") 38 warn_print("Font Ubuntu Bold could not be found.")
39 39
@@ -180,12 +180,29 @@ def gain(volume, old_volume=None):
180 20 * math.log10(max(volume, 0.1) / max(old_volume, 0.1)), 180 20 * math.log10(max(volume, 0.1) / max(old_volume, 0.1)),
181 max(volume, 0)] 181 max(volume, 0)]
182 182
183def debug_print(message, with_trace=False): 183def debug_print(message, with_trace=None):
184 if with_trace is None:
185 with_trace = (Logger.getEffectiveLevel() < logging.WARN)
186 with_trace &= (sys.exc_info()[0] is not None)
187
184 Logger.debug('MusicSampler: ' + message, exc_info=with_trace) 188 Logger.debug('MusicSampler: ' + message, exc_info=with_trace)
185 189
186def error_print(message, with_trace=False): 190def error_print(message, exit=False, with_trace=None):
187 Logger.error('MusicSampler: ' + message, exc_info=with_trace) 191 if with_trace is None:
192 with_trace = (Logger.getEffectiveLevel() < logging.WARN)
193 with_trace &= (sys.exc_info()[0] is not None)
194
195 # FIXME: handle it correctly when in a thread
196 if exit:
197 Logger.critical('MusicSampler: ' + message, exc_info=with_trace)
198 sys.exit(1)
199 else:
200 Logger.error('MusicSampler: ' + message, exc_info=with_trace)
201
202def warn_print(message, with_trace=None):
203 if with_trace is None:
204 with_trace = (Logger.getEffectiveLevel() < logging.WARN)
205 with_trace &= (sys.exc_info()[0] is not None)
188 206
189def warn_print(message, with_trace=False):
190 Logger.warn('MusicSampler: ' + message, exc_info=with_trace) 207 Logger.warn('MusicSampler: ' + message, exc_info=with_trace)
191 208
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):
93 self.key_config, self.open_files = self.parse_config() 93 self.key_config, self.open_files = self.parse_config()
94 except Exception as e: 94 except Exception as e:
95 error_print("Error while loading configuration: {}".format(e), 95 error_print("Error while loading configuration: {}".format(e),
96 with_trace=True) 96 with_trace=True, exit=True)
97 sys.exit()
98 else: 97 else:
99 self.success() 98 self.success()
100 99
@@ -306,12 +305,13 @@ class Mapping(RelativeLayout):
306 try: 305 try:
307 config = yaml.safe_load(stream) 306 config = yaml.safe_load(stream)
308 except Exception as e: 307 except Exception as e:
309 error_print("Error while loading config file: {}".format(e)) 308 error_print("Error while loading config file: {}".format(e),
310 sys.exit() 309 exit=True)
311 stream.close() 310 stream.close()
312 311
313 if not isinstance(config, dict): 312 if not isinstance(config, dict):
314 raise Exception("Top level config is supposed to be a hash") 313 error_print("Top level config is supposed to be a hash",
314 exit=True)
315 315
316 if 'aliases' in config and isinstance(config['aliases'], dict): 316 if 'aliases' in config and isinstance(config['aliases'], dict):
317 aliases = config['aliases'] 317 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
2import audioop 2import audioop
3import time 3import time
4 4
5from .helpers import Config 5from .helpers import Config, error_print
6 6
7sample_width = Config.sample_width 7sample_width = Config.sample_width
8 8
9def sample_width_to_dtype(sample_width): 9def sample_width_to_dtype():
10 if sample_width == 1 or sample_width == 2 or sample_width == 4: 10 if sample_width == 1 or sample_width == 2 or sample_width == 4:
11 return 'int' + str(8*sample_width) 11 return 'int' + str(8*sample_width)
12 else: 12 else:
13 raise "Unknown sample width" 13 error_print("Unknown sample width, setting default value 2.")
14 Config.sample_width = 2
15 return 'int16'
14 16
15def _latency(latency): 17def _latency(latency):
16 if latency == "high" or latency == "low": 18 if latency == "high" or latency == "low":
@@ -23,7 +25,7 @@ class Mixer:
23 self.stream = sd.RawOutputStream( 25 self.stream = sd.RawOutputStream(
24 samplerate=Config.frame_rate, 26 samplerate=Config.frame_rate,
25 channels=Config.channels, 27 channels=Config.channels,
26 dtype=sample_width_to_dtype(Config.sample_width), 28 dtype=sample_width_to_dtype(),
27 latency=_latency(Config.latency), 29 latency=_latency(Config.latency),
28 blocksize=Config.blocksize, 30 blocksize=Config.blocksize,
29 callback=self.play_callback) 31 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 @@
1class GainEffect: 1class GainEffect:
2 effect_types = [ 2 effect_types = [
3 'noop',
3 'fade' 4 'fade'
4 ] 5 ]
5 6
@@ -8,7 +9,7 @@ class GainEffect:
8 if effect in self.effect_types: 9 if effect in self.effect_types:
9 self.effect = effect 10 self.effect = effect
10 else: 11 else:
11 raise Exception("Unknown effect {}".format(effect)) 12 self.effect = 'noop'
12 13
13 self.start = start 14 self.start = start
14 self.end = end 15 self.end = end
@@ -29,6 +30,16 @@ class GainEffect:
29 current_loop, 30 current_loop,
30 frame_count) 31 frame_count)
31 32
33 # Noop
34 def noop_init(self, **kwargs):
35 pass
36
37 def noop_get_last_gain(self, **kwargs):
38 return 0
39
40 def noop_get_next_gain(self, **kwargs):
41 return [0, True]
42
32 # Fading 43 # Fading
33 def fade_init(self, gain=0, **kwargs): 44 def fade_init(self, gain=0, **kwargs):
34 self.audio_segment_frame_count = self.audio_segment.frame_count() 45 self.audio_segment_frame_count = self.audio_segment.frame_count()