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

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,319 +1,275 @@
1
+ import autoBind from 'auto-bind';
1
2
  import { ParameterType } from 'jspsych';
2
3
 
4
+ var _package = {
5
+ name: "@jspsych/plugin-audio-button-response",
6
+ version: "2.0.1",
7
+ description: "jsPsych plugin for playing an audio file and getting a button response",
8
+ type: "module",
9
+ main: "dist/index.cjs",
10
+ exports: {
11
+ import: "./dist/index.js",
12
+ require: "./dist/index.cjs"
13
+ },
14
+ typings: "dist/index.d.ts",
15
+ unpkg: "dist/index.browser.min.js",
16
+ files: [
17
+ "src",
18
+ "dist"
19
+ ],
20
+ source: "src/index.ts",
21
+ scripts: {
22
+ test: "jest",
23
+ "test:watch": "npm test -- --watch",
24
+ tsc: "tsc",
25
+ build: "rollup --config",
26
+ "build:watch": "npm run build -- --watch"
27
+ },
28
+ repository: {
29
+ type: "git",
30
+ url: "git+https://github.com/jspsych/jsPsych.git",
31
+ directory: "packages/plugin-audio-button-response"
32
+ },
33
+ author: "Kristin Diep",
34
+ license: "MIT",
35
+ bugs: {
36
+ url: "https://github.com/jspsych/jsPsych/issues"
37
+ },
38
+ homepage: "https://www.jspsych.org/latest/plugins/audio-button-response",
39
+ peerDependencies: {
40
+ jspsych: ">=7.1.0"
41
+ },
42
+ devDependencies: {
43
+ "@jspsych/config": "^3.0.0",
44
+ "@jspsych/test-utils": "^1.2.0"
45
+ }
46
+ };
47
+
3
48
  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
