I'm trying to create a simple controllable and non-blocking looping wavplayer. The following class created using simpleaudio works, however, there is a brief pause and noticeable popping/snapping noise between each loop of the audio. I'm honestly not sure what's causing it and I'm not sure how I could fix it either than using a different audio module entirely. Any suggestions?
import simpleaudio as sa
from threading import Thread
class WavPlayer(Thread):
def __init__(self,filepath,loop=False):
self.loop = loop
self.wav_obj = sa.WaveObject.from_wave_file(filepath)
self.play_obj = None
Thread.__init__(self)
def run(self):
self.play_obj = self.wav_obj.play() #initialize play buffer and play once
while self.loop is True:
if not self.play_obj.is_playing():
print("played again")
self.play_obj = self.wav_obj.play()
def terminate(self):
print("music terminated")
self.play_obj.stop()
self.loop = False
self.join()
main_loop = WavPlayer("main_loop.wav",True)
menu_loop = WavPlayer("menu.wav",True)
main_loop.start()
z = input("Enter to end theme looping")
main_loop.loop = False
z = input("Enter to terminate music")
main_loop.terminate()
print("playing next song")
menu_loop.start()
cont = ''
while cont != 'n':
cont = input("continue testing non-blocking? enter n to stop: ")
menu_loop.terminate()