From aee1334ca47ff55c815eee204fe03683a572be0f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Isma=C3=ABl=20Bouya?= Date: Mon, 18 Jul 2016 21:17:12 +0200 Subject: [PATCH] Add fading --- helpers/__init__.py | 2 +- helpers/action.py | 21 +++++++++++------ helpers/mapping.py | 2 +- helpers/music_effect.py | 50 +++++++++++++++++++++++++++++++++++++++++ helpers/music_file.py | 48 ++++++++++++++++++++++++++++++++++----- 5 files changed, 108 insertions(+), 15 deletions(-) create mode 100644 helpers/music_effect.py diff --git a/helpers/__init__.py b/helpers/__init__.py index 9d66638..f5ad848 100644 --- a/helpers/__init__.py +++ b/helpers/__init__.py @@ -131,7 +131,7 @@ def duration_to_min_sec(duration): def gain(volume, old_volume=None): if old_volume is None: - return 20 * math.log10(volume / 100) + return 20 * math.log10(max(volume, 0.1) / 100) else: return [ 20 * math.log10(max(volume, 0.1) / max(old_volume, 0.1)), diff --git a/helpers/action.py b/helpers/action.py index 218c316..b8e44e6 100644 --- a/helpers/action.py +++ b/helpers/action.py @@ -102,9 +102,9 @@ class Action: def stop_all_actions(self, **kwargs): self.mapping.stop_all_running() - def volume(self, music=None, value=100, delta=False, **kwargs): + def volume(self, music=None, value=100, fade=0, delta=False, **kwargs): if music is not None: - music.set_volume(value, delta=delta) + music.set_volume(value, delta=delta, fade=fade) else: self.mapping.set_master_volume(value, delta=delta) @@ -192,22 +192,29 @@ class Action: return "moving all musics to position {}s" \ .format(value) - def volume_print(self, music=None, value=100, delta=False, **kwargs): + def volume_print(self, music=None, + value=100, delta=False, fade=0, **kwargs): + message = "" if delta: if music is not None: - return "{:+d}% to volume of « {} »" \ + message += "{:+d}% to volume of « {} »" \ .format(value, music.name) else: - return "{:+d}% to volume" \ + message += "{:+d}% to volume" \ .format(value) else: if music is not None: - return "setting volume of « {} » to {}%" \ + message += "setting volume of « {} » to {}%" \ .format(music.name, value) else: - return "setting volume to {}%" \ + message += "setting volume to {}%" \ .format(value) + if music is not None and fade > 0: + message += " with {}s fade".format(fade) + + return message + def wait_print(self, duration=0, music=None, **kwargs): if music is None: return "waiting {}s" \ diff --git a/helpers/mapping.py b/helpers/mapping.py index 19fc3c5..9420acf 100644 --- a/helpers/mapping.py +++ b/helpers/mapping.py @@ -91,7 +91,7 @@ class Mapping(RelativeLayout): stream = open(Config.yml_file, "r") try: config = yaml.load(stream) - except yaml.scanner.ScannerError as e: + except Exception as e: error_print("Error while loading config file: {}".format(e)) sys.exit() stream.close() diff --git a/helpers/music_effect.py b/helpers/music_effect.py new file mode 100644 index 0000000..ef8dc90 --- /dev/null +++ b/helpers/music_effect.py @@ -0,0 +1,50 @@ +class GainEffect: + effect_types = [ + 'fade' + ] + + def __init__(self, effect, audio_segment, start, end, **kwargs): + if effect in self.effect_types: + self.effect = effect + else: + raise Exception("Unknown effect {}".format(effect)) + + self.start = start + self.end = end + self.audio_segment = audio_segment + getattr(self, self.effect + "_init")(**kwargs) + + def get_last_gain(self): + return getattr(self, self.effect + "_get_last_gain")() + + def get_next_gain(self, current_frame, frame_count): + # This returns two values: + # - The first one is the gain to apply on that frame + # - The last one is True or False depending on whether it is the last + # call to the function and the last gain should be saved permanently + return getattr(self, self.effect + "_get_next_gain")( + current_frame, + frame_count) + + # Fading + def fade_init(self, gain=0, **kwargs): + self.first_frame = int(self.audio_segment.frame_rate * self.start) + self.last_frame = int(self.audio_segment.frame_rate * self.end) + self.gain= gain + + def fade_get_last_gain(self): + return self.gain + + def fade_get_next_gain(self, current_frame, frame_count): + if current_frame >= self.last_frame: + return [self.gain, True] + elif current_frame < self.first_frame: + return [0, False] + else: + return [ + (current_frame - self.first_frame) / \ + (self.last_frame - self.first_frame) * self.gain, + False + ] + + diff --git a/helpers/music_file.py b/helpers/music_file.py index 3053008..810bc22 100644 --- a/helpers/music_file.py +++ b/helpers/music_file.py @@ -10,6 +10,7 @@ import audioop from .lock import Lock from . import Config, gain, debug_print, error_print from .mixer import Mixer +from .music_effect import GainEffect file_lock = Lock("file") @@ -81,7 +82,7 @@ class MusicFile(Machine): self.music_lock = Lock("music__" + filename) self.wait_event = threading.Event() self.db_gain = 0 - self.volume_factor = 1 + self.gain_effects = [] threading.Thread(name="MSMusicLoad", target=self.load).start() @@ -134,7 +135,6 @@ class MusicFile(Machine): self.current_audio_segment = self.audio_segment self.current_frame = int(start_at * self.audio_segment.frame_rate) if ms_fi > 0: - # FIXME: apply it to repeated when looping? self.a_s_with_effect = self \ .current_audio_segment[ms : ms+ms_fi] \ .fade_in(ms_fi) @@ -213,7 +213,10 @@ class MusicFile(Machine): nb_frames += end_i - start_i self.current_frame += end_i - start_i - data = audioop.mul(data, Config.sample_width, self.volume_factor) + # FIXME: self.effects_next_gain should take into account the loop number + volume_factor = self.volume_factor(self.effects_next_gain(nb_frames)) + + data = audioop.mul(data, Config.sample_width, volume_factor) return [data, nb_frames] @@ -222,6 +225,7 @@ class MusicFile(Machine): if not (self.is_loaded_playing() or self.is_loaded_paused()): return with self.music_lock: + self.abandon_all_effects() self.a_s_with_effect = None self.current_frame = max( 0, @@ -229,6 +233,28 @@ class MusicFile(Machine): + int(value * self.audio_segment.frame_rate)) # FIXME: si on fait un seek + delta, adapter le "loop" + def effects_next_gain(self, frame_count): + db_gain = 0 + for gain_effect in self.gain_effects: + [new_gain, last_gain] = gain_effect.get_next_gain( + self.current_frame, + frame_count) + if last_gain: + self.set_gain(new_gain) + self.gain_effects.remove(gain_effect) + else: + db_gain += new_gain + return db_gain + + + def abandon_all_effects(self): + db_gain = 0 + for gain_effect in self.gain_effects: + db_gain += gain_effect.get_last_gain() + + self.gain_effects = [] + self.set_gain(db_gain) + def stop(self, fade_out=0, wait=False): if self.is_loaded_playing(): ms = int(self.sound_position * 1000) @@ -245,19 +271,29 @@ class MusicFile(Machine): self.stop_playing() self.stopped() + def volume_factor(self, additional_gain): + return 10 ** ( (self.db_gain + additional_gain) / 20) + def set_gain(self, db_gain, absolute=False): if absolute: self.db_gain = db_gain else: self.db_gain += db_gain - self.volume_factor = 10 ** (self.db_gain / 20) - def set_volume(self, value, delta=False): + def set_volume(self, value, delta=False, fade=0): [db_gain, self.volume] = gain( value + int(delta) * self.volume, self.volume) - self.set_gain(db_gain) + if fade > 0: + self.gain_effects.append(GainEffect( + "fade", + self.current_audio_segment, + self.sound_position, + self.sound_position + fade, + gain=db_gain)) + else: + self.set_gain(db_gain) def wait_end(self): self.wait_event.clear() -- 2.41.0