X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2FPython%2FMusicSampler.git;a=blobdiff_plain;f=helpers%2Fmusic_file.py;h=aef0adce00c4f55f539b67dd07479bdae7327b7e;hp=b40de1a2f9138a852376bfcc50c058b21af82766;hb=9925ce3b588fc449a3e0f5a6506a2d29207928d3;hpb=29597680758e4924aa71fc021465189e153f2016 diff --git a/helpers/music_file.py b/helpers/music_file.py index b40de1a..aef0adc 100644 --- a/helpers/music_file.py +++ b/helpers/music_file.py @@ -1,63 +1,112 @@ import threading import pydub -import math import time from transitions.extensions import HierarchicalMachine as Machine -import pyaudio as pa -import sounddevice as sd import os.path +import audioop + from .lock import Lock -file_lock = Lock("file") +from . import Config, gain, debug_print, error_print +from .mixer import Mixer +from .music_effect import GainEffect -pyaudio = pa.PyAudio() +file_lock = Lock("file") class MusicFile(Machine): - def __init__(self, filename, name = None, gain = 1): + def __init__(self, filename, mapping, name=None, gain=1): states = [ 'initial', 'loading', 'failed', - { 'name': 'loaded', 'children': ['stopped', 'playing', 'paused', 'stopping'] } + { + 'name': 'loaded', + 'children': ['stopped', 'playing', 'paused', 'stopping'] + } ] transitions = [ - { 'trigger': 'load', 'source': 'initial', 'dest': 'loading'}, - { 'trigger': 'fail', 'source': 'loading', 'dest': 'failed'}, - { 'trigger': 'success', 'source': 'loading', 'dest': 'loaded_stopped'}, - { 'trigger': 'start_playing', 'source': 'loaded_stopped', 'dest': 'loaded_playing'}, - { 'trigger': 'pause', 'source': 'loaded_playing', 'dest': 'loaded_paused'}, - { 'trigger': 'unpause', 'source': 'loaded_paused', 'dest': 'loaded_playing'}, - { 'trigger': 'stop_playing', 'source': ['loaded_playing','loaded_paused'], 'dest': 'loaded_stopping'}, - { 'trigger': 'stopped', 'source': 'loaded_stopping', 'dest': 'loaded_stopped'} + { + 'trigger': 'load', + 'source': 'initial', + 'dest': 'loading' + }, + { + 'trigger': 'fail', + 'source': 'loading', + 'dest': 'failed' + }, + { + 'trigger': 'success', + 'source': 'loading', + 'dest': 'loaded_stopped' + }, + { + 'trigger': 'start_playing', + 'source': 'loaded_stopped', + 'dest': 'loaded_playing' + }, + { + 'trigger': 'pause', + 'source': 'loaded_playing', + 'dest': 'loaded_paused' + }, + { + 'trigger': 'unpause', + 'source': 'loaded_paused', + 'dest': 'loaded_playing' + }, + { + 'trigger': 'stop_playing', + 'source': ['loaded_playing','loaded_paused'], + 'dest': 'loaded_stopping' + }, + { + 'trigger': 'stopped', + 'source': 'loaded_stopping', + 'dest': 'loaded_stopped', + 'after': 'trigger_stopped_events' + } ] - Machine.__init__(self, states=states, transitions=transitions, initial='initial') + Machine.__init__(self, states=states, + transitions=transitions, initial='initial') + self.volume = 100 + self.mapping = mapping self.filename = filename - self.stream = None self.name = name or filename self.audio_segment = None - self.gain = gain + self.audio_segment_frame_width = 0 + self.initial_volume_factor = gain self.music_lock = Lock("music__" + filename) + self.wait_event = threading.Event() + self.db_gain = 0 + self.gain_effects = [] - self.flag_paused = False - threading.Thread(name = "MSMusicLoad", target = self.load).start() + threading.Thread(name="MSMusicLoad", target=self.load).start() def on_enter_loading(self): with file_lock: try: - print("Loading « {} »".format(self.name)) - volume_factor = 20 * math.log10(self.gain) - self.audio_segment = pydub.AudioSegment.from_file(self.filename).set_frame_rate(44100).apply_gain(volume_factor) + debug_print("Loading « {} »".format(self.name)) + self.mixer = self.mapping.mixer or Mixer() + initial_db_gain = gain(self.initial_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(initial_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_') @@ -75,33 +124,35 @@ class MusicFile(Machine): else: return 0 - def play(self, fade_in = 0, volume = 100, start_at = 0): - self.db_gain = self.volume_to_gain(volume) - ms = int(start_at * 1000) - ms_fi = max(1, int(fade_in * 1000)) - with self.music_lock: - self.current_audio_segment = (self.audio_segment + self.db_gain).fade(from_gain=-120, duration=ms_fi, start=ms) - self.before_loaded_playing(initial_frame = int(start_at * self.audio_segment.frame_rate)) - self.start_playing() + def play(self, fade_in=0, volume=100, loop=0, start_at=0): + # FIXME: create a "reinitialize" method + self.gain_effects = [] + self.set_gain(gain(volume) + self.mapping.master_gain, absolute=True) + self.volume = volume + self.current_loop = 0 + if loop < 0: + self.last_loop = float('inf') + else: + self.last_loop = loop - def before_loaded_playing(self, initial_frame = 0): - self.current_frame = initial_frame with self.music_lock: - segment = self.current_audio_segment + self.current_audio_segment = self.audio_segment + self.current_frame = int(start_at * self.audio_segment.frame_rate) + if fade_in > 0: + db_gain = gain(self.volume, 0)[0] + self.set_gain(-db_gain) + self.gain_effects.append(GainEffect( + "fade", + self.current_audio_segment, + self.current_loop, + self.sound_position, + self.sound_position + fade_in, + gain=db_gain)) - self.stream = sd.RawOutputStream(samplerate=segment.frame_rate, - channels=segment.channels, - dtype='int' + str(8*segment.sample_width), # FIXME: ? - latency=1., - callback=self.play_callback, - finished_callback=self.finished_callback - ) + self.start_playing() def on_enter_loaded_playing(self): - self.stream.start() - - def on_enter_loaded_paused(self): - self.stream.stop() + self.mixer.add_file(self) def finished_callback(self): if self.is_loaded_playing(): @@ -109,54 +160,146 @@ class MusicFile(Machine): if self.is_loaded_stopping(): self.stopped() - def play_callback(self, out_data, frame_count, time_info, status_flags): + def trigger_stopped_events(self): + self.mixer.remove_file(self) + self.wait_event.set() + + def play_callback(self, out_data_length, frame_count): + if self.is_loaded_paused(): + return b'\0' * out_data_length + with self.music_lock: - audio_segment = self.current_audio_segment.get_sample_slice_data( - start_sample=self.current_frame, - end_sample=self.current_frame + frame_count - ) - self.current_frame += frame_count - if len(audio_segment) == 0: - raise sd.CallbackStop + [data, nb_frames] = self.get_next_sample(frame_count) + if nb_frames < frame_count: + if self.is_loaded_playing() and\ + self.current_loop < self.last_loop: + self.current_loop += 1 + self.current_frame = 0 + [new_data, new_nb_frames] = self.get_next_sample( + frame_count - nb_frames) + data += new_data + nb_frames += new_nb_frames + elif nb_frames == 0: + # FIXME: too slow when mixing multiple streams + threading.Thread( + name="MSFinishedCallback", + target=self.finished_callback).start() + + return data.ljust(out_data_length, b'\0') + + def get_next_sample(self, frame_count): + fw = self.audio_segment_frame_width + + data = b"" + nb_frames = 0 + + segment = self.current_audio_segment + max_val = int(segment.frame_count()) - out_data[:] = audio_segment.ljust(len(out_data), b'\0') + start_i = max(self.current_frame, 0) + end_i = min(self.current_frame + frame_count, max_val) + data += segment._data[start_i*fw : end_i*fw] + nb_frames += end_i - start_i + self.current_frame += end_i - start_i - def stop(self, fade_out = 0): + volume_factor = self.volume_factor(self.effects_next_gain(nb_frames)) + + data = audioop.mul(data, Config.sample_width, volume_factor) + + return [data, nb_frames] + + def seek(self, value=0, delta=False): + # We don't want to do that while stopping + if not (self.is_loaded_playing() or self.is_loaded_paused()): + return + with self.music_lock: + self.abandon_all_effects() + if delta: + frame_count = int(self.audio_segment.frame_count()) + frame_diff = int(value * self.audio_segment.frame_rate) + self.current_frame += frame_diff + while self.current_frame < 0: + self.current_loop -= 1 + self.current_frame += frame_count + while self.current_frame > frame_count: + self.current_loop += 1 + self.current_frame -= frame_count + if self.current_loop < 0: + self.current_loop = 0 + self.current_frame = 0 + if self.current_loop > self.last_loop: + self.current_loop = self.last_loop + self.current_frame = frame_count + else: + self.current_frame = max( + 0, + int(value * self.audio_segment.frame_rate)) + + 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, + self.current_loop, + 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) ms_fo = max(1, int(fade_out * 1000)) + new_audio_segment = self.current_audio_segment[: ms+ms_fo] \ + .fade_out(ms_fo) with self.music_lock: - self.current_audio_segment = self.current_audio_segment[:ms + ms_fo].fade_out(ms_fo) - self.stop_playing() + self.current_audio_segment = new_audio_segment + self.stop_playing() + if wait: + self.wait_end() else: self.stop_playing() self.stopped() - def set_volume(self, value): - if self.is_loaded_stopped(): - return - - db_gain = self.volume_to_gain(value) - new_audio_segment = self.current_audio_segment + (db_gain - self.db_gain) - self.db_gain = db_gain - with self.music_lock: - self.current_audio_segment = new_audio_segment - - def volume_to_gain(self, volume): - return 20 * math.log10(max(volume, 0.0001) / 100) + def volume_factor(self, additional_gain): + return 10 ** ( (self.db_gain + additional_gain) / 20) - def wait_end(self): - # FIXME: todo - pass + def set_gain(self, db_gain, absolute=False): + if absolute: + self.db_gain = db_gain + else: + self.db_gain += db_gain -# Add some more functions to AudioSegments -def get_sample_slice_data(self, start_sample=0, end_sample=float('inf')): - max_val = int(self.frame_count()) + def set_volume(self, value, delta=False, fade=0): + [db_gain, self.volume] = gain( + value + int(delta) * self.volume, + self.volume) - start_i = max(start_sample, 0) * self.frame_width - end_i = min(end_sample, max_val) * self.frame_width + if fade > 0: + self.gain_effects.append(GainEffect( + "fade", + self.current_audio_segment, + self.current_loop, + self.sound_position, + self.sound_position + fade, + gain=db_gain)) + else: + self.set_gain(db_gain) - return self._data[start_i:end_i] + def wait_end(self): + self.wait_event.clear() + self.wait_event.wait() -pydub.AudioSegment.get_sample_slice_data = get_sample_slice_data