Skip to content

Commit

Permalink
use AudioPlayer class for audio-keyboard-response
Browse files Browse the repository at this point in the history
  • Loading branch information
jodeleeuw committed Nov 8, 2023
1 parent cd2a94b commit f158774
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions packages/plugin-audio-keyboard-response/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import autoBind from "auto-bind";
import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from "jspsych";

import { AudioPlayer } from "../../jspsych/src/modules/plugin-api/AudioPlayer";

const info = <const>{
name: "audio-keyboard-response",
parameters: {
Expand Down Expand Up @@ -61,7 +63,7 @@ type Info = typeof info;
*/
class AudioKeyboardResponsePlugin implements JsPsychPlugin<Info> {
static info = info;
private audio;
private audio: AudioPlayer;
private params: TrialType<Info>;
private display: HTMLElement;
private response: { rt: number; key: string } = { rt: null, key: null };
Expand Down Expand Up @@ -175,28 +177,32 @@ class AudioKeyboardResponsePlugin implements JsPsychPlugin<Info> {
}
}

simulate(
async simulate(
trial: TrialType<Info>,
simulation_mode,
simulation_options: any,
load_callback: () => void
) {
if (simulation_mode == "data-only") {
load_callback();
this.simulate_data_only(trial, simulation_options);
return this.simulate_data_only(trial, simulation_options);
}
if (simulation_mode == "visual") {
this.simulate_visual(trial, simulation_options, load_callback);
return this.simulate_visual(trial, simulation_options, load_callback);
}
}

private simulate_data_only(trial: TrialType<Info>, simulation_options) {
const data = this.create_simulation_data(trial, simulation_options);

this.jsPsych.finishTrial(data);
return data;
}

private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {
private async simulate_visual(
trial: TrialType<Info>,
simulation_options,
load_callback: () => void
) {
const data = this.create_simulation_data(trial, simulation_options);

const display_element = this.jsPsych.getDisplayElement();
Expand All @@ -207,14 +213,16 @@ class AudioKeyboardResponsePlugin implements JsPsychPlugin<Info> {
}
};

this.trial(display_element, trial, () => {
const result = await this.trial(display_element, trial, () => {
load_callback();
if (!trial.response_allowed_while_playing) {
this.audio.addEventListener("ended", respond);
} else {
respond();
}
});

return result;
}

private create_simulation_data(trial: TrialType<Info>, simulation_options) {
Expand Down

0 comments on commit f158774

Please sign in to comment.