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)),
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)
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" \
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()
--- /dev/null
+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
+ ]
+
+
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")
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()
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)
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]
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,
+ 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)
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()