I'm trying to add a simple metronome to my cross-platform pyQt program, but having a lot of difficulty getting accurate timing. Playing the sound seems to work the best using PyGame's sound system. I tried Phonon, but it just wasn't consistent at all and QSound doesn't work at all on my system. I've tried handling timing with python's time library and using QTimers, but QTimeLine seems to work the best.
The timing at lower tempo is not too bad... still a hiccup here and there. Higher tempos are not accurate at all however.
Any ideas/suggestions would be greatly appreciated!
Here's some code:
class Metronome(object):
def init_metronome(self):
self.metronome_timer = QtCore.QTimeLine(100000)
self.metronome_timer.valueChanged.connect(self.tick)
self.metronome_timer.setCurveShape(3) #linear curve
self.ui.pushButton_metronome.toggled.connect(self.toggle_metronome)
self.ui.spinBox_metronome_bpm.valueChanged.connect(self.set_metronome_bpm)
pygame.mixer.init()
self.sound = pygame.mixer.Sound("./sounds/tick.wav")
def toggle_metronome(self):
if self.ui.pushButton_metronome.isChecked() == True:
self.set_metronome_bpm()
self.metronome_timer.start()
else:
self.metronome_timer.stop()
def set_metronome_bpm(self):
bpm = self.ui.spinBox_metronome_bpm.value()
self.metronome_timer.setUpdateInterval(60./bpm * 1000)
def tick(self):
QtCore.QCoreApplication.processEvents()
self.sound.play()