-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin-ils-audio-button-response.js
281 lines (272 loc) · 10.6 KB
/
plugin-ils-audio-button-response.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
var jsPsychIlsAudioButtonResponse = (function (jspsych) {
'use strict';
const info = {
name: "audio-button-response",
parameters: {
/** The audio to be played. */
stimulus: {
type: jspsych.ParameterType.AUDIO,
pretty_name: "Stimulus",
default: undefined,
},
/** Array containing the label(s) for the button(s). */
choices: {
type: jspsych.ParameterType.STRING,
pretty_name: "Choices",
default: undefined,
array: true,
},
/** The HTML for creating button. Can create own style. Use the "%choice%" string to indicate where the label from the choices parameter should be inserted. */
button_html: {
type: jspsych.ParameterType.HTML_STRING,
pretty_name: "Button HTML",
default: '<button class="jspsych-btn">%choice%</button>',
array: true,
},
/** Any content here will be displayed below the stimulus. */
prompt: {
type: jspsych.ParameterType.HTML_STRING,
pretty_name: "Prompt",
default: null,
},
/** The maximum duration to wait for a response. */
trial_duration: {
type: jspsych.ParameterType.INT,
pretty_name: "Trial duration",
default: null,
},
/** Vertical margin of button. */
margin_vertical: {
type: jspsych.ParameterType.STRING,
pretty_name: "Margin vertical",
default: "0px",
},
/** Horizontal margin of button. */
margin_horizontal: {
type: jspsych.ParameterType.STRING,
pretty_name: "Margin horizontal",
default: "8px",
},
/** If true, the trial will end when user makes a response. */
response_ends_trial: {
type: jspsych.ParameterType.BOOL,
pretty_name: "Response ends trial",
default: true,
},
/** If true, then the trial will end as soon as the audio file finishes playing. */
trial_ends_after_audio: {
type: jspsych.ParameterType.BOOL,
pretty_name: "Trial ends after audio",
default: false,
},
/**
* If true, then responses are allowed while the audio is playing.
* If false, then the audio must finish playing before a response is accepted.
*/
response_allowed_while_playing: {
type: jspsych.ParameterType.BOOL,
pretty_name: "Response allowed while playing",
default: true,
},
clear_html_on_exit: {
type: jspsych.ParameterType.BOOL,
pretty_name: "Clear the html when the trial exits",
default: true,
}
},
};
/**
* **audio-button-response**
*
* Ils plugin for playing an audio file and getting a button response
*
* This plugin is adapted from the jsPsych's AudioButtonResponsePlugin
*
* @author Maarten Duijndam
* @author Kristin Diep
* @see {@link https://www.jspsych.org/plugins/jspsych-audio-button-response/ audio-button-response plugin documentation on jspsych.org}
*/
class IlsAudioButtonResponsePlugin {
constructor(jsPsych) {
this.jsPsych = jsPsych;
}
trial(display_element, trial, on_load) {
// hold the .resolve() function from the Promise that ends the trial
let trial_complete;
// setup stimulus
var context = this.jsPsych.pluginAPI.audioContext();
var audio;
// store response
var response = {
rt: null,
button: null,
};
// record webaudio context start time
var startTime;
// load audio file
this.jsPsych.pluginAPI
.getAudioBuffer(trial.stimulus)
.then(function (buffer) {
if (context !== null) {
audio = context.createBufferSource();
audio.buffer = buffer;
audio.connect(context.destination);
}
else {
audio = buffer;
audio.currentTime = 0;
}
setupTrial();
})
.catch(function (err) {
console.error(`Failed to load audio file "${trial.stimulus}". Try checking the file path. We recommend using the preload plugin to load audio files.`);
console.error(err);
});
const setupTrial = () => {
// set up end event if trial needs it
if (trial.trial_ends_after_audio) {
audio.addEventListener("ended", end_trial);
}
// enable buttons after audio ends if necessary
if (!trial.response_allowed_while_playing && !trial.trial_ends_after_audio) {
audio.addEventListener("ended", enable_buttons);
}
//show prompt if there is one
let html = "";
if (trial.prompt !== null) {
html += trial.prompt;
}
//display buttons
var buttons = [];
if (Array.isArray(trial.button_html)) {
if (trial.button_html.length == trial.choices.length) {
buttons = trial.button_html;
}
else {
console.error("Error in audio-button-response plugin. The length of the button_html array does not equal the length of the choices array");
}
}
else {
for (var i = 0; i < trial.choices.length; i++) {
buttons.push(trial.button_html);
}
}
html += '<div id="jspsych-audio-button-response-btngroup">';
for (var i = 0; i < trial.choices.length; i++) {
var str = buttons[i].replace(/%choice%/g, trial.choices[i]);
html +=
'<div class="jspsych-audio-button-response-button" style="cursor: pointer; display: inline-block; margin:' +
trial.margin_vertical +
" " +
trial.margin_horizontal +
'" id="jspsych-audio-button-response-button-' +
i +
'" data-choice="' +
i +
'">' +
str +
"</div>";
}
html += "</div>";
display_element.innerHTML = html;
if (trial.response_allowed_while_playing) {
enable_buttons();
}
else {
disable_buttons();
}
// start time
startTime = performance.now();
// start audio
if (context !== null) {
startTime = context.currentTime;
audio.start(startTime);
}
else {
audio.play();
}
// end trial if time limit is set
if (trial.trial_duration !== null) {
this.jsPsych.pluginAPI.setTimeout(function () {
end_trial();
}, trial.trial_duration);
}
on_load();
};
// function to handle responses by the subject
function after_response(choice) {
// measure rt
var endTime = performance.now();
var rt = Math.round(endTime - startTime);
if (context !== null) {
endTime = context.currentTime;
rt = Math.round((endTime - startTime) * 1000);
}
response.button = parseInt(choice);
response.rt = rt;
// disable all the buttons after a response
disable_buttons();
if (trial.response_ends_trial) {
end_trial();
}
}
// function to end trial when it is time
const end_trial = () => {
// kill any remaining setTimeout handlers
this.jsPsych.pluginAPI.clearAllTimeouts();
// stop the audio file if it is playing
// remove end event listeners if they exist
if (context !== null) {
audio.stop();
}
else {
audio.pause();
}
audio.removeEventListener("ended", end_trial);
audio.removeEventListener("ended", enable_buttons);
// gather the data to store for the trial
var trial_data = {
rt: response.rt,
stimulus: trial.stimulus,
response: response.button,
};
// clear the display
if (trial.clear_html_on_exit) {
display_element.innerHTML = "";
}
// move on to the next trial
this.jsPsych.finishTrial(trial_data);
trial_complete();
};
function button_response(e) {
var choice = e.currentTarget.getAttribute("data-choice"); // don't use dataset for jsdom compatibility
after_response(choice);
}
function disable_buttons() {
var btns = document.querySelectorAll(".jspsych-audio-button-response-button");
for (var i = 0; i < btns.length; i++) {
var btn_el = btns[i].querySelector("button");
if (btn_el) {
btn_el.disabled = true;
}
btns[i].removeEventListener("click", button_response);
}
}
function enable_buttons() {
var btns = document.querySelectorAll(".jspsych-audio-button-response-button");
for (var i = 0; i < btns.length; i++) {
var btn_el = btns[i].querySelector("button");
if (btn_el) {
btn_el.disabled = false;
}
btns[i].addEventListener("click", button_response);
}
}
return new Promise((resolve) => {
trial_complete = resolve;
});
}
}
IlsAudioButtonResponsePlugin.info = info;
return IlsAudioButtonResponsePlugin;
})(jsPsychModule);