PsychoPy在脑电图实验中播放不规则延迟的声音

时间:2018-06-08 14:30:09

标签: python real-time delay eventtrigger psychopy

我正在使用PsychoPy准备一个奇怪的范例,我在屏幕上预设了7秒的图片,在图片演示期间,每隔半秒就会显示14个连续的声音。对于每个声音开始,我向并行端口发送一个触发器。然后,我进行了计时测试,看看声音是否与触发器同时播放。

虽然触发器看起来很好并且每半秒钟到达一次,但声音并非如此。在触发后延迟100ms-200ms播放声音,连续声音的开启之间的差异不是半秒。声音文件的大小没有区别,延迟似乎是非常短的声音(10毫秒)和较长的声音(250毫秒)的情况。我尝试在每次试验的最初阶段添加ISI并在此期间加载图片和声音,但它没有帮助。

我还尝试在每个声音之前添加ISI并在这些短片ISI中加载声音,但它也没有帮助。你知道如何解决这个问题吗? ISI有什么办法可以提供帮助吗?我是否应该在试验开始时有一个ISI来加载该试验的所有刺激或每次试验的多个ISI,每个刺激一个?

视觉刺激(即图片)的延迟很小且在试验中保持不变。

下面的代码只播放两个声音,但很好地证明了这个问题。

from __future__ import absolute_import, division
from psychopy import locale_setup, sound, gui, visual, core, data, event, logging, parallel
from psychopy.constants import (NOT_STARTED, STARTED, PLAYING, PAUSED,
                                STOPPED, FINISHED, PRESSED, RELEASED, FOREVER)

# Setup the Window
win = visual.Window(
    size=(1920, 1080), fullscr=True, screen=0,
    allowGUI=True, allowStencil=False,
    monitor='testMonitor', color=[-1, -1, -1], colorSpace='rgb',
    blendMode='avg', useFBO=True)
snd_2_tr = 'sound1.wav'
snd_3_tr = 'sound2.wav'
picture_tr = 'pic.jpg'
win.setRecordFrameIntervals(True)
win._refreshThreshold = 1 / 60.0 + 0.004
image_tr = visual.ImageStim(
    win=win, name='image_tr',
    image='sin', mask=None,
    ori=0, pos=(0, 0), size=None,
    color=[1,1,1], colorSpace='rgb', opacity=1,
    flipHoriz=False, flipVert=False,
    texRes=128, interpolate=True, depth=0.0)
sound_2_tr = sound.Sound('A', secs=-1)
sound_2_tr.setVolume(1)
sound_3_tr = sound.Sound('A', secs=-1)
sound_3_tr.setVolume(1)
image_port_tr = parallel.ParallelPort(address='0xD010')
sound_2_port_tr = parallel.ParallelPort(address='0xD010')
sound_3_port_tr = parallel.ParallelPort(address='0xD010')
ISI_17 = core.StaticPeriod(win=win, screenHz=60, name='ISI_17')

# ------Prepare to start Routine "main_tr"-------
main_trClock = core.Clock()
t = 0
main_trClock.reset()  # clock
frameN = -1
continueRoutine = True
# keep track of which components have finished
main_trComponents = [image_tr, sound_2_tr, sound_3_tr, image_port_tr, sound_2_port_tr, sound_3_port_tr, ISI_17]
for thisComponent in main_trComponents:
    if hasattr(thisComponent, 'status'):
        thisComponent.status = NOT_STARTED

