@jspsych/plugin-audio-keyboard-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 +230 -230
- package/dist/index.browser.min.js +2 -2
- package/dist/index.browser.min.js.map +1 -1
- package/dist/index.cjs +229 -229
- package/dist/index.d.ts +115 -115
- package/dist/index.js +229 -229
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -27,9 +27,15 @@ See the [contributing to jsPsych](https://www.jspsych.org/latest/developers/cont
|
|
|
27
27
|
|
|
28
28
|
## Citation
|
|
29
29
|
|
|
30
|
-
If you use this library in academic work,
|
|
30
|
+
If you use this library in academic work, the preferred citation is:
|
|
31
31
|
|
|
32
|
-
de Leeuw, J.R. (
|
|
32
|
+
de Leeuw, J.R., Gilbert, R.A., & Luchterhandt, B. (2023). jsPsych: Enabling an open-source collaborative ecosystem of behavioral experiments. *Journal of Open Source Software*, *8*(85), 5351, [https://joss.theoj.org/papers/10.21105/joss.05351](https://joss.theoj.org/papers/10.21105/joss.05351).
|
|
33
|
+
|
|
34
|
+
This paper is an updated description of jsPsych and includes all current core team members. It replaces the earlier paper that described jsPsych:
|
|
35
|
+
|
|
36
|
+
de Leeuw, J.R. (2015). jsPsych: A JavaScript library for creating behavioral experiments in a Web browser. *Behavior Research Methods*, _47_(1), 1-12. doi:[10.3758/s13428-014-0458-y](http://link.springer.com/article/10.3758%2Fs13428-014-0458-y)
|
|
37
|
+
|
|
38
|
+
Citations help us demonstrate that this library is used and valued, which allows us to continue working on it.
|
|
33
39
|
|
|
34
40
|
## Contributors
|
|
35
41
|
|
package/dist/index.browser.js
CHANGED
|
@@ -1,238 +1,238 @@
|
|
|
1
1
|
var jsPsychAudioKeyboardResponse = (function (jspsych) {
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
const info = {
|
|
5
|
-
name: "audio-keyboard-response",
|
|
6
|
-
parameters: {
|
|
7
|
-
/** The audio file to be played. */
|
|
8
|
-
stimulus: {
|
|
9
|
-
type: jspsych.ParameterType.AUDIO,
|
|
10
|
-
pretty_name: "Stimulus",
|
|
11
|
-
default: undefined,
|
|
12
|
-
},
|
|
13
|
-
/** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */
|
|
14
|
-
choices: {
|
|
15
|
-
type: jspsych.ParameterType.KEYS,
|
|
16
|
-
pretty_name: "Choices",
|
|
17
|
-
default: "ALL_KEYS",
|
|
18
|
-
},
|
|
19
|
-
/** Any content here will be displayed below the stimulus. */
|
|
20
|
-
prompt: {
|
|
21
|
-
type: jspsych.ParameterType.HTML_STRING,
|
|
22
|
-
pretty_name: "Prompt",
|
|
23
|
-
default: null,
|
|
24
|
-
},
|
|
25
|
-
/** The maximum duration to wait for a response. */
|
|
26
|
-
trial_duration: {
|
|
27
|
-
type: jspsych.ParameterType.INT,
|
|
28
|
-
pretty_name: "Trial duration",
|
|
29
|
-
default: null,
|
|
30
|
-
},
|
|
31
|
-
/** If true, the trial will end when user makes a response. */
|
|
32
|
-
response_ends_trial: {
|
|
33
|
-
type: jspsych.ParameterType.BOOL,
|
|
34
|
-
pretty_name: "Response ends trial",
|
|
35
|
-
default: true,
|
|
36
|
-
},
|
|
37
|
-
/** If true, then the trial will end as soon as the audio file finishes playing. */
|
|
38
|
-
trial_ends_after_audio: {
|
|
39
|
-
type: jspsych.ParameterType.BOOL,
|
|
40
|
-
pretty_name: "Trial ends after audio",
|
|
41
|
-
default: false,
|
|
42
|
-
},
|
|
43
|
-
/** If true, then responses are allowed while the audio is playing. If false, then the audio must finish playing before a response is accepted. */
|
|
44
|
-
response_allowed_while_playing: {
|
|
45
|
-
type: jspsych.ParameterType.BOOL,
|
|
46
|
-
pretty_name: "Response allowed while playing",
|
|
47
|
-
default: true,
|
|
48
|
-
},
|
|
49
|
-
},
|
|
50
|
-
};
|
|
51
|
-
/**
|
|
52
|
-
* **audio-keyboard-response**
|
|
53
|
-
*
|
|
54
|
-
* jsPsych plugin for playing an audio file and getting a keyboard response
|
|
55
|
-
*
|
|
56
|
-
* @author Josh de Leeuw
|
|
57
|
-
* @see {@link https://www.jspsych.org/plugins/jspsych-audio-keyboard-response/ audio-keyboard-response plugin documentation on jspsych.org}
|
|
58
|
-
*/
|
|
59
|
-
class AudioKeyboardResponsePlugin {
|
|
60
|
-
constructor(jsPsych) {
|
|
61
|
-
this.jsPsych = jsPsych;
|
|
62
|
-
}
|
|
63
|
-
trial(display_element, trial, on_load) {
|
|
64
|
-
// hold the .resolve() function from the Promise that ends the trial
|
|
65
|
-
let trial_complete;
|
|
66
|
-
// setup stimulus
|
|
67
|
-
var context = this.jsPsych.pluginAPI.audioContext();
|
|
68
|
-
// store response
|
|
69
|
-
var response = {
|
|
70
|
-
rt: null,
|
|
71
|
-
key: null,
|
|
72
|
-
};
|
|
73
|
-
// record webaudio context start time
|
|
74
|
-
var startTime;
|
|
75
|
-
// load audio file
|
|
76
|
-
this.jsPsych.pluginAPI
|
|
77
|
-
.getAudioBuffer(trial.stimulus)
|
|
78
|
-
.then((buffer) => {
|
|
79
|
-
if (context !== null) {
|
|
80
|
-
this.audio = context.createBufferSource();
|
|
81
|
-
this.audio.buffer = buffer;
|
|
82
|
-
this.audio.connect(context.destination);
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
this.audio = buffer;
|
|
86
|
-
this.audio.currentTime = 0;
|
|
87
|
-
}
|
|
88
|
-
setupTrial();
|
|
89
|
-
})
|
|
90
|
-
.catch((err) => {
|
|
91
|
-
console.error(`Failed to load audio file "${trial.stimulus}". Try checking the file path. We recommend using the preload plugin to load audio files.`);
|
|
92
|
-
console.error(err);
|
|
93
|
-
});
|
|
94
|
-
const setupTrial = () => {
|
|
95
|
-
// set up end event if trial needs it
|
|
96
|
-
if (trial.trial_ends_after_audio) {
|
|
97
|
-
this.audio.addEventListener("ended", end_trial);
|
|
98
|
-
}
|
|
99
|
-
// show prompt if there is one
|
|
100
|
-
if (trial.prompt !== null) {
|
|
101
|
-
display_element.innerHTML = trial.prompt;
|
|
102
|
-
}
|
|
103
|
-
// start audio
|
|
104
|
-
if (context !== null) {
|
|
105
|
-
startTime = context.currentTime;
|
|
106
|
-
this.audio.start(startTime);
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
this.audio.play();
|
|
110
|
-
}
|
|
111
|
-
// start keyboard listener when trial starts or sound ends
|
|
112
|
-
if (trial.response_allowed_while_playing) {
|
|
113
|
-
setup_keyboard_listener();
|
|
114
|
-
}
|
|
115
|
-
else if (!trial.trial_ends_after_audio) {
|
|
116
|
-
this.audio.addEventListener("ended", setup_keyboard_listener);
|
|
117
|
-
}
|
|
118
|
-
// end trial if time limit is set
|
|
119
|
-
if (trial.trial_duration !== null) {
|
|
120
|
-
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
121
|
-
end_trial();
|
|
122
|
-
}, trial.trial_duration);
|
|
123
|
-
}
|
|
124
|
-
on_load();
|
|
125
|
-
};
|
|
126
|
-
// function to end trial when it is time
|
|
127
|
-
const end_trial = () => {
|
|
128
|
-
// kill any remaining setTimeout handlers
|
|
129
|
-
this.jsPsych.pluginAPI.clearAllTimeouts();
|
|
130
|
-
// stop the audio file if it is playing
|
|
131
|
-
// remove end event listeners if they exist
|
|
132
|
-
if (context !== null) {
|
|
133
|
-
this.audio.stop();
|
|
134
|
-
}
|
|
135
|
-
else {
|
|
136
|
-
this.audio.pause();
|
|
137
|
-
}
|
|
138
|
-
this.audio.removeEventListener("ended", end_trial);
|
|
139
|
-
this.audio.removeEventListener("ended", setup_keyboard_listener);
|
|
140
|
-
// kill keyboard listeners
|
|
141
|
-
this.jsPsych.pluginAPI.cancelAllKeyboardResponses();
|
|
142
|
-
// gather the data to store for the trial
|
|
143
|
-
var trial_data = {
|
|
144
|
-
rt: response.rt,
|
|
145
|
-
stimulus: trial.stimulus,
|
|
146
|
-
response: response.key,
|
|
147
|
-
};
|
|
148
|
-
// clear the display
|
|
149
|
-
display_element.innerHTML = "";
|
|
150
|
-
// move on to the next trial
|
|
151
|
-
this.jsPsych.finishTrial(trial_data);
|
|
152
|
-
trial_complete();
|
|
153
|
-
};
|
|
154
|
-
// function to handle responses by the subject
|
|
155
|
-
function after_response(info) {
|
|
156
|
-
// only record the first response
|
|
157
|
-
if (response.key == null) {
|
|
158
|
-
response = info;
|
|
159
|
-
}
|
|
160
|
-
if (trial.response_ends_trial) {
|
|
161
|
-
end_trial();
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
const setup_keyboard_listener = () => {
|
|
165
|
-
// start the response listener
|
|
166
|
-
if (context !== null) {
|
|
167
|
-
this.jsPsych.pluginAPI.getKeyboardResponse({
|
|
168
|
-
callback_function: after_response,
|
|
169
|
-
valid_responses: trial.choices,
|
|
170
|
-
rt_method: "audio",
|
|
171
|
-
persist: false,
|
|
172
|
-
allow_held_key: false,
|
|
173
|
-
audio_context: context,
|
|
174
|
-
audio_context_start_time: startTime,
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
else {
|
|
178
|
-
this.jsPsych.pluginAPI.getKeyboardResponse({
|
|
179
|
-
callback_function: after_response,
|
|
180
|
-
valid_responses: trial.choices,
|
|
181
|
-
rt_method: "performance",
|
|
182
|
-
persist: false,
|
|
183
|
-
allow_held_key: false,
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
|
-
return new Promise((resolve) => {
|
|
188
|
-
trial_complete = resolve;
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
simulate(trial, simulation_mode, simulation_options, load_callback) {
|
|
192
|
-
if (simulation_mode == "data-only") {
|
|
193
|
-
load_callback();
|
|
194
|
-
this.simulate_data_only(trial, simulation_options);
|
|
195
|
-
}
|
|
196
|
-
if (simulation_mode == "visual") {
|
|
197
|
-
this.simulate_visual(trial, simulation_options, load_callback);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
simulate_data_only(trial, simulation_options) {
|
|
201
|
-
const data = this.create_simulation_data(trial, simulation_options);
|
|
202
|
-
this.jsPsych.finishTrial(data);
|
|
203
|
-
}
|
|
204
|
-
simulate_visual(trial, simulation_options, load_callback) {
|
|
205
|
-
const data = this.create_simulation_data(trial, simulation_options);
|
|
206
|
-
const display_element = this.jsPsych.getDisplayElement();
|
|
207
|
-
const respond = () => {
|
|
208
|
-
if (data.rt !== null) {
|
|
209
|
-
this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
|
|
210
|
-
}
|
|
211
|
-
};
|
|
212
|
-
this.trial(display_element, trial, () => {
|
|
213
|
-
load_callback();
|
|
214
|
-
if (!trial.response_allowed_while_playing) {
|
|
215
|
-
this.audio.addEventListener("ended", respond);
|
|
216
|
-
}
|
|
217
|
-
else {
|
|
218
|
-
respond();
|
|
219
|
-
}
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
create_simulation_data(trial, simulation_options) {
|
|
223
|
-
const default_data = {
|
|
224
|
-
stimulus: trial.stimulus,
|
|
225
|
-
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
226
|
-
response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
|
|
227
|
-
};
|
|
228
|
-
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
229
|
-
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
230
|
-
return data;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
4
|
+
const info = {
|
|
5
|
+
name: "audio-keyboard-response",
|
|
6
|
+
parameters: {
|
|
7
|
+
/** The audio file to be played. */
|
|
8
|
+
stimulus: {
|
|
9
|
+
type: jspsych.ParameterType.AUDIO,
|
|
10
|
+
pretty_name: "Stimulus",
|
|
11
|
+
default: undefined,
|
|
12
|
+
},
|
|
13
|
+
/** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */
|
|
14
|
+
choices: {
|
|
15
|
+
type: jspsych.ParameterType.KEYS,
|
|
16
|
+
pretty_name: "Choices",
|
|
17
|
+
default: "ALL_KEYS",
|
|
18
|
+
},
|
|
19
|
+
/** Any content here will be displayed below the stimulus. */
|
|
20
|
+
prompt: {
|
|
21
|
+
type: jspsych.ParameterType.HTML_STRING,
|
|
22
|
+
pretty_name: "Prompt",
|
|
23
|
+
default: null,
|
|
24
|
+
},
|
|
25
|
+
/** The maximum duration to wait for a response. */
|
|
26
|
+
trial_duration: {
|
|
27
|
+
type: jspsych.ParameterType.INT,
|
|
28
|
+
pretty_name: "Trial duration",
|
|
29
|
+
default: null,
|
|
30
|
+
},
|
|
31
|
+
/** If true, the trial will end when user makes a response. */
|
|
32
|
+
response_ends_trial: {
|
|
33
|
+
type: jspsych.ParameterType.BOOL,
|
|
34
|
+
pretty_name: "Response ends trial",
|
|
35
|
+
default: true,
|
|
36
|
+
},
|
|
37
|
+
/** If true, then the trial will end as soon as the audio file finishes playing. */
|
|
38
|
+
trial_ends_after_audio: {
|
|
39
|
+
type: jspsych.ParameterType.BOOL,
|
|
40
|
+
pretty_name: "Trial ends after audio",
|
|
41
|
+
default: false,
|
|
42
|
+
},
|
|
43
|
+
/** If true, then responses are allowed while the audio is playing. If false, then the audio must finish playing before a response is accepted. */
|
|
44
|
+
response_allowed_while_playing: {
|
|
45
|
+
type: jspsych.ParameterType.BOOL,
|
|
46
|
+
pretty_name: "Response allowed while playing",
|
|
47
|
+
default: true,
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* **audio-keyboard-response**
|
|
53
|
+
*
|
|
54
|
+
* jsPsych plugin for playing an audio file and getting a keyboard response
|
|
55
|
+
*
|
|
56
|
+
* @author Josh de Leeuw
|
|
57
|
+
* @see {@link https://www.jspsych.org/plugins/jspsych-audio-keyboard-response/ audio-keyboard-response plugin documentation on jspsych.org}
|
|
58
|
+
*/
|
|
59
|
+
class AudioKeyboardResponsePlugin {
|
|
60
|
+
constructor(jsPsych) {
|
|
61
|
+
this.jsPsych = jsPsych;
|
|
62
|
+
}
|
|
63
|
+
trial(display_element, trial, on_load) {
|
|
64
|
+
// hold the .resolve() function from the Promise that ends the trial
|
|
65
|
+
let trial_complete;
|
|
66
|
+
// setup stimulus
|
|
67
|
+
var context = this.jsPsych.pluginAPI.audioContext();
|
|
68
|
+
// store response
|
|
69
|
+
var response = {
|
|
70
|
+
rt: null,
|
|
71
|
+
key: null,
|
|
72
|
+
};
|
|
73
|
+
// record webaudio context start time
|
|
74
|
+
var startTime;
|
|
75
|
+
// load audio file
|
|
76
|
+
this.jsPsych.pluginAPI
|
|
77
|
+
.getAudioBuffer(trial.stimulus)
|
|
78
|
+
.then((buffer) => {
|
|
79
|
+
if (context !== null) {
|
|
80
|
+
this.audio = context.createBufferSource();
|
|
81
|
+
this.audio.buffer = buffer;
|
|
82
|
+
this.audio.connect(context.destination);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
this.audio = buffer;
|
|
86
|
+
this.audio.currentTime = 0;
|
|
87
|
+
}
|
|
88
|
+
setupTrial();
|
|
89
|
+
})
|
|
90
|
+
.catch((err) => {
|
|
91
|
+
console.error(`Failed to load audio file "${trial.stimulus}". Try checking the file path. We recommend using the preload plugin to load audio files.`);
|
|
92
|
+
console.error(err);
|
|
93
|
+
});
|
|
94
|
+
const setupTrial = () => {
|
|
95
|
+
// set up end event if trial needs it
|
|
96
|
+
if (trial.trial_ends_after_audio) {
|
|
97
|
+
this.audio.addEventListener("ended", end_trial);
|
|
98
|
+
}
|
|
99
|
+
// show prompt if there is one
|
|
100
|
+
if (trial.prompt !== null) {
|
|
101
|
+
display_element.innerHTML = trial.prompt;
|
|
102
|
+
}
|
|
103
|
+
// start audio
|
|
104
|
+
if (context !== null) {
|
|
105
|
+
startTime = context.currentTime;
|
|
106
|
+
this.audio.start(startTime);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
this.audio.play();
|
|
110
|
+
}
|
|
111
|
+
// start keyboard listener when trial starts or sound ends
|
|
112
|
+
if (trial.response_allowed_while_playing) {
|
|
113
|
+
setup_keyboard_listener();
|
|
114
|
+
}
|
|
115
|
+
else if (!trial.trial_ends_after_audio) {
|
|
116
|
+
this.audio.addEventListener("ended", setup_keyboard_listener);
|
|
117
|
+
}
|
|
118
|
+
// end trial if time limit is set
|
|
119
|
+
if (trial.trial_duration !== null) {
|
|
120
|
+
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
121
|
+
end_trial();
|
|
122
|
+
}, trial.trial_duration);
|
|
123
|
+
}
|
|
124
|
+
on_load();
|
|
125
|
+
};
|
|
126
|
+
// function to end trial when it is time
|
|
127
|
+
const end_trial = () => {
|
|
128
|
+
// kill any remaining setTimeout handlers
|
|
129
|
+
this.jsPsych.pluginAPI.clearAllTimeouts();
|
|
130
|
+
// stop the audio file if it is playing
|
|
131
|
+
// remove end event listeners if they exist
|
|
132
|
+
if (context !== null) {
|
|
133
|
+
this.audio.stop();
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
this.audio.pause();
|
|
137
|
+
}
|
|
138
|
+
this.audio.removeEventListener("ended", end_trial);
|
|
139
|
+
this.audio.removeEventListener("ended", setup_keyboard_listener);
|
|
140
|
+
// kill keyboard listeners
|
|
141
|
+
this.jsPsych.pluginAPI.cancelAllKeyboardResponses();
|
|
142
|
+
// gather the data to store for the trial
|
|
143
|
+
var trial_data = {
|
|
144
|
+
rt: response.rt,
|
|
145
|
+
stimulus: trial.stimulus,
|
|
146
|
+
response: response.key,
|
|
147
|
+
};
|
|
148
|
+
// clear the display
|
|
149
|
+
display_element.innerHTML = "";
|
|
150
|
+
// move on to the next trial
|
|
151
|
+
this.jsPsych.finishTrial(trial_data);
|
|
152
|
+
trial_complete();
|
|
153
|
+
};
|
|
154
|
+
// function to handle responses by the subject
|
|
155
|
+
function after_response(info) {
|
|
156
|
+
// only record the first response
|
|
157
|
+
if (response.key == null) {
|
|
158
|
+
response = info;
|
|
159
|
+
}
|
|
160
|
+
if (trial.response_ends_trial) {
|
|
161
|
+
end_trial();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
const setup_keyboard_listener = () => {
|
|
165
|
+
// start the response listener
|
|
166
|
+
if (context !== null) {
|
|
167
|
+
this.jsPsych.pluginAPI.getKeyboardResponse({
|
|
168
|
+
callback_function: after_response,
|
|
169
|
+
valid_responses: trial.choices,
|
|
170
|
+
rt_method: "audio",
|
|
171
|
+
persist: false,
|
|
172
|
+
allow_held_key: false,
|
|
173
|
+
audio_context: context,
|
|
174
|
+
audio_context_start_time: startTime,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
this.jsPsych.pluginAPI.getKeyboardResponse({
|
|
179
|
+
callback_function: after_response,
|
|
180
|
+
valid_responses: trial.choices,
|
|
181
|
+
rt_method: "performance",
|
|
182
|
+
persist: false,
|
|
183
|
+
allow_held_key: false,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
return new Promise((resolve) => {
|
|
188
|
+
trial_complete = resolve;
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
simulate(trial, simulation_mode, simulation_options, load_callback) {
|
|
192
|
+
if (simulation_mode == "data-only") {
|
|
193
|
+
load_callback();
|
|
194
|
+
this.simulate_data_only(trial, simulation_options);
|
|
195
|
+
}
|
|
196
|
+
if (simulation_mode == "visual") {
|
|
197
|
+
this.simulate_visual(trial, simulation_options, load_callback);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
simulate_data_only(trial, simulation_options) {
|
|
201
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
202
|
+
this.jsPsych.finishTrial(data);
|
|
203
|
+
}
|
|
204
|
+
simulate_visual(trial, simulation_options, load_callback) {
|
|
205
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
206
|
+
const display_element = this.jsPsych.getDisplayElement();
|
|
207
|
+
const respond = () => {
|
|
208
|
+
if (data.rt !== null) {
|
|
209
|
+
this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
this.trial(display_element, trial, () => {
|
|
213
|
+
load_callback();
|
|
214
|
+
if (!trial.response_allowed_while_playing) {
|
|
215
|
+
this.audio.addEventListener("ended", respond);
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
respond();
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
create_simulation_data(trial, simulation_options) {
|
|
223
|
+
const default_data = {
|
|
224
|
+
stimulus: trial.stimulus,
|
|
225
|
+
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
226
|
+
response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
|
|
227
|
+
};
|
|
228
|
+
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
229
|
+
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
230
|
+
return data;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
233
|
AudioKeyboardResponsePlugin.info = info;
|
|
234
234
|
|
|
235
235
|
return AudioKeyboardResponsePlugin;
|
|
236
236
|
|
|
237
237
|
})(jsPsychModule);
|
|
238
|
-
//# sourceMappingURL=index.browser.js.map
|
|
238
|
+
//# sourceMappingURL=https://unpkg.com/@jspsych/plugin-audio-keyboard-response@1.1.3/dist/index.browser.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var jsPsychAudioKeyboardResponse=function(e){"use strict";function t(e,t){for(var
|
|
2
|
-
//# sourceMappingURL=index.browser.min.js.map
|
|
1
|
+
var jsPsychAudioKeyboardResponse=function(e){"use strict";function t(e,t){for(var i=0;i<t.length;i++){var a=t[i];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(e,(n=a.key,r=void 0,"symbol"==typeof(r=function(e,t){if("object"!=typeof e||null===e)return e;var i=e[Symbol.toPrimitive];if(void 0!==i){var a=i.call(e,t||"default");if("object"!=typeof a)return a;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(n,"string"))?r:String(r)),a)}var n,r}var i={name:"audio-keyboard-response",parameters:{stimulus:{type:e.ParameterType.AUDIO,pretty_name:"Stimulus",default:void 0},choices:{type:e.ParameterType.KEYS,pretty_name:"Choices",default:"ALL_KEYS"},prompt:{type:e.ParameterType.HTML_STRING,pretty_name:"Prompt",default:null},trial_duration:{type:e.ParameterType.INT,pretty_name:"Trial duration",default:null},response_ends_trial:{type:e.ParameterType.BOOL,pretty_name:"Response ends trial",default:!0},trial_ends_after_audio:{type:e.ParameterType.BOOL,pretty_name:"Trial ends after audio",default:!1},response_allowed_while_playing:{type:e.ParameterType.BOOL,pretty_name:"Response allowed while playing",default:!0}}},a=function(){function e(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.jsPsych=t}var i,a,n;return i=e,a=[{key:"trial",value:function(e,t,i){var a,n,r=this,s=this.jsPsych.pluginAPI.audioContext(),o={rt:null,key:null};this.jsPsych.pluginAPI.getAudioBuffer(t.stimulus).then((function(e){null!==s?(r.audio=s.createBufferSource(),r.audio.buffer=e,r.audio.connect(s.destination)):(r.audio=e,r.audio.currentTime=0),l()})).catch((function(e){console.error('Failed to load audio file "'.concat(t.stimulus,'". Try checking the file path. We recommend using the preload plugin to load audio files.')),console.error(e)}));var l=function(){t.trial_ends_after_audio&&r.audio.addEventListener("ended",u),null!==t.prompt&&(e.innerHTML=t.prompt),null!==s?(n=s.currentTime,r.audio.start(n)):r.audio.play(),t.response_allowed_while_playing?c():t.trial_ends_after_audio||r.audio.addEventListener("ended",c),null!==t.trial_duration&&r.jsPsych.pluginAPI.setTimeout((function(){u()}),t.trial_duration),i()},u=function i(){r.jsPsych.pluginAPI.clearAllTimeouts(),null!==s?r.audio.stop():r.audio.pause(),r.audio.removeEventListener("ended",i),r.audio.removeEventListener("ended",c),r.jsPsych.pluginAPI.cancelAllKeyboardResponses();var n={rt:o.rt,stimulus:t.stimulus,response:o.key};e.innerHTML="",r.jsPsych.finishTrial(n),a()};function d(e){null==o.key&&(o=e),t.response_ends_trial&&u()}var c=function(){null!==s?r.jsPsych.pluginAPI.getKeyboardResponse({callback_function:d,valid_responses:t.choices,rt_method:"audio",persist:!1,allow_held_key:!1,audio_context:s,audio_context_start_time:n}):r.jsPsych.pluginAPI.getKeyboardResponse({callback_function:d,valid_responses:t.choices,rt_method:"performance",persist:!1,allow_held_key:!1})};return new Promise((function(e){a=e}))}},{key:"simulate",value:function(e,t,i,a){"data-only"==t&&(a(),this.simulate_data_only(e,i)),"visual"==t&&this.simulate_visual(e,i,a)}},{key:"simulate_data_only",value:function(e,t){var i=this.create_simulation_data(e,t);this.jsPsych.finishTrial(i)}},{key:"simulate_visual",value:function(e,t,i){var a=this,n=this.create_simulation_data(e,t),r=this.jsPsych.getDisplayElement(),s=function(){null!==n.rt&&a.jsPsych.pluginAPI.pressKey(n.response,n.rt)};this.trial(r,e,(function(){i(),e.response_allowed_while_playing?s():a.audio.addEventListener("ended",s)}))}},{key:"create_simulation_data",value:function(e,t){var i={stimulus:e.stimulus,rt:this.jsPsych.randomization.sampleExGaussian(500,50,1/150,!0),response:this.jsPsych.pluginAPI.getValidKey(e.choices)},a=this.jsPsych.pluginAPI.mergeSimulationData(i,t);return this.jsPsych.pluginAPI.ensureSimulationDataConsistency(e,a),a}}],a&&t(i.prototype,a),n&&t(i,n),Object.defineProperty(i,"prototype",{writable:!1}),e}();return a.info=i,a}(jsPsychModule);
|
|
2
|
+
//# sourceMappingURL=https://unpkg.com/@jspsych/plugin-audio-keyboard-response@1.1.3/dist/index.browser.min.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.browser.min.js","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"audio-keyboard-response\",\n parameters: {\n /** The audio file to be played. */\n stimulus: {\n type: ParameterType.AUDIO,\n pretty_name: \"Stimulus\",\n default: undefined,\n },\n /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */\n choices: {\n type: ParameterType.KEYS,\n pretty_name: \"Choices\",\n default: \"ALL_KEYS\",\n },\n /** Any content here will be displayed below the stimulus. */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /** The maximum duration to wait for a response. */\n trial_duration: {\n type: ParameterType.INT,\n pretty_name: \"Trial duration\",\n default: null,\n },\n /** If true, the trial will end when user makes a response. */\n response_ends_trial: {\n type: ParameterType.BOOL,\n pretty_name: \"Response ends trial\",\n default: true,\n },\n /** If true, then the trial will end as soon as the audio file finishes playing. */\n trial_ends_after_audio: {\n type: ParameterType.BOOL,\n pretty_name: \"Trial ends after audio\",\n default: false,\n },\n /** If true, then responses are allowed while the audio is playing. If false, then the audio must finish playing before a response is accepted. */\n response_allowed_while_playing: {\n type: ParameterType.BOOL,\n pretty_name: \"Response allowed while playing\",\n default: true,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **audio-keyboard-response**\n *\n * jsPsych plugin for playing an audio file and getting a keyboard response\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-audio-keyboard-response/ audio-keyboard-response plugin documentation on jspsych.org}\n */\nclass AudioKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n private audio;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>, on_load: () => void) {\n // hold the .resolve() function from the Promise that ends the trial\n let trial_complete;\n\n // setup stimulus\n var context = this.jsPsych.pluginAPI.audioContext();\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // record webaudio context start time\n var startTime;\n\n // load audio file\n this.jsPsych.pluginAPI\n .getAudioBuffer(trial.stimulus)\n .then((buffer) => {\n if (context !== null) {\n this.audio = context.createBufferSource();\n this.audio.buffer = buffer;\n this.audio.connect(context.destination);\n } else {\n this.audio = buffer;\n this.audio.currentTime = 0;\n }\n setupTrial();\n })\n .catch((err) => {\n console.error(\n `Failed to load audio file \"${trial.stimulus}\". Try checking the file path. We recommend using the preload plugin to load audio files.`\n );\n console.error(err);\n });\n\n const setupTrial = () => {\n // set up end event if trial needs it\n if (trial.trial_ends_after_audio) {\n this.audio.addEventListener(\"ended\", end_trial);\n }\n\n // show prompt if there is one\n if (trial.prompt !== null) {\n display_element.innerHTML = trial.prompt;\n }\n\n // start audio\n if (context !== null) {\n startTime = context.currentTime;\n this.audio.start(startTime);\n } else {\n this.audio.play();\n }\n\n // start keyboard listener when trial starts or sound ends\n if (trial.response_allowed_while_playing) {\n setup_keyboard_listener();\n } else if (!trial.trial_ends_after_audio) {\n this.audio.addEventListener(\"ended\", setup_keyboard_listener);\n }\n\n // end trial if time limit is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n end_trial();\n }, trial.trial_duration);\n }\n\n on_load();\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\n // stop the audio file if it is playing\n // remove end event listeners if they exist\n if (context !== null) {\n this.audio.stop();\n } else {\n this.audio.pause();\n }\n\n this.audio.removeEventListener(\"ended\", end_trial);\n this.audio.removeEventListener(\"ended\", setup_keyboard_listener);\n\n // kill keyboard listeners\n this.jsPsych.pluginAPI.cancelAllKeyboardResponses();\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // clear the display\n display_element.innerHTML = \"\";\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n\n trial_complete();\n };\n\n // function to handle responses by the subject\n function after_response(info) {\n // only record the first response\n if (response.key == null) {\n response = info;\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n }\n\n const setup_keyboard_listener = () => {\n // start the response listener\n if (context !== null) {\n this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"audio\",\n persist: false,\n allow_held_key: false,\n audio_context: context,\n audio_context_start_time: startTime,\n });\n } else {\n this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n });\n }\n };\n\n return new Promise((resolve) => {\n trial_complete = resolve;\n });\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n const respond = () => {\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n };\n\n this.trial(display_element, trial, () => {\n load_callback();\n if (!trial.response_allowed_while_playing) {\n this.audio.addEventListener(\"ended\", respond);\n } else {\n respond();\n }\n });\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n}\n\nexport default AudioKeyboardResponsePlugin;\n"],"names":["info","name","parameters","stimulus","type","ParameterType","AUDIO","pretty_name","default","undefined","choices","KEYS","prompt","HTML_STRING","trial_duration","INT","response_ends_trial","BOOL","trial_ends_after_audio","response_allowed_while_playing","AudioKeyboardResponsePlugin","jsPsych","_classCallCheck","this","value","display_element","trial","on_load","trial_complete","startTime","_this","context","pluginAPI","audioContext","response","rt","key","getAudioBuffer","then","buffer","audio","createBufferSource","connect","destination","currentTime","setupTrial","err","console","error","addEventListener","end_trial","innerHTML","start","play","setup_keyboard_listener","setTimeout","clearAllTimeouts","stop","pause","removeEventListener","cancelAllKeyboardResponses","trial_data","finishTrial","after_response","getKeyboardResponse","callback_function","valid_responses","rt_method","persist","allow_held_key","audio_context","audio_context_start_time","Promise","resolve","simulation_mode","simulation_options","load_callback","simulate_data_only","simulate_visual","data","create_simulation_data","_this2","getDisplayElement","respond","pressKey","default_data","randomization","sampleExGaussian","getValidKey","mergeSimulationData","ensureSimulationDataConsistency"],"mappings":"gOAEA,IAAMA,EAAc,CAClBC,KAAM,0BACNC,WAAY,CAEVC,SAAU,CACRC,KAAMC,EAAaA,cAACC,MACpBC,YAAa,WACbC,aAASC,GAGXC,QAAS,CACPN,KAAMC,EAAaA,cAACM,KACpBJ,YAAa,UACbC,QAAS,YAGXI,OAAQ,CACNR,KAAMC,EAAaA,cAACQ,YACpBN,YAAa,SACbC,QAAS,MAGXM,eAAgB,CACdV,KAAMC,EAAaA,cAACU,IACpBR,YAAa,iBACbC,QAAS,MAGXQ,oBAAqB,CACnBZ,KAAMC,EAAaA,cAACY,KACpBV,YAAa,sBACbC,SAAS,GAGXU,uBAAwB,CACtBd,KAAMC,EAAaA,cAACY,KACpBV,YAAa,yBACbC,SAAS,GAGXW,+BAAgC,CAC9Bf,KAAMC,EAAaA,cAACY,KACpBV,YAAa,iCACbC,SAAS,KAeTY,aAIJ,SAAAA,EAAoBC,gGAAgBC,CAAAC,KAAAH,GAAhBG,KAAOF,QAAPA,CAAoB,sCAExCG,MAAA,SAAMC,EAA8BC,EAAwBC,GAAmB,IAEzEC,EAYAC,EAdyEC,EAAAP,KAKzEQ,EAAUR,KAAKF,QAAQW,UAAUC,eAGjCC,EAAW,CACbC,GAAI,KACJC,IAAK,MAOPb,KAAKF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AU0D,gCAAgChE,EAAOqD,GAEvDA,CACR,iGA/MM3D,EAAIpB,KAAGA"}
|
|
1
|
+
{"version":3,"file":"index.browser.min.js","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"audio-keyboard-response\",\n parameters: {\n /** The audio file to be played. */\n stimulus: {\n type: ParameterType.AUDIO,\n pretty_name: \"Stimulus\",\n default: undefined,\n },\n /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */\n choices: {\n type: ParameterType.KEYS,\n pretty_name: \"Choices\",\n default: \"ALL_KEYS\",\n },\n /** Any content here will be displayed below the stimulus. */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /** The maximum duration to wait for a response. */\n trial_duration: {\n type: ParameterType.INT,\n pretty_name: \"Trial duration\",\n default: null,\n },\n /** If true, the trial will end when user makes a response. */\n response_ends_trial: {\n type: ParameterType.BOOL,\n pretty_name: \"Response ends trial\",\n default: true,\n },\n /** If true, then the trial will end as soon as the audio file finishes playing. */\n trial_ends_after_audio: {\n type: ParameterType.BOOL,\n pretty_name: \"Trial ends after audio\",\n default: false,\n },\n /** If true, then responses are allowed while the audio is playing. If false, then the audio must finish playing before a response is accepted. */\n response_allowed_while_playing: {\n type: ParameterType.BOOL,\n pretty_name: \"Response allowed while playing\",\n default: true,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **audio-keyboard-response**\n *\n * jsPsych plugin for playing an audio file and getting a keyboard response\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-audio-keyboard-response/ audio-keyboard-response plugin documentation on jspsych.org}\n */\nclass AudioKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n private audio;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>, on_load: () => void) {\n // hold the .resolve() function from the Promise that ends the trial\n let trial_complete;\n\n // setup stimulus\n var context = this.jsPsych.pluginAPI.audioContext();\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // record webaudio context start time\n var startTime;\n\n // load audio file\n this.jsPsych.pluginAPI\n .getAudioBuffer(trial.stimulus)\n .then((buffer) => {\n if (context !== null) {\n this.audio = context.createBufferSource();\n this.audio.buffer = buffer;\n this.audio.connect(context.destination);\n } else {\n this.audio = buffer;\n this.audio.currentTime = 0;\n }\n setupTrial();\n })\n .catch((err) => {\n console.error(\n `Failed to load audio file \"${trial.stimulus}\". Try checking the file path. We recommend using the preload plugin to load audio files.`\n );\n console.error(err);\n });\n\n const setupTrial = () => {\n // set up end event if trial needs it\n if (trial.trial_ends_after_audio) {\n this.audio.addEventListener(\"ended\", end_trial);\n }\n\n // show prompt if there is one\n if (trial.prompt !== null) {\n display_element.innerHTML = trial.prompt;\n }\n\n // start audio\n if (context !== null) {\n startTime = context.currentTime;\n this.audio.start(startTime);\n } else {\n this.audio.play();\n }\n\n // start keyboard listener when trial starts or sound ends\n if (trial.response_allowed_while_playing) {\n setup_keyboard_listener();\n } else if (!trial.trial_ends_after_audio) {\n this.audio.addEventListener(\"ended\", setup_keyboard_listener);\n }\n\n // end trial if time limit is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n end_trial();\n }, trial.trial_duration);\n }\n\n on_load();\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\n // stop the audio file if it is playing\n // remove end event listeners if they exist\n if (context !== null) {\n this.audio.stop();\n } else {\n this.audio.pause();\n }\n\n this.audio.removeEventListener(\"ended\", end_trial);\n this.audio.removeEventListener(\"ended\", setup_keyboard_listener);\n\n // kill keyboard listeners\n this.jsPsych.pluginAPI.cancelAllKeyboardResponses();\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // clear the display\n display_element.innerHTML = \"\";\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n\n trial_complete();\n };\n\n // function to handle responses by the subject\n function after_response(info) {\n // only record the first response\n if (response.key == null) {\n response = info;\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n }\n\n const setup_keyboard_listener = () => {\n // start the response listener\n if (context !== null) {\n this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"audio\",\n persist: false,\n allow_held_key: false,\n audio_context: context,\n audio_context_start_time: startTime,\n });\n } else {\n this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n });\n }\n };\n\n return new Promise((resolve) => {\n trial_complete = resolve;\n });\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n const respond = () => {\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n };\n\n this.trial(display_element, trial, () => {\n load_callback();\n if (!trial.response_allowed_while_playing) {\n this.audio.addEventListener(\"ended\", respond);\n } else {\n respond();\n }\n });\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n}\n\nexport default AudioKeyboardResponsePlugin;\n"],"names":["info","name","parameters","stimulus","type","ParameterType","AUDIO","pretty_name","default","undefined","choices","KEYS","prompt","HTML_STRING","trial_duration","INT","response_ends_trial","BOOL","trial_ends_after_audio","response_allowed_while_playing","AudioKeyboardResponsePlugin","jsPsych","_classCallCheck","this","key","value","display_element","trial","on_load","trial_complete","startTime","_this","context","pluginAPI","audioContext","response","rt","getAudioBuffer","then","buffer","audio","createBufferSource","connect","destination","currentTime","setupTrial","err","console","error","concat","addEventListener","end_trial","innerHTML","start","play","setup_keyboard_listener","setTimeout","clearAllTimeouts","stop","pause","removeEventListener","cancelAllKeyboardResponses","trial_data","finishTrial","after_response","getKeyboardResponse","callback_function","valid_responses","rt_method","persist","allow_held_key","audio_context","audio_context_start_time","Promise","resolve","simulation_mode","simulation_options","load_callback","simulate_data_only","simulate_visual","data","create_simulation_data","_this2","getDisplayElement","respond","pressKey","default_data","randomization","sampleExGaussian","getValidKey","mergeSimulationData","ensureSimulationDataConsistency"],"mappings":"yiBAEA,IAAMA,EAAc,CAClBC,KAAM,0BACNC,WAAY,CAEVC,SAAU,CACRC,KAAMC,EAAaA,cAACC,MACpBC,YAAa,WACbC,aAASC,GAGXC,QAAS,CACPN,KAAMC,EAAaA,cAACM,KACpBJ,YAAa,UACbC,QAAS,YAGXI,OAAQ,CACNR,KAAMC,EAAaA,cAACQ,YACpBN,YAAa,SACbC,QAAS,MAGXM,eAAgB,CACdV,KAAMC,EAAaA,cAACU,IACpBR,YAAa,iBACbC,QAAS,MAGXQ,oBAAqB,CACnBZ,KAAMC,EAAaA,cAACY,KACpBV,YAAa,sBACbC,SAAS,GAGXU,uBAAwB,CACtBd,KAAMC,EAAaA,cAACY,KACpBV,YAAa,yBACbC,SAAS,GAGXW,+BAAgC,CAC9Bf,KAAMC,EAAaA,c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