@jspsych/plugin-audio-button-response 1.1.1 → 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 +47 -0
- 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 +3 -3
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
jsPsych is a JavaScript framework for creating behavioral experiments that run in a web browser.
|
|
4
|
+
|
|
5
|
+
## Plugin Description
|
|
6
|
+
|
|
7
|
+
The audio-button-response plugin plays audio files and records responses generated with a button click. The audio will be played using the WebAudio API, allowing for reasonably precise timing of the playback by measuring it against the WebAudio specific clock. If WebAudio API is not supported by the browser, the files will be played using HTML5 audio.
|
|
8
|
+
|
|
9
|
+
## Examples
|
|
10
|
+
|
|
11
|
+
Several example experiments and plugin demonstrations are available in the `/examples` folder.
|
|
12
|
+
After you've downloaded the [latest release](https://github.com/jspsych/jsPsych/releases), double-click on an example HTML file to run it in your web browser, and open it with a programming-friendly text editor to see how it works.
|
|
13
|
+
|
|
14
|
+
## Documentation
|
|
15
|
+
|
|
16
|
+
Documentation for this plugin is available [here](https://www.jspsych.org/latest/plugins/audio-button-response).
|
|
17
|
+
|
|
18
|
+
## Getting help
|
|
19
|
+
|
|
20
|
+
For questions about using the library, please use the GitHub [discussions forum](https://github.com/jspsych/jsPsych/discussions).
|
|
21
|
+
You can also browse through the history of Q&A on the forum to find related questions.
|
|
22
|
+
|
|
23
|
+
## Contributing
|
|
24
|
+
|
|
25
|
+
We :heart: contributions!
|
|
26
|
+
See the [contributing to jsPsych](https://www.jspsych.org/latest/developers/contributing/) documentation page for more information about how you can help.
|
|
27
|
+
|
|
28
|
+
## Citation
|
|
29
|
+
|
|
30
|
+
If you use this library in academic work, the preferred citation is:
|
|
31
|
+
|
|
32
|
+
de Leeuw, J.R., Gilbert, R.A., & Luchterhandt, B. (2023). jsPsych: Enabling an open-source collaborative ecosystem of behavioral experiments. *Journal of Open Source Software*, *8*(85), 5351, [https://joss.theoj.org/papers/10.21105/joss.05351](https://joss.theoj.org/papers/10.21105/joss.05351).
|
|
33
|
+
|
|
34
|
+
This paper is an updated description of jsPsych and includes all current core team members. It replaces the earlier paper that described jsPsych:
|
|
35
|
+
|
|
36
|
+
de Leeuw, J.R. (2015). jsPsych: A JavaScript library for creating behavioral experiments in a Web browser. *Behavior Research Methods*, _47_(1), 1-12. doi:[10.3758/s13428-014-0458-y](http://link.springer.com/article/10.3758%2Fs13428-014-0458-y)
|
|
37
|
+
|
|
38
|
+
Citations help us demonstrate that this library is used and valued, which allows us to continue working on it.
|
|
39
|
+
|
|
40
|
+
## Contributors
|
|
41
|
+
|
|
42
|
+
jsPsych is open source project with [numerous contributors](https://github.com/jspsych/jsPsych/graphs/contributors).
|
|
43
|
+
The project is currently managed by the core team of Josh de Leeuw ([@jodeleeuw](https://github.com/jodeleeuw)), Becky Gilbert ([@becky-gilbert](https://github.com/becky-gilbert)), and Björn Luchterhandt ([@bjoluc](https://github.com/bjoluc)).
|
|
44
|
+
|
|
45
|
+
jsPsych was created by [Josh de Leeuw](http://www.twitter.com/joshdeleeuw).
|
|
46
|
+
|
|
47
|
+
We're also grateful for the generous support from a [Mozilla Open Source Support award](https://www.mozilla.org/en-US/moss/), which funded development of the library from 2020-2021.
|
package/dist/index.browser.js
CHANGED
|
@@ -1,305 +1,305 @@
|
|
|
1
1
|
var jsPsychAudioButtonResponse = (function (jspsych) {
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
const info = {
|
|
5
|
-
name: "audio-button-response",
|
|
6
|
-
parameters: {
|
|
7
|
-
/** The audio to be played. */
|
|
8
|
-
stimulus: {
|
|
9
|
-
type: jspsych.ParameterType.AUDIO,
|
|
10
|
-
pretty_name: "Stimulus",
|
|
11
|
-
default: undefined,
|
|
12
|
-
},
|
|
13
|
-
/** Array containing the label(s) for the button(s). */
|
|
14
|
-
choices: {
|
|
15
|
-
type: jspsych.ParameterType.STRING,
|
|
16
|
-
pretty_name: "Choices",
|
|
17
|
-
default: undefined,
|
|
18
|
-
array: true,
|
|
19
|
-
},
|
|
20
|
-
/** 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. */
|
|
21
|
-
button_html: {
|
|
22
|
-
type: jspsych.ParameterType.HTML_STRING,
|
|
23
|
-
pretty_name: "Button HTML",
|
|
24
|
-
default: '<button class="jspsych-btn">%choice%</button>',
|
|
25
|
-
array: true,
|
|
26
|
-
},
|
|
27
|
-
/** Any content here will be displayed below the stimulus. */
|
|
28
|
-
prompt: {
|
|
29
|
-
type: jspsych.ParameterType.HTML_STRING,
|
|
30
|
-
pretty_name: "Prompt",
|
|
31
|
-
default: null,
|
|
32
|
-
},
|
|
33
|
-
/** The maximum duration to wait for a response. */
|
|
34
|
-
trial_duration: {
|
|
35
|
-
type: jspsych.ParameterType.INT,
|
|
36
|
-
pretty_name: "Trial duration",
|
|
37
|
-
default: null,
|
|
38
|
-
},
|
|
39
|
-
/** Vertical margin of button. */
|
|
40
|
-
margin_vertical: {
|
|
41
|
-
type: jspsych.ParameterType.STRING,
|
|
42
|
-
pretty_name: "Margin vertical",
|
|
43
|
-
default: "0px",
|
|
44
|
-
},
|
|
45
|
-
/** Horizontal margin of button. */
|
|
46
|
-
margin_horizontal: {
|
|
47
|
-
type: jspsych.ParameterType.STRING,
|
|
48
|
-
pretty_name: "Margin horizontal",
|
|
49
|
-
default: "8px",
|
|
50
|
-
},
|
|
51
|
-
/** If true, the trial will end when user makes a response. */
|
|
52
|
-
response_ends_trial: {
|
|
53
|
-
type: jspsych.ParameterType.BOOL,
|
|
54
|
-
pretty_name: "Response ends trial",
|
|
55
|
-
default: true,
|
|
56
|
-
},
|
|
57
|
-
/** If true, then the trial will end as soon as the audio file finishes playing. */
|
|
58
|
-
trial_ends_after_audio: {
|
|
59
|
-
type: jspsych.ParameterType.BOOL,
|
|
60
|
-
pretty_name: "Trial ends after audio",
|
|
61
|
-
default: false,
|
|
62
|
-
},
|
|
63
|
-
/**
|
|
64
|
-
* If true, then responses are allowed while the audio is playing.
|
|
65
|
-
* If false, then the audio must finish playing before a response is accepted.
|
|
66
|
-
*/
|
|
67
|
-
response_allowed_while_playing: {
|
|
68
|
-
type: jspsych.ParameterType.BOOL,
|
|
69
|
-
pretty_name: "Response allowed while playing",
|
|
70
|
-
default: true,
|
|
71
|
-
},
|
|
72
|
-
},
|
|
73
|
-
};
|
|
74
|
-
/**
|
|
75
|
-
* **audio-button-response**
|
|
76
|
-
*
|
|
77
|
-
* jsPsych plugin for playing an audio file and getting a button response
|
|
78
|
-
*
|
|
79
|
-
* @author Kristin Diep
|
|
80
|
-
* @see {@link https://www.jspsych.org/plugins/jspsych-audio-button-response/ audio-button-response plugin documentation on jspsych.org}
|
|
81
|
-
*/
|
|
82
|
-
class AudioButtonResponsePlugin {
|
|
83
|
-
constructor(jsPsych) {
|
|
84
|
-
this.jsPsych = jsPsych;
|
|
85
|
-
}
|
|
86
|
-
trial(display_element, trial, on_load) {
|
|
87
|
-
// hold the .resolve() function from the Promise that ends the trial
|
|
88
|
-
let trial_complete;
|
|
89
|
-
// setup stimulus
|
|
90
|
-
var context = this.jsPsych.pluginAPI.audioContext();
|
|
91
|
-
// store response
|
|
92
|
-
var response = {
|
|
93
|
-
rt: null,
|
|
94
|
-
button: null,
|
|
95
|
-
};
|
|
96
|
-
// record webaudio context start time
|
|
97
|
-
var startTime;
|
|
98
|
-
// load audio file
|
|
99
|
-
this.jsPsych.pluginAPI
|
|
100
|
-
.getAudioBuffer(trial.stimulus)
|
|
101
|
-
.then((buffer) => {
|
|
102
|
-
if (context !== null) {
|
|
103
|
-
this.audio = context.createBufferSource();
|
|
104
|
-
this.audio.buffer = buffer;
|
|
105
|
-
this.audio.connect(context.destination);
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
this.audio = buffer;
|
|
109
|
-
this.audio.currentTime = 0;
|
|
110
|
-
}
|
|
111
|
-
setupTrial();
|
|
112
|
-
})
|
|
113
|
-
.catch((err) => {
|
|
114
|
-
console.error(`Failed to load audio file "${trial.stimulus}". Try checking the file path. We recommend using the preload plugin to load audio files.`);
|
|
115
|
-
console.error(err);
|
|
116
|
-
});
|
|
117
|
-
const setupTrial = () => {
|
|
118
|
-
// set up end event if trial needs it
|
|
119
|
-
if (trial.trial_ends_after_audio) {
|
|
120
|
-
this.audio.addEventListener("ended", end_trial);
|
|
121
|
-
}
|
|
122
|
-
// enable buttons after audio ends if necessary
|
|
123
|
-
if (!trial.response_allowed_while_playing && !trial.trial_ends_after_audio) {
|
|
124
|
-
this.audio.addEventListener("ended", enable_buttons);
|
|
125
|
-
}
|
|
126
|
-
//display buttons
|
|
127
|
-
var buttons = [];
|
|
128
|
-
if (Array.isArray(trial.button_html)) {
|
|
129
|
-
if (trial.button_html.length == trial.choices.length) {
|
|
130
|
-
buttons = trial.button_html;
|
|
131
|
-
}
|
|
132
|
-
else {
|
|
133
|
-
console.error("Error in audio-button-response plugin. The length of the button_html array does not equal the length of the choices array");
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
for (var i = 0; i < trial.choices.length; i++) {
|
|
138
|
-
buttons.push(trial.button_html);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
var html = '<div id="jspsych-audio-button-response-btngroup">';
|
|
142
|
-
for (var i = 0; i < trial.choices.length; i++) {
|
|
143
|
-
var str = buttons[i].replace(/%choice%/g, trial.choices[i]);
|
|
144
|
-
html +=
|
|
145
|
-
'<div class="jspsych-audio-button-response-button" style="cursor: pointer; display: inline-block; margin:' +
|
|
146
|
-
trial.margin_vertical +
|
|
147
|
-
" " +
|
|
148
|
-
trial.margin_horizontal +
|
|
149
|
-
'" id="jspsych-audio-button-response-button-' +
|
|
150
|
-
i +
|
|
151
|
-
'" data-choice="' +
|
|
152
|
-
i +
|
|
153
|
-
'">' +
|
|
154
|
-
str +
|
|
155
|
-
"</div>";
|
|
156
|
-
}
|
|
157
|
-
html += "</div>";
|
|
158
|
-
//show prompt if there is one
|
|
159
|
-
if (trial.prompt !== null) {
|
|
160
|
-
html += trial.prompt;
|
|
161
|
-
}
|
|
162
|
-
display_element.innerHTML = html;
|
|
163
|
-
if (trial.response_allowed_while_playing) {
|
|
164
|
-
enable_buttons();
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
disable_buttons();
|
|
168
|
-
}
|
|
169
|
-
// start time
|
|
170
|
-
startTime = performance.now();
|
|
171
|
-
// start audio
|
|
172
|
-
if (context !== null) {
|
|
173
|
-
startTime = context.currentTime;
|
|
174
|
-
this.audio.start(startTime);
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
this.audio.play();
|
|
178
|
-
}
|
|
179
|
-
// end trial if time limit is set
|
|
180
|
-
if (trial.trial_duration !== null) {
|
|
181
|
-
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
182
|
-
end_trial();
|
|
183
|
-
}, trial.trial_duration);
|
|
184
|
-
}
|
|
185
|
-
on_load();
|
|
186
|
-
};
|
|
187
|
-
// function to handle responses by the subject
|
|
188
|
-
function after_response(choice) {
|
|
189
|
-
// measure rt
|
|
190
|
-
var endTime = performance.now();
|
|
191
|
-
var rt = Math.round(endTime - startTime);
|
|
192
|
-
if (context !== null) {
|
|
193
|
-
endTime = context.currentTime;
|
|
194
|
-
rt = Math.round((endTime - startTime) * 1000);
|
|
195
|
-
}
|
|
196
|
-
response.button = parseInt(choice);
|
|
197
|
-
response.rt = rt;
|
|
198
|
-
// disable all the buttons after a response
|
|
199
|
-
disable_buttons();
|
|
200
|
-
if (trial.response_ends_trial) {
|
|
201
|
-
end_trial();
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
// function to end trial when it is time
|
|
205
|
-
const end_trial = () => {
|
|
206
|
-
// kill any remaining setTimeout handlers
|
|
207
|
-
this.jsPsych.pluginAPI.clearAllTimeouts();
|
|
208
|
-
// stop the audio file if it is playing
|
|
209
|
-
// remove end event listeners if they exist
|
|
210
|
-
if (context !== null) {
|
|
211
|
-
this.audio.stop();
|
|
212
|
-
}
|
|
213
|
-
else {
|
|
214
|
-
this.audio.pause();
|
|
215
|
-
}
|
|
216
|
-
this.audio.removeEventListener("ended", end_trial);
|
|
217
|
-
this.audio.removeEventListener("ended", enable_buttons);
|
|
218
|
-
// gather the data to store for the trial
|
|
219
|
-
var trial_data = {
|
|
220
|
-
rt: response.rt,
|
|
221
|
-
stimulus: trial.stimulus,
|
|
222
|
-
response: response.button,
|
|
223
|
-
};
|
|
224
|
-
// clear the display
|
|
225
|
-
display_element.innerHTML = "";
|
|
226
|
-
// move on to the next trial
|
|
227
|
-
this.jsPsych.finishTrial(trial_data);
|
|
228
|
-
trial_complete();
|
|
229
|
-
};
|
|
230
|
-
function button_response(e) {
|
|
231
|
-
var choice = e.currentTarget.getAttribute("data-choice"); // don't use dataset for jsdom compatibility
|
|
232
|
-
after_response(choice);
|
|
233
|
-
}
|
|
234
|
-
function disable_buttons() {
|
|
235
|
-
var btns = document.querySelectorAll(".jspsych-audio-button-response-button");
|
|
236
|
-
for (var i = 0; i < btns.length; i++) {
|
|
237
|
-
var btn_el = btns[i].querySelector("button");
|
|
238
|
-
if (btn_el) {
|
|
239
|
-
btn_el.disabled = true;
|
|
240
|
-
}
|
|
241
|
-
btns[i].removeEventListener("click", button_response);
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
function enable_buttons() {
|
|
245
|
-
var btns = document.querySelectorAll(".jspsych-audio-button-response-button");
|
|
246
|
-
for (var i = 0; i < btns.length; i++) {
|
|
247
|
-
var btn_el = btns[i].querySelector("button");
|
|
248
|
-
if (btn_el) {
|
|
249
|
-
btn_el.disabled = false;
|
|
250
|
-
}
|
|
251
|
-
btns[i].addEventListener("click", button_response);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
return new Promise((resolve) => {
|
|
255
|
-
trial_complete = resolve;
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
simulate(trial, simulation_mode, simulation_options, load_callback) {
|
|
259
|
-
if (simulation_mode == "data-only") {
|
|
260
|
-
load_callback();
|
|
261
|
-
this.simulate_data_only(trial, simulation_options);
|
|
262
|
-
}
|
|
263
|
-
if (simulation_mode == "visual") {
|
|
264
|
-
this.simulate_visual(trial, simulation_options, load_callback);
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
create_simulation_data(trial, simulation_options) {
|
|
268
|
-
const default_data = {
|
|
269
|
-
stimulus: trial.stimulus,
|
|
270
|
-
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
271
|
-
response: this.jsPsych.randomization.randomInt(0, trial.choices.length - 1),
|
|
272
|
-
};
|
|
273
|
-
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
274
|
-
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
275
|
-
return data;
|
|
276
|
-
}
|
|
277
|
-
simulate_data_only(trial, simulation_options) {
|
|
278
|
-
const data = this.create_simulation_data(trial, simulation_options);
|
|
279
|
-
this.jsPsych.finishTrial(data);
|
|
280
|
-
}
|
|
281
|
-
simulate_visual(trial, simulation_options, load_callback) {
|
|
282
|
-
const data = this.create_simulation_data(trial, simulation_options);
|
|
283
|
-
const display_element = this.jsPsych.getDisplayElement();
|
|
284
|
-
const respond = () => {
|
|
285
|
-
if (data.rt !== null) {
|
|
286
|
-
this.jsPsych.pluginAPI.clickTarget(display_element.querySelector(`div[data-choice="${data.response}"] button`), data.rt);
|
|
287
|
-
}
|
|
288
|
-
};
|
|
289
|
-
this.trial(display_element, trial, () => {
|
|
290
|
-
load_callback();
|
|
291
|
-
if (!trial.response_allowed_while_playing) {
|
|
292
|
-
this.audio.addEventListener("ended", respond);
|
|
293
|
-
}
|
|
294
|
-
else {
|
|
295
|
-
respond();
|
|
296
|
-
}
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
}
|
|
4
|
+
const info = {
|
|
5
|
+
name: "audio-button-response",
|
|
6
|
+
parameters: {
|
|
7
|
+
/** The audio to be played. */
|
|
8
|
+
stimulus: {
|
|
9
|
+
type: jspsych.ParameterType.AUDIO,
|
|
10
|
+
pretty_name: "Stimulus",
|
|
11
|
+
default: undefined,
|
|
12
|
+
},
|
|
13
|
+
/** Array containing the label(s) for the button(s). */
|
|
14
|
+
choices: {
|
|
15
|
+
type: jspsych.ParameterType.STRING,
|
|
16
|
+
pretty_name: "Choices",
|
|
17
|
+
default: undefined,
|
|
18
|
+
array: true,
|
|
19
|
+
},
|
|
20
|
+
/** 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. */
|
|
21
|
+
button_html: {
|
|
22
|
+
type: jspsych.ParameterType.HTML_STRING,
|
|
23
|
+
pretty_name: "Button HTML",
|
|
24
|
+
default: '<button class="jspsych-btn">%choice%</button>',
|
|
25
|
+
array: true,
|
|
26
|
+
},
|
|
27
|
+
/** Any content here will be displayed below the stimulus. */
|
|
28
|
+
prompt: {
|
|
29
|
+
type: jspsych.ParameterType.HTML_STRING,
|
|
30
|
+
pretty_name: "Prompt",
|
|
31
|
+
default: null,
|
|
32
|
+
},
|
|
33
|
+
/** The maximum duration to wait for a response. */
|
|
34
|
+
trial_duration: {
|
|
35
|
+
type: jspsych.ParameterType.INT,
|
|
36
|
+
pretty_name: "Trial duration",
|
|
37
|
+
default: null,
|
|
38
|
+
},
|
|
39
|
+
/** Vertical margin of button. */
|
|
40
|
+
margin_vertical: {
|
|
41
|
+
type: jspsych.ParameterType.STRING,
|
|
42
|
+
pretty_name: "Margin vertical",
|
|
43
|
+
default: "0px",
|
|
44
|
+
},
|
|
45
|
+
/** Horizontal margin of button. */
|
|
46
|
+
margin_horizontal: {
|
|
47
|
+
type: jspsych.ParameterType.STRING,
|
|
48
|
+
pretty_name: "Margin horizontal",
|
|
49
|
+
default: "8px",
|
|
50
|
+
},
|
|
51
|
+
/** If true, the trial will end when user makes a response. */
|
|
52
|
+
response_ends_trial: {
|
|
53
|
+
type: jspsych.ParameterType.BOOL,
|
|
54
|
+
pretty_name: "Response ends trial",
|
|
55
|
+
default: true,
|
|
56
|
+
},
|
|
57
|
+
/** If true, then the trial will end as soon as the audio file finishes playing. */
|
|
58
|
+
trial_ends_after_audio: {
|
|
59
|
+
type: jspsych.ParameterType.BOOL,
|
|
60
|
+
pretty_name: "Trial ends after audio",
|
|
61
|
+
default: false,
|
|
62
|
+
},
|
|
63
|
+
/**
|
|
64
|
+
* If true, then responses are allowed while the audio is playing.
|
|
65
|
+
* If false, then the audio must finish playing before a response is accepted.
|
|
66
|
+
*/
|
|
67
|
+
response_allowed_while_playing: {
|
|
68
|
+
type: jspsych.ParameterType.BOOL,
|
|
69
|
+
pretty_name: "Response allowed while playing",
|
|
70
|
+
default: true,
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* **audio-button-response**
|
|
76
|
+
*
|
|
77
|
+
* jsPsych plugin for playing an audio file and getting a button response
|
|
78
|
+
*
|
|
79
|
+
* @author Kristin Diep
|
|
80
|
+
* @see {@link https://www.jspsych.org/plugins/jspsych-audio-button-response/ audio-button-response plugin documentation on jspsych.org}
|
|
81
|
+
*/
|
|
82
|
+
class AudioButtonResponsePlugin {
|
|
83
|
+
constructor(jsPsych) {
|
|
84
|
+
this.jsPsych = jsPsych;
|
|
85
|
+
}
|
|
86
|
+
trial(display_element, trial, on_load) {
|
|
87
|
+
// hold the .resolve() function from the Promise that ends the trial
|
|
88
|
+
let trial_complete;
|
|
89
|
+
// setup stimulus
|
|
90
|
+
var context = this.jsPsych.pluginAPI.audioContext();
|
|
91
|
+
// store response
|
|
92
|
+
var response = {
|
|
93
|
+
rt: null,
|
|
94
|
+
button: null,
|
|
95
|
+
};
|
|
96
|
+
// record webaudio context start time
|
|
97
|
+
var startTime;
|
|
98
|
+
// load audio file
|
|
99
|
+
this.jsPsych.pluginAPI
|
|
100
|
+
.getAudioBuffer(trial.stimulus)
|
|
101
|
+
.then((buffer) => {
|
|
102
|
+
if (context !== null) {
|
|
103
|
+
this.audio = context.createBufferSource();
|
|
104
|
+
this.audio.buffer = buffer;
|
|
105
|
+
this.audio.connect(context.destination);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
this.audio = buffer;
|
|
109
|
+
this.audio.currentTime = 0;
|
|
110
|
+
}
|
|
111
|
+
setupTrial();
|
|
112
|
+
})
|
|
113
|
+
.catch((err) => {
|
|
114
|
+
console.error(`Failed to load audio file "${trial.stimulus}". Try checking the file path. We recommend using the preload plugin to load audio files.`);
|
|
115
|
+
console.error(err);
|
|
116
|
+
});
|
|
117
|
+
const setupTrial = () => {
|
|
118
|
+
// set up end event if trial needs it
|
|
119
|
+
if (trial.trial_ends_after_audio) {
|
|
120
|
+
this.audio.addEventListener("ended", end_trial);
|
|
121
|
+
}
|
|
122
|
+
// enable buttons after audio ends if necessary
|
|
123
|
+
if (!trial.response_allowed_while_playing && !trial.trial_ends_after_audio) {
|
|
124
|
+
this.audio.addEventListener("ended", enable_buttons);
|
|
125
|
+
}
|
|
126
|
+
//display buttons
|
|
127
|
+
var buttons = [];
|
|
128
|
+
if (Array.isArray(trial.button_html)) {
|
|
129
|
+
if (trial.button_html.length == trial.choices.length) {
|
|
130
|
+
buttons = trial.button_html;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
console.error("Error in audio-button-response plugin. The length of the button_html array does not equal the length of the choices array");
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
for (var i = 0; i < trial.choices.length; i++) {
|
|
138
|
+
buttons.push(trial.button_html);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
var html = '<div id="jspsych-audio-button-response-btngroup">';
|
|
142
|
+
for (var i = 0; i < trial.choices.length; i++) {
|
|
143
|
+
var str = buttons[i].replace(/%choice%/g, trial.choices[i]);
|
|
144
|
+
html +=
|
|
145
|
+
'<div class="jspsych-audio-button-response-button" style="cursor: pointer; display: inline-block; margin:' +
|
|
146
|
+
trial.margin_vertical +
|
|
147
|
+
" " +
|
|
148
|
+
trial.margin_horizontal +
|
|
149
|
+
'" id="jspsych-audio-button-response-button-' +
|
|
150
|
+
i +
|
|
151
|
+
'" data-choice="' +
|
|
152
|
+
i +
|
|
153
|
+
'">' +
|
|
154
|
+
str +
|
|
155
|
+
"</div>";
|
|
156
|
+
}
|
|
157
|
+
html += "</div>";
|
|
158
|
+
//show prompt if there is one
|
|
159
|
+
if (trial.prompt !== null) {
|
|
160
|
+
html += trial.prompt;
|
|
161
|
+
}
|
|
162
|
+
display_element.innerHTML = html;
|
|
163
|
+
if (trial.response_allowed_while_playing) {
|
|
164
|
+
enable_buttons();
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
disable_buttons();
|
|
168
|
+
}
|
|
169
|
+
// start time
|
|
170
|
+
startTime = performance.now();
|
|
171
|
+
// start audio
|
|
172
|
+
if (context !== null) {
|
|
173
|
+
startTime = context.currentTime;
|
|
174
|
+
this.audio.start(startTime);
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
this.audio.play();
|
|
178
|
+
}
|
|
179
|
+
// end trial if time limit is set
|
|
180
|
+
if (trial.trial_duration !== null) {
|
|
181
|
+
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
182
|
+
end_trial();
|
|
183
|
+
}, trial.trial_duration);
|
|
184
|
+
}
|
|
185
|
+
on_load();
|
|
186
|
+
};
|
|
187
|
+
// function to handle responses by the subject
|
|
188
|
+
function after_response(choice) {
|
|
189
|
+
// measure rt
|
|
190
|
+
var endTime = performance.now();
|
|
191
|
+
var rt = Math.round(endTime - startTime);
|
|
192
|
+
if (context !== null) {
|
|
193
|
+
endTime = context.currentTime;
|
|
194
|
+
rt = Math.round((endTime - startTime) * 1000);
|
|
195
|
+
}
|
|
196
|
+
response.button = parseInt(choice);
|
|
197
|
+
response.rt = rt;
|
|
198
|
+
// disable all the buttons after a response
|
|
199
|
+
disable_buttons();
|
|
200
|
+
if (trial.response_ends_trial) {
|
|
201
|
+
end_trial();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
// function to end trial when it is time
|
|
205
|
+
const end_trial = () => {
|
|
206
|
+
// kill any remaining setTimeout handlers
|
|
207
|
+
this.jsPsych.pluginAPI.clearAllTimeouts();
|
|
208
|
+
// stop the audio file if it is playing
|
|
209
|
+
// remove end event listeners if they exist
|
|
210
|
+
if (context !== null) {
|
|
211
|
+
this.audio.stop();
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
this.audio.pause();
|
|
215
|
+
}
|
|
216
|
+
this.audio.removeEventListener("ended", end_trial);
|
|
217
|
+
this.audio.removeEventListener("ended", enable_buttons);
|
|
218
|
+
// gather the data to store for the trial
|
|
219
|
+
var trial_data = {
|
|
220
|
+
rt: response.rt,
|
|
221
|
+
stimulus: trial.stimulus,
|
|
222
|
+
response: response.button,
|
|
223
|
+
};
|
|
224
|
+
// clear the display
|
|
225
|
+
display_element.innerHTML = "";
|
|
226
|
+
// move on to the next trial
|
|
227
|
+
this.jsPsych.finishTrial(trial_data);
|
|
228
|
+
trial_complete();
|
|
229
|
+
};
|
|
230
|
+
function button_response(e) {
|
|
231
|
+
var choice = e.currentTarget.getAttribute("data-choice"); // don't use dataset for jsdom compatibility
|
|
232
|
+
after_response(choice);
|
|
233
|
+
}
|
|
234
|
+
function disable_buttons() {
|
|
235
|
+
var btns = document.querySelectorAll(".jspsych-audio-button-response-button");
|
|
236
|
+
for (var i = 0; i < btns.length; i++) {
|
|
237
|
+
var btn_el = btns[i].querySelector("button");
|
|
238
|
+
if (btn_el) {
|
|
239
|
+
btn_el.disabled = true;
|
|
240
|
+
}
|
|
241
|
+
btns[i].removeEventListener("click", button_response);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
function enable_buttons() {
|
|
245
|
+
var btns = document.querySelectorAll(".jspsych-audio-button-response-button");
|
|
246
|
+
for (var i = 0; i < btns.length; i++) {
|
|
247
|
+
var btn_el = btns[i].querySelector("button");
|
|
248
|
+
if (btn_el) {
|
|
249
|
+
btn_el.disabled = false;
|
|
250
|
+
}
|
|
251
|
+
btns[i].addEventListener("click", button_response);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return new Promise((resolve) => {
|
|
255
|
+
trial_complete = resolve;
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
simulate(trial, simulation_mode, simulation_options, load_callback) {
|
|
259
|
+
if (simulation_mode == "data-only") {
|
|
260
|
+
load_callback();
|
|
261
|
+
this.simulate_data_only(trial, simulation_options);
|
|
262
|
+
}
|
|
263
|
+
if (simulation_mode == "visual") {
|
|
264
|
+
this.simulate_visual(trial, simulation_options, load_callback);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
create_simulation_data(trial, simulation_options) {
|
|
268
|
+
const default_data = {
|
|
269
|
+
stimulus: trial.stimulus,
|
|
270
|
+
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
271
|
+
response: this.jsPsych.randomization.randomInt(0, trial.choices.length - 1),
|
|
272
|
+
};
|
|
273
|
+
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
274
|
+
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
275
|
+
return data;
|
|
276
|
+
}
|
|
277
|
+
simulate_data_only(trial, simulation_options) {
|
|
278
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
279
|
+
this.jsPsych.finishTrial(data);
|
|
280
|
+
}
|
|
281
|
+
simulate_visual(trial, simulation_options, load_callback) {
|
|
282
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
283
|
+
const display_element = this.jsPsych.getDisplayElement();
|
|
284
|
+
const respond = () => {
|
|
285
|
+
if (data.rt !== null) {
|
|
286
|
+
this.jsPsych.pluginAPI.clickTarget(display_element.querySelector(`div[data-choice="${data.response}"] button`), data.rt);
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
this.trial(display_element, trial, () => {
|
|
290
|
+
load_callback();
|
|
291
|
+
if (!trial.response_allowed_while_playing) {
|
|
292
|
+
this.audio.addEventListener("ended", respond);
|
|
293
|
+
}
|
|
294
|
+
else {
|
|
295
|
+
respond();
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
300
|
AudioButtonResponsePlugin.info = info;
|
|
301
301
|
|
|
302
302
|
return AudioButtonResponsePlugin;
|
|
303
303
|
|
|
304
304
|
})(jsPsychModule);
|
|
305
|
-
//# sourceMappingURL=index.browser.js.map
|
|
305
|
+
//# sourceMappingURL=https://unpkg.com/@jspsych/plugin-audio-button-response@1.1.3/dist/index.browser.js.map
|