fMRI experiment with sound and rest

Hello everyone, I am trying to code an fMRI experiment that including an fMRI trigger codes for a loop of sound - rest - sound - rest. This is what I have so far, but I am very new to coding so I am basically not sure how to add the rest times to the loop (which now consists of sound only). Any help would be highly appreciated.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
This demo illustrates using hardware.emulator.launchScan() to either start a
real scan, or emulate sync pulses. Emulation is to allow debugging script timing
offline, without requiring a scanner (or a hardware sync pulse generator).
"""

# Author: Jeremy R. Gray

from psychopy import sound, visual, event, core, gui
from psychopy.hardware.emulator import launchScan
from psychopy.constants import STARTED, PLAYING

# settings for launchScan:
MR_settings = {
    'TR': 2.000,     # duration (sec) per whole-brain volume
    'volumes': 5,    # number of whole-brain 3D volumes per scanning run
    'sync': 'slash', # character to use as the sync timing event; assumed to come at start of a volume
    'skip': 0,       # number of volumes lacking a sync pulse at start of scan (for T1 stabilization)
    'sound': True    # in test mode: play a tone as a reminder of scanner noise
    }
infoDlg = gui.DlgFromDict(MR_settings, title='fMRI parameters', order=['TR', 'volumes'])
if not infoDlg.OK:
    core.quit()

win = visual.Window(fullscr=False)
globalClock = core.Clock()

# summary of run timing, for each key press:
output = u'vol    onset key\n'
for i in range(-1 * MR_settings['skip'], 0):
    output += u'%d prescan skip (no sync)\n' % i

counter = visual.TextStim(win, height=.05, pos=(0, 0), color=win.rgb + 0.5)
output += u"  0    0.000 sync  [Start of scanning run, vol 0]\n"

# launch: operator selects Scan or Test (emulate); see API documentation
vol = launchScan(win, MR_settings, globalClock=globalClock)
counter.setText(u"%d volumes\n%.3f seconds" % (0, 0.0))
counter.draw()
win.flip()

duration = MR_settings['volumes'] * MR_settings['TR']
# note: globalClock has been reset to 0.0 by launchScan()
while globalClock.getTime() < duration:
    allKeys = event.getKeys()
    for key in allKeys:
        if key == MR_settings['sync']:
            onset = globalClock.getTime()
            # do your experiment code at this point if you want it sync'd to the TR
            def play_sound(timing):
                mySound = sound.Sound('/Users/a1/Downloads/Sound', secs=-1, stereo=True, hamming=True)
                duration = core.CountdownTimer(timing)
                duration.reset()
                while duration.getTime() > 0:
                    if mySound.status not in [STARTED, PLAYING]:
                        mySound.play()
                    win.flip()
                mySound.stop()
            
            play_sound(10)
            
        else:
            # handle keys (many fiber-optic buttons become key-board key-presses)
            output += u"%3d  %7.3f %s\n" % (vol-1, globalClock.getTime(), str(key))
            if key == 'escape':
                output += u'user cancel, '
                break

t = globalClock.getTime()
win.flip()

output += u"End of scan (vol 0..%d = %d of %s). Total duration = %7.3f sec" % (vol - 1, vol, MR_settings['volumes'], t)
print(output)

win.close()
core.quit()

# The contents of this file are in the public domain.

Hello

a simple way is to copy&paste the routine and set the volume to 0.

Best wishes Jens

This is an interesting idea!! Thanks :slight_smile: