@jspsych/plugin-audio-slider-response 1.1.3 → 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,351 +1,305 @@
1
+ import autoBind from 'auto-bind';
1
2
  import { ParameterType } from 'jspsych';
2
3
 
4
+ var _package = {
5
+ name: "@jspsych/plugin-audio-slider-response",
6
+ version: "2.0.1",
7
+ description: "",
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 --passWithNoTests",
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-slider-response"
32
+ },
33
+ author: "Josh de Leeuw",
34
+ license: "MIT",
35
+ bugs: {
36
+ url: "https://github.com/jspsych/jsPsych/issues"
37
+ },
38
+ homepage: "https://www.jspsych.org/latest/plugins/audio-slider-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-slider-response",
5
- parameters: {
6
- /** The audio file to be played. */
7
- stimulus: {
8
- type: ParameterType.AUDIO,
9
- pretty_name: "Stimulus",
10
- default: undefined,
11
- },
12
- /** Sets the minimum value of the slider. */
13
- min: {
14
- type: ParameterType.INT,
15
- pretty_name: "Min slider",
16
- default: 0,
17
- },
18
- /** Sets the maximum value of the slider */
19
- max: {
20
- type: ParameterType.INT,
21
- pretty_name: "Max slider",
22
- default: 100,
23
- },
24
- /** Sets the starting value of the slider */
25
- slider_start: {
26
- type: ParameterType.INT,
27
- pretty_name: "Slider starting value",
28
- default: 50,
29
- },
30
- /** Sets the step of the slider */
31
- step: {
32
- type: ParameterType.INT,
33
- pretty_name: "Step",
34
- default: 1,
35
- },
36
- /** Array containing the labels for the slider. Labels will be displayed at equidistant locations along the slider. */
37
- labels: {
38
- type: ParameterType.HTML_STRING,
39
- pretty_name: "Labels",
40
- default: [],
41
- array: true,
42
- },
43
- /** Width of the slider in pixels. */
44
- slider_width: {
45
- type: ParameterType.INT,
46
- pretty_name: "Slider width",
47
- default: null,
48
- },
49
- /** Label of the button to advance. */
50
- button_label: {
51
- type: ParameterType.STRING,
52
- pretty_name: "Button label",
53
- default: "Continue",
54
- array: false,
55
- },
56
- /** If true, the participant will have to move the slider before continuing. */
57
- require_movement: {
58
- type: ParameterType.BOOL,
59
- pretty_name: "Require movement",
60
- default: false,
61
- },
62
- /** Any content here will be displayed below the slider. */
63
- prompt: {
64
- type: ParameterType.HTML_STRING,
65
- pretty_name: "Prompt",
66
- default: null,
67
- },
68
- /** How long to show the trial. */
69
- trial_duration: {
70
- type: ParameterType.INT,
71
- pretty_name: "Trial duration",
72
- default: null,
73
- },
74
- /** If true, trial will end when user makes a response. */
75
- response_ends_trial: {
76
- type: ParameterType.BOOL,
77
- pretty_name: "Response ends trial",
78
- default: true,
79
- },
80
- /** If true, then the trial will end as soon as the audio file finishes playing. */
81
- trial_ends_after_audio: {
82
- type: ParameterType.BOOL,
83
- pretty_name: "Trial ends after audio",
84
- default: false,
85
- },
86
- /** If true, then responses are allowed while the audio is playing. If false, then the audio must finish playing before a response is accepted. */
87
- response_allowed_while_playing: {
88
- type: ParameterType.BOOL,
89
- pretty_name: "Response allowed while playing",
90
- default: true,
91
- },
49
+ name: "audio-slider-response",
50
+ version: _package.version,
51
+ parameters: {
52
+ stimulus: {
53
+ type: ParameterType.AUDIO,
54
+ default: void 0
55
+ },
56
+ min: {
57
+ type: ParameterType.INT,
58
+ default: 0
59
+ },
60
+ max: {
61
+ type: ParameterType.INT,
62
+ default: 100
63
+ },
64
+ slider_start: {
65
+ type: ParameterType.INT,
66
+ default: 50
67
+ },
68
+ step: {
69
+ type: ParameterType.INT,
70
+ default: 1
71
+ },
72
+ labels: {
73
+ type: ParameterType.HTML_STRING,
74
+ default: [],
75
+ array: true
76
+ },
77
+ slider_width: {
78
+ type: ParameterType.INT,
79
+ default: null
80
+ },
81
+ button_label: {
82
+ type: ParameterType.STRING,
83
+ default: "Continue",
84
+ array: false
85
+ },
86
+ require_movement: {
87
+ type: ParameterType.BOOL,
88
+ default: false
89
+ },
90
+ prompt: {
91
+ type: ParameterType.HTML_STRING,
92
+ default: null
93
+ },
94
+ trial_duration: {
95
+ type: ParameterType.INT,
96
+ default: null
97
+ },
98
+ response_ends_trial: {
99
+ type: ParameterType.BOOL,
100
+ default: true
92
101
  },
102
+ trial_ends_after_audio: {
103
+ type: ParameterType.BOOL,
104
+ default: false
105
+ },
106
+ response_allowed_while_playing: {
107
+ type: ParameterType.BOOL,
108
+ default: true
109
+ }
110
+ },
111
+ data: {
112
+ response: {
113
+ type: ParameterType.INT
114
+ },
115
+ rt: {
116
+ type: ParameterType.INT
117
+ },
118
+ stimulus: {
119
+ type: ParameterType.STRING
120
+ },
121
+ slider_start: {
122
+ type: ParameterType.INT
123
+ }
124
+ }
93
125
  };
