@jspsych/plugin-audio-button-response 1.1.2 → 1.2.0

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/dist/index.js CHANGED
@@ -1,301 +1,320 @@
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
+ /** The delay of enabling button */
72
+ enable_button_after: {
73
+ type: ParameterType.INT,
74
+ pretty_name: "Enable button after",
75
+ default: 0,
76
+ },
77
+ },
78
+ };
79
+ /**
80
+ * **audio-button-response**
81
+ *
82
+ * jsPsych plugin for playing an audio file and getting a button response
83
+ *
84
+ * @author Kristin Diep
85
+ * @see {@link https://www.jspsych.org/plugins/jspsych-audio-button-response/ audio-button-response plugin documentation on jspsych.org}
86
+ */
87
+ class AudioButtonResponsePlugin {
88
+ constructor(jsPsych) {
89
+ this.jsPsych = jsPsych;
90
+ }
91
+ trial(display_element, trial, on_load) {
92
+ // hold the .resolve() function from the Promise that ends the trial
93
+ let trial_complete;
94
+ // setup stimulus
95
+ var context = this.jsPsych.pluginAPI.audioContext();
96
+ // store response
97
+ var response = {
98
+ rt: null,
99
+ button: null,
100
+ };
101
+ // record webaudio context start time
102
+ var startTime;
103
+ // load audio file
104
+ this.jsPsych.pluginAPI
105
+ .getAudioBuffer(trial.stimulus)
106
+ .then((buffer) => {
107
+ if (context !== null) {
108
+ this.audio = context.createBufferSource();
109
+ this.audio.buffer = buffer;
110
+ this.audio.connect(context.destination);
111
+ }
112
+ else {
113
+ this.audio = buffer;
114
+ this.audio.currentTime = 0;
115
+ }
116
+ setupTrial();
117
+ })
118
+ .catch((err) => {
119
+ console.error(`Failed to load audio file "${trial.stimulus}". Try checking the file path. We recommend using the preload plugin to load audio files.`);
120
+ console.error(err);
121
+ });
122
+ const setupTrial = () => {
123
+ // set up end event if trial needs it
124
+ if (trial.trial_ends_after_audio) {
125
+ this.audio.addEventListener("ended", end_trial);
126
+ }
127
+ // enable buttons after audio ends if necessary
128
+ if (!trial.response_allowed_while_playing && !trial.trial_ends_after_audio) {
129
+ this.audio.addEventListener("ended", enable_buttons);
130
+ }
131
+ //display buttons
132
+ var buttons = [];
133
+ if (Array.isArray(trial.button_html)) {
134
+ if (trial.button_html.length == trial.choices.length) {
135
+ buttons = trial.button_html;
136
+ }
137
+ else {
138
+ console.error("Error in audio-button-response plugin. The length of the button_html array does not equal the length of the choices array");
139
+ }
140
+ }
141
+ else {
142
+ for (var i = 0; i < trial.choices.length; i++) {
143
+ buttons.push(trial.button_html);
144
+ }
145
+ }
146
+ var html = '<div id="jspsych-audio-button-response-btngroup">';
147
+ for (var i = 0; i < trial.choices.length; i++) {
148
+ var str = buttons[i].replace(/%choice%/g, trial.choices[i]);
149
+ html +=
150
+ '<div class="jspsych-audio-button-response-button" style="cursor: pointer; display: inline-block; margin:' +
151
+ trial.margin_vertical +
152
+ " " +
153
+ trial.margin_horizontal +
154
+ '" id="jspsych-audio-button-response-button-' +
155
+ i +
156
+ '" data-choice="' +
157
+ i +
158
+ '">' +
159
+ str +
160
+ "</div>";
161
+ }
162
+ html += "</div>";
163
+ //show prompt if there is one
164
+ if (trial.prompt !== null) {
165
+ html += trial.prompt;
166
+ }
167
+ display_element.innerHTML = html;
168
+ if (trial.response_allowed_while_playing) {
169
+ disable_buttons();
170
+ enable_buttons();
171
+ }
172
+ else {
173
+ disable_buttons();
174
+ }
175
+ // start time
176
+ startTime = performance.now();
177
+ // start audio
178
+ if (context !== null) {
179
+ startTime = context.currentTime;
180
+ this.audio.start(startTime);
181
+ }
182
+ else {
183
+ this.audio.play();
184
+ }
185
+ // end trial if time limit is set
186
+ if (trial.trial_duration !== null) {
187
+ this.jsPsych.pluginAPI.setTimeout(() => {
188
+ end_trial();
189
+ }, trial.trial_duration);
190
+ }
191
+ on_load();
192
+ };
193
+ // function to handle responses by the subject
194
+ function after_response(choice) {
195
+ // measure rt
196
+ var endTime = performance.now();
197
+ var rt = Math.round(endTime - startTime);
198
+ if (context !== null) {
199
+ endTime = context.currentTime;
200
+ rt = Math.round((endTime - startTime) * 1000);
201
+ }
202
+ response.button = parseInt(choice);
203
+ response.rt = rt;
204
+ // disable all the buttons after a response
205
+ disable_buttons();
206
+ if (trial.response_ends_trial) {
207
+ end_trial();
208
+ }
209
+ }
210
+ // function to end trial when it is time
211
+ const end_trial = () => {
212
+ // kill any remaining setTimeout handlers
213
+ this.jsPsych.pluginAPI.clearAllTimeouts();
214
+ // stop the audio file if it is playing
215
+ // remove end event listeners if they exist
216
+ if (context !== null) {
217
+ this.audio.stop();
218
+ }
219
+ else {
220
+ this.audio.pause();
221
+ }
222
+ this.audio.removeEventListener("ended", end_trial);
223
+ this.audio.removeEventListener("ended", enable_buttons);
224
+ // gather the data to store for the trial
225
+ var trial_data = {
226
+ rt: response.rt,
227
+ stimulus: trial.stimulus,
228
+ response: response.button,
229
+ };
230
+ // clear the display
231
+ display_element.innerHTML = "";
232
+ // move on to the next trial
233
+ this.jsPsych.finishTrial(trial_data);
234
+ trial_complete();
235
+ };
236
+ const enable_buttons_with_delay = (delay) => {
237
+ this.jsPsych.pluginAPI.setTimeout(enable_buttons_without_delay, delay);
238
+ };
239
+ function button_response(e) {
240
+ var choice = e.currentTarget.getAttribute("data-choice"); // don't use dataset for jsdom compatibility
241
+ after_response(choice);
242
+ }
243
+ function disable_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 = true;
249
+ }
250
+ btns[i].removeEventListener("click", button_response);
251
+ }
252
+ }
253
+ function enable_buttons_without_delay() {
254
+ var btns = document.querySelectorAll(".jspsych-audio-button-response-button");
255
+ for (var i = 0; i < btns.length; i++) {
256
+ var btn_el = btns[i].querySelector("button");
257
+ if (btn_el) {
258
+ btn_el.disabled = false;
259
+ }
260
+ btns[i].addEventListener("click", button_response);
261
+ }
262
+ }
263
+ function enable_buttons() {
264
+ if (trial.enable_button_after > 0) {
265
+ enable_buttons_with_delay(trial.enable_button_after);
266
+ }
267
+ else {
268
+ enable_buttons_without_delay();
269
+ }
270
+ }
271
+ return new Promise((resolve) => {
272
+ trial_complete = resolve;
273
+ });
274
+ }
275
+ simulate(trial, simulation_mode, simulation_options, load_callback) {
276
+ if (simulation_mode == "data-only") {
277
+ load_callback();
278
+ this.simulate_data_only(trial, simulation_options);
279
+ }
280
+ if (simulation_mode == "visual") {
281
+ this.simulate_visual(trial, simulation_options, load_callback);
282
+ }
283
+ }
284
+ create_simulation_data(trial, simulation_options) {
285
+ const default_data = {
286
+ stimulus: trial.stimulus,
287
+ rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true) +
288
+ trial.enable_button_after,
289
+ response: this.jsPsych.randomization.randomInt(0, trial.choices.length - 1),
290
+ };
291
+ const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
292
+ this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
293
+ return data;
294
+ }
295
+ simulate_data_only(trial, simulation_options) {
296
+ const data = this.create_simulation_data(trial, simulation_options);
297
+ this.jsPsych.finishTrial(data);
298
+ }
299
+ simulate_visual(trial, simulation_options, load_callback) {
300
+ const data = this.create_simulation_data(trial, simulation_options);
301
+ const display_element = this.jsPsych.getDisplayElement();
302
+ const respond = () => {
303
+ if (data.rt !== null) {
304
+ this.jsPsych.pluginAPI.clickTarget(display_element.querySelector(`div[data-choice="${data.response}"] button`), data.rt);
305
+ }
306
+ };
307
+ this.trial(display_element, trial, () => {
308
+ load_callback();
309
+ if (!trial.response_allowed_while_playing) {
310
+ this.audio.addEventListener("ended", respond);
311
+ }
312
+ else {
313
+ respond();
314
+ }
315
+ });
316
+ }
317
+ }
299
318
  AudioButtonResponsePlugin.info = info;
300
319
 
301
320
  export { AudioButtonResponsePlugin as default };