@jspsych/plugin-audio-slider-response 1.1.2 → 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/README.md +8 -2
- package/dist/index.browser.js +348 -348
- package/dist/index.browser.min.js +2 -2
- package/dist/index.browser.min.js.map +1 -1
- package/dist/index.cjs +347 -347
- package/dist/index.d.ts +203 -203
- package/dist/index.js +347 -347
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,352 +1,352 @@
|
|
|
1
1
|
import { ParameterType } from 'jspsych';
|
|
2
2
|
|
|
3
|
-
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
|
-
},
|
|
92
|
-
},
|
|
93
|
-
};
|
|
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
|
-
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();
|
|
263
|
-
};
|
|
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
|
-
}
|
|
272
|
-
}
|
|
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);
|
|
309
|
-
}
|
|
310
|
-
}
|
|
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);
|
|
325
|
-
}
|
|
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
|
-
});
|
|
348
|
-
}
|
|
349
|
-
}
|
|
3
|
+
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
|
+
},
|
|
92
|
+
},
|
|
93
|
+
};
|
|
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
|
+
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();
|
|
263
|
+
};
|
|
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
|
+
}
|
|
272
|
+
}
|
|
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);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
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);
|
|
325
|
+
}
|
|
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
|
+
});
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
350
|
AudioSliderResponsePlugin.info = info;
|
|
351
351
|
|
|
352
352
|
export { AudioSliderResponsePlugin as default };
|