@jspsych/plugin-audio-button-response 1.1.2 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -2
- package/dist/index.browser.js +297 -297
- package/dist/index.browser.min.js +2 -2
- package/dist/index.browser.min.js.map +1 -1
- package/dist/index.cjs +296 -296
- package/dist/index.d.ts +161 -161
- package/dist/index.js +296 -296
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,301 +1,301 @@
|
|
|
1
1
|
import { ParameterType } from 'jspsych';
|
|
2
2
|
|
|
3
|
-
const info = {
|
|
4
|
-
name: "audio-button-response",
|
|
5
|
-
parameters: {
|
|
6
|
-
/** The audio to be played. */
|
|
7
|
-
stimulus: {
|
|
8
|
-
type: ParameterType.AUDIO,
|
|
9
|
-
pretty_name: "Stimulus",
|
|
10
|
-
default: undefined,
|
|
11
|
-
},
|
|
12
|
-
/** Array containing the label(s) for the button(s). */
|
|
13
|
-
choices: {
|
|
14
|
-
type: ParameterType.STRING,
|
|
15
|
-
pretty_name: "Choices",
|
|
16
|
-
default: undefined,
|
|
17
|
-
array: true,
|
|
18
|
-
},
|
|
19
|
-
/** 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. */
|
|
20
|
-
button_html: {
|
|
21
|
-
type: ParameterType.HTML_STRING,
|
|
22
|
-
pretty_name: "Button HTML",
|
|
23
|
-
default: '<button class="jspsych-btn">%choice%</button>',
|
|
24
|
-
array: true,
|
|
25
|
-
},
|
|
26
|
-
/** Any content here will be displayed below the stimulus. */
|
|
27
|
-
prompt: {
|
|
28
|
-
type: ParameterType.HTML_STRING,
|
|
29
|
-
pretty_name: "Prompt",
|
|
30
|
-
default: null,
|
|
31
|
-
},
|
|
32
|
-
/** The maximum duration to wait for a response. */
|
|
33
|
-
trial_duration: {
|
|
34
|
-
type: ParameterType.INT,
|
|
35
|
-
pretty_name: "Trial duration",
|
|
36
|
-
default: null,
|
|
37
|
-
},
|
|
38
|
-
/** Vertical margin of button. */
|
|
39
|
-
margin_vertical: {
|
|
40
|
-
type: ParameterType.STRING,
|
|
41
|
-
pretty_name: "Margin vertical",
|
|
42
|
-
default: "0px",
|
|
43
|
-
},
|
|
44
|
-
/** Horizontal margin of button. */
|
|
45
|
-
margin_horizontal: {
|
|
46
|
-
type: ParameterType.STRING,
|
|
47
|
-
pretty_name: "Margin horizontal",
|
|
48
|
-
default: "8px",
|
|
49
|
-
},
|
|
50
|
-
/** If true, the trial will end when user makes a response. */
|
|
51
|
-
response_ends_trial: {
|
|
52
|
-
type: ParameterType.BOOL,
|
|
53
|
-
pretty_name: "Response ends trial",
|
|
54
|
-
default: true,
|
|
55
|
-
},
|
|
56
|
-
/** If true, then the trial will end as soon as the audio file finishes playing. */
|
|
57
|
-
trial_ends_after_audio: {
|
|
58
|
-
type: ParameterType.BOOL,
|
|
59
|
-
pretty_name: "Trial ends after audio",
|
|
60
|
-
default: false,
|
|
61
|
-
},
|
|
62
|
-
/**
|
|
63
|
-
* If true, then responses are allowed while the audio is playing.
|
|
64
|
-
* If false, then the audio must finish playing before a response is accepted.
|
|
65
|
-
*/
|
|
66
|
-
response_allowed_while_playing: {
|
|
67
|
-
type: ParameterType.BOOL,
|
|
68
|
-
pretty_name: "Response allowed while playing",
|
|
69
|
-
default: true,
|
|
70
|
-
},
|
|
71
|
-
},
|
|
72
|
-
};
|
|
73
|
-
/**
|
|
74
|
-
* **audio-button-response**
|
|
75
|
-
*
|
|
76
|
-
* jsPsych plugin for playing an audio file and getting a button response
|
|
77
|
-
*
|
|
78
|
-
* @author Kristin Diep
|
|
79
|
-
* @see {@link https://www.jspsych.org/plugins/jspsych-audio-button-response/ audio-button-response plugin documentation on jspsych.org}
|
|
80
|
-
*/
|
|
81
|
-
class AudioButtonResponsePlugin {
|
|
82
|
-
constructor(jsPsych) {
|
|
83
|
-
this.jsPsych = jsPsych;
|
|
84
|
-
}
|
|
85
|
-
trial(display_element, trial, on_load) {
|
|
86
|
-
// hold the .resolve() function from the Promise that ends the trial
|
|
87
|
-
let trial_complete;
|
|
88
|
-
// setup stimulus
|
|
89
|
-
var context = this.jsPsych.pluginAPI.audioContext();
|
|
90
|
-
// store response
|
|
91
|
-
var response = {
|
|
92
|
-
rt: null,
|
|
93
|
-
button: null,
|
|
94
|
-
};
|
|
95
|
-
// record webaudio context start time
|
|
96
|
-
var startTime;
|
|
97
|
-
// load audio file
|
|
98
|
-
this.jsPsych.pluginAPI
|
|
99
|
-
.getAudioBuffer(trial.stimulus)
|
|
100
|
-
.then((buffer) => {
|
|
101
|
-
if (context !== null) {
|
|
102
|
-
this.audio = context.createBufferSource();
|
|
103
|
-
this.audio.buffer = buffer;
|
|
104
|
-
this.audio.connect(context.destination);
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
this.audio = buffer;
|
|
108
|
-
this.audio.currentTime = 0;
|
|
109
|
-
}
|
|
110
|
-
setupTrial();
|
|
111
|
-
})
|
|
112
|
-
.catch((err) => {
|
|
113
|
-
console.error(`Failed to load audio file "${trial.stimulus}". Try checking the file path. We recommend using the preload plugin to load audio files.`);
|
|
114
|
-
console.error(err);
|
|
115
|
-
});
|
|
116
|
-
const setupTrial = () => {
|
|
117
|
-
// set up end event if trial needs it
|
|
118
|
-
if (trial.trial_ends_after_audio) {
|
|
119
|
-
this.audio.addEventListener("ended", end_trial);
|
|
120
|
-
}
|
|
121
|
-
// enable buttons after audio ends if necessary
|
|
122
|
-
if (!trial.response_allowed_while_playing && !trial.trial_ends_after_audio) {
|
|
123
|
-
this.audio.addEventListener("ended", enable_buttons);
|
|
124
|
-
}
|
|
125
|
-
//display buttons
|
|
126
|
-
var buttons = [];
|
|
127
|
-
if (Array.isArray(trial.button_html)) {
|
|
128
|
-
if (trial.button_html.length == trial.choices.length) {
|
|
129
|
-
buttons = trial.button_html;
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
console.error("Error in audio-button-response plugin. The length of the button_html array does not equal the length of the choices array");
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
else {
|
|
136
|
-
for (var i = 0; i < trial.choices.length; i++) {
|
|
137
|
-
buttons.push(trial.button_html);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
var html = '<div id="jspsych-audio-button-response-btngroup">';
|
|
141
|
-
for (var i = 0; i < trial.choices.length; i++) {
|
|
142
|
-
var str = buttons[i].replace(/%choice%/g, trial.choices[i]);
|
|
143
|
-
html +=
|
|
144
|
-
'<div class="jspsych-audio-button-response-button" style="cursor: pointer; display: inline-block; margin:' +
|
|
145
|
-
trial.margin_vertical +
|
|
146
|
-
" " +
|
|
147
|
-
trial.margin_horizontal +
|
|
148
|
-
'" id="jspsych-audio-button-response-button-' +
|
|
149
|
-
i +
|
|
150
|
-
'" data-choice="' +
|
|
151
|
-
i +
|
|
152
|
-
'">' +
|
|
153
|
-
str +
|
|
154
|
-
"</div>";
|
|
155
|
-
}
|
|
156
|
-
html += "</div>";
|
|
157
|
-
//show prompt if there is one
|
|
158
|
-
if (trial.prompt !== null) {
|
|
159
|
-
html += trial.prompt;
|
|
160
|
-
}
|
|
161
|
-
display_element.innerHTML = html;
|
|
162
|
-
if (trial.response_allowed_while_playing) {
|
|
163
|
-
enable_buttons();
|
|
164
|
-
}
|
|
165
|
-
else {
|
|
166
|
-
disable_buttons();
|
|
167
|
-
}
|
|
168
|
-
// start time
|
|
169
|
-
startTime = performance.now();
|
|
170
|
-
// start audio
|
|
171
|
-
if (context !== null) {
|
|
172
|
-
startTime = context.currentTime;
|
|
173
|
-
this.audio.start(startTime);
|
|
174
|
-
}
|
|
175
|
-
else {
|
|
176
|
-
this.audio.play();
|
|
177
|
-
}
|
|
178
|
-
// end trial if time limit is set
|
|
179
|
-
if (trial.trial_duration !== null) {
|
|
180
|
-
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
181
|
-
end_trial();
|
|
182
|
-
}, trial.trial_duration);
|
|
183
|
-
}
|
|
184
|
-
on_load();
|
|
185
|
-
};
|
|
186
|
-
// function to handle responses by the subject
|
|
187
|
-
function after_response(choice) {
|
|
188
|
-
// measure rt
|
|
189
|
-
var endTime = performance.now();
|
|
190
|
-
var rt = Math.round(endTime - startTime);
|
|
191
|
-
if (context !== null) {
|
|
192
|
-
endTime = context.currentTime;
|
|
193
|
-
rt = Math.round((endTime - startTime) * 1000);
|
|
194
|
-
}
|
|
195
|
-
response.button = parseInt(choice);
|
|
196
|
-
response.rt = rt;
|
|
197
|
-
// disable all the buttons after a response
|
|
198
|
-
disable_buttons();
|
|
199
|
-
if (trial.response_ends_trial) {
|
|
200
|
-
end_trial();
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
// function to end trial when it is time
|
|
204
|
-
const end_trial = () => {
|
|
205
|
-
// kill any remaining setTimeout handlers
|
|
206
|
-
this.jsPsych.pluginAPI.clearAllTimeouts();
|
|
207
|
-
// stop the audio file if it is playing
|
|
208
|
-
// remove end event listeners if they exist
|
|
209
|
-
if (context !== null) {
|
|
210
|
-
this.audio.stop();
|
|
211
|
-
}
|
|
212
|
-
else {
|
|
213
|
-
this.audio.pause();
|
|
214
|
-
}
|
|
215
|
-
this.audio.removeEventListener("ended", end_trial);
|
|
216
|
-
this.audio.removeEventListener("ended", enable_buttons);
|
|
217
|
-
// gather the data to store for the trial
|
|
218
|
-
var trial_data = {
|
|
219
|
-
rt: response.rt,
|
|
220
|
-
stimulus: trial.stimulus,
|
|
221
|
-
response: response.button,
|
|
222
|
-
};
|
|
223
|
-
// clear the display
|
|
224
|
-
display_element.innerHTML = "";
|
|
225
|
-
// move on to the next trial
|
|
226
|
-
this.jsPsych.finishTrial(trial_data);
|
|
227
|
-
trial_complete();
|
|
228
|
-
};
|
|
229
|
-
function button_response(e) {
|
|
230
|
-
var choice = e.currentTarget.getAttribute("data-choice"); // don't use dataset for jsdom compatibility
|
|
231
|
-
after_response(choice);
|
|
232
|
-
}
|
|
233
|
-
function disable_buttons() {
|
|
234
|
-
var btns = document.querySelectorAll(".jspsych-audio-button-response-button");
|
|
235
|
-
for (var i = 0; i < btns.length; i++) {
|
|
236
|
-
var btn_el = btns[i].querySelector("button");
|
|
237
|
-
if (btn_el) {
|
|
238
|
-
btn_el.disabled = true;
|
|
239
|
-
}
|
|
240
|
-
btns[i].removeEventListener("click", button_response);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
function enable_buttons() {
|
|
244
|
-
var btns = document.querySelectorAll(".jspsych-audio-button-response-button");
|
|
245
|
-
for (var i = 0; i < btns.length; i++) {
|
|
246
|
-
var btn_el = btns[i].querySelector("button");
|
|
247
|
-
if (btn_el) {
|
|
248
|
-
btn_el.disabled = false;
|
|
249
|
-
}
|
|
250
|
-
btns[i].addEventListener("click", button_response);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
return new Promise((resolve) => {
|
|
254
|
-
trial_complete = resolve;
|
|
255
|
-
});
|
|
256
|
-
}
|
|
257
|
-
simulate(trial, simulation_mode, simulation_options, load_callback) {
|
|
258
|
-
if (simulation_mode == "data-only") {
|
|
259
|
-
load_callback();
|
|
260
|
-
this.simulate_data_only(trial, simulation_options);
|
|
261
|
-
}
|
|
262
|
-
if (simulation_mode == "visual") {
|
|
263
|
-
this.simulate_visual(trial, simulation_options, load_callback);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
create_simulation_data(trial, simulation_options) {
|
|
267
|
-
const default_data = {
|
|
268
|
-
stimulus: trial.stimulus,
|
|
269
|
-
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
270
|
-
response: this.jsPsych.randomization.randomInt(0, trial.choices.length - 1),
|
|
271
|
-
};
|
|
272
|
-
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
273
|
-
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
274
|
-
return data;
|
|
275
|
-
}
|
|
276
|
-
simulate_data_only(trial, simulation_options) {
|
|
277
|
-
const data = this.create_simulation_data(trial, simulation_options);
|
|
278
|
-
this.jsPsych.finishTrial(data);
|
|
279
|
-
}
|
|
280
|
-
simulate_visual(trial, simulation_options, load_callback) {
|
|
281
|
-
const data = this.create_simulation_data(trial, simulation_options);
|
|
282
|
-
const display_element = this.jsPsych.getDisplayElement();
|
|
283
|
-
const respond = () => {
|
|
284
|
-
if (data.rt !== null) {
|
|
285
|
-
this.jsPsych.pluginAPI.clickTarget(display_element.querySelector(`div[data-choice="${data.response}"] button`), data.rt);
|
|
286
|
-
}
|
|
287
|
-
};
|
|
288
|
-
this.trial(display_element, trial, () => {
|
|
289
|
-
load_callback();
|
|
290
|
-
if (!trial.response_allowed_while_playing) {
|
|
291
|
-
this.audio.addEventListener("ended", respond);
|
|
292
|
-
}
|
|
293
|
-
else {
|
|
294
|
-
respond();
|
|
295
|
-
}
|
|
296
|
-
});
|
|
297
|
-
}
|
|
298
|
-
}
|
|
3
|
+
const info = {
|
|
4
|
+
name: "audio-button-response",
|
|
5
|
+
parameters: {
|
|
6
|
+
/** The audio to be played. */
|
|
7
|
+
stimulus: {
|
|
8
|
+
type: ParameterType.AUDIO,
|
|
9
|
+
pretty_name: "Stimulus",
|
|
10
|
+
default: undefined,
|
|
11
|
+
},
|
|
12
|
+
/** Array containing the label(s) for the button(s). */
|
|
13
|
+
choices: {
|
|
14
|
+
type: ParameterType.STRING,
|
|
15
|
+
pretty_name: "Choices",
|
|
16
|
+
default: undefined,
|
|
17
|
+
array: true,
|
|
18
|
+
},
|
|
19
|
+
/** 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. */
|
|
20
|
+
button_html: {
|
|
21
|
+
type: ParameterType.HTML_STRING,
|
|
22
|
+
pretty_name: "Button HTML",
|
|
23
|
+
default: '<button class="jspsych-btn">%choice%</button>',
|
|
24
|
+
array: true,
|
|
25
|
+
},
|
|
26
|
+
/** Any content here will be displayed below the stimulus. */
|
|
27
|
+
prompt: {
|
|
28
|
+
type: ParameterType.HTML_STRING,
|
|
29
|
+
pretty_name: "Prompt",
|
|
30
|
+
default: null,
|
|
31
|
+
},
|
|
32
|
+
/** The maximum duration to wait for a response. */
|
|
33
|
+
trial_duration: {
|
|
34
|
+
type: ParameterType.INT,
|
|
35
|
+
pretty_name: "Trial duration",
|
|
36
|
+
default: null,
|
|
37
|
+
},
|
|
38
|
+
/** Vertical margin of button. */
|
|
39
|
+
margin_vertical: {
|
|
40
|
+
type: ParameterType.STRING,
|
|
41
|
+
pretty_name: "Margin vertical",
|
|
42
|
+
default: "0px",
|
|
43
|
+
},
|
|
44
|
+
/** Horizontal margin of button. */
|
|
45
|
+
margin_horizontal: {
|
|
46
|
+
type: ParameterType.STRING,
|
|
47
|
+
pretty_name: "Margin horizontal",
|
|
48
|
+
default: "8px",
|
|
49
|
+
},
|
|
50
|
+
/** If true, the trial will end when user makes a response. */
|
|
51
|
+
response_ends_trial: {
|
|
52
|
+
type: ParameterType.BOOL,
|
|
53
|
+
pretty_name: "Response ends trial",
|
|
54
|
+
default: true,
|
|
55
|
+
},
|
|
56
|
+
/** If true, then the trial will end as soon as the audio file finishes playing. */
|
|
57
|
+
trial_ends_after_audio: {
|
|
58
|
+
type: ParameterType.BOOL,
|
|
59
|
+
pretty_name: "Trial ends after audio",
|
|
60
|
+
default: false,
|
|
61
|
+
},
|
|
62
|
+
/**
|
|
63
|
+
* If true, then responses are allowed while the audio is playing.
|
|
64
|
+
* If false, then the audio must finish playing before a response is accepted.
|
|
65
|
+
*/
|
|
66
|
+
response_allowed_while_playing: {
|
|
67
|
+
type: ParameterType.BOOL,
|
|
68
|
+
pretty_name: "Response allowed while playing",
|
|
69
|
+
default: true,
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* **audio-button-response**
|
|
75
|
+
*
|
|
76
|
+
* jsPsych plugin for playing an audio file and getting a button response
|
|
77
|
+
*
|
|
78
|
+
* @author Kristin Diep
|
|
79
|
+
* @see {@link https://www.jspsych.org/plugins/jspsych-audio-button-response/ audio-button-response plugin documentation on jspsych.org}
|
|
80
|
+
*/
|
|
81
|
+
class AudioButtonResponsePlugin {
|
|
82
|
+
constructor(jsPsych) {
|
|
83
|
+
this.jsPsych = jsPsych;
|
|
84
|
+
}
|
|
85
|
+
trial(display_element, trial, on_load) {
|
|
86
|
+
// hold the .resolve() function from the Promise that ends the trial
|
|
87
|
+
let trial_complete;
|
|
88
|
+
// setup stimulus
|
|
89
|
+
var context = this.jsPsych.pluginAPI.audioContext();
|
|
90
|
+
// store response
|
|
91
|
+
var response = {
|
|
92
|
+
rt: null,
|
|
93
|
+
button: null,
|
|
94
|
+
};
|
|
95
|
+
// record webaudio context start time
|
|
96
|
+
var startTime;
|
|
97
|
+
// load audio file
|
|
98
|
+
this.jsPsych.pluginAPI
|
|
99
|
+
.getAudioBuffer(trial.stimulus)
|
|
100
|
+
.then((buffer) => {
|
|
101
|
+
if (context !== null) {
|
|
102
|
+
this.audio = context.createBufferSource();
|
|
103
|
+
this.audio.buffer = buffer;
|
|
104
|
+
this.audio.connect(context.destination);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
this.audio = buffer;
|
|
108
|
+
this.audio.currentTime = 0;
|
|
109
|
+
}
|
|
110
|
+
setupTrial();
|
|
111
|
+
})
|
|
112
|
+
.catch((err) => {
|
|
113
|
+
console.error(`Failed to load audio file "${trial.stimulus}". Try checking the file path. We recommend using the preload plugin to load audio files.`);
|
|
114
|
+
console.error(err);
|
|
115
|
+
});
|
|
116
|
+
const setupTrial = () => {
|
|
117
|
+
// set up end event if trial needs it
|
|
118
|
+
if (trial.trial_ends_after_audio) {
|
|
119
|
+
this.audio.addEventListener("ended", end_trial);
|
|
120
|
+
}
|
|
121
|
+
// enable buttons after audio ends if necessary
|
|
122
|
+
if (!trial.response_allowed_while_playing && !trial.trial_ends_after_audio) {
|
|
123
|
+
this.audio.addEventListener("ended", enable_buttons);
|
|
124
|
+
}
|
|
125
|
+
//display buttons
|
|
126
|
+
var buttons = [];
|
|
127
|
+
if (Array.isArray(trial.button_html)) {
|
|
128
|
+
if (trial.button_html.length == trial.choices.length) {
|
|
129
|
+
buttons = trial.button_html;
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
console.error("Error in audio-button-response plugin. The length of the button_html array does not equal the length of the choices array");
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
for (var i = 0; i < trial.choices.length; i++) {
|
|
137
|
+
buttons.push(trial.button_html);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
var html = '<div id="jspsych-audio-button-response-btngroup">';
|
|
141
|
+
for (var i = 0; i < trial.choices.length; i++) {
|
|
142
|
+
var str = buttons[i].replace(/%choice%/g, trial.choices[i]);
|
|
143
|
+
html +=
|
|
144
|
+
'<div class="jspsych-audio-button-response-button" style="cursor: pointer; display: inline-block; margin:' +
|
|
145
|
+
trial.margin_vertical +
|
|
146
|
+
" " +
|
|
147
|
+
trial.margin_horizontal +
|
|
148
|
+
'" id="jspsych-audio-button-response-button-' +
|
|
149
|
+
i +
|
|
150
|
+
'" data-choice="' +
|
|
151
|
+
i +
|
|
152
|
+
'">' +
|
|
153
|
+
str +
|
|
154
|
+
"</div>";
|
|
155
|
+
}
|
|
156
|
+
html += "</div>";
|
|
157
|
+
//show prompt if there is one
|
|
158
|
+
if (trial.prompt !== null) {
|
|
159
|
+
html += trial.prompt;
|
|
160
|
+
}
|
|
161
|
+
display_element.innerHTML = html;
|
|
162
|
+
if (trial.response_allowed_while_playing) {
|
|
163
|
+
enable_buttons();
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
disable_buttons();
|
|
167
|
+
}
|
|
168
|
+
// start time
|
|
169
|
+
startTime = performance.now();
|
|
170
|
+
// start audio
|
|
171
|
+
if (context !== null) {
|
|
172
|
+
startTime = context.currentTime;
|
|
173
|
+
this.audio.start(startTime);
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
this.audio.play();
|
|
177
|
+
}
|
|
178
|
+
// end trial if time limit is set
|
|
179
|
+
if (trial.trial_duration !== null) {
|
|
180
|
+
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
181
|
+
end_trial();
|
|
182
|
+
}, trial.trial_duration);
|
|
183
|
+
}
|
|
184
|
+
on_load();
|
|
185
|
+
};
|
|
186
|
+
// function to handle responses by the subject
|
|
187
|
+
function after_response(choice) {
|
|
188
|
+
// measure rt
|
|
189
|
+
var endTime = performance.now();
|
|
190
|
+
var rt = Math.round(endTime - startTime);
|
|
191
|
+
if (context !== null) {
|
|
192
|
+
endTime = context.currentTime;
|
|
193
|
+
rt = Math.round((endTime - startTime) * 1000);
|
|
194
|
+
}
|
|
195
|
+
response.button = parseInt(choice);
|
|
196
|
+
response.rt = rt;
|
|
197
|
+
// disable all the buttons after a response
|
|
198
|
+
disable_buttons();
|
|
199
|
+
if (trial.response_ends_trial) {
|
|
200
|
+
end_trial();
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
// function to end trial when it is time
|
|
204
|
+
const end_trial = () => {
|
|
205
|
+
// kill any remaining setTimeout handlers
|
|
206
|
+
this.jsPsych.pluginAPI.clearAllTimeouts();
|
|
207
|
+
// stop the audio file if it is playing
|
|
208
|
+
// remove end event listeners if they exist
|
|
209
|
+
if (context !== null) {
|
|
210
|
+
this.audio.stop();
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
this.audio.pause();
|
|
214
|
+
}
|
|
215
|
+
this.audio.removeEventListener("ended", end_trial);
|
|
216
|
+
this.audio.removeEventListener("ended", enable_buttons);
|
|
217
|
+
// gather the data to store for the trial
|
|
218
|
+
var trial_data = {
|
|
219
|
+
rt: response.rt,
|
|
220
|
+
stimulus: trial.stimulus,
|
|
221
|
+
response: response.button,
|
|
222
|
+
};
|
|
223
|
+
// clear the display
|
|
224
|
+
display_element.innerHTML = "";
|
|
225
|
+
// move on to the next trial
|
|
226
|
+
this.jsPsych.finishTrial(trial_data);
|
|
227
|
+
trial_complete();
|
|
228
|
+
};
|
|
229
|
+
function button_response(e) {
|
|
230
|
+
var choice = e.currentTarget.getAttribute("data-choice"); // don't use dataset for jsdom compatibility
|
|
231
|
+
after_response(choice);
|
|
232
|
+
}
|
|
233
|
+
function disable_buttons() {
|
|
234
|
+
var btns = document.querySelectorAll(".jspsych-audio-button-response-button");
|
|
235
|
+
for (var i = 0; i < btns.length; i++) {
|
|
236
|
+
var btn_el = btns[i].querySelector("button");
|
|
237
|
+
if (btn_el) {
|
|
238
|
+
btn_el.disabled = true;
|
|
239
|
+
}
|
|
240
|
+
btns[i].removeEventListener("click", button_response);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
function enable_buttons() {
|
|
244
|
+
var btns = document.querySelectorAll(".jspsych-audio-button-response-button");
|
|
245
|
+
for (var i = 0; i < btns.length; i++) {
|
|
246
|
+
var btn_el = btns[i].querySelector("button");
|
|
247
|
+
if (btn_el) {
|
|
248
|
+
btn_el.disabled = false;
|
|
249
|
+
}
|
|
250
|
+
btns[i].addEventListener("click", button_response);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return new Promise((resolve) => {
|
|
254
|
+
trial_complete = resolve;
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
simulate(trial, simulation_mode, simulation_options, load_callback) {
|
|
258
|
+
if (simulation_mode == "data-only") {
|
|
259
|
+
load_callback();
|
|
260
|
+
this.simulate_data_only(trial, simulation_options);
|
|
261
|
+
}
|
|
262
|
+
if (simulation_mode == "visual") {
|
|
263
|
+
this.simulate_visual(trial, simulation_options, load_callback);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
create_simulation_data(trial, simulation_options) {
|
|
267
|
+
const default_data = {
|
|
268
|
+
stimulus: trial.stimulus,
|
|
269
|
+
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
270
|
+
response: this.jsPsych.randomization.randomInt(0, trial.choices.length - 1),
|
|
271
|
+
};
|
|
272
|
+
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
273
|
+
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
274
|
+
return data;
|
|
275
|
+
}
|
|
276
|
+
simulate_data_only(trial, simulation_options) {
|
|
277
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
278
|
+
this.jsPsych.finishTrial(data);
|
|
279
|
+
}
|
|
280
|
+
simulate_visual(trial, simulation_options, load_callback) {
|
|
281
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
282
|
+
const display_element = this.jsPsych.getDisplayElement();
|
|
283
|
+
const respond = () => {
|
|
284
|
+
if (data.rt !== null) {
|
|
285
|
+
this.jsPsych.pluginAPI.clickTarget(display_element.querySelector(`div[data-choice="${data.response}"] button`), data.rt);
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
this.trial(display_element, trial, () => {
|
|
289
|
+
load_callback();
|
|
290
|
+
if (!trial.response_allowed_while_playing) {
|
|
291
|
+
this.audio.addEventListener("ended", respond);
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
respond();
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
299
|
AudioButtonResponsePlugin.info = info;
|
|
300
300
|
|
|
301
301
|
export { AudioButtonResponsePlugin as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jspsych/plugin-audio-button-response",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"description": "jsPsych plugin for playing an audio file and getting a button response",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"jspsych": ">=7.1.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@jspsych/config": "^
|
|
40
|
+
"@jspsych/config": "^2.0.0",
|
|
41
41
|
"@jspsych/test-utils": "^1.1.2"
|
|
42
42
|
}
|
|
43
43
|
}
|