@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/src/index.ts CHANGED
@@ -1,347 +1,370 @@
1
+ import autoBind from "auto-bind";
1
2
  import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from "jspsych";
2
3
 
4
+ import { AudioPlayerInterface } from "../../jspsych/src/modules/plugin-api/AudioPlayer";
5
+ import { version } from "../package.json";
6
+
3
7
  const info = <const>{
4
8
  name: "audio-slider-response",
9
+ version: version,
5
10
  parameters: {
6
- /** The audio file to be played. */
11
+ /** Audio file to be played. */
7
12
  stimulus: {
8
13
  type: ParameterType.AUDIO,
9
- pretty_name: "Stimulus",
10
14
  default: undefined,
11
15
  },
12
16
  /** Sets the minimum value of the slider. */
13
17
  min: {
14
18
  type: ParameterType.INT,
15
- pretty_name: "Min slider",
16
19
  default: 0,
17
20
  },
18
21
  /** Sets the maximum value of the slider */
19
22
  max: {
20
23
  type: ParameterType.INT,
21
- pretty_name: "Max slider",
22
24
  default: 100,
23
25
  },
24
26
  /** Sets the starting value of the slider */
25
27
  slider_start: {
26
28
  type: ParameterType.INT,
27
- pretty_name: "Slider starting value",
28
29
  default: 50,
29
30
  },
30
- /** Sets the step of the slider */
31
+ /** Sets the step of the slider. This is the smallest amount by which the slider can change. */
31
32
  step: {
32
33
  type: ParameterType.INT,
33
- pretty_name: "Step",
34
34
  default: 1,
35
35
  },
36
- /** Array containing the labels for the slider. Labels will be displayed at equidistant locations along the slider. */
36
+ /** Labels displayed at equidistant locations on the slider. For example, two labels will be placed at the ends of the
37
+ * slider. Three labels would place two at the ends and one in the middle. Four will place two at the ends, and the
38
+ * other two will be at 33% and 67% of the slider width.
39
+ */
37
40
  labels: {
38
41
  type: ParameterType.HTML_STRING,
39
- pretty_name: "Labels",
40
42
  default: [],
41
43
  array: true,
42
44
  },
43
- /** Width of the slider in pixels. */
45
+ /** Set the width of the slider in pixels. If left null, then the width will be equal to the widest element in the display. */
44
46
  slider_width: {
45
47
  type: ParameterType.INT,
46
- pretty_name: "Slider width",
47
48
  default: null,
48
49
  },
49
- /** Label of the button to advance. */
50
+ /** Label of the button to end the trial. */
50
51
  button_label: {
51
52
  type: ParameterType.STRING,
52
- pretty_name: "Button label",
53
53
  default: "Continue",
54
54
  array: false,
55
55
  },
56
- /** If true, the participant will have to move the slider before continuing. */
56
+ /** If true, the participant must move the slider before clicking the continue button. */
57
57
  require_movement: {
58
58
  type: ParameterType.BOOL,
59
- pretty_name: "Require movement",
60
59
  default: false,
61
60
  },
62
- /** Any content here will be displayed below the slider. */
61
+ /** This string can contain HTML markup. Any content here will be displayed below the stimulus. The intention is
62
+ * that it can be used to provide a reminder about the action the participant is supposed to take (e.g., which key to press).
63
+ */
63
64
  prompt: {
64
65
  type: ParameterType.HTML_STRING,
65
- pretty_name: "Prompt",
66
66
  default: null,
67
67
  },
68
- /** How long to show the trial. */
68
+ /** How long to wait for the participant to make a response before ending the trial in milliseconds. If
69
+ * the participant fails to make a response before this timer is reached, the participant's response will be
70
+ * recorded as null for the trial and the trial will end. If the value of this parameter is null, then the trial
71
+ * will wait for a response indefinitely.
72
+ */
69
73
  trial_duration: {
70
74
  type: ParameterType.INT,
71
- pretty_name: "Trial duration",
72
75
  default: null,
73
76
  },
74
- /** If true, trial will end when user makes a response. */
77
+ /** If true, then the trial will end whenever the participant makes a response (assuming they make their response
78
+ * before the cutoff specified by the `trial_duration` parameter). If false, then the trial will continue until the
79
+ * value for `trial_duration` is reached. You can set this parameter to `false` to force the participant to listen to
80
+ * the stimulus for a fixed amount of time, even if they respond before the time is complete.
81
+ */
75
82
  response_ends_trial: {
76
83
  type: ParameterType.BOOL,
77
- pretty_name: "Response ends trial",
78
84
  default: true,
79
85
  },
80
86
  /** If true, then the trial will end as soon as the audio file finishes playing. */
81
87
  trial_ends_after_audio: {
82
88
  type: ParameterType.BOOL,
83
- pretty_name: "Trial ends after audio",
84
89
  default: false,
85
90
  },
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. */
91
+ /** If true, then responses are allowed while the audio is playing. If false, then the audio must finish playing before
92
+ * the slider is enabled and the trial can end via the next button click. Once the audio has played all the way through,
93
+ * the slider is enabled and a response is allowed (including while the audio is being re-played via on-screen playback controls).
94
+ */
87
95
  response_allowed_while_playing: {
88
96
  type: ParameterType.BOOL,
89
- pretty_name: "Response allowed while playing",
90
97
  default: true,
91
98
  },
92
99
  },
100
+ data: {
101
+ /** The numeric value of the slider. */
102
+ response: {
103
+ type: ParameterType.INT,
104
+ },
105
+ /** The time in milliseconds for the participant to make a response. The time is measured from when the stimulus first
106
+ * began playing until the participant's response.
107
+ */
108
+ rt: {
109
+ type: ParameterType.INT,
110
+ },
111
+ /** The path of the audio file that was played. */
112
+ stimulus: {
113
+ type: ParameterType.STRING,
114
+ },
115
+ /** The starting value of the slider. */
116
+ slider_start: {
117
+ type: ParameterType.INT,
118
+ },
119
+ },
93
120
  };
