]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blobdiff - helpers/music_file.py
Remove unused functions
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / music_file.py
index 6da547b8473678b78985272ed7b72f548ab4b368..5baab7ef36d0bf09b92e888f013554506602b59e 100644 (file)
@@ -6,7 +6,8 @@ from transitions.extensions import HierarchicalMachine as Machine
 import os.path
 
 from .lock import Lock
-from . import gain
+from . import Config, gain
+from .mixer import Mixer
 
 file_lock = Lock("file")
 
@@ -31,6 +32,8 @@ class MusicFile(Machine):
 
         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
@@ -48,7 +51,7 @@ class MusicFile(Machine):
             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:
@@ -95,7 +98,7 @@ class MusicFile(Machine):
         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():
@@ -104,7 +107,7 @@ class MusicFile(Machine):
             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):
@@ -121,6 +124,7 @@ class MusicFile(Machine):
                     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')
@@ -208,13 +212,3 @@ class MusicFile(Machine):
         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