+ self.name = name or filename
+ self.audio_segment = None
+ self.initial_volume_factor = gain
+ self.music_lock = Lock("music__" + filename)
+
+ threading.Thread(name="MSMusicLoad", target=self.load).start()
+
+ def reload_properties(self, name=None, gain=1):
+ self.name = name or self.filename
+ if gain != self.initial_volume_factor:
+ self.initial_volume_factor = gain
+ self.reload_music_file()
+
+ def reload_music_file(self):
+ with file_lock:
+ try:
+ if self.filename.startswith("/"):
+ filename = self.filename
+ else:
+ filename = Config.music_path + self.filename
+
+ debug_print("Reloading « {} »".format(self.name))
+ initial_db_gain = gain(self.initial_volume_factor * 100)
+ self.audio_segment = pydub.AudioSegment \
+ .from_file(filename) \
+ .set_frame_rate(Config.frame_rate) \
+ .set_channels(Config.channels) \
+ .set_sample_width(Config.sample_width) \
+ .apply_gain(initial_db_gain)
+ except Exception as e:
+ error_print("failed to reload « {} »: {}"\
+ .format(self.name, e))
+ self.loading_error = e
+ self.to_failed()
+ else:
+ debug_print("Reloaded « {} »".format(self.name))
+
+ # Machine related events
+ def on_enter_loading(self):
+ with file_lock:
+ try:
+ if self.filename.startswith("/"):
+ filename = self.filename
+ else:
+ filename = Config.music_path + self.filename
+
+ 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(filename) \
+ .set_frame_rate(Config.frame_rate) \
+ .set_channels(Config.channels) \
+ .set_sample_width(Config.sample_width) \
+ .apply_gain(initial_db_gain)
+ self.sound_duration = self.audio_segment.duration_seconds
+ except Exception as e:
+ error_print("failed to load « {} »: {}".format(self.name, e))
+ self.loading_error = e
+ self.fail()
+ else:
+ self.success()
+ debug_print("Loaded « {} »".format(self.name))
+
+ def on_enter_loaded(self):
+ self.cleanup()
+
+ def cleanup(self):
+ self.gain_effects = []
+ self.set_gain(0, absolute=True)
+ self.current_audio_segment = None
+ self.volume = 100
+ self.wait_event = threading.Event()
+ self.current_loop = 0
+
+ def on_enter_loaded_playing(self):
+ self.mixer.add_file(self)
+
+ # Machine related states
+ def is_in_use(self):
+ return self.is_loaded(allow_substates=True) and not self.is_loaded()
+
+ def is_in_use_not_stopping(self):
+ return self.is_loaded_playing() or self.is_loaded_paused()
+
+ # Machine related triggers
+ def trigger_stopped_events(self):
+ self.mixer.remove_file(self)
+ self.wait_event.set()
+ self.cleanup()
+
+ # Actions and properties called externally
+ @property
+ def sound_position(self):
+ if self.is_in_use():
+ return self.current_frame / self.current_audio_segment.frame_rate
+ else:
+ return 0
+
+ def play(self, fade_in=0, volume=100, loop=0, start_at=0):
+ self.set_gain(gain(volume) + self.mapping.master_gain, absolute=True)
+ self.volume = volume
+ if loop < 0:
+ self.last_loop = float('inf')
+ else:
+ self.last_loop = loop
+
+ with self.music_lock:
+ self.current_audio_segment = self.audio_segment
+ self.current_frame = int(start_at * self.audio_segment.frame_rate)
+
+ self.start_playing()
+
+ if fade_in > 0:
+ db_gain = gain(self.volume, 0)[0]
+ self.set_gain(-db_gain)
+ self.add_fade_effect(db_gain, fade_in)
+
+ def seek(self, value=0, delta=False):
+ if not self.is_in_use_not_stopping():
+ 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 stop(self, fade_out=0, wait=False, set_wait_id=None):
+ 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 = new_audio_segment
+ self.stop_playing()
+ if wait:
+ if set_wait_id is not None:
+ self.mapping.add_wait_id(set_wait_id, self.wait_event)
+ self.wait_end()