94
121
 
95
122
  type Info = typeof info;
96
123
 
97
124
  /**
98
- * **audio-slider-response**
125
+ * This plugin plays an audio file and allows the participant to respond by dragging a slider.
126
+ *
127
+ * If the browser supports it, audio files are played using the WebAudio API. This allows for reasonably precise timing of the
128
+ * playback. The timing of responses generated is measured against the WebAudio specific clock, improving the measurement of
129
+ * response times. If the browser does not support the WebAudio API, then the audio file is played with HTML5 audio.
99
130
  *
100
- * jsPsych plugin for playing audio and getting a slider response
131
+ * Audio files can be automatically preloaded by jsPsych using the [`preload` plugin](preload.md). However, if you are using
132
+ * timeline variables or another dynamic method to specify the audio stimulus, then you will need
133
+ * to [manually preload](../overview/media-preloading.md#manual-preloading) the audio.
101
134
  *
135
+ * The trial can end when the participant responds, or if the participant has failed to respond within a fixed length of time. You can also prevent the slider response from being made before the audio has finished playing.
102
136
  * @author Josh de Leeuw
103
- * @see {@link https://www.jspsych.org/plugins/jspsych-audio-slider-response/ audio-slider-response plugin documentation on jspsych.org}
137
+ * @see {@link https://www.jspsych.org/latest/plugins/audio-slider-response/ audio-slider-response plugin documentation on jspsych.org}
104
138
  */
