How to send a sound and trigger at the same time over a task block?

I am trying to create an experiment where particiapnts are shown a baseline and then have a 10s task block. During this task block, I want them to tap their finger at 50bpm for 10s. To do this, I want a metronome sound (a tick sound which will be repeated at 50bpm) so that they can tap their finger to the rhyhtm.

Whilst having triggers which mark the beginning of the entire task block, I also want triggers to be sent at each tick sound so that I can align their neuroimaging data (which is being concurrently recorded) with the tapping. I have the code below so far but Iā€™m not quite sure how to work the looping over 10 seconds with the bpm and would appreciate any help in donig so! I would also appreciate any suggestions as to simplifications as I am aware that it is a big block of code which may impact the accuracy of expeirmental timings. Thank you!

frequency = 50

            trigger_sent = False
            next_flip = self.__win.getFutureFlipTime(clock='ptb')
            now = ptb.GetSecs()

            while self.__clock.getTime() < duration:
                if not trigger_sent:
                    self.__win.callOnFlip(self.__port.write, trigger.encode())
                    trigger_sent = True
                if ptb.getSecs() = now + (60/frequency):
                    now = ptb.GetSecs()

1 Like