So I wanted to build a metronome and decided to use pyaudio. I know there are other ways but I want to make something else later with that.
Thats my Code so far:
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
def play_audio(filename):
wf=wave.open(filename,"rb")
p=pyaudio.PyAudio()
stream_play=p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
data = wf.readframes(CHUNK)
while True:
if data != '':
stream_play.write(data)
data = wf.readframes(CHUNK)
if data == b"":
break
stream_play.stop_stream()
stream_play.close()
p.terminate()
def metronom(bpm):
while True:
play_callback(beep)
time.sleep(60/bpm)
beep="beep-07a.wav"
I thought it worked out, but unfortunately the timing is completely off. I also tried it with a callback function, but had the same wrong timing.
THE SOLUTION NOW LOOKS LIKE THIS
def metronom(bpm):
while True:
startTime = time.time()
play_callback(beep)
endTime=time.time()-startTime
time.sleep(60/bpm-endTime)