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