@jspsych/plugin-audio-keyboard-response 1.1.2 → 2.0.0

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 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, please cite the [paper that describes jsPsych](http://link.springer.com/article/10.3758%2Fs13428-014-0458-y):
30
+ If you use this library in academic work, the preferred citation is:
31
31
 
32
- 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
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
 
@@ -1,238 +1,269 @@
1
1
  var jsPsychAudioKeyboardResponse = (function (jspsych) {
2
- 'use strict';
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
- }
233
- AudioKeyboardResponsePlugin.info = info;
234
-
235
- return AudioKeyboardResponsePlugin;
2
+ 'use strict';
3
+
4
+ function getDefaultExportFromCjs (x) {
5
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
6
+ }
7
+
8
+ // Gets all non-builtin properties up the prototype chain
9
+ const getAllProperties = object => {
10
+ const properties = new Set();
11
+
12
+ do {
13
+ for (const key of Reflect.ownKeys(object)) {
14
+ properties.add([object, key]);
15
+ }
16
+ } while ((object = Reflect.getPrototypeOf(object)) && object !== Object.prototype);
17
+
18
+ return properties;
19
+ };
20
+
21
+ var autoBind = (self, {include, exclude} = {}) => {
22
+ const filter = key => {
23
+ const match = pattern => typeof pattern === 'string' ? key === pattern : pattern.test(key);
24
+
25
+ if (include) {
26
+ return include.some(match);
27
+ }
28
+
29
+ if (exclude) {
30
+ return !exclude.some(match);
31
+ }
32
+
33
+ return true;
34
+ };
35
+
36
+ for (const [object, key] of getAllProperties(self.constructor.prototype)) {
37
+ if (key === 'constructor' || !filter(key)) {
38
+ continue;
39
+ }
40
+
41
+ const descriptor = Reflect.getOwnPropertyDescriptor(object, key);
42
+ if (descriptor && typeof descriptor.value === 'function') {
43
+ self[key] = self[key].bind(self);
44
+ }
45
+ }
46
+
47
+ return self;
48
+ };
49
+
50
+ var autoBind$1 = /*@__PURE__*/getDefaultExportFromCjs(autoBind);
51
+
52
+ var _package = {
53
+ name: "@jspsych/plugin-audio-keyboard-response",
54
+ version: "2.0.0",
55
+ description: "jsPsych plugin for playing an audio file and getting a keyboard response",
56
+ type: "module",
57
+ main: "dist/index.cjs",
58
+ exports: {
59
+ import: "./dist/index.js",
60
+ require: "./dist/index.cjs"
61
+ },
62
+ typings: "dist/index.d.ts",
63
+ unpkg: "dist/index.browser.min.js",
64
+ files: [
65
+ "src",
66
+ "dist"
67
+ ],
68
+ source: "src/index.ts",
69
+ scripts: {
70
+ test: "jest",
71
+ "test:watch": "npm test -- --watch",
72
+ tsc: "tsc",
73
+ build: "rollup --config",
74
+ "build:watch": "npm run build -- --watch"
75
+ },
76
+ repository: {
77
+ type: "git",
78
+ url: "git+https://github.com/jspsych/jsPsych.git",
79
+ directory: "packages/plugin-audio-keyboard-response"
80
+ },
81
+ author: "Josh de Leeuw",
82
+ license: "MIT",
83
+ bugs: {
84
+ url: "https://github.com/jspsych/jsPsych/issues"
85
+ },
86
+ homepage: "https://www.jspsych.org/latest/plugins/audio-keyboard-response",
87
+ peerDependencies: {
88
+ jspsych: ">=7.1.0"
89
+ },
90
+ devDependencies: {
91
+ "@jspsych/config": "^3.0.0",
92
+ "@jspsych/test-utils": "^1.2.0"
93
+ }
94
+ };
95
+
96
+ const info = {
97
+ name: "audio-keyboard-response",
98
+ version: _package.version,
99
+ parameters: {
100
+ stimulus: {
101
+ type: jspsych.ParameterType.AUDIO,
102
+ default: void 0
103
+ },
104
+ choices: {
105
+ type: jspsych.ParameterType.KEYS,
106
+ default: "ALL_KEYS"
107
+ },
108
+ prompt: {
109
+ type: jspsych.ParameterType.HTML_STRING,
110
+ pretty_name: "Prompt",
111
+ default: null
112
+ },
113
+ trial_duration: {
114
+ type: jspsych.ParameterType.INT,
115
+ default: null
116
+ },
117
+ response_ends_trial: {
118
+ type: jspsych.ParameterType.BOOL,
119
+ default: true
120
+ },
121
+ trial_ends_after_audio: {
122
+ type: jspsych.ParameterType.BOOL,
123
+ pretty_name: "Trial ends after audio",
124
+ default: false
125
+ },
126
+ response_allowed_while_playing: {
127
+ type: jspsych.ParameterType.BOOL,
128
+ default: true
129
+ }
130
+ },
131
+ data: {
132
+ response: {
133
+ type: jspsych.ParameterType.STRING
134
+ },
135
+ rt: {
136
+ type: jspsych.ParameterType.INT
137
+ },
138
+ stimulus: {
139
+ type: jspsych.ParameterType.STRING
140
+ }
141
+ }
142
+ };
143
+ class AudioKeyboardResponsePlugin {
144
+ constructor(jsPsych) {
145
+ this.jsPsych = jsPsych;
146
+ autoBind$1(this);
147
+ }
148
+ static info = info;
149
+ audio;
150
+ params;
151
+ display;
152
+ response = { rt: null, key: null };
153
+ startTime;
154
+ finish;
155
+ trial(display_element, trial, on_load) {
156
+ return new Promise(async (resolve) => {
157
+ this.finish = resolve;
158
+ this.params = trial;
159
+ this.display = display_element;
160
+ this.audio = await this.jsPsych.pluginAPI.getAudioPlayer(trial.stimulus);
161
+ if (trial.trial_ends_after_audio) {
162
+ this.audio.addEventListener("ended", this.end_trial);
163
+ }
164
+ if (trial.prompt !== null) {
165
+ display_element.innerHTML = trial.prompt;
166
+ }
167
+ this.startTime = this.jsPsych.pluginAPI.audioContext()?.currentTime;
168
+ if (trial.response_allowed_while_playing) {
169
+ this.setup_keyboard_listener();
170
+ } else if (!trial.trial_ends_after_audio) {
171
+ this.audio.addEventListener("ended", this.setup_keyboard_listener);
172
+ }
173
+ if (trial.trial_duration !== null) {
174
+ this.jsPsych.pluginAPI.setTimeout(() => {
175
+ this.end_trial();
176
+ }, trial.trial_duration);
177
+ }
178
+ on_load();
179
+ this.audio.play();
180
+ });
181
+ }
182
+ end_trial() {
183
+ this.jsPsych.pluginAPI.clearAllTimeouts();
184
+ this.audio.stop();
185
+ this.audio.removeEventListener("ended", this.end_trial);
186
+ this.audio.removeEventListener("ended", this.setup_keyboard_listener);
187
+ this.jsPsych.pluginAPI.cancelAllKeyboardResponses();
188
+ var trial_data = {
189
+ rt: this.response.rt,
190
+ response: this.response.key,
191
+ stimulus: this.params.stimulus
192
+ };
193
+ this.display.innerHTML = "";
194
+ this.finish(trial_data);
195
+ }
196
+ after_response(info2) {
197
+ this.response = info2;
198
+ if (this.params.response_ends_trial) {
199
+ this.end_trial();
200
+ }
201
+ }
202
+ setup_keyboard_listener() {
203
+ if (this.jsPsych.pluginAPI.useWebaudio) {
204
+ this.jsPsych.pluginAPI.getKeyboardResponse({
205
+ callback_function: this.after_response,
206
+ valid_responses: this.params.choices,
207
+ rt_method: "audio",
208
+ persist: false,
209
+ allow_held_key: false,
210
+ audio_context: this.jsPsych.pluginAPI.audioContext(),
211
+ audio_context_start_time: this.startTime
212
+ });
213
+ } else {
214
+ this.jsPsych.pluginAPI.getKeyboardResponse({
215
+ callback_function: this.after_response,
216
+ valid_responses: this.params.choices,
217
+ rt_method: "performance",
218
+ persist: false,
219
+ allow_held_key: false
220
+ });
221
+ }
222
+ }
223
+ async simulate(trial, simulation_mode, simulation_options, load_callback) {
224
+ if (simulation_mode == "data-only") {
225
+ load_callback();
226
+ return this.simulate_data_only(trial, simulation_options);
227
+ }
228
+ if (simulation_mode == "visual") {
229
+ return this.simulate_visual(trial, simulation_options, load_callback);
230
+ }
231
+ }
232
+ simulate_data_only(trial, simulation_options) {
233
+ const data = this.create_simulation_data(trial, simulation_options);
234
+ return data;
235
+ }
236
+ async simulate_visual(trial, simulation_options, load_callback) {
237
+ const data = this.create_simulation_data(trial, simulation_options);
238
+ const display_element = this.jsPsych.getDisplayElement();
239
+ const respond = () => {
240
+ if (data.rt !== null) {
241
+ this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
242
+ }
243
+ };
244
+ const result = await this.trial(display_element, trial, () => {
245
+ load_callback();
246
+ if (!trial.response_allowed_while_playing) {
247
+ this.audio.addEventListener("ended", respond);
248
+ } else {
249
+ respond();
250
+ }
251
+ });
252
+ return result;
253
+ }
254
+ create_simulation_data(trial, simulation_options) {
255
+ const default_data = {
256
+ stimulus: trial.stimulus,
257
+ rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
258
+ response: this.jsPsych.pluginAPI.getValidKey(trial.choices)
259
+ };
260
+ const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
261
+ this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
262
+ return data;
263
+ }
264
+ }
265
+
266
+ return AudioKeyboardResponsePlugin;
236
267
 
237
268
  })(jsPsychModule);
