I am new to python and I am trying to create a simple metronome program. When I run my program I am running into a problem where the audio does not sound evenly spread out. I am running this on a Raspberry Pi 2 Model B. I have also tried using sleep() and time.monotonic(), but I ran into the same problem. Does anyone know what is going on here?
import time
import pygame
path = ("/home/pi/Documents/Loop/")
sound_file = ["kick.wav"]
pygame.mixer.init()
pygame.mixer.music.load(path + sound_file)
BPM = 60
BPMS = 60/(4*BPM)
#Metronome plays on quarter notes
delay = BPMS
past = 0
note = 1
while True:
now = time.perf_counter()
if now >= past + delay:
if note == 1:
pygame.mixer.music.play()
elif note == 2:
pygame.mixer.music.play()
elif note == 3:
pygame.mixer.music.play()
elif note == 4:
pygame.mixer.music.play()
past = now
note = note + 1
if note > 4:
note = 1