@jspsych/plugin-audio-keyboard-response 1.1.3 → 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/dist/index.d.ts CHANGED
@@ -1,35 +1,49 @@
1
- import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from "jspsych";
1
+ import { JsPsychPlugin, ParameterType, JsPsych, TrialType } from 'jspsych';
2
+
2
3
  declare const info: {
3
4
  readonly name: "audio-keyboard-response";
5
+ readonly version: string;
4
6
  readonly parameters: {
5
7
  /** The audio file to be played. */
6
8
  readonly stimulus: {
7
9
  readonly type: ParameterType.AUDIO;
8
- readonly pretty_name: "Stimulus";
9
10
  readonly default: any;
10
11
  };
11
- /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */
12
+ /** This array contains the key(s) that the participant is allowed to press in order to respond to the stimulus.
13
+ * Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) -
14
+ * see [this page](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values)
15
+ * and [this page (event.key column)](https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/)
16
+ * for more examples. Any key presses that are not listed in the array will be ignored. The default value of `"ALL_KEYS"`
17
+ * means that all keys will be accepted as valid responses. Specifying `"NO_KEYS"` will mean that no responses are allowed.
18
+ */
12
19
  readonly choices: {
13
20
  readonly type: ParameterType.KEYS;
14
- readonly pretty_name: "Choices";
15
21
  readonly default: "ALL_KEYS";
16
22
  };
17
- /** Any content here will be displayed below the stimulus. */
23
+ /** This string can contain HTML markup. Any content here will be displayed below the stimulus. The intention is that
24
+ * it can be used to provide a reminder about the action the participant is supposed to take (e.g., which key to press).
25
+ */
18
26
  readonly prompt: {
19
27
  readonly type: ParameterType.HTML_STRING;
20
28
  readonly pretty_name: "Prompt";
21
29
  readonly default: any;
22
30
  };
23
- /** The maximum duration to wait for a response. */
31
+ /** How long to wait for the participant to make a response before ending the trial in milliseconds. If the
32
+ * participant fails to make a response before this timer is reached, the participant's response will be
33
+ * recorded as null for the trial and the trial will end. If the value of this parameter is null, then the
34
+ * trial will wait for a response indefinitely.
35
+ */
24
36
  readonly trial_duration: {
25
37
  readonly type: ParameterType.INT;
26
- readonly pretty_name: "Trial duration";
27
38
  readonly default: any;
28
39
  };
29
- /** If true, the trial will end when user makes a response. */
40
+ /** If true, then the trial will end whenever the participant makes a response (assuming they make their
41
+ * response before the cutoff specified by the `trial_duration` parameter). If false, then the trial will
42
+ * continue until the value for `trial_duration` is reached. You can use set this parameter to `false` to
43
+ * force the participant to listen to the stimulus for a fixed amount of time, even if they respond before the time is complete
44
+ */
30
45
  readonly response_ends_trial: {
31
46
  readonly type: ParameterType.BOOL;
32
- readonly pretty_name: "Response ends trial";
33
47
  readonly default: true;
34
48
  };
35
49
  /** If true, then the trial will end as soon as the audio file finishes playing. */
@@ -38,56 +52,97 @@ declare const info: {
38
52
  readonly pretty_name: "Trial ends after audio";
39
53
  readonly default: false;
40
54
  };
41
- /** If true, then responses are allowed while the audio is playing. If false, then the audio must finish playing before a response is accepted. */
55
+ /** If true, then responses are allowed while the audio is playing. If false, then the audio must finish
56
+ * playing before a keyboard response is accepted. Once the audio has played all the way through, a valid
57
+ * keyboard response is allowed (including while the audio is being re-played via on-screen playback controls).
58
+ */
42
59
  readonly response_allowed_while_playing: {
43
60
  readonly type: ParameterType.BOOL;
44
- readonly pretty_name: "Response allowed while playing";
45
61
  readonly default: true;
46
62
  };
47
63
  };
64
+ readonly data: {
65
+ /** Indicates which key the participant pressed. If no key was pressed before the trial ended, then the value will be `null`. */
66
+ readonly response: {
67
+ readonly type: ParameterType.STRING;
68
+ };
69
+ /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus
70
+ * first began playing until the participant made a key response. If no key was pressed before the trial ended, then the
71
+ * value will be `null`.
72
+ */
73
+ readonly rt: {
74
+ readonly type: ParameterType.INT;
75
+ };
76
+ /** Path to the audio file that played during the trial. */
77
+ readonly stimulus: {
78
+ readonly type: ParameterType.STRING;
79
+ };
80
+ };
48
81
  };