- },
49
+ name: "audio-button-response",
50
+ version: _package.version,
51
+ parameters: {
52
+ stimulus: {
53
+ type: ParameterType.AUDIO,
54
+ default: void 0
55
+ },
56
+ choices: {
57
+ type: ParameterType.STRING,
58
+ default: void 0,
59
+ array: true
60
+ },
61
+ button_html: {
62
+ type: ParameterType.FUNCTION,
63
+ default: function(choice, choice_index) {
64
+ return `<button class="jspsych-btn">${choice}</button>`;
65
+ }
66
+ },
67
+ prompt: {
68
+ type: ParameterType.HTML_STRING,
69
+ default: null
70
+ },
71
+ trial_duration: {
72
+ type: ParameterType.INT,
73
+ default: null
74
+ },
75
+ button_layout: {
76
+ type: ParameterType.STRING,
77
+ default: "grid"
78
+ },
79
+ grid_rows: {
80
+ type: ParameterType.INT,
81
+ default: 1
82
+ },
83
+ grid_columns: {
84
+ type: ParameterType.INT,
85
+ default: null
86
+ },
87
+ response_ends_trial: {
88
+ type: ParameterType.BOOL,
89
+ default: true
77
90
  },
91
+ trial_ends_after_audio: {
92
+ type: ParameterType.BOOL,
93
+ default: false
94
+ },
95
+ response_allowed_while_playing: {
96
+ type: ParameterType.BOOL,
97
+ default: true
98
+ },
99
+ enable_button_after: {
100
+ type: ParameterType.INT,
101
+ default: 0
102
+ }
103
+ },
104
+ data: {
105
+ rt: {
106
+ type: ParameterType.INT
107
+ },
108
+ response: {
109
+ type: ParameterType.INT
110
+ }
111
+ }
78
112
  };
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
113
  class AudioButtonResponsePlugin {
88
- constructor(jsPsych) {
89
- this.jsPsych = jsPsych;
114
+ constructor(jsPsych) {
115
+ this.jsPsych = jsPsych;
116
+ this.buttonElements = [];
117
+ this.response = { rt: null, button: null };
118
+ this.disable_buttons = () => {
119
+ for (const button of this.buttonElements) {
120
+ button.setAttribute("disabled", "disabled");
121
+ }
122
+ };
123
+ this.enable_buttons_without_delay = () => {
124
+ for (const button of this.buttonElements) {
125
+ button.removeAttribute("disabled");
126
+ }
127
+ };
128
+ this.enable_buttons_with_delay = (delay) => {
129
+ this.jsPsych.pluginAPI.setTimeout(this.enable_buttons_without_delay, delay);
130
+ };
131
+ this.after_response = (choice) => {
132
+ var endTime = performance.now();
133
+ var rt = Math.round(endTime - this.startTime);
134
+ if (this.context !== null) {
135
+ endTime = this.context.currentTime;
136
+ rt = Math.round((endTime - this.startTime) * 1e3);
137
+ }
138
+ this.response.button = parseInt(choice);
139
+ this.response.rt = rt;
140
+ this.disable_buttons();
141
+ if (this.params.response_ends_trial) {
142
+ this.end_trial();
143
+ }
144
+ };
145
+ this.end_trial = () => {
146
+ this.audio.stop();
147
+ this.audio.removeEventListener("ended", this.end_trial);
148
+ this.audio.removeEventListener("ended", this.enable_buttons);
149
+ var trial_data = {
150
+ rt: this.response.rt,
151
+ stimulus: this.params.stimulus,
152
+ response: this.response.button
153
+ };
154
+ this.trial_complete(trial_data);
155
+ };
156
+ autoBind(this);
157
+ }
158
+ async trial(display_element, trial, on_load) {
159
+ this.params = trial;
160
+ this.display = display_element;
161
+ this.context = this.jsPsych.pluginAPI.audioContext();
162
+ this.audio = await this.jsPsych.pluginAPI.getAudioPlayer(trial.stimulus);
163
+ if (trial.trial_ends_after_audio) {
164
+ this.audio.addEventListener("ended", this.end_trial);
165
+ }
166
+ if (!trial.response_allowed_while_playing && !trial.trial_ends_after_audio) {
167
+ this.audio.addEventListener("ended", this.enable_buttons);
168
+ }
169
+ const buttonGroupElement = document.createElement("div");
170
+ buttonGroupElement.id = "jspsych-audio-button-response-btngroup";
171
+ if (trial.button_layout === "grid") {
172
+ buttonGroupElement.classList.add("jspsych-btn-group-grid");
173
+ if (trial.grid_rows === null && trial.grid_columns === null) {
174
+ throw new Error(
175
+ "You cannot set `grid_rows` to `null` without providing a value for `grid_columns`."
176
+ );
177
+ }
178
+ const n_cols = trial.grid_columns === null ? Math.ceil(trial.choices.length / trial.grid_rows) : trial.grid_columns;
179
+ const n_rows = trial.grid_rows === null ? Math.ceil(trial.choices.length / trial.grid_columns) : trial.grid_rows;
180
+ buttonGroupElement.style.gridTemplateColumns = `repeat(${n_cols}, 1fr)`;
181
+ buttonGroupElement.style.gridTemplateRows = `repeat(${n_rows}, 1fr)`;
182
+ } else if (trial.button_layout === "flex") {
183
+ buttonGroupElement.classList.add("jspsych-btn-group-flex");
184
+ }
185
+ for (const [choiceIndex, choice] of trial.choices.entries()) {
186
+ buttonGroupElement.insertAdjacentHTML("beforeend", trial.button_html(choice, choiceIndex));
187
+ const buttonElement = buttonGroupElement.lastChild;
188
+ buttonElement.dataset.choice = choiceIndex.toString();
189
+ buttonElement.addEventListener("click", () => {
190
+ this.after_response(choiceIndex);
191
+ });
192
+ this.buttonElements.push(buttonElement);
193
+ }
194
+ display_element.appendChild(buttonGroupElement);
195
+ if (trial.prompt !== null) {
196
+ display_element.insertAdjacentHTML("beforeend", trial.prompt);
197
+ }
198
+ if (trial.response_allowed_while_playing) {
199
+ if (trial.enable_button_after > 0) {
200
+ this.disable_buttons();
201
+ this.enable_buttons();
202
+ }
203
+ } else {
204
+ this.disable_buttons();
90
205
  }
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
- });
206
+ if (trial.trial_duration !== null) {
207
+ this.jsPsych.pluginAPI.setTimeout(() => {
208
+ this.end_trial();
209
+ }, trial.trial_duration);
274
210
  }
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
- }
211
+ on_load();
212
+ this.startTime = performance.now();
213
+ if (this.context !== null) {
214
+ this.startTime = this.context.currentTime;
283
215
  }
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;
216
+ this.audio.play();
217
+ return new Promise((resolve) => {
218
+ this.trial_complete = resolve;
219
+ });
220
+ }
221
+ enable_buttons() {
222
+ if (this.params.enable_button_after > 0) {
223
+ this.enable_buttons_with_delay(this.params.enable_button_after);
224
+ } else {
225
+ this.enable_buttons_without_delay();
294
226
  }
295
- simulate_data_only(trial, simulation_options) {
296
- const data = this.create_simulation_data(trial, simulation_options);
297
- this.jsPsych.finishTrial(data);
227
+ }
228
+ async simulate(trial, simulation_mode, simulation_options, load_callback) {
229
+ if (simulation_mode == "data-only") {
230
+ load_callback();
231
+ this.simulate_data_only(trial, simulation_options);
298
232
  }
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
- });
233
+ if (simulation_mode == "visual") {
234
+ this.simulate_visual(trial, simulation_options, load_callback);
316
235
  }
236
+ }
237
+ create_simulation_data(trial, simulation_options) {
238
+ const default_data = {
239
+ stimulus: trial.stimulus,
240
+ rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true) + trial.enable_button_after,
241
+ response: this.jsPsych.randomization.randomInt(0, trial.choices.length - 1)
242
+ };
243
+ const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
244
+ this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
245
+ return data;
246
+ }
247
+ simulate_data_only(trial, simulation_options) {
248
+ const data = this.create_simulation_data(trial, simulation_options);
249
+ this.jsPsych.finishTrial(data);
250
+ }
251
+ simulate_visual(trial, simulation_options, load_callback) {
252
+ const data = this.create_simulation_data(trial, simulation_options);
253
+ const display_element = this.jsPsych.getDisplayElement();
254
+ const respond = () => {
255
+ if (data.rt !== null) {
256
+ this.jsPsych.pluginAPI.clickTarget(
257
+ display_element.querySelector(
258
+ `#jspsych-audio-button-response-btngroup [data-choice="${data.response}"]`
259
+ ),
260
+ data.rt
261
+ );
262
+ }
263
+ };
264
+ this.trial(display_element, trial, () => {
265
+ load_callback();
266
+ if (!trial.response_allowed_while_playing) {
267
+ this.audio.addEventListener("ended", respond);
268
+ } else {
269
+ respond();
270
+ }
271
+ });
272
+ }
317
273
  }
318
274
  AudioButtonResponsePlugin.info = info;
319
275