PyQT5 QSound delay between sounds

197 Views Asked by At

I am making a digital piano software with PyQt5 and one of the functions of it is to be able to play notes automatically. I have registered several musical notes under a list, and am trying to use QSound to play them, however when I do, they play all at the same time. How would I be able to add a delay between sounds? Thanks for the help

def playSheetMusic(self, Piano):
        sheetmusic = ["A5", "G4", "C4", "C4", "C4"]
        note1 = sheetmusic[0]
        file1 = "pianokeys/" + note1 +".wav"
        note2 = sheetmusic[1]
        file2 = "pianokeys/" + note2 +".wav"
        QSound.play(file1)
        QSound.play(file2)
1

There are 1 best solutions below

3
Sahith Kurapati On

You should simply be able to add a delay with qWait() function like so:

from PyQt4 import QtTest

def playSheetMusic(self, Piano):
        sheetmusic = ["A5", "G4", "C4", "C4", "C4"]
        note1 = sheetmusic[0]
        file1 = "pianokeys/" + note1 +".wav"
        note2 = sheetmusic[1]
        file2 = "pianokeys/" + note2 +".wav"
        QSound.play(file1)
        QtTest.QTest.qWait(1000)        # put however many milliseconds delay you want
        QSound.play(file2)

Hope this helps :)