105
139
  class AudioSliderResponsePlugin implements JsPsychPlugin<Info> {
106
140
  static info = info;
107
- private audio;
108
-
109
- constructor(private jsPsych: JsPsych) {}
110
-
111
- trial(display_element: HTMLElement, trial: TrialType<Info>, on_load: () => void) {
112
- // hold the .resolve() function from the Promise that ends the trial
113
- let trial_complete;
141
+ private audio: AudioPlayerInterface;
142
+ private context: AudioContext;
143
+ private params: TrialType<Info>;
144
+ private display: HTMLElement;
145
+ private response: { rt: number; response: number } = { rt: null, response: null };
146
+ private startTime: number;
147
+ private half_thumb_width: number;
148
+ private trial_complete: (trial_data: {
149
+ rt: number;
150
+ slider_start: number;
151
+ response: number;
152
+ }) => void;
153
+
154
+ constructor(private jsPsych: JsPsych) {
155
+ autoBind(this);
156
+ }
114
157
 
158
+ async trial(display_element: HTMLElement, trial: TrialType<Info>, on_load: () => void) {
159
+ this.params = trial;
160
+ this.display = display_element;
161
+ // for storing data related to response
162
+ this.response;
115
163
  // half of the thumb width value from jspsych.css, used to adjust the label positions
116
- var half_thumb_width = 7.5;
164
+ this.half_thumb_width = 7.5;
165
+ // hold the .resolve() function from the Promise that ends the trial
166
+ this.trial_complete;
117
167
 
118
168
  // setup stimulus
119
- var context = this.jsPsych.pluginAPI.audioContext();
169
+ this.context = this.jsPsych.pluginAPI.audioContext();
120
170
 
121
- // record webaudio context start time
122
- var startTime;
171
+ // load audio file
172
+ this.audio = await this.jsPsych.pluginAPI.getAudioPlayer(trial.stimulus);
123
173
 
124
- // for storing data related to response
125
- var response;
174
+ this.setupTrial();
126
175
 
127
- // load audio file
128
- this.jsPsych.pluginAPI
129
- .getAudioBuffer(trial.stimulus)
130
- .then((buffer) => {
131
- if (context !== null) {
132
- this.audio = context.createBufferSource();
133
- this.audio.buffer = buffer;
134
- this.audio.connect(context.destination);
135
- } else {
136
- this.audio = buffer;
137
- this.audio.currentTime = 0;
138
- }
139
- setupTrial();
140
- })
141
- .catch((err) => {
142
- console.error(
143
- `Failed to load audio file "${trial.stimulus}". Try checking the file path. We recommend using the preload plugin to load audio files.`
144
- );
145
- console.error(err);
146
- });
176
+ on_load();
147
177
 
148
- const setupTrial = () => {
149
- // set up end event if trial needs it
150
- if (trial.trial_ends_after_audio) {
151
- this.audio.addEventListener("ended", end_trial);
152
- }
178
+ return new Promise((resolve) => {
179
+ this.trial_complete = resolve;
180
+ });
181
+ }
153
182
 
154
- // enable slider after audio ends if necessary
155
- if (!trial.response_allowed_while_playing && !trial.trial_ends_after_audio) {
156
- this.audio.addEventListener("ended", enable_slider);
157
- }
183
+ // to enable slider after audio ends
184
+ private enable_slider() {
185
+ document.querySelector<HTMLInputElement>("#jspsych-audio-slider-response-response").disabled =
186
+ false;
187
+ if (!this.params.require_movement) {
188
+ document.querySelector<HTMLButtonElement>("#jspsych-audio-slider-response-next").disabled =
189
+ false;
190
+ }
191
+ }
192
+
193
+ private setupTrial = () => {
194
+ // set up end event if trial needs it
195
+ if (this.params.trial_ends_after_audio) {
196
+ this.audio.addEventListener("ended", this.end_trial);
197
+ }
158
198
 
159
- var html = '<div id="jspsych-audio-slider-response-wrapper" style="margin: 100px 0px;">';
199
+ // enable slider after audio ends if necessary
200
+ if (!this.params.response_allowed_while_playing && !this.params.trial_ends_after_audio) {
201
+ this.audio.addEventListener("ended", this.enable_slider);
202
+ }
203
+
204
+ var html = '<div id="jspsych-audio-slider-response-wrapper" style="margin: 100px 0px;">';
205
+ html +=
206
+ '<div class="jspsych-audio-slider-response-container" style="position:relative; margin: 0 auto 3em auto; width:';
207
+ if (this.params.slider_width !== null) {
208
+ html += this.params.slider_width + "px;";
209
+ } else {
210
+ html += "auto;";
211
+ }
212
+ html += '">';
213
+ html +=
214
+ '<input type="range" class="jspsych-slider" value="' +
215
+ this.params.slider_start +
216
+ '" min="' +
217
+ this.params.min +
218
+ '" max="' +
219
+ this.params.max +
220
+ '" step="' +
221
+ this.params.step +
222
+ '" id="jspsych-audio-slider-response-response"';
223
+ if (!this.params.response_allowed_while_playing) {
224
+ html += " disabled";
225
+ }
226
+ html += "></input><div>";
227
+ for (var j = 0; j < this.params.labels.length; j++) {
228
+ var label_width_perc = 100 / (this.params.labels.length - 1);
229
+ var percent_of_range = j * (100 / (this.params.labels.length - 1));
230
+ var percent_dist_from_center = ((percent_of_range - 50) / 50) * 100;
231
+ var offset = (percent_dist_from_center * this.half_thumb_width) / 100;
160
232
  html +=
161
- '<div class="jspsych-audio-slider-response-container" style="position:relative; margin: 0 auto 3em auto; width:';
162
- if (trial.slider_width !== null) {
163
- html += trial.slider_width + "px;";
164
- } else {
165
- html += "auto;";
166
- }
167
- html += '">';
233
+ '<div style="border: 1px solid transparent; display: inline-block; position: absolute; ' +
234
+ "left:calc(" +
235
+ percent_of_range +
236
+ "% - (" +
237
+ label_width_perc +
238
+ "% / 2) - " +
239
+ offset +
240
+ "px); text-align: center; width: " +
241
+ label_width_perc +
242
+ '%;">';
168
243
  html +=
169
- '<input type="range" class="jspsych-slider" value="' +
170
- trial.slider_start +
171
- '" min="' +
172
- trial.min +
173
- '" max="' +
174
- trial.max +
175
- '" step="' +
176
- trial.step +
177
- '" id="jspsych-audio-slider-response-response"';
178
- if (!trial.response_allowed_while_playing) {
179
- html += " disabled";
180
- }
181
- html += "></input><div>";
182
- for (var j = 0; j < trial.labels.length; j++) {
183
- var label_width_perc = 100 / (trial.labels.length - 1);
184
- var percent_of_range = j * (100 / (trial.labels.length - 1));
185
- var percent_dist_from_center = ((percent_of_range - 50) / 50) * 100;
186
- var offset = (percent_dist_from_center * half_thumb_width) / 100;
187
- html +=
188
- '<div style="border: 1px solid transparent; display: inline-block; position: absolute; ' +
189
- "left:calc(" +
190
- percent_of_range +
191
- "% - (" +
192
- label_width_perc +
193
- "% / 2) - " +
194
- offset +
195
- "px); text-align: center; width: " +
196
- label_width_perc +
197
- '%;">';
198
- html += '<span style="text-align: center; font-size: 80%;">' + trial.labels[j] + "</span>";
199
- html += "</div>";
200
- }
201
- html += "</div>";
202
- html += "</div>";
244
+ '<span style="text-align: center; font-size: 80%;">' + this.params.labels[j] + "</span>";
203
245
  html += "</div>";
246
+ }
247
+ html += "</div>";
248
+ html += "</div>";
249
+ html += "</div>";
204
250
 
205
- if (trial.prompt !== null) {
206
- html += trial.prompt;
207
- }
208
-
209
- // add submit button
210
- var next_disabled_attribute = "";
211
- if (trial.require_movement || !trial.response_allowed_while_playing) {
212
- next_disabled_attribute = "disabled";
213
- }
214
- html +=
215
- '<button id="jspsych-audio-slider-response-next" class="jspsych-btn" ' +
216
- next_disabled_attribute +
217
- ">" +
218
- trial.button_label +
219
- "</button>";
251
+ if (this.params.prompt !== null) {
252
+ html += this.params.prompt;
253
+ }
220
254
 
221
- display_element.innerHTML = html;
255
+ // add submit button
256
+ var next_disabled_attribute = "";
257
+ if (this.params.require_movement || !this.params.response_allowed_while_playing) {
258
+ next_disabled_attribute = "disabled";
259
+ }
260
+ html +=
261
+ '<button id="jspsych-audio-slider-response-next" class="jspsych-btn" ' +
262
+ next_disabled_attribute +
263
+ ">" +
264
+ this.params.button_label +
265
+ "</button>";
266
+
267
+ this.display.innerHTML = html;
268
+
269
+ this.response = {
270
+ rt: null,
271
+ response: null,
272
+ };
222
273
 
223
- response = {
224
- rt: null,
225
- response: null,
226
- };
274
+ if (!this.params.response_allowed_while_playing) {
275
+ this.display.querySelector<HTMLInputElement>(
276
+ "#jspsych-audio-slider-response-response"
277
+ ).disabled = true;
278
+ this.display.querySelector<HTMLInputElement>("#jspsych-audio-slider-response-next").disabled =
279
+ true;
280
+ }
227
281
 
228
- if (!trial.response_allowed_while_playing) {
229
- display_element.querySelector<HTMLInputElement>(
230
- "#jspsych-audio-slider-response-response"
231
- ).disabled = true;
232
- display_element.querySelector<HTMLInputElement>(
282
+ if (this.params.require_movement) {
283
+ const enable_button = () => {
284
+ this.display.querySelector<HTMLInputElement>(
233
285
  "#jspsych-audio-slider-response-next"
234
- ).disabled = true;
235
- }
236
-
237
- if (trial.require_movement) {
238
- const enable_button = () => {
239
- display_element.querySelector<HTMLInputElement>(
240
- "#jspsych-audio-slider-response-next"
241
- ).disabled = false;
242
- };
286
+ ).disabled = false;
287
+ };
243
288
 
244
- display_element
245
- .querySelector("#jspsych-audio-slider-response-response")
246
- .addEventListener("mousedown", enable_button);
289
+ this.display
290
+ .querySelector("#jspsych-audio-slider-response-response")
291
+ .addEventListener("mousedown", enable_button);
247
292
 
248
- display_element
249
- .querySelector("#jspsych-audio-slider-response-response")
250
- .addEventListener("touchstart", enable_button);
293
+ this.display
294
+ .querySelector("#jspsych-audio-slider-response-response")
295
+ .addEventListener("touchstart", enable_button);
251
296
 
252
- display_element
253
- .querySelector("#jspsych-audio-slider-response-response")
254
- .addEventListener("change", enable_button);
255
- }
256
-
257
- display_element
258
- .querySelector("#jspsych-audio-slider-response-next")
259
- .addEventListener("click", () => {
260
- // measure response time
261
- var endTime = performance.now();
262
- var rt = Math.round(endTime - startTime);
263
- if (context !== null) {
264
- endTime = context.currentTime;
265
- rt = Math.round((endTime - startTime) * 1000);
266
- }
267
- response.rt = rt;
268
- response.response = display_element.querySelector<HTMLInputElement>(
269
- "#jspsych-audio-slider-response-response"
270
- ).valueAsNumber;
271
-
272
- if (trial.response_ends_trial) {
273
- end_trial();
274
- } else {
275
- display_element.querySelector<HTMLInputElement>(
276
- "#jspsych-audio-slider-response-next"
277
- ).disabled = true;
278
- }
279
- });
280
-
281
- startTime = performance.now();
282
- // start audio
283
- if (context !== null) {
284
- startTime = context.currentTime;
285
- this.audio.start(startTime);
286
- } else {
287
- this.audio.play();
288
- }
297
+ this.display
298
+ .querySelector("#jspsych-audio-slider-response-response")
299
+ .addEventListener("change", enable_button);
300
+ }
289
301
 
290
- // end trial if trial_duration is set
291
- if (trial.trial_duration !== null) {
292
- this.jsPsych.pluginAPI.setTimeout(() => {
293
- end_trial();
294
- }, trial.trial_duration);
295
- }
302
+ this.display
303
+ .querySelector("#jspsych-audio-slider-response-next")
304
+ .addEventListener("click", () => {
305
+ // measure response time
306
+ var endTime = performance.now();
307
+ var rt = Math.round(endTime - this.startTime);
308
+ if (this.context !== null) {
309
+ endTime = this.context.currentTime;
310
+ rt = Math.round((endTime - this.startTime) * 1000);
311
+ }
312
+ this.response.rt = rt;
313
+ this.response.response = this.display.querySelector<HTMLInputElement>(
314
+ "#jspsych-audio-slider-response-response"
315
+ ).valueAsNumber;
296
316
 
297
- on_load();
298
- };
317
+ if (this.params.response_ends_trial) {
318
+ this.end_trial();
319
+ } else {
320
+ this.display.querySelector<HTMLInputElement>(
321
+ "#jspsych-audio-slider-response-next"
322
+ ).disabled = true;
323
+ }
324
+ });
299
325
 
300
- // function to enable slider after audio ends
301
- function enable_slider() {
302
- document.querySelector<HTMLInputElement>("#jspsych-audio-slider-response-response").disabled =
303
- false;
304
- if (!trial.require_movement) {
305
- document.querySelector<HTMLButtonElement>("#jspsych-audio-slider-response-next").disabled =
306
- false;
307
- }
326
+ //record start time
327
+ this.startTime = performance.now();
328
+ // record webaudio context start time
329
+ if (this.context !== null) {
330
+ this.startTime = this.context.currentTime;
308
331
  }
309
332
 
310
- const end_trial = () => {
311
- // kill any remaining setTimeout handlers
312
- this.jsPsych.pluginAPI.clearAllTimeouts();
333
+ // start audio
334
+ this.audio.play();
313
335
 
314
- // stop the audio file if it is playing
315
- // remove end event listeners if they exist
316
- if (context !== null) {
317
- this.audio.stop();
318
- } else {
319
- this.audio.pause();
320
- }
336
+ // end trial if trial_duration is set
337
+ if (this.params.trial_duration !== null) {
338
+ this.jsPsych.pluginAPI.setTimeout(() => {
339
+ this.end_trial();
340
+ }, this.params.trial_duration);
341
+ }
342
+ };
321
343
 
322
- this.audio.removeEventListener("ended", end_trial);
323
- this.audio.removeEventListener("ended", enable_slider);
344
+ private end_trial = () => {
345
+ // kill any remaining setTimeout handlers
346
+ this.jsPsych.pluginAPI.clearAllTimeouts();
324
347
 
325
- // save data
326
- var trialdata = {
327
- rt: response.rt,
328
- stimulus: trial.stimulus,
329
- slider_start: trial.slider_start,
330
- response: response.response,
331
- };
348
+ // stop the audio file if it is playing
349
+ this.audio.stop();
332
350
 
333
- display_element.innerHTML = "";
351
+ // remove end event listeners if they exist
352
+ this.audio.removeEventListener("ended", this.end_trial);
353
+ this.audio.removeEventListener("ended", this.enable_slider);
334
354
 
335
- // next trial
336
- this.jsPsych.finishTrial(trialdata);
337
-
338
- trial_complete();
355
+ // save data
356
+ var trialdata = {
357
+ rt: this.response.rt,
358
+ stimulus: this.params.stimulus,
359
+ slider_start: this.params.slider_start,
360
+ response: this.response.response,
339
361
  };
340
362
 
341
- return new Promise((resolve) => {
342
- trial_complete = resolve;
343
- });
344
- }
363
+ this.display.innerHTML = "";
364
+
365
+ // next trial
366
+ this.trial_complete(trialdata);
367
+ };
345
368
 
346
369
  simulate(
347
370
  trial: TrialType<Info>,