forked from kaczmarj/demo-psychopy-paradigm
-
Notifications
You must be signed in to change notification settings - Fork 3
/
audio_check.py
155 lines (136 loc) · 4.22 KB
/
audio_check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"""
Check audio volume for PsychoPy tasks.
Instructions:
1. Tell your participant to press buttons when they can hear the song well.
2. Press "space" to start the audio.
3. Watch for button presses.
4. Start increasing master volume for the stimulus computer or the scanner headphones.
5. When the participant presses buttons, stop tuning.
6. Press "space" to close the window.
"""
from __future__ import absolute_import, division, print_function
import time
import os.path as op
import sys
import psychopy
from psychopy import core, event, visual, sound
from psychopy.constants import STARTED, STOPPED # pylint: disable=E0401
psychopy.prefs.general['audioLib'] = ['sounddevice', 'pygame']
# psychopy.prefs.general['audioDevice'] = ['Built-in Output']
# Constants
TRIAL_DICT = {1: 'visual',
2: 'visual/auditory',
3: 'motor',
4: 'motor/auditory'}
RUN_DURATION = 450 # time for trials in task
LEAD_IN_DURATION = 6 # fixation before trials
END_SCREEN_DURATION = 2
def close_on_esc(win):
"""
Closes window if escape is pressed
"""
if 'escape' in event.getKeys():
win.close()
core.quit()
def draw_until_keypress(win, stim, continueKeys=['5']):
"""
"""
response = event.BuilderKeyResponse()
win.callOnFlip(response.clock.reset)
event.clearEvents(eventType='keyboard')
while True:
if isinstance(stim, list):
for s in stim:
s.draw()
else:
stim.draw()
keys = event.getKeys(keyList=continueKeys)
if any([ck in keys for ck in continueKeys]):
return
close_on_esc(win)
win.flip()
def draw(win, stim, duration, clock):
"""
Draw stimulus for a given duration.
Parameters
----------
win : (visual.Window)
stim : object with `.draw()` method
duration : (numeric)
duration in seconds to display the stimulus
"""
# Use a busy loop instead of sleeping so we can exit early if need be.
start_time = time.time()
response = event.BuilderKeyResponse()
response.tStart = start_time
response.frameNStart = 0
response.status = STARTED
win.callOnFlip(response.clock.reset)
event.clearEvents(eventType='keyboard')
while time.time() - start_time < duration:
stim.draw()
keys = event.getKeys(keyList=['1', '2'], timeStamped=clock)
if keys:
response.keys.extend(keys)
response.rt.append(response.clock.getTime())
close_on_esc(win)
win.flip()
response.status = STOPPED
return response.keys, response.rt
if __name__ == '__main__':
# Ensure that relative paths start from the same directory as this script
try:
script_dir = op.dirname(op.abspath(__file__)).decode(sys.getfilesystemencoding())
except AttributeError:
script_dir = op.dirname(op.abspath(__file__))
window = visual.Window(
fullscr=False,
size=(800, 600),
monitor='testMonitor',
units='pix',
allowStencil=False,
allowGUI=False,
color='black',
colorSpace='rgb',
blendMode='avg',
useFBO=True)
query = visual.TextStim(
win=window,
name='waiting',
text='Press when you can hear the music well',
font=u'Arial',
height=40,
pos=(0, 0),
wrapWidth=None,
ori=0,
color='white',
colorSpace='rgb',
opacity=1,
depth=-1.0)
waiting = visual.TextStim(
win=window,
name='waiting',
text='Stay tuned for a tune',
font=u'Arial',
height=40,
pos=(0, 0),
wrapWidth=None,
ori=0,
color='white',
colorSpace='rgb',
opacity=1,
depth=-1.0)
# Tones
audio_stimulus = sound.Sound(op.join(script_dir, 'stimuli', 'audio', 'Bleu.wav'))
window.flip()
draw_until_keypress(win=window, stim=waiting, continueKeys=['space'])
window.flip()
routine_clock = core.Clock()
audio_stimulus.play()
draw_until_keypress(win=window, stim=query, continueKeys=['space'])
audio_stimulus.stop()
window.flip()
# make sure everything is closed down
del(audio_stimulus, waiting, query)
window.close()
core.quit()