49
82
  type Info = typeof info;
50
83
  /**
51
- * **audio-keyboard-response**
84
+ * This plugin plays audio files and records responses generated with the keyboard.
85
+ *
86
+ * If the browser supports it, audio files are played using the WebAudio API. This allows for reasonably precise timing of the
87
+ * playback. The timing of responses generated is measured against the WebAudio specific clock, improving the measurement of
88
+ * response times. If the browser does not support the WebAudio API, then the audio file is played with HTML5 audio.
52
89
  *
53
- * jsPsych plugin for playing an audio file and getting a keyboard response
90
+ * Audio files can be automatically preloaded by jsPsych using the [`preload` plugin](preload.md). However, if you are using
91
+ * 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.
92
+ *
93
+ * The trial can end when the participant responds, when the audio file has finished playing, or if the participant has
94
+ * failed to respond within a fixed length of time. You can also prevent a keyboard response from being recorded before
95
+ * the audio has finished playing.
54
96
  *
55
97
  * @author Josh de Leeuw
56
- * @see {@link https://www.jspsych.org/plugins/jspsych-audio-keyboard-response/ audio-keyboard-response plugin documentation on jspsych.org}
98
+ * @see {@link https://www.jspsych.org/latest/plugins/audio-keyboard-response/ audio-keyboard-response plugin documentation on jspsych.org}
57
99
  */
58
100
  declare class AudioKeyboardResponsePlugin implements JsPsychPlugin<Info> {
59
101
  private jsPsych;
60
102
  static info: {
61
103
  readonly name: "audio-keyboard-response";
104
+ readonly version: string;
62
105
  readonly parameters: {
63
106
  /** The audio file to be played. */
64
107
  readonly stimulus: {
65
108
  readonly type: ParameterType.AUDIO;
66
- readonly pretty_name: "Stimulus";
67
109
  readonly default: any;
68
110
  };
69
- /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */
111
+ /** This array contains the key(s) that the participant is allowed to press in order to respond to the stimulus.
112
+ * Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) -
113
+ * see [this page](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values)
114
+ * and [this page (event.key column)](https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/)
115
+ * for more examples. Any key presses that are not listed in the array will be ignored. The default value of `"ALL_KEYS"`
116
+ * means that all keys will be accepted as valid responses. Specifying `"NO_KEYS"` will mean that no responses are allowed.
117
+ */
70
118
  readonly choices: {
71
119
  readonly type: ParameterType.KEYS;
72
- readonly pretty_name: "Choices";
73
120
  readonly default: "ALL_KEYS";
74
121
  };
75
- /** Any content here will be displayed below the stimulus. */
122
+ /** This string can contain HTML markup. Any content here will be displayed below the stimulus. The intention is that
123
+ * it can be used to provide a reminder about the action the participant is supposed to take (e.g., which key to press).
124
+ */
76
125
  readonly prompt: {
77
126
  readonly type: ParameterType.HTML_STRING;
78
127
  readonly pretty_name: "Prompt";
79
128
  readonly default: any;
80
129
  };
81
- /** The maximum duration to wait for a response. */
130
+ /** How long to wait for the participant to make a response before ending the trial in milliseconds. If the
131
+ * participant fails to make a response before this timer is reached, the participant's response will be
132
+ * recorded as null for the trial and the trial will end. If the value of this parameter is null, then the
133
+ * trial will wait for a response indefinitely.
134
+ */
82
135
  readonly trial_duration: {
83
136
  readonly type: ParameterType.INT;
84
- readonly pretty_name: "Trial duration";
85
137
  readonly default: any;
86
138
  };
87
- /** If true, the trial will end when user makes a response. */
139
+ /** If true, then the trial will end whenever the participant makes a response (assuming they make their
140
+ * response before the cutoff specified by the `trial_duration` parameter). If false, then the trial will
141
+ * continue until the value for `trial_duration` is reached. You can use set this parameter to `false` to
142
+ * force the participant to listen to the stimulus for a fixed amount of time, even if they respond before the time is complete
143
+ */
88
144
  readonly response_ends_trial: {
89
145
  readonly type: ParameterType.BOOL;
90
- readonly pretty_name: "Response ends trial";
91
146
  readonly default: true;
92
147
  };
93
148
  /** If true, then the trial will end as soon as the audio file finishes playing. */
@@ -96,20 +151,48 @@ declare class AudioKeyboardResponsePlugin implements JsPsychPlugin<Info> {
96
151
  readonly pretty_name: "Trial ends after audio";
97
152
  readonly default: false;
98
153
  };
99
- /** If true, then responses are allowed while the audio is playing. If false, then the audio must finish playing before a response is accepted. */
154
+ /** If true, then responses are allowed while the audio is playing. If false, then the audio must finish
155
+ * playing before a keyboard response is accepted. Once the audio has played all the way through, a valid
156
+ * keyboard response is allowed (including while the audio is being re-played via on-screen playback controls).
157
+ */
100
158
  readonly response_allowed_while_playing: {
101
159
  readonly type: ParameterType.BOOL;
102
- readonly pretty_name: "Response allowed while playing";
103
160
  readonly default: true;
104
161
  };
105
162
  };
163
+ readonly data: {
164
+ /** Indicates which key the participant pressed. If no key was pressed before the trial ended, then the value will be `null`. */
165
+ readonly response: {
166
+ readonly type: ParameterType.STRING;
167
+ };
168
+ /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus
169
+ * first began playing until the participant made a key response. If no key was pressed before the trial ended, then the
170
+ * value will be `null`.
171
+ */
172
+ readonly rt: {
173
+ readonly type: ParameterType.INT;
174
+ };
175
+ /** Path to the audio file that played during the trial. */
176
+ readonly stimulus: {
177
+ readonly type: ParameterType.STRING;
178
+ };
179
+ };
106
180
  };
107
181
  private audio;
182
+ private params;
183
+ private display;
184
+ private response;
185
+ private startTime;
186
+ private finish;
108
187
  constructor(jsPsych: JsPsych);
109
188
  trial(display_element: HTMLElement, trial: TrialType<Info>, on_load: () => void): Promise<unknown>;
110
- simulate(trial: TrialType<Info>, simulation_mode: any, simulation_options: any, load_callback: () => void): void;
189
+ private end_trial;
190
+ private after_response;
191
+ private setup_keyboard_listener;
192
+ simulate(trial: TrialType<Info>, simulation_mode: any, simulation_options: any, load_callback: () => void): Promise<any>;
111
193
  private simulate_data_only;
112
194
  private simulate_visual;
113
195
  private create_simulation_data;
114
196
  }