94
- /**
95
- * **audio-slider-response**
96
- *
97
- * jsPsych plugin for playing audio and getting a slider response
98
- *
99
- * @author Josh de Leeuw
100
- * @see {@link https://www.jspsych.org/plugins/jspsych-audio-slider-response/ audio-slider-response plugin documentation on jspsych.org}
101
- */
102
126
  class AudioSliderResponsePlugin {
103
- constructor(jsPsych) {
104
- this.jsPsych = jsPsych;
105
- }
106
- trial(display_element, trial, on_load) {
107
- // hold the .resolve() function from the Promise that ends the trial
108
- let trial_complete;
109
- // half of the thumb width value from jspsych.css, used to adjust the label positions
110
- var half_thumb_width = 7.5;
111
- // setup stimulus
112
- var context = this.jsPsych.pluginAPI.audioContext();
113
- // record webaudio context start time
114
- var startTime;
115
- // for storing data related to response
116
- var response;
117
- // load audio file
118
- this.jsPsych.pluginAPI
119
- .getAudioBuffer(trial.stimulus)
120
- .then((buffer) => {
121
- if (context !== null) {
122
- this.audio = context.createBufferSource();
123
- this.audio.buffer = buffer;
124
- this.audio.connect(context.destination);
125
- }
126
- else {
127
- this.audio = buffer;
128
- this.audio.currentTime = 0;
129
- }
130
- setupTrial();
131
- })
132
- .catch((err) => {
133
- console.error(`Failed to load audio file "${trial.stimulus}". Try checking the file path. We recommend using the preload plugin to load audio files.`);
134
- console.error(err);
135
- });
136
- const setupTrial = () => {
137
- // set up end event if trial needs it
138
- if (trial.trial_ends_after_audio) {
139
- this.audio.addEventListener("ended", end_trial);
140
- }
141
- // enable slider after audio ends if necessary
142
- if (!trial.response_allowed_while_playing && !trial.trial_ends_after_audio) {
143
- this.audio.addEventListener("ended", enable_slider);
144
- }
145
- var html = '<div id="jspsych-audio-slider-response-wrapper" style="margin: 100px 0px;">';
146
- html +=
147
- '<div class="jspsych-audio-slider-response-container" style="position:relative; margin: 0 auto 3em auto; width:';
148
- if (trial.slider_width !== null) {
149
- html += trial.slider_width + "px;";
150
- }
151
- else {
152
- html += "auto;";
153
- }
154
- html += '">';
155
- html +=
156
- '<input type="range" class="jspsych-slider" value="' +
157
- trial.slider_start +
158
- '" min="' +
159
- trial.min +
160
- '" max="' +
161
- trial.max +
162
- '" step="' +
163
- trial.step +
164
- '" id="jspsych-audio-slider-response-response"';
165
- if (!trial.response_allowed_while_playing) {
166
- html += " disabled";
167
- }
168
- html += "></input><div>";
169
- for (var j = 0; j < trial.labels.length; j++) {
170
- var label_width_perc = 100 / (trial.labels.length - 1);
171
- var percent_of_range = j * (100 / (trial.labels.length - 1));
172
- var percent_dist_from_center = ((percent_of_range - 50) / 50) * 100;
173
- var offset = (percent_dist_from_center * half_thumb_width) / 100;
174
- html +=
175
- '<div style="border: 1px solid transparent; display: inline-block; position: absolute; ' +
176
- "left:calc(" +
177
- percent_of_range +
178
- "% - (" +
179
- label_width_perc +
180
- "% / 2) - " +
181
- offset +
182
- "px); text-align: center; width: " +
183
- label_width_perc +
184
- '%;">';
185
- html += '<span style="text-align: center; font-size: 80%;">' + trial.labels[j] + "</span>";
186
- html += "</div>";
187
- }
188
- html += "</div>";
189
- html += "</div>";
190
- html += "</div>";
191
- if (trial.prompt !== null) {
192
- html += trial.prompt;
193
- }
194
- // add submit button
195
- var next_disabled_attribute = "";
196
- if (trial.require_movement || !trial.response_allowed_while_playing) {
197
- next_disabled_attribute = "disabled";
198
- }
199
- html +=
200
- '<button id="jspsych-audio-slider-response-next" class="jspsych-btn" ' +
201
- next_disabled_attribute +
202
- ">" +
203
- trial.button_label +
204
- "</button>";
205
- display_element.innerHTML = html;
206
- response = {
207
- rt: null,
208
- response: null,
209
- };
210
- if (!trial.response_allowed_while_playing) {
211
- display_element.querySelector("#jspsych-audio-slider-response-response").disabled = true;
212
- display_element.querySelector("#jspsych-audio-slider-response-next").disabled = true;
213
- }
214
- if (trial.require_movement) {
215
- const enable_button = () => {
216
- display_element.querySelector("#jspsych-audio-slider-response-next").disabled = false;
217
- };
218
- display_element
219
- .querySelector("#jspsych-audio-slider-response-response")
220
- .addEventListener("mousedown", enable_button);
221
- display_element
222
- .querySelector("#jspsych-audio-slider-response-response")
223
- .addEventListener("touchstart", enable_button);
224
- display_element
225
- .querySelector("#jspsych-audio-slider-response-response")
226
- .addEventListener("change", enable_button);
227
- }
228
- display_element
229
- .querySelector("#jspsych-audio-slider-response-next")
230
- .addEventListener("click", () => {
231
- // measure response time
232
- var endTime = performance.now();
233
- var rt = Math.round(endTime - startTime);
234
- if (context !== null) {
235
- endTime = context.currentTime;
236
- rt = Math.round((endTime - startTime) * 1000);
237
- }
238
- response.rt = rt;
239
- response.response = display_element.querySelector("#jspsych-audio-slider-response-response").valueAsNumber;
240
- if (trial.response_ends_trial) {
241
- end_trial();
242
- }
243
- else {
244
- display_element.querySelector("#jspsych-audio-slider-response-next").disabled = true;
245
- }
246
- });
247
- startTime = performance.now();
248
- // start audio
249
- if (context !== null) {
250
- startTime = context.currentTime;
251
- this.audio.start(startTime);
252
- }
253
- else {
254
- this.audio.play();
255
- }
256
- // end trial if trial_duration is set
257
- if (trial.trial_duration !== null) {
258
- this.jsPsych.pluginAPI.setTimeout(() => {
259
- end_trial();
260
- }, trial.trial_duration);
261
- }
262
- on_load();
127
+ constructor(jsPsych) {
128
+ this.jsPsych = jsPsych;
129
+ this.response = { rt: null, response: null };
130
+ this.setupTrial = () => {
131
+ if (this.params.trial_ends_after_audio) {
132
+ this.audio.addEventListener("ended", this.end_trial);
133
+ }
134
+ if (!this.params.response_allowed_while_playing && !this.params.trial_ends_after_audio) {
135
+ this.audio.addEventListener("ended", this.enable_slider);
136
+ }
137
+ var html = '<div id="jspsych-audio-slider-response-wrapper" style="margin: 100px 0px;">';
138
+ html += '<div class="jspsych-audio-slider-response-container" style="position:relative; margin: 0 auto 3em auto; width:';
139
+ if (this.params.slider_width !== null) {
140
+ html += this.params.slider_width + "px;";
141
+ } else {
142
+ html += "auto;";
143
+ }
144
+ html += '">';
145
+ html += '<input type="range" class="jspsych-slider" value="' + this.params.slider_start + '" min="' + this.params.min + '" max="' + this.params.max + '" step="' + this.params.step + '" id="jspsych-audio-slider-response-response"';
146
+ if (!this.params.response_allowed_while_playing) {
147
+ html += " disabled";
148
+ }
149
+ html += "></input><div>";
150
+ for (var j = 0; j < this.params.labels.length; j++) {
151
+ var label_width_perc = 100 / (this.params.labels.length - 1);
152
+ var percent_of_range = j * (100 / (this.params.labels.length - 1));
153
+ var percent_dist_from_center = (percent_of_range - 50) / 50 * 100;
154
+ var offset = percent_dist_from_center * this.half_thumb_width / 100;
155
+ html += '<div style="border: 1px solid transparent; display: inline-block; position: absolute; left:calc(' + percent_of_range + "% - (" + label_width_perc + "% / 2) - " + offset + "px); text-align: center; width: " + label_width_perc + '%;">';
156
+ html += '<span style="text-align: center; font-size: 80%;">' + this.params.labels[j] + "</span>";
157
+ html += "</div>";
158
+ }
159
+ html += "</div>";
160
+ html += "</div>";
161
+ html += "</div>";
162
+ if (this.params.prompt !== null) {
163
+ html += this.params.prompt;
164
+ }
165
+ var next_disabled_attribute = "";
166
+ if (this.params.require_movement || !this.params.response_allowed_while_playing) {
167
+ next_disabled_attribute = "disabled";
168
+ }
169
+ html += '<button id="jspsych-audio-slider-response-next" class="jspsych-btn" ' + next_disabled_attribute + ">" + this.params.button_label + "</button>";
170
+ this.display.innerHTML = html;
171
+ this.response = {
172
+ rt: null,
173
+ response: null
174
+ };
175
+ if (!this.params.response_allowed_while_playing) {
176
+ this.display.querySelector(
177
+ "#jspsych-audio-slider-response-response"
178
+ ).disabled = true;
179
+ this.display.querySelector("#jspsych-audio-slider-response-next").disabled = true;
180
+ }
181
+ if (this.params.require_movement) {
182
+ const enable_button = () => {
183
+ this.display.querySelector(
184
+ "#jspsych-audio-slider-response-next"
185
+ ).disabled = false;
263
186
  };
264
- // function to enable slider after audio ends
265
- function enable_slider() {
266
- document.querySelector("#jspsych-audio-slider-response-response").disabled =
267
- false;
268
- if (!trial.require_movement) {
269
- document.querySelector("#jspsych-audio-slider-response-next").disabled =
270
- false;
271
- }
187
+ this.display.querySelector("#jspsych-audio-slider-response-response").addEventListener("mousedown", enable_button);
188
+ this.display.querySelector("#jspsych-audio-slider-response-response").addEventListener("touchstart", enable_button);
189
+ this.display.querySelector("#jspsych-audio-slider-response-response").addEventListener("change", enable_button);
190
+ }
191
+ this.display.querySelector("#jspsych-audio-slider-response-next").addEventListener("click", () => {
192
+ var endTime = performance.now();
193
+ var rt = Math.round(endTime - this.startTime);
194
+ if (this.context !== null) {
195
+ endTime = this.context.currentTime;
196
+ rt = Math.round((endTime - this.startTime) * 1e3);
272
197
  }
273
- const end_trial = () => {
274
- // kill any remaining setTimeout handlers
275
- this.jsPsych.pluginAPI.clearAllTimeouts();
276
- // stop the audio file if it is playing
277
- // remove end event listeners if they exist
278
- if (context !== null) {
279
- this.audio.stop();
280
- }
281
- else {
282
- this.audio.pause();
283
- }
284
- this.audio.removeEventListener("ended", end_trial);
285
- this.audio.removeEventListener("ended", enable_slider);
286
- // save data
287
- var trialdata = {
288
- rt: response.rt,
289
- stimulus: trial.stimulus,
290
- slider_start: trial.slider_start,
291
- response: response.response,
292
- };
293
- display_element.innerHTML = "";
294
- // next trial
295
- this.jsPsych.finishTrial(trialdata);
296
- trial_complete();
297
- };
298
- return new Promise((resolve) => {
299
- trial_complete = resolve;
300
- });
301
- }
302
- simulate(trial, simulation_mode, simulation_options, load_callback) {
303
- if (simulation_mode == "data-only") {
304
- load_callback();
305
- this.simulate_data_only(trial, simulation_options);
306
- }
307
- if (simulation_mode == "visual") {
308
- this.simulate_visual(trial, simulation_options, load_callback);
198
+ this.response.rt = rt;
199
+ this.response.response = this.display.querySelector(
200
+ "#jspsych-audio-slider-response-response"
201
+ ).valueAsNumber;
202
+ if (this.params.response_ends_trial) {
203
+ this.end_trial();
204
+ } else {
205
+ this.display.querySelector(
206
+ "#jspsych-audio-slider-response-next"
207
+ ).disabled = true;
309
208
  }
209
+ });
210
+ this.startTime = performance.now();
211
+ if (this.context !== null) {
212
+ this.startTime = this.context.currentTime;
213
+ }
214
+ this.audio.play();
215
+ if (this.params.trial_duration !== null) {
216
+ this.jsPsych.pluginAPI.setTimeout(() => {
217
+ this.end_trial();
218
+ }, this.params.trial_duration);
219
+ }
220
+ };
221
+ this.end_trial = () => {
222
+ this.jsPsych.pluginAPI.clearAllTimeouts();
223
+ this.audio.stop();
224
+ this.audio.removeEventListener("ended", this.end_trial);
225
+ this.audio.removeEventListener("ended", this.enable_slider);
226
+ var trialdata = {
227
+ rt: this.response.rt,
228
+ stimulus: this.params.stimulus,
229
+ slider_start: this.params.slider_start,
230
+ response: this.response.response
231
+ };
232
+ this.display.innerHTML = "";
233
+ this.trial_complete(trialdata);
234
+ };
235
+ autoBind(this);
236
+ }
237
+ async trial(display_element, trial, on_load) {
238
+ this.params = trial;
239
+ this.display = display_element;
240
+ this.response;
241
+ this.half_thumb_width = 7.5;
242
+ this.trial_complete;
243
+ this.context = this.jsPsych.pluginAPI.audioContext();
244
+ this.audio = await this.jsPsych.pluginAPI.getAudioPlayer(trial.stimulus);
245
+ this.setupTrial();
246
+ on_load();
247
+ return new Promise((resolve) => {
248
+ this.trial_complete = resolve;
249
+ });
250
+ }
251
+ enable_slider() {
252
+ document.querySelector("#jspsych-audio-slider-response-response").disabled = false;
253
+ if (!this.params.require_movement) {
254
+ document.querySelector("#jspsych-audio-slider-response-next").disabled = false;
310
255
  }
311
- create_simulation_data(trial, simulation_options) {
312
- const default_data = {
313
- stimulus: trial.stimulus,
314
- slider_start: trial.slider_start,
315
- response: this.jsPsych.randomization.randomInt(trial.min, trial.max),
316
- rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
317
- };
318
- const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
319
- this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
320
- return data;
321
- }
322
- simulate_data_only(trial, simulation_options) {
323
- const data = this.create_simulation_data(trial, simulation_options);
324
- this.jsPsych.finishTrial(data);
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);
325
261
  }
326
- simulate_visual(trial, simulation_options, load_callback) {
327
- const data = this.create_simulation_data(trial, simulation_options);
328
- const display_element = this.jsPsych.getDisplayElement();
329
- const respond = () => {
330
- if (data.rt !== null) {
331
- const el = display_element.querySelector("input[type='range']");
332
- setTimeout(() => {
333
- this.jsPsych.pluginAPI.clickTarget(el);
334
- el.valueAsNumber = data.response;
335
- }, data.rt / 2);
336
- this.jsPsych.pluginAPI.clickTarget(display_element.querySelector("button"), data.rt);
337
- }
338
- };
339
- this.trial(display_element, trial, () => {
340
- load_callback();
341
- if (!trial.response_allowed_while_playing) {
342
- this.audio.addEventListener("ended", respond);
343
- }
344
- else {
345
- respond();
346
- }
347
- });
262
+ if (simulation_mode == "visual") {
263
+ this.simulate_visual(trial, simulation_options, load_callback);
348
264
  }
265
+ }
266
+ create_simulation_data(trial, simulation_options) {
267
+ const default_data = {
268
+ stimulus: trial.stimulus,
269
+ slider_start: trial.slider_start,
270
+ response: this.jsPsych.randomization.randomInt(trial.min, trial.max),
271
+ rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true)
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
+ const el = display_element.querySelector("input[type='range']");
287
+ setTimeout(() => {
288
+ this.jsPsych.pluginAPI.clickTarget(el);
289
+ el.valueAsNumber = data.response;
290
+ }, data.rt / 2);
291
+ this.jsPsych.pluginAPI.clickTarget(display_element.querySelector("button"), data.rt);
292
+ }
293
+ };
294
+ this.trial(display_element, trial, () => {
295
+ load_callback();
296
+ if (!trial.response_allowed_while_playing) {
297
+ this.audio.addEventListener("ended", respond);
298
+ } else {
299
+ respond();
300
+ }
301
+ });
302
+ }
349
303
  }
350
304
  AudioSliderResponsePlugin.info = info;
351
305