# -------Start Routine "main_tr"-------
while continueRoutine:
    # get current time
    t = main_trClock.getTime()
    frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
    # update/draw components on each frame

    # *image_tr* updates
    if frameN >= 60 and image_tr.status == NOT_STARTED:
        # keep track of start time/frame for later
        image_tr.tStart = t
        image_tr.frameNStart = frameN  # exact frame index
        image_tr.setAutoDraw(True)
    if image_tr.status == STARTED and frameN >= (image_tr.frameNStart + 420):
        image_tr.setAutoDraw(False)

    # start/stop sound_2_tr
    if frameN >= 90 and sound_2_tr.status == NOT_STARTED:
        # keep track of start time/frame for later
        sound_2_tr.tStart = t
        sound_2_tr.frameNStart = frameN  # exact frame index
        sound_2_tr.play()  # start the sound (it finishes automatically)
    if sound_2_tr.status == STARTED and t >= (sound_2_tr.tStart + 0.25):
        sound_2_tr.stop()  # stop the sound (if longer than duration)
    # start/stop sound_3_tr
    if frameN >= 120 and sound_3_tr.status == NOT_STARTED:
        # keep track of start time/frame for later
        sound_3_tr.tStart = t
        sound_3_tr.frameNStart = frameN  # exact frame index
        sound_3_tr.play()  # start the sound (it finishes automatically)
    if sound_3_tr.status == STARTED and t >= (sound_3_tr.tStart + 0.25):
        sound_3_tr.stop()  # stop the sound (if longer than duration)
    # *sound_1_port_tr* updates
    if frameN >= 60 and image_port_tr.status == NOT_STARTED:
        # keep track of start time/frame for later
        image_port_tr.tStart = t
        image_port_tr.frameNStart = frameN  # exact frame index
        image_port_tr.status = STARTED
        win.callOnFlip(image_port_tr.setData, int(triggers_image_tr))
    if image_port_tr.status == STARTED and frameN >= (image_port_tr.frameNStart + 15):
        image_port_tr.status = STOPPED
        win.callOnFlip(image_port_tr.setData, int(0))
    # *sound_2_port_tr* updates
    if frameN >= 90 and sound_2_port_tr.status == NOT_STARTED:
        # keep track of start time/frame for later
        sound_2_port_tr.tStart = t
        sound_2_port_tr.frameNStart = frameN  # exact frame index
        sound_2_port_tr.status = STARTED
        win.callOnFlip(sound_2_port_tr.setData, int(triggers_sound_2_tr))
    if sound_2_port_tr.status == STARTED and frameN >= (sound_2_port_tr.frameNStart + 15):
        sound_2_port_tr.status = STOPPED
        win.callOnFlip(sound_2_port_tr.setData, int(0))
    # *sound_3_port_tr* updates
    if frameN >= 120 and sound_3_port_tr.status == NOT_STARTED:
        # keep track of start time/frame for later
        sound_3_port_tr.tStart = t
        sound_3_port_tr.frameNStart = frameN  # exact frame index
        sound_3_port_tr.status = STARTED
        win.callOnFlip(sound_3_port_tr.setData, int(triggers_sound_3_tr))
    if sound_3_port_tr.status == STARTED and frameN >= (sound_3_port_tr.frameNStart + 15):
        sound_3_port_tr.status = STOPPED
        win.callOnFlip(sound_3_port_tr.setData, int(0))
    # *ISI_17* period
    if frameN >= 0 and ISI_17.status == NOT_STARTED:
        # keep track of start time/frame for later
        ISI_17.tStart = t
        ISI_17.frameNStart = frameN  # exact frame index
        ISI_17.start(60 * frameDur)
    elif ISI_17.status == STARTED:  # one frame should pass before updating params and completing
        # updating other components during *ISI_17*
        image_tr.setImage(picture_tr)
        sound_2_tr.setSound(snd_2_tr, secs=0.25)
        sound_3_tr.setSound(snd_3_tr, secs=0.25)
        # component updates done
        ISI_17.complete()  # finish the static period

    # check if all components have finished
    if not continueRoutine:  # a component has requested a forced-end of Routine
        break
    continueRoutine = False  # will revert to True if at least one component still running
    for thisComponent in main_trComponents:
        if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
            continueRoutine = True
            break  # at least one component has not yet finished

    # check for quit (the Esc key)
    if endExpNow or event.getKeys(keyList=["escape"]):
        core.quit()

    # refresh the screen
    if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
        win.flip()

0 个答案:

没有答案