115
- export default AudioKeyboardResponsePlugin;
197
+
198
+ export { AudioKeyboardResponsePlugin as default };
package/dist/index.js CHANGED
@@ -1,233 +1,212 @@
1
+ import autoBind from 'auto-bind';
1
2
  import { ParameterType } from 'jspsych';
2
3
 
4
+ var _package = {
5
+ name: "@jspsych/plugin-audio-keyboard-response",
6
+ version: "2.0.0",
7
+ description: "jsPsych plugin for playing an audio file and getting a keyboard response",
8
+ type: "module",
9
+ main: "dist/index.cjs",
10
+ exports: {
11
+ import: "./dist/index.js",
12
+ require: "./dist/index.cjs"
13
+ },
14
+ typings: "dist/index.d.ts",
15
+ unpkg: "dist/index.browser.min.js",
16
+ files: [
17
+ "src",
18
+ "dist"
19
+ ],
20
+ source: "src/index.ts",
21
+ scripts: {
22
+ test: "jest",
23
+ "test:watch": "npm test -- --watch",
24
+ tsc: "tsc",
25
+ build: "rollup --config",
26
+ "build:watch": "npm run build -- --watch"
27
+ },
28
+ repository: {
29
+ type: "git",
30
+ url: "git+https://github.com/jspsych/jsPsych.git",
31
+ directory: "packages/plugin-audio-keyboard-response"
32
+ },
33
+ author: "Josh de Leeuw",
34
+ license: "MIT",
35
+ bugs: {
36
+ url: "https://github.com/jspsych/jsPsych/issues"
37
+ },
38
+ homepage: "https://www.jspsych.org/latest/plugins/audio-keyboard-response",
39
+ peerDependencies: {
40
+ jspsych: ">=7.1.0"
41
+ },
42
+ devDependencies: {
43
+ "@jspsych/config": "^3.0.0",
44
+ "@jspsych/test-utils": "^1.2.0"
45
+ }
46
+ };
47
+
3
48
  const info = {
4
- name: "audio-keyboard-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
- /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */
13
- choices: {
14
- type: ParameterType.KEYS,
15
- pretty_name: "Choices",
16
- default: "ALL_KEYS",
17
- },
18
- /** Any content here will be displayed below the stimulus. */
19
- prompt: {
20
- type: ParameterType.HTML_STRING,
21
- pretty_name: "Prompt",
22
- default: null,
23
- },
24
- /** The maximum duration to wait for a response. */
25
- trial_duration: {
26
- type: ParameterType.INT,
27
- pretty_name: "Trial duration",
28
- default: null,
29
- },
30
- /** If true, the trial will end when user makes a response. */
31
- response_ends_trial: {
32
- type: ParameterType.BOOL,
33
- pretty_name: "Response ends trial",
34
- default: true,
35
- },
36
- /** If true, then the trial will end as soon as the audio file finishes playing. */
37
- trial_ends_after_audio: {
38
- type: ParameterType.BOOL,
39
- pretty_name: "Trial ends after audio",
40
- default: false,
41
- },
42
- /** If true, then responses are allowed while the audio is playing. If false, then the audio must finish playing before a response is accepted. */
43
- response_allowed_while_playing: {
44
- type: ParameterType.BOOL,
45
- pretty_name: "Response allowed while playing",
46
- default: true,
47
- },
49
+ name: "audio-keyboard-response",
50
+ version: _package.version,
51
+ parameters: {
52
+ stimulus: {
53
+ type: ParameterType.AUDIO,
54
+ default: void 0
48
55
  },
49
- };
50
- /**
51
- * **audio-keyboard-response**
52
- *
53
- * jsPsych plugin for playing an audio file and getting a keyboard response
54
- *
55
- * @author Josh de Leeuw
56
- * @see {@link https://www.jspsych.org/plugins/jspsych-audio-keyboard-response/ audio-keyboard-response plugin documentation on jspsych.org}
57
- */
58
- class AudioKeyboardResponsePlugin {
59
- constructor(jsPsych) {
60
- this.jsPsych = jsPsych;
56
+ choices: {
57
+ type: ParameterType.KEYS,
58
+ default: "ALL_KEYS"
59
+ },
60
+ prompt: {
61
+ type: ParameterType.HTML_STRING,
62
+ pretty_name: "Prompt",
63
+ default: null
64
+ },
65
+ trial_duration: {
66
+ type: ParameterType.INT,
67
+ default: null
68
+ },
69
+ response_ends_trial: {
70
+ type: ParameterType.BOOL,
71
+ default: true
72
+ },
73
+ trial_ends_after_audio: {
74
+ type: ParameterType.BOOL,
75
+ pretty_name: "Trial ends after audio",
76
+ default: false
77
+ },
78
+ response_allowed_while_playing: {
79
+ type: ParameterType.BOOL,
80
+ default: true
61
81
  }
62
- trial(display_element, trial, on_load) {
63
- // hold the .resolve() function from the Promise that ends the trial
64
- let trial_complete;
65
- // setup stimulus
66
- var context = this.jsPsych.pluginAPI.audioContext();
67
- // store response
68
- var response = {
69
- rt: null,
70
- key: null,
71
- };
72
- // record webaudio context start time
73
- var startTime;
74
- // load audio file
75
- this.jsPsych.pluginAPI
76
- .getAudioBuffer(trial.stimulus)
77
- .then((buffer) => {
78
- if (context !== null) {
79
- this.audio = context.createBufferSource();
80
- this.audio.buffer = buffer;
81
- this.audio.connect(context.destination);
82
- }
83
- else {
84
- this.audio = buffer;
85
- this.audio.currentTime = 0;
86
- }
87
- setupTrial();
88
- })
89
- .catch((err) => {
90
- console.error(`Failed to load audio file "${trial.stimulus}". Try checking the file path. We recommend using the preload plugin to load audio files.`);
91
- console.error(err);
92
- });
93
- const setupTrial = () => {
94
- // set up end event if trial needs it
95
- if (trial.trial_ends_after_audio) {
96
- this.audio.addEventListener("ended", end_trial);
97
- }
98
- // show prompt if there is one
99
- if (trial.prompt !== null) {
100
- display_element.innerHTML = trial.prompt;
101
- }
102
- // start audio
103
- if (context !== null) {
104
- startTime = context.currentTime;
105
- this.audio.start(startTime);
106
- }
107
- else {
108
- this.audio.play();
109
- }
110
- // start keyboard listener when trial starts or sound ends
111
- if (trial.response_allowed_while_playing) {
112
- setup_keyboard_listener();
113
- }
114
- else if (!trial.trial_ends_after_audio) {
115
- this.audio.addEventListener("ended", setup_keyboard_listener);
116
- }
117
- // end trial if time limit is set
118
- if (trial.trial_duration !== null) {
119
- this.jsPsych.pluginAPI.setTimeout(() => {
120
- end_trial();
121
- }, trial.trial_duration);
122
- }
123
- on_load();
124
- };
125
- // function to end trial when it is time
126
- const end_trial = () => {
127
- // kill any remaining setTimeout handlers
128
- this.jsPsych.pluginAPI.clearAllTimeouts();
129
- // stop the audio file if it is playing
130
- // remove end event listeners if they exist
131
- if (context !== null) {
132
- this.audio.stop();
133
- }
134
- else {
135
- this.audio.pause();
136
- }
137
- this.audio.removeEventListener("ended", end_trial);
138
- this.audio.removeEventListener("ended", setup_keyboard_listener);
139
- // kill keyboard listeners
140
- this.jsPsych.pluginAPI.cancelAllKeyboardResponses();
141
- // gather the data to store for the trial
142
- var trial_data = {
143
- rt: response.rt,
144
- stimulus: trial.stimulus,
145
- response: response.key,
146
- };
147
- // clear the display
148
- display_element.innerHTML = "";
149
- // move on to the next trial
150
- this.jsPsych.finishTrial(trial_data);
151
- trial_complete();
152
- };
153
- // function to handle responses by the subject
154
- function after_response(info) {
155
- // only record the first response
156
- if (response.key == null) {
157
- response = info;
158
- }
159
- if (trial.response_ends_trial) {
160
- end_trial();
161
- }
162
- }
163
- const setup_keyboard_listener = () => {
164
- // start the response listener
165
- if (context !== null) {
166
- this.jsPsych.pluginAPI.getKeyboardResponse({
167
- callback_function: after_response,
168
- valid_responses: trial.choices,
169
- rt_method: "audio",
170
- persist: false,
171
- allow_held_key: false,
172
- audio_context: context,
173
- audio_context_start_time: startTime,
174
- });
175
- }
176
- else {
177
- this.jsPsych.pluginAPI.getKeyboardResponse({
178
- callback_function: after_response,
179
- valid_responses: trial.choices,
180
- rt_method: "performance",
181
- persist: false,
182
- allow_held_key: false,
183
- });
184
- }
185
- };
186
- return new Promise((resolve) => {
187
- trial_complete = resolve;
188
- });
82
+ },
83
+ data: {
84
+ response: {
85
+ type: ParameterType.STRING
86
+ },
87
+ rt: {
88
+ type: ParameterType.INT
89
+ },
90
+ stimulus: {
91
+ type: ParameterType.STRING
189
92
  }
190
- simulate(trial, simulation_mode, simulation_options, load_callback) {
191
- if (simulation_mode == "data-only") {
192
- load_callback();
193
- this.simulate_data_only(trial, simulation_options);
194
- }
195
- if (simulation_mode == "visual") {
196
- this.simulate_visual(trial, simulation_options, load_callback);
197
- }
93
+ }
94
+ };
95
+ class AudioKeyboardResponsePlugin {
96
+ constructor(jsPsych) {
97
+ this.jsPsych = jsPsych;
98
+ this.response = { rt: null, key: null };
99
+ autoBind(this);
100
+ }
101
+ trial(display_element, trial, on_load) {
102
+ return new Promise(async (resolve) => {
103
+ this.finish = resolve;
104
+ this.params = trial;
105
+ this.display = display_element;
106
+ this.audio = await this.jsPsych.pluginAPI.getAudioPlayer(trial.stimulus);
107
+ if (trial.trial_ends_after_audio) {
108
+ this.audio.addEventListener("ended", this.end_trial);
109
+ }
110
+ if (trial.prompt !== null) {
111
+ display_element.innerHTML = trial.prompt;
112
+ }
113
+ this.startTime = this.jsPsych.pluginAPI.audioContext()?.currentTime;
114
+ if (trial.response_allowed_while_playing) {
115
+ this.setup_keyboard_listener();
116
+ } else if (!trial.trial_ends_after_audio) {
117
+ this.audio.addEventListener("ended", this.setup_keyboard_listener);
118
+ }
119
+ if (trial.trial_duration !== null) {
120
+ this.jsPsych.pluginAPI.setTimeout(() => {
121
+ this.end_trial();
122
+ }, trial.trial_duration);
123
+ }
124
+ on_load();
125
+ this.audio.play();
126
+ });
127
+ }
128
+ end_trial() {
129
+ this.jsPsych.pluginAPI.clearAllTimeouts();
130
+ this.audio.stop();
131
+ this.audio.removeEventListener("ended", this.end_trial);
132
+ this.audio.removeEventListener("ended", this.setup_keyboard_listener);
133
+ this.jsPsych.pluginAPI.cancelAllKeyboardResponses();
134
+ var trial_data = {
135
+ rt: this.response.rt,
136
+ response: this.response.key,
137
+ stimulus: this.params.stimulus
138
+ };
139
+ this.display.innerHTML = "";
140
+ this.finish(trial_data);
141
+ }
142
+ after_response(info2) {
143
+ this.response = info2;
144
+ if (this.params.response_ends_trial) {
145
+ this.end_trial();
198
146
  }
199
- simulate_data_only(trial, simulation_options) {
200
- const data = this.create_simulation_data(trial, simulation_options);
201
- this.jsPsych.finishTrial(data);
147
+ }
148
+ setup_keyboard_listener() {
149
+ if (this.jsPsych.pluginAPI.useWebaudio) {
150
+ this.jsPsych.pluginAPI.getKeyboardResponse({
151
+ callback_function: this.after_response,
152
+ valid_responses: this.params.choices,
153
+ rt_method: "audio",
154
+ persist: false,
155
+ allow_held_key: false,
156
+ audio_context: this.jsPsych.pluginAPI.audioContext(),
157
+ audio_context_start_time: this.startTime
158
+ });
159
+ } else {
160
+ this.jsPsych.pluginAPI.getKeyboardResponse({
161
+ callback_function: this.after_response,
162
+ valid_responses: this.params.choices,
163
+ rt_method: "performance",
164
+ persist: false,
165
+ allow_held_key: false
166
+ });
202
167
  }
203
- simulate_visual(trial, simulation_options, load_callback) {
204
- const data = this.create_simulation_data(trial, simulation_options);
205
- const display_element = this.jsPsych.getDisplayElement();
206
- const respond = () => {
207
- if (data.rt !== null) {
208
- this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
209
- }
210
- };
211
- this.trial(display_element, trial, () => {
212
- load_callback();
213
- if (!trial.response_allowed_while_playing) {
214
- this.audio.addEventListener("ended", respond);
215
- }
216
- else {
217
- respond();
218
- }
219
- });
168
+ }
169
+ async simulate(trial, simulation_mode, simulation_options, load_callback) {
170
+ if (simulation_mode == "data-only") {
171
+ load_callback();
172
+ return this.simulate_data_only(trial, simulation_options);
220
173
  }
221
- create_simulation_data(trial, simulation_options) {
222
- const default_data = {
223
- stimulus: trial.stimulus,
224
- rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
225
- response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
226
- };
227
- const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
228
- this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
229
- return data;
174
+ if (simulation_mode == "visual") {
175
+ return this.simulate_visual(trial, simulation_options, load_callback);
230
176
  }
177
+ }
178
+ simulate_data_only(trial, simulation_options) {
179
+ const data = this.create_simulation_data(trial, simulation_options);
180
+ return data;
181
+ }
182
+ async simulate_visual(trial, simulation_options, load_callback) {
183
+ const data = this.create_simulation_data(trial, simulation_options);
184
+ const display_element = this.jsPsych.getDisplayElement();
185
+ const respond = () => {
186
+ if (data.rt !== null) {
187
+ this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
188
+ }
189
+ };
190
+ const result = await this.trial(display_element, trial, () => {
191
+ load_callback();
192
+ if (!trial.response_allowed_while_playing) {
193
+ this.audio.addEventListener("ended", respond);
194
+ } else {
195
+ respond();
196
+ }
197
+ });
198
+ return result;
199
+ }
200
+ create_simulation_data(trial, simulation_options) {
201
+ const default_data = {
202
+ stimulus: trial.stimulus,
203
+ rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
204
+ response: this.jsPsych.pluginAPI.getValidKey(trial.choices)
205
+ };
206
+ const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
207
+ this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
208
+ return data;
209
+ }
231
210
  }
232
211
  AudioKeyboardResponsePlugin.info = info;
233
212