class Player(object): def __init__(self, files): self.sampler = Sampler(44100) self._sounds = [] for i in files: self.load(i) def load(self, fname): print('loading', fname) self._sounds += [Sound.from_file(fname).resample(44100)] return len(self._sounds) - 1 def play(self, index): self.sampler.play(self._sounds[index]) def is_playing(self, index): try: return self._sounds[index].playing except: return False def stop(self, index=None): try: if index: self.sampler.remove(self._sounds[index]) else: for i in self._sounds: self.sampler.remove(i) except: pass def pitch(self, index, val): self._sounds[index].pitch_shift = val def stretch(self, index, val): self._sounds[index].stretch_factor = val def sound(self, index): return self._sounds[index]
# Play the song sampler.play(audio) print("***Starting***") while True: try: # Read stream audiobuffer = stream.read(buffer_size) signal = np.frombuffer(audiobuffer, dtype=np.float32) tempo = tempo_o(signal) # Update speed at which song is played try: sfactor = tempo_o.get_bpm()/tempo audio.stretch_factor = sfactor except ZeroDivisionError: sfactor = 1 audio.stretch_factor = sfactor print("Tempo:", tempo_o.get_bpm()) except KeyboardInterrupt: break print("\n***Done Recording***") stream.stop_stream() stream.close() sampler.remove(audio) p.terminate()