238
- //# sourceMappingURL=index.browser.js.map
269
+ //# sourceMappingURL=https://unpkg.com/@jspsych/plugin-audio-keyboard-response@2.0.0/dist/index.browser.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.browser.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":["ParameterType"],"mappings":";;;EAEA,MAAM,IAAI,GAAU;EAClB,IAAA,IAAI,EAAE,yBAAyB;EAC/B,IAAA,UAAU,EAAE;;EAEV,QAAA,QAAQ,EAAE;cACR,IAAI,EAAEA,qBAAa,CAAC,KAAK;EACzB,YAAA,WAAW,EAAE,UAAU;EACvB,YAAA,OAAO,EAAE,SAAS;EACnB,SAAA;;EAED,QAAA,OAAO,EAAE;cACP,IAAI,EAAEA,qBAAa,CAAC,IAAI;EACxB,YAAA,WAAW,EAAE,SAAS;EACtB,YAAA,OAAO,EAAE,UAAU;EACpB,SAAA;;EAED,QAAA,MAAM,EAAE;cACN,IAAI,EAAEA,qBAAa,CAAC,WAAW;EAC/B,YAAA,WAAW,EAAE,QAAQ;EACrB,YAAA,OAAO,EAAE,IAAI;EACd,SAAA;;EAED,QAAA,cAAc,EAAE;cACd,IAAI,EAAEA,qBAAa,CAAC,GAAG;EACvB,YAAA,WAAW,EAAE,gBAAgB;EAC7B,YAAA,OAAO,EAAE,IAAI;EACd,SAAA;;EAED,QAAA,mBAAmB,EAAE;cACnB,IAAI,EAAEA,qBAAa,CAAC,IAAI;EACxB,YAAA,WAAW,EAAE,qBAAqB;EAClC,YAAA,OAAO,EAAE,IAAI;EACd,SAAA;;EAED,QAAA,sBAAsB,EAAE;cACtB,IAAI,EAAEA,qBAAa,CAAC,IAAI;EACxB,YAAA,WAAW,EAAE,wBAAwB;EACrC,YAAA,OAAO,EAAE,KAAK;EACf,SAAA;;EAED,QAAA,8BAA8B,EAAE;cAC9B,IAAI,EAAEA,qBAAa,CAAC,IAAI;EACxB,YAAA,WAAW,EAAE,gCAAgC;EAC7C,YAAA,OAAO,EAAE,IAAI;EACd,SAAA;EACF,KAAA;GACF,CAAC;EAIF;;;;;;;EAOG;EACH,MAAM,2BAA2B,CAAA;EAI/B,IAAA,WAAA,CAAoB,OAAgB,EAAA;UAAh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AC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC;EACzD,aAAA;EACH,SAAC,CAAC;UAEF,IAAI,CAAC,KAAK,CAAC,eAAe,EAAE,KAAK,EAAE,MAAK;EACtC,YAAA,aAAa,EAAE,CAAC;EAChB,YAAA,IAAI,CAAC,KAAK,CAAC,8BAA8B,EAAE;kBACzC,IAAI,CAAC,KAAK,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;EAC/C,aAAA;EAAM,iBAAA;EACL,gBAAA,OAAO,EAAE,CAAC;EACX,aAAA;EACH,SAAC,CAAC,CAAC;OACJ;MAEO,sBAAsB,CAAC,KAAsB,EAAE,kBAAkB,EAAA;EACvE,QAAA,MAAM,YAAY,GAAG;cACnB,QAAQ,EAAE,KAAK,CAAC,QAAQ;EACxB,YAAA,EAAE,EAAE,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,gBAAgB,CAAC,GAAG,EAAE,EAAE,EAAE,CAAC,GAAG,GAAG,EAAE,IAAI,CAAC;EACvE,YAAA,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,WAAW,CAAC,KAAK,CAAC,OAAO,CAAC;WAC5D,CAAC;EAEF,QAAA,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,mBAAmB,CAAC,YAAY,EAAE,kBAAkB,CAAC,CAAC;UAE1F,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,+BAA+B,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;EAEpE,QAAA,OAAO,IAAI,CAAC;OACb;;EA/MM,2BAAI,CAAA,IAAA,GAAG,IAAI;;;;;;;;"}
1
+ {"version":3,"file":"index.browser.js","sources":["../../../node_modules/auto-bind/index.js","../src/index.ts"],"sourcesContent":["'use strict';\n\n// Gets all non-builtin properties up the prototype chain\nconst getAllProperties = object => {\n\tconst properties = new Set();\n\n\tdo {\n\t\tfor (const key of Reflect.ownKeys(object)) {\n\t\t\tproperties.add([object, key]);\n\t\t}\n\t} while ((object = Reflect.getPrototypeOf(object)) && object !== Object.prototype);\n\n\treturn properties;\n};\n\nmodule.exports = (self, {include, exclude} = {}) => {\n\tconst filter = key => {\n\t\tconst match = pattern => typeof pattern === 'string' ? key === pattern : pattern.test(key);\n\n\t\tif (include) {\n\t\t\treturn include.some(match);\n\t\t}\n\n\t\tif (exclude) {\n\t\t\treturn !exclude.some(match);\n\t\t}\n\n\t\treturn true;\n\t};\n\n\tfor (const [object, key] of getAllProperties(self.constructor.prototype)) {\n\t\tif (key === 'constructor' || !filter(key)) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst descriptor = Reflect.getOwnPropertyDescriptor(object, key);\n\t\tif (descriptor && typeof descriptor.value === 'function') {\n\t\t\tself[key] = self[key].bind(self);\n\t\t}\n\t}\n\n\treturn self;\n};\n","import autoBind from \"auto-bind\";\nimport { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nimport { AudioPlayerInterface } from \"../../jspsych/src/modules/plugin-api/AudioPlayer\";\nimport { version } from \"../package.json\";\n\nconst info = <const>{\n name: \"audio-keyboard-response\",\n version: version,\n parameters: {\n /** The audio file to be played. */\n stimulus: {\n type: ParameterType.AUDIO,\n default: undefined,\n },\n /** This array contains the key(s) that the participant is allowed to press in order to respond to the stimulus.\n * Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) -\n * see [this page](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values)\n * and [this page (event.key column)](https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/)\n * for more examples. Any key presses that are not listed in the array will be ignored. The default value of `\"ALL_KEYS\"`\n * means that all keys will be accepted as valid responses. Specifying `\"NO_KEYS\"` will mean that no responses are allowed.\n */\n choices: {\n type: ParameterType.KEYS,\n default: \"ALL_KEYS\",\n },\n /** This string can contain HTML markup. Any content here will be displayed below the stimulus. The intention is that\n * it can be used to provide a reminder about the action the participant is supposed to take (e.g., which key to press).\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /** How long to wait for the participant to make a response before ending the trial in milliseconds. If the\n * participant fails to make a response before this timer is reached, the participant's response will be\n * recorded as null for the trial and the trial will end. If the value of this parameter is null, then the\n * trial will wait for a response indefinitely.\n */\n trial_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /** If true, then the trial will end whenever the participant makes a response (assuming they make their\n * response before the cutoff specified by the `trial_duration` parameter). If false, then the trial will\n * continue until the value for `trial_duration` is reached. You can use set this parameter to `false` to\n * force the participant to listen to the stimulus for a fixed amount of time, even if they respond before the time is complete\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\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\n * playing before a keyboard response is accepted. Once the audio has played all the way through, a valid\n * keyboard response is allowed (including while the audio is being re-played via on-screen playback controls).\n */\n response_allowed_while_playing: {\n type: ParameterType.BOOL,\n default: true,\n },\n },\n data: {\n /** Indicates which key the participant pressed. If no key was pressed before the trial ended, then the value will be `null`. */\n response: {\n type: ParameterType.STRING,\n },\n /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus\n * first began playing until the participant made a key response. If no key was pressed before the trial ended, then the\n * value will be `null`.\n */\n rt: {\n type: ParameterType.INT,\n },\n /** Path to the audio file that played during the trial. */\n stimulus: {\n type: ParameterType.STRING,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * This plugin plays audio files and records responses generated with the keyboard.\n *\n * If the browser supports it, audio files are played using the WebAudio API. This allows for reasonably precise timing of the\n * playback. The timing of responses generated is measured against the WebAudio specific clock, improving the measurement of\n * response times. If the browser does not support the WebAudio API, then the audio file is played with HTML5 audio.\n *\n * Audio files can be automatically preloaded by jsPsych using the [`preload` plugin](preload.md). However, if you are using\n * timeline variables or another dynamic method to specify the audio stimulus, then you will need to [manually preload](../overview/media-preloading.md#manual-preloading) the audio.\n *\n * The trial can end when the participant responds, when the audio file has finished playing, or if the participant has\n * failed to respond within a fixed length of time. You can also prevent a keyboard response from being recorded before\n * the audio has finished playing.\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/latest/plugins/audio-keyboard-response/ audio-keyboard-response plugin documentation on jspsych.org}\n */\nclass AudioKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n private audio: AudioPlayerInterface;\n private params: TrialType<Info>;\n private display: HTMLElement;\n private response: { rt: number; key: string } = { rt: null, key: null };\n private startTime: number;\n private finish: ({}: { rt: number; response: string; stimulus: string }) => void;\n\n constructor(private jsPsych: JsPsych) {\n autoBind(this);\n }\n\n trial(display_element: HTMLElement, trial: TrialType<Info>, on_load: () => void) {\n return new Promise(async (resolve) => {\n this.finish = resolve;\n this.params = trial;\n this.display = display_element;\n // load audio file\n this.audio = await this.jsPsych.pluginAPI.getAudioPlayer(trial.stimulus);\n\n // set up end event if trial needs it\n if (trial.trial_ends_after_audio) {\n this.audio.addEventListener(\"ended\", this.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 playing audio here to record time\n // use this for offsetting RT measurement in\n // setup_keyboard_listener\n this.startTime = this.jsPsych.pluginAPI.audioContext()?.currentTime;\n\n // start keyboard listener when trial starts or sound ends\n if (trial.response_allowed_while_playing) {\n this.setup_keyboard_listener();\n } else if (!trial.trial_ends_after_audio) {\n this.audio.addEventListener(\"ended\", this.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 this.end_trial();\n }, trial.trial_duration);\n }\n\n // call trial on_load method because we are done with all loading setup\n on_load();\n\n this.audio.play();\n });\n }\n\n private end_trial() {\n // kill any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\n // stop the audio file if it is playing\n this.audio.stop();\n\n // remove end event listeners if they exist\n this.audio.removeEventListener(\"ended\", this.end_trial);\n this.audio.removeEventListener(\"ended\", this.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: this.response.rt,\n response: this.response.key,\n stimulus: this.params.stimulus,\n };\n\n // clear the display\n this.display.innerHTML = \"\";\n\n // move on to the next trial\n this.finish(trial_data);\n }\n\n private after_response(info: { key: string; rt: number }) {\n this.response = info;\n if (this.params.response_ends_trial) {\n this.end_trial();\n }\n }\n\n private setup_keyboard_listener() {\n // start the response listener\n if (this.jsPsych.pluginAPI.useWebaudio) {\n this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: this.after_response,\n valid_responses: this.params.choices,\n rt_method: \"audio\",\n persist: false,\n allow_held_key: false,\n audio_context: this.jsPsych.pluginAPI.audioContext(),\n audio_context_start_time: this.startTime,\n });\n } else {\n this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: this.after_response,\n valid_responses: this.params.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n });\n }\n }\n\n async 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 return this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n return 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 return data;\n }\n\n private async simulate_visual(\n trial: TrialType<Info>,\n simulation_options,\n load_callback: () => void\n ) {\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 const result = await 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 return result;\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":["version","ParameterType","autoBind","info"],"mappings":";;;;;;;CAEA;CACA,MAAM,gBAAgB,GAAG,MAAM,IAAI;CACnC,CAAC,MAAM,UAAU,GAAG,IAAI,GAAG,EAAE,CAAC;AAC9B;CACA,CAAC,GAAG;CACJ,EAAE,KAAK,MAAM,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;CAC7C,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC;CACjC,GAAG;CACH,EAAE,QAAQ,CAAC,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,KAAK,MAAM,KAAK,MAAM,CAAC,SAAS,EAAE;AACpF;CACA,CAAC,OAAO,UAAU,CAAC;CACnB,CAAC,CAAC;AACF;KACA,QAAc,GAAG,CAAC,IAAI,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,EAAE,KAAK;CACpD,CAAC,MAAM,MAAM,GAAG,GAAG,IAAI;CACvB,EAAE,MAAM,KAAK,GAAG,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ,GAAG,GAAG,KAAK,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC7F;CACA,EAAE,IAA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