import os.path
from .lock import Lock
-from . import gain
+from . import Config, gain
+from .mixer import Mixer
file_lock = Lock("file")
Machine.__init__(self, states=states, transitions=transitions, initial='initial')
+ # FIXME: catch error here
+ self.mixer = mapping.mixer or Mixer()
self.volume = 100
self.mapping = mapping
self.filename = filename
try:
print("Loading « {} »".format(self.name))
db_gain = gain(self.volume_factor * 100)
- self.audio_segment = pydub.AudioSegment.from_file(self.filename).set_frame_rate(44100).set_channels(2).set_sample_width(2).apply_gain(db_gain)
+ 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(db_gain)
self.audio_segment_frame_width = self.audio_segment.frame_width
self.sound_duration = self.audio_segment.duration_seconds
except Exception as e:
self.start_playing()
def on_enter_loaded_playing(self):
- self.mapping.mixer.add_file(self)
+ self.mixer.add_file(self)
def finished_callback(self):
if self.is_loaded_playing():
self.stopped()
def trigger_stopped_events(self):
- self.mapping.mixer.remove_file(self)
+ self.mixer.remove_file(self)
self.wait_event.set()
def play_callback(self, out_data_length, frame_count):
data += new_data
nb_frames += new_nb_frames
elif nb_frames == 0:
+ # FIXME: too slow
threading.Thread(name = "MSFinishedCallback", target=self.finished_callback).start()
return data.ljust(out_data_length, b'\0')
self.wait_event.clear()
self.wait_event.wait()
-# 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())
-
- start_i = max(start_sample, 0) * self.frame_width
- end_i = min(end_sample, max_val) * self.frame_width
-
- return self._data[start_i:end_i]
-
-pydub.AudioSegment.get_sample_slice_data = get_sample_slice_data