#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This experiment was created using PsychoPy3 Experiment Builder (v2020.1.3),
    on Сентябрь 21, 2020, at 16:47
If you publish work using this script the most relevant publication is:

    Peirce J, Gray JR, Simpson S, MacAskill M, Höchenberger R, Sogo H, Kastman E, Lindeløv JK. (2019) 
        PsychoPy2: Experiments in behavior made easy Behav Res 51: 195. 
        https://doi.org/10.3758/s13428-018-01193-y

"""

from __future__ import absolute_import, division

from psychopy import locale_setup
from psychopy import prefs
prefs.hardware['audioLib'] = 'pyo'
prefs.hardware['audioLatencyMode'] = '3'
from psychopy import sound, gui, visual, core, data, event, logging, clock, microphone
from psychopy.constants import (NOT_STARTED, STARTED, PLAYING, PAUSED,
                                STOPPED, FINISHED, PRESSED, RELEASED, FOREVER)

import numpy as np  # whole numpy lib is available, prepend 'np.'
from numpy import (sin, cos, tan, log, log10, pi, average,
                   sqrt, std, deg2rad, rad2deg, linspace, asarray)
from numpy.random import random, randint, normal, shuffle
import os  # handy system and path functions
import sys  # to get file system encoding

from psychopy.hardware import keyboard



# Ensure that relative paths start from the same directory as this script
_thisDir = os.path.dirname(os.path.abspath(__file__))
os.chdir(_thisDir)

# Store info about the experiment session
psychopyVersion = '2020.1.3'
expName = 'peavler_training'  # from the Builder filename that created this script
expInfo = {'participant': '', 'session': 'training'}
dlg = gui.DlgFromDict(dictionary=expInfo, sortKeys=False, title=expName)
if dlg.OK == False:
    core.quit()  # user pressed cancel
expInfo['date'] = data.getDateStr()  # add a simple timestamp
expInfo['expName'] = expName
expInfo['psychopyVersion'] = psychopyVersion

# Data file name stem = absolute path + name; later add .psyexp, .csv, .log, etc
filename = _thisDir + os.sep + u'data/%s_%s_%s' % (expInfo['participant'], expName, expInfo['date'])

# An ExperimentHandler isn't essential but helps with data saving
thisExp = data.ExperimentHandler(name=expName, version='',
    extraInfo=expInfo, runtimeInfo=None,
    originPath='D:\\GoogleDisk\\Neurotech_lab\\cogLoad_project\\scripts\\peavlerRepl\\v15\\peavler_training_lastrun.py',
    savePickle=True, saveWideText=True,
    dataFileName=filename)
# save a log file for detail verbose info
logFile = logging.LogFile(filename+'.log', level=logging.EXP)
logging.console.setLevel(logging.WARNING)  # this outputs to the screen, not a file

endExpNow = False  # flag for 'escape' or other condition => quit the exp
frameTolerance = 0.001  # how close to onset before 'same' frame

# Start Code - component code to be run before the window creation
wavDirName = filename + '_wav'
if not os.path.isdir(wavDirName):
    os.makedirs(wavDirName)  # to hold .wav files

# Setup the Window
win = visual.Window(
    size=[1920, 1080], fullscr=True, screen=0, 
    winType='pyglet', allowGUI=False, allowStencil=False,
    monitor='testMonitor', color=[0, 0, 0], colorSpace='rgb',
    blendMode='avg', useFBO=True, 
    units='height')

# Enable sound input/output:
microphone.switchOn()
# store frame rate of monitor if we can measure it
expInfo['frameRate'] = win.getActualFrameRate()
if expInfo['frameRate'] != None:
    frameDur = 1.0 / round(expInfo['frameRate'])
else:
    frameDur = 1.0 / 60.0  # could not measure, so guess

# create a default keyboard (e.g. to check for escape)
defaultKeyboard = keyboard.Keyboard()

# Initialize components for Routine "randomizer"
randomizerClock = core.Clock()
from numpy.random import choice
import random
from numpy import arange, concatenate

def randomDigits(numOfReps):
    if numOfReps<=10:
        return choice(10, size=numOfReps, replace=False)
    else:
        arr1 = list(choice(10, size=10, replace=False))
        arr2 = list(choice(10, size=numOfReps-10, replace=False))
        while arr2[0] == arr1[-1]:
            random.shuffle(arr2)
        return arr1+arr2

# Initialize components for Routine "instruction"
instructionClock = core.Clock()
instruction_text = visual.TextStim(win=win, name='instruction_text',
    text='default text',
    font='Arial',
    pos=(0, 0), height=0.056, wrapWidth=None, ori=0, 
    color='white', colorSpace='rgb', opacity=1, 
    languageStyle='LTR',
    depth=0.0);
start_sound = sound.Sound('A', secs=-1, stereo=True, hamming=True,
    name='start_sound')
start_sound.setVolume(1)
exclamationMark = visual.TextStim(win=win, name='exclamationMark',
    text='!',
    font='Arial',
    pos=(0, 0), height=0.076, wrapWidth=None, ori=0, 
    color=[1,1,1], colorSpace='rgb', opacity=1, 
    languageStyle='LTR',
    depth=-2.0);

# Initialize components for Routine "baseline_2s"
baseline_2sClock = core.Clock()
fixCross = visual.ImageStim(
    win=win,
    name='fixCross', 
    image='num/FixCross.png', 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)

# Initialize components for Routine "digits"
digitsClock = core.Clock()
sound_digit = sound.Sound('A', secs=-1, stereo=True, hamming=True,
    name='sound_digit')
sound_digit.setVolume(1)
FixCross_answ = visual.ImageStim(
    win=win,
    name='FixCross_answ', 
    image='num/FixCross.png', 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=-1.0)

# Initialize components for Routine "retention_1s"
retention_1sClock = core.Clock()
fixCross_1s = visual.ImageStim(
    win=win,
    name='fixCross_1s', 
    image='num/FixCross.png', 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)

# Initialize components for Routine "repeat"
repeatClock = core.Clock()
mic_image = visual.ImageStim(
    win=win,
    name='mic_image', 
    image='num/mic_white.png', mask=None,
    ori=0, pos=(0, 0), size=(0.67, 0.36),
    color=[1,1,1], colorSpace='rgb', opacity=1,
    flipHoriz=False, flipVert=False,
    texRes=128, interpolate=True, depth=-2.0)

# Initialize components for Routine "end_block"
end_blockClock = core.Clock()
end = sound.Sound('A', secs=-1, stereo=True, hamming=True,
    name='end')
end.setVolume(1)
FixCross_end = visual.ImageStim(
    win=win,
    name='FixCross_end', 
    image='num/FixCross.png', mask=None,
    ori=0, pos=(0, 0), size=None,
    color=[1,1,1], colorSpace='rgb', opacity=0,
    flipHoriz=False, flipVert=False,
    texRes=128, interpolate=True, depth=-1.0)

# Initialize components for Routine "rest"
restClock = core.Clock()
rest_instruction = visual.TextStim(win=win, name='rest_instruction',
    text='default text',
    font='Arial',
    pos=(0, 0), height=0.056, wrapWidth=None, ori=0, 
    color='white', colorSpace='rgb', opacity=1, 
    languageStyle='LTR',
    depth=0.0);
key_resp_2 = keyboard.Keyboard()

# Create some handy timers
globalClock = core.Clock()  # to track the time since experiment started
routineTimer = core.CountdownTimer()  # to track time remaining of each (non-slip) routine 

# ------Prepare to start Routine "randomizer"-------
continueRoutine = True
# update component parameters for each repeat
# keep track of which components have finished
randomizerComponents = []
for thisComponent in randomizerComponents:
    thisComponent.tStart = None
    thisComponent.tStop = None
    thisComponent.tStartRefresh = None
    thisComponent.tStopRefresh = None
    if hasattr(thisComponent, 'status'):
        thisComponent.status = NOT_STARTED
# reset timers
t = 0
_timeToFirstFrame = win.getFutureFlipTime(clock="now")
randomizerClock.reset(-_timeToFirstFrame)  # t0 is time of first possible flip
frameN = -1

# -------Run Routine "randomizer"-------
while continueRoutine:
    # get current time
    t = randomizerClock.getTime()
    tThisFlip = win.getFutureFlipTime(clock=randomizerClock)
    tThisFlipGlobal = win.getFutureFlipTime(clock=None)
    frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
    # update/draw components on each frame
    
    # check for quit (typically the Esc key)
    if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
        core.quit()
    
    # 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 randomizerComponents:
        if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
            continueRoutine = True
            break  # at least one component has not yet finished
    
    # refresh the screen
    if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
        win.flip()

# -------Ending Routine "randomizer"-------
for thisComponent in randomizerComponents:
    if hasattr(thisComponent, "setAutoDraw"):
        thisComponent.setAutoDraw(False)
# the Routine "randomizer" was not non-slip safe, so reset the non-slip timer
routineTimer.reset()

# set up handler to look after randomisation of conditions etc
trials = data.TrialHandler(nReps=1, method='random', 
    extraInfo=expInfo, originPath=-1,
    trialList=data.importConditions('trainingTrials_'+str(int(expInfo['participant'])%2)+'.xlsx'),
    seed=None, name='trials')
thisExp.addLoop(trials)  # add the loop to the experiment
thisTrial = trials.trialList[0]  # so we can initialise stimuli with some values
# abbreviate parameter names if possible (e.g. rgb = thisTrial.rgb)
if thisTrial != None:
    for paramName in thisTrial:
        exec('{} = thisTrial[paramName]'.format(paramName))

for thisTrial in trials:
    currentLoop = trials
    # abbreviate parameter names if possible (e.g. rgb = thisTrial.rgb)
    if thisTrial != None:
        for paramName in thisTrial:
            exec('{} = thisTrial[paramName]'.format(paramName))
    
    # ------Prepare to start Routine "instruction"-------
    continueRoutine = True
    # update component parameters for each repeat
    instruction_text.setColor(instrCol, colorSpace='rgb')
    instruction_text.setText(inst)
    start_sound.setSound('num/start.wav', hamming=True)
    start_sound.setVolume(1, log=False)
    # keep track of which components have finished
    instructionComponents = [instruction_text, start_sound, exclamationMark]
    for thisComponent in instructionComponents:
        thisComponent.tStart = None
        thisComponent.tStop = None
        thisComponent.tStartRefresh = None
        thisComponent.tStopRefresh = None
        if hasattr(thisComponent, 'status'):
            thisComponent.status = NOT_STARTED
    # reset timers
    t = 0
    _timeToFirstFrame = win.getFutureFlipTime(clock="now")
    instructionClock.reset(-_timeToFirstFrame)  # t0 is time of first possible flip
    frameN = -1
    
    # -------Run Routine "instruction"-------
    while continueRoutine:
        # get current time
        t = instructionClock.getTime()
        tThisFlip = win.getFutureFlipTime(clock=instructionClock)
        tThisFlipGlobal = win.getFutureFlipTime(clock=None)
        frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
        # update/draw components on each frame
        
        # *instruction_text* updates
        if instruction_text.status == NOT_STARTED and tThisFlip >= 0.5-frameTolerance:
            # keep track of start time/frame for later
            instruction_text.frameNStart = frameN  # exact frame index
            instruction_text.tStart = t  # local t and not account for scr refresh
            instruction_text.tStartRefresh = tThisFlipGlobal  # on global time
            win.timeOnFlip(instruction_text, 'tStartRefresh')  # time at next scr refresh
            instruction_text.setAutoDraw(True)
        if instruction_text.status == STARTED:
            # is it time to stop? (based on global clock, using actual start)
            if tThisFlipGlobal > instruction_text.tStartRefresh + 1-frameTolerance:
                # keep track of stop time/frame for later
                instruction_text.tStop = t  # not accounting for scr refresh
                instruction_text.frameNStop = frameN  # exact frame index
                win.timeOnFlip(instruction_text, 'tStopRefresh')  # time at next scr refresh
                instruction_text.setAutoDraw(False)
        # start/stop start_sound
        if start_sound.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
            # keep track of start time/frame for later
            start_sound.frameNStart = frameN  # exact frame index
            start_sound.tStart = t  # local t and not account for scr refresh
            start_sound.tStartRefresh = tThisFlipGlobal  # on global time
            start_sound.play(when=win)  # sync with win flip
        
        # *exclamationMark* updates
        if exclamationMark.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
            # keep track of start time/frame for later
            exclamationMark.frameNStart = frameN  # exact frame index
            exclamationMark.tStart = t  # local t and not account for scr refresh
            exclamationMark.tStartRefresh = tThisFlipGlobal  # on global time
            win.timeOnFlip(exclamationMark, 'tStartRefresh')  # time at next scr refresh
            exclamationMark.setAutoDraw(True)
        if exclamationMark.status == STARTED:
            # is it time to stop? (based on global clock, using actual start)
            if tThisFlipGlobal > exclamationMark.tStartRefresh + 0.5-frameTolerance:
                # keep track of stop time/frame for later
                exclamationMark.tStop = t  # not accounting for scr refresh
                exclamationMark.frameNStop = frameN  # exact frame index
                win.timeOnFlip(exclamationMark, 'tStopRefresh')  # time at next scr refresh
                exclamationMark.setAutoDraw(False)
        
        # check for quit (typically the Esc key)
        if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
            core.quit()
        
        # 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 instructionComponents:
            if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
                continueRoutine = True
                break  # at least one component has not yet finished
        
        # refresh the screen
        if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
            win.flip()
    
    # -------Ending Routine "instruction"-------
    for thisComponent in instructionComponents:
        if hasattr(thisComponent, "setAutoDraw"):
            thisComponent.setAutoDraw(False)
    trials.addData('instruction_text.started', instruction_text.tStartRefresh)
    trials.addData('instruction_text.stopped', instruction_text.tStopRefresh)
    start_sound.stop()  # ensure sound has stopped at end of routine
    trials.addData('start_sound.started', start_sound.tStartRefresh)
    trials.addData('start_sound.stopped', start_sound.tStopRefresh)
    trials.addData('exclamationMark.started', exclamationMark.tStartRefresh)
    trials.addData('exclamationMark.stopped', exclamationMark.tStopRefresh)
    # the Routine "instruction" was not non-slip safe, so reset the non-slip timer
    routineTimer.reset()
    
    # ------Prepare to start Routine "baseline_2s"-------
    continueRoutine = True
    routineTimer.add(3.000000)
    # update component parameters for each repeat
    # keep track of which components have finished
    baseline_2sComponents = [fixCross]
    for thisComponent in baseline_2sComponents:
        thisComponent.tStart = None
        thisComponent.tStop = None
        thisComponent.tStartRefresh = None
        thisComponent.tStopRefresh = None
        if hasattr(thisComponent, 'status'):
            thisComponent.status = NOT_STARTED
    # reset timers
    t = 0
    _timeToFirstFrame = win.getFutureFlipTime(clock="now")
    baseline_2sClock.reset(-_timeToFirstFrame)  # t0 is time of first possible flip
    frameN = -1
    
    # -------Run Routine "baseline_2s"-------
    while continueRoutine and routineTimer.getTime() > 0:
        # get current time
        t = baseline_2sClock.getTime()
        tThisFlip = win.getFutureFlipTime(clock=baseline_2sClock)
        tThisFlipGlobal = win.getFutureFlipTime(clock=None)
        frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
        # update/draw components on each frame
        
        # *fixCross* updates
        if fixCross.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
            # keep track of start time/frame for later
            fixCross.frameNStart = frameN  # exact frame index
            fixCross.tStart = t  # local t and not account for scr refresh
            fixCross.tStartRefresh = tThisFlipGlobal  # on global time
            win.timeOnFlip(fixCross, 'tStartRefresh')  # time at next scr refresh
            fixCross.setAutoDraw(True)
        if fixCross.status == STARTED:
            # is it time to stop? (based on global clock, using actual start)
            if tThisFlipGlobal > fixCross.tStartRefresh + 3-frameTolerance:
                # keep track of stop time/frame for later
                fixCross.tStop = t  # not accounting for scr refresh
                fixCross.frameNStop = frameN  # exact frame index
                win.timeOnFlip(fixCross, 'tStopRefresh')  # time at next scr refresh
                fixCross.setAutoDraw(False)
        
        # check for quit (typically the Esc key)
        if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
            core.quit()
        
        # 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 baseline_2sComponents:
            if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
                continueRoutine = True
                break  # at least one component has not yet finished
        
        # refresh the screen
        if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
            win.flip()
    
    # -------Ending Routine "baseline_2s"-------
    for thisComponent in baseline_2sComponents:
        if hasattr(thisComponent, "setAutoDraw"):
            thisComponent.setAutoDraw(False)
    trials.addData('fixCross.started', fixCross.tStartRefresh)
    trials.addData('fixCross.stopped', fixCross.tStopRefresh)
    
    # set up handler to look after randomisation of conditions etc
    present_sounds = data.TrialHandler(nReps=1, method='sequential', 
        extraInfo=expInfo, originPath=-1,
        trialList=data.importConditions('sounds.xlsx', selection=randomDigits(numOfReps)),
        seed=None, name='present_sounds')
    thisExp.addLoop(present_sounds)  # add the loop to the experiment
    thisPresent_sound = present_sounds.trialList[0]  # so we can initialise stimuli with some values
    # abbreviate parameter names if possible (e.g. rgb = thisPresent_sound.rgb)
    if thisPresent_sound != None:
        for paramName in thisPresent_sound:
            exec('{} = thisPresent_sound[paramName]'.format(paramName))
    
    for thisPresent_sound in present_sounds:
        currentLoop = present_sounds
        # abbreviate parameter names if possible (e.g. rgb = thisPresent_sound.rgb)
        if thisPresent_sound != None:
            for paramName in thisPresent_sound:
                exec('{} = thisPresent_sound[paramName]'.format(paramName))
        
        # ------Prepare to start Routine "digits"-------
        continueRoutine = True
        # update component parameters for each repeat
        sound_digit.setSound(sounds, hamming=True)
        sound_digit.setVolume(1, log=False)
        # keep track of which components have finished
        digitsComponents = [sound_digit, FixCross_answ]
        for thisComponent in digitsComponents:
            thisComponent.tStart = None
            thisComponent.tStop = None
            thisComponent.tStartRefresh = None
            thisComponent.tStopRefresh = None
            if hasattr(thisComponent, 'status'):
                thisComponent.status = NOT_STARTED
        # reset timers
        t = 0
        _timeToFirstFrame = win.getFutureFlipTime(clock="now")
        digitsClock.reset(-_timeToFirstFrame)  # t0 is time of first possible flip
        frameN = -1
        
        # -------Run Routine "digits"-------
        while continueRoutine:
            # get current time
            t = digitsClock.getTime()
            tThisFlip = win.getFutureFlipTime(clock=digitsClock)
            tThisFlipGlobal = win.getFutureFlipTime(clock=None)
            frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
            # update/draw components on each frame
            # start/stop sound_digit
            if sound_digit.status == NOT_STARTED and t >= 0.0-frameTolerance:
                # keep track of start time/frame for later
                sound_digit.frameNStart = frameN  # exact frame index
                sound_digit.tStart = t  # local t and not account for scr refresh
                sound_digit.tStartRefresh = tThisFlipGlobal  # on global time
                sound_digit.play()  # start the sound (it finishes automatically)
            
            # *FixCross_answ* updates
            if FixCross_answ.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
                # keep track of start time/frame for later
                FixCross_answ.frameNStart = frameN  # exact frame index
                FixCross_answ.tStart = t  # local t and not account for scr refresh
                FixCross_answ.tStartRefresh = tThisFlipGlobal  # on global time
                win.timeOnFlip(FixCross_answ, 'tStartRefresh')  # time at next scr refresh
                FixCross_answ.setAutoDraw(True)
            if FixCross_answ.status == STARTED:
                # is it time to stop? (based on global clock, using actual start)
                if tThisFlipGlobal > FixCross_answ.tStartRefresh + 2.0-frameTolerance:
                    # keep track of stop time/frame for later
                    FixCross_answ.tStop = t  # not accounting for scr refresh
                    FixCross_answ.frameNStop = frameN  # exact frame index
                    win.timeOnFlip(FixCross_answ, 'tStopRefresh')  # time at next scr refresh
                    FixCross_answ.setAutoDraw(False)
            
            # check for quit (typically the Esc key)
            if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
                core.quit()
            
            # 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 digitsComponents:
                if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
                    continueRoutine = True
                    break  # at least one component has not yet finished
            
            # refresh the screen
            if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
                win.flip()
        
        # -------Ending Routine "digits"-------
        for thisComponent in digitsComponents:
            if hasattr(thisComponent, "setAutoDraw"):
                thisComponent.setAutoDraw(False)
        sound_digit.stop()  # ensure sound has stopped at end of routine
        present_sounds.addData('sound_digit.started', sound_digit.tStart)
        present_sounds.addData('sound_digit.stopped', sound_digit.tStop)
        present_sounds.addData('FixCross_answ.started', FixCross_answ.tStartRefresh)
        present_sounds.addData('FixCross_answ.stopped', FixCross_answ.tStopRefresh)
        # the Routine "digits" was not non-slip safe, so reset the non-slip timer
        routineTimer.reset()
        thisExp.nextEntry()
        
    # completed 1 repeats of 'present_sounds'
    
    
    # ------Prepare to start Routine "retention_1s"-------
    continueRoutine = True
    routineTimer.add(1.000000)
    # update component parameters for each repeat
    # keep track of which components have finished
    retention_1sComponents = [fixCross_1s]
    for thisComponent in retention_1sComponents:
        thisComponent.tStart = None
        thisComponent.tStop = None
        thisComponent.tStartRefresh = None
        thisComponent.tStopRefresh = None
        if hasattr(thisComponent, 'status'):
            thisComponent.status = NOT_STARTED
    # reset timers
    t = 0
    _timeToFirstFrame = win.getFutureFlipTime(clock="now")
    retention_1sClock.reset(-_timeToFirstFrame)  # t0 is time of first possible flip
    frameN = -1
    
    # -------Run Routine "retention_1s"-------
    while continueRoutine and routineTimer.getTime() > 0:
        # get current time
        t = retention_1sClock.getTime()
        tThisFlip = win.getFutureFlipTime(clock=retention_1sClock)
        tThisFlipGlobal = win.getFutureFlipTime(clock=None)
        frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
        # update/draw components on each frame
        
        # *fixCross_1s* updates
        if fixCross_1s.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
            # keep track of start time/frame for later
            fixCross_1s.frameNStart = frameN  # exact frame index
            fixCross_1s.tStart = t  # local t and not account for scr refresh
            fixCross_1s.tStartRefresh = tThisFlipGlobal  # on global time
            win.timeOnFlip(fixCross_1s, 'tStartRefresh')  # time at next scr refresh
            fixCross_1s.setAutoDraw(True)
        if fixCross_1s.status == STARTED:
            # is it time to stop? (based on global clock, using actual start)
            if tThisFlipGlobal > fixCross_1s.tStartRefresh + 1.0-frameTolerance:
                # keep track of stop time/frame for later
                fixCross_1s.tStop = t  # not accounting for scr refresh
                fixCross_1s.frameNStop = frameN  # exact frame index
                win.timeOnFlip(fixCross_1s, 'tStopRefresh')  # time at next scr refresh
                fixCross_1s.setAutoDraw(False)
        
        # check for quit (typically the Esc key)
        if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
            core.quit()
        
        # 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 retention_1sComponents:
            if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
                continueRoutine = True
                break  # at least one component has not yet finished
        
        # refresh the screen
        if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
            win.flip()
    
    # -------Ending Routine "retention_1s"-------
    for thisComponent in retention_1sComponents:
        if hasattr(thisComponent, "setAutoDraw"):
            thisComponent.setAutoDraw(False)
    trials.addData('fixCross_1s.started', fixCross_1s.tStartRefresh)
    trials.addData('fixCross_1s.stopped', fixCross_1s.tStopRefresh)
    
    # ------Prepare to start Routine "repeat"-------
    continueRoutine = True
    # update component parameters for each repeat
    if skip_mic==1:
        continueRoutine = False
    mic_1 = microphone.AdvAudioCapture(name='mic_1', saveDir=wavDirName, stereo=False, chnl=0)
    # keep track of which components have finished
    repeatComponents = [mic_1, mic_image]
    for thisComponent in repeatComponents:
        thisComponent.tStart = None
        thisComponent.tStop = None
        thisComponent.tStartRefresh = None
        thisComponent.tStopRefresh = None
        if hasattr(thisComponent, 'status'):
            thisComponent.status = NOT_STARTED
    # reset timers
    t = 0
    _timeToFirstFrame = win.getFutureFlipTime(clock="now")
    repeatClock.reset(-_timeToFirstFrame)  # t0 is time of first possible flip
    frameN = -1
    
    # -------Run Routine "repeat"-------
    while continueRoutine:
        # get current time
        t = repeatClock.getTime()
        tThisFlip = win.getFutureFlipTime(clock=repeatClock)
        tThisFlipGlobal = win.getFutureFlipTime(clock=None)
        frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
        # update/draw components on each frame
        
        # *mic_1* updates
        if mic_1.status == NOT_STARTED and t >= 0-frameTolerance:
            # keep track of start time/frame for later
            mic_1.frameNStart = frameN  # exact frame index
            mic_1.tStart = t  # local t and not account for scr refresh
            mic_1.tStartRefresh = tThisFlipGlobal  # on global time
            win.timeOnFlip(mic_1, 'tStartRefresh')  # time at next scr refresh
            mic_1.status = STARTED
            mic_1.record(sec=micDuration, block=False)  # start the recording thread
        
        if mic_1.status == STARTED and not mic_1.recorder.running:
            mic_1.status = FINISHED
        
        # *mic_image* updates
        if mic_image.status == NOT_STARTED and tThisFlip >= 0-frameTolerance:
            # keep track of start time/frame for later
            mic_image.frameNStart = frameN  # exact frame index
            mic_image.tStart = t  # local t and not account for scr refresh
            mic_image.tStartRefresh = tThisFlipGlobal  # on global time
            win.timeOnFlip(mic_image, 'tStartRefresh')  # time at next scr refresh
            mic_image.setAutoDraw(True)
        if mic_image.status == STARTED:
            # is it time to stop? (based on global clock, using actual start)
            if tThisFlipGlobal > mic_image.tStartRefresh + micDuration-frameTolerance:
                # keep track of stop time/frame for later
                mic_image.tStop = t  # not accounting for scr refresh
                mic_image.frameNStop = frameN  # exact frame index
                win.timeOnFlip(mic_image, 'tStopRefresh')  # time at next scr refresh
                mic_image.setAutoDraw(False)
        
        # check for quit (typically the Esc key)
        if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
            core.quit()
        
        # 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 repeatComponents:
            if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
                continueRoutine = True
                break  # at least one component has not yet finished
        
        # refresh the screen
        if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
            win.flip()
    
    # -------Ending Routine "repeat"-------
    for thisComponent in repeatComponents:
        if hasattr(thisComponent, "setAutoDraw"):
            thisComponent.setAutoDraw(False)
    # mic_1 stop & responses
    mic_1.stop()  # sometimes helpful
    if not mic_1.savedFile:
        mic_1.savedFile = None
    # store data for trials (TrialHandler)
    trials.addData('mic_1.filename', mic_1.savedFile)
    trials.addData('mic_1.started', mic_1.tStart)
    trials.addData('mic_1.stopped', mic_1.tStop)
    trials.addData('mic_image.started', mic_image.tStartRefresh)
    trials.addData('mic_image.stopped', mic_image.tStopRefresh)
    # the Routine "repeat" was not non-slip safe, so reset the non-slip timer
    routineTimer.reset()
    
    # ------Prepare to start Routine "end_block"-------
    continueRoutine = True
    # update component parameters for each repeat
    end.setSound('num/end_block.wav', hamming=True)
    end.setVolume(1, log=False)
    # keep track of which components have finished
    end_blockComponents = [end, FixCross_end]
    for thisComponent in end_blockComponents:
        thisComponent.tStart = None
        thisComponent.tStop = None
        thisComponent.tStartRefresh = None
        thisComponent.tStopRefresh = None
        if hasattr(thisComponent, 'status'):
            thisComponent.status = NOT_STARTED
    # reset timers
    t = 0
    _timeToFirstFrame = win.getFutureFlipTime(clock="now")
    end_blockClock.reset(-_timeToFirstFrame)  # t0 is time of first possible flip
    frameN = -1
    
    # -------Run Routine "end_block"-------
    while continueRoutine:
        # get current time
        t = end_blockClock.getTime()
        tThisFlip = win.getFutureFlipTime(clock=end_blockClock)
        tThisFlipGlobal = win.getFutureFlipTime(clock=None)
        frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
        # update/draw components on each frame
        # start/stop end
        if end.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
            # keep track of start time/frame for later
            end.frameNStart = frameN  # exact frame index
            end.tStart = t  # local t and not account for scr refresh
            end.tStartRefresh = tThisFlipGlobal  # on global time
            end.play(when=win)  # sync with win flip
        
        # *FixCross_end* updates
        if FixCross_end.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
            # keep track of start time/frame for later
            FixCross_end.frameNStart = frameN  # exact frame index
            FixCross_end.tStart = t  # local t and not account for scr refresh
            FixCross_end.tStartRefresh = tThisFlipGlobal  # on global time
            win.timeOnFlip(FixCross_end, 'tStartRefresh')  # time at next scr refresh
            FixCross_end.setAutoDraw(True)
        if FixCross_end.status == STARTED:
            # is it time to stop? (based on global clock, using actual start)
            if tThisFlipGlobal > FixCross_end.tStartRefresh + 5.0-frameTolerance:
                # keep track of stop time/frame for later
                FixCross_end.tStop = t  # not accounting for scr refresh
                FixCross_end.frameNStop = frameN  # exact frame index
                win.timeOnFlip(FixCross_end, 'tStopRefresh')  # time at next scr refresh
                FixCross_end.setAutoDraw(False)
        
        # check for quit (typically the Esc key)
        if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
            core.quit()
        
        # 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 end_blockComponents:
            if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
                continueRoutine = True
                break  # at least one component has not yet finished
        
        # refresh the screen
        if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
            win.flip()
    
    # -------Ending Routine "end_block"-------
    for thisComponent in end_blockComponents:
        if hasattr(thisComponent, "setAutoDraw"):
            thisComponent.setAutoDraw(False)
    end.stop()  # ensure sound has stopped at end of routine
    trials.addData('end.started', end.tStartRefresh)
    trials.addData('end.stopped', end.tStopRefresh)
    trials.addData('FixCross_end.started', FixCross_end.tStartRefresh)
    trials.addData('FixCross_end.stopped', FixCross_end.tStopRefresh)
    # the Routine "end_block" was not non-slip safe, so reset the non-slip timer
    routineTimer.reset()
    thisExp.nextEntry()
    
# completed 1 repeats of 'trials'


# ------Prepare to start Routine "rest"-------
continueRoutine = True
# update component parameters for each repeat
rest_instruction.setColor('white', colorSpace='rgb')
rest_instruction.setText('Конец тренировки')
key_resp_2.keys = []
key_resp_2.rt = []
_key_resp_2_allKeys = []
# keep track of which components have finished
restComponents = [rest_instruction, key_resp_2]
for thisComponent in restComponents:
    thisComponent.tStart = None
    thisComponent.tStop = None
    thisComponent.tStartRefresh = None
    thisComponent.tStopRefresh = None
    if hasattr(thisComponent, 'status'):
        thisComponent.status = NOT_STARTED
# reset timers
t = 0
_timeToFirstFrame = win.getFutureFlipTime(clock="now")
restClock.reset(-_timeToFirstFrame)  # t0 is time of first possible flip
frameN = -1

# -------Run Routine "rest"-------
while continueRoutine:
    # get current time
    t = restClock.getTime()
    tThisFlip = win.getFutureFlipTime(clock=restClock)
    tThisFlipGlobal = win.getFutureFlipTime(clock=None)
    frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
    # update/draw components on each frame
    
    # *rest_instruction* updates
    if rest_instruction.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
        # keep track of start time/frame for later
        rest_instruction.frameNStart = frameN  # exact frame index
        rest_instruction.tStart = t  # local t and not account for scr refresh
        rest_instruction.tStartRefresh = tThisFlipGlobal  # on global time
        win.timeOnFlip(rest_instruction, 'tStartRefresh')  # time at next scr refresh
        rest_instruction.setAutoDraw(True)
    
    # *key_resp_2* updates
    waitOnFlip = False
    if key_resp_2.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
        # keep track of start time/frame for later
        key_resp_2.frameNStart = frameN  # exact frame index
        key_resp_2.tStart = t  # local t and not account for scr refresh
        key_resp_2.tStartRefresh = tThisFlipGlobal  # on global time
        win.timeOnFlip(key_resp_2, 'tStartRefresh')  # time at next scr refresh
        key_resp_2.status = STARTED
        # keyboard checking is just starting
        waitOnFlip = True
        win.callOnFlip(key_resp_2.clock.reset)  # t=0 on next screen flip
        win.callOnFlip(key_resp_2.clearEvents, eventType='keyboard')  # clear events on next screen flip
    if key_resp_2.status == STARTED and not waitOnFlip:
        theseKeys = key_resp_2.getKeys(keyList=['space'], waitRelease=False)
        _key_resp_2_allKeys.extend(theseKeys)
        if len(_key_resp_2_allKeys):
            key_resp_2.keys = _key_resp_2_allKeys[-1].name  # just the last key pressed
            key_resp_2.rt = _key_resp_2_allKeys[-1].rt
            # a response ends the routine
            continueRoutine = False
    
    # check for quit (typically the Esc key)
    if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
        core.quit()
    
    # 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 restComponents:
        if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
            continueRoutine = True
            break  # at least one component has not yet finished
    
    # refresh the screen
    if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
        win.flip()

# -------Ending Routine "rest"-------
for thisComponent in restComponents:
    if hasattr(thisComponent, "setAutoDraw"):
        thisComponent.setAutoDraw(False)
thisExp.addData('rest_instruction.started', rest_instruction.tStartRefresh)
thisExp.addData('rest_instruction.stopped', rest_instruction.tStopRefresh)
# check responses
if key_resp_2.keys in ['', [], None]:  # No response was made
    key_resp_2.keys = None
thisExp.addData('key_resp_2.keys',key_resp_2.keys)
if key_resp_2.keys != None:  # we had a response
    thisExp.addData('key_resp_2.rt', key_resp_2.rt)
thisExp.addData('key_resp_2.started', key_resp_2.tStartRefresh)
thisExp.addData('key_resp_2.stopped', key_resp_2.tStopRefresh)
thisExp.nextEntry()
# the Routine "rest" was not non-slip safe, so reset the non-slip timer
routineTimer.reset()

# Flip one final time so any remaining win.callOnFlip() 
# and win.timeOnFlip() tasks get executed before quitting
win.flip()

# these shouldn't be strictly necessary (should auto-save)
thisExp.saveAsWideText(filename+'.csv')
thisExp.saveAsPickle(filename)
logging.flush()
# make sure everything is closed down
thisExp.abort()  # or data files will save again on exit
win.close()
core.quit()
