From 63ba5a8dc2aa4ec3e6f203b0ba4db249ecf0b00e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Wed, 27 Jul 2016 21:33:09 +0200 Subject: Rename helpers to music_sampler --- helpers/mixer.py | 63 -------------------------------------------------------- 1 file changed, 63 deletions(-) delete mode 100644 helpers/mixer.py (limited to 'helpers/mixer.py') diff --git a/helpers/mixer.py b/helpers/mixer.py deleted file mode 100644 index 9242b61..0000000 --- a/helpers/mixer.py +++ /dev/null @@ -1,63 +0,0 @@ -import sounddevice as sd -import audioop -import time - -from . import Config - -sample_width = Config.sample_width - -def sample_width_to_dtype(sample_width): - if sample_width == 1 or sample_width == 2 or sample_width == 4: - return 'int' + str(8*sample_width) - else: - raise "Unknown sample width" - -def _latency(latency): - if latency == "high" or latency == "low": - return latency - else: - return float(latency) - -class Mixer: - def __init__(self): - self.stream = sd.RawOutputStream( - samplerate=Config.frame_rate, - channels=Config.channels, - dtype=sample_width_to_dtype(Config.sample_width), - latency=_latency(Config.latency), - blocksize=Config.blocksize, - callback=self.play_callback) - self.open_files = [] - - def add_file(self, music_file): - if music_file not in self.open_files: - self.open_files.append(music_file) - self.start() - - def remove_file(self, music_file): - if music_file in self.open_files: - self.open_files.remove(music_file) - if len(self.open_files) == 0: - self.stop() - - def stop(self): - self.stream.stop() - - def start(self): - self.stream.start() - - def play_callback(self, out_data, frame_count, time_info, status_flags): - out_data_length = len(out_data) - empty_data = b"\0" * out_data_length - data = b"\0" * out_data_length - - for open_file in self.open_files: - file_data = open_file.play_callback(out_data_length, frame_count) - - if data == empty_data: - data = file_data - elif file_data != empty_data: - data = audioop.add(data, file_data, sample_width) - - out_data[:] = data - -- cgit v1.2.3