+ data += segment._data[start_i*fw : end_i*fw]
+
+ frame_count = max(
+ 0,
+ self.current_frame_with_effect + frame_count - max_val)
+
+ self.current_frame_with_effect += end_i - start_i
+ self.current_frame += end_i - start_i
+ nb_frames += end_i - start_i
+
+ if frame_count > 0:
+ self.a_s_with_effect = None
+
+ segment = self.current_audio_segment
+ max_val = int(segment.frame_count())
+
+ 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
+
+ # 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]
+
+ 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()
+ self.a_s_with_effect = None
+ self.current_frame = max(
+ 0,
+ int(delta) * self.current_frame
+ + 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)
+ 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:
+ self.wait_end()
+ else:
+ 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