diff --git a/.changeset/cuddly-coins-fold.md b/.changeset/cuddly-coins-fold.md new file mode 100644 index 0000000000..eb675c9fb5 --- /dev/null +++ b/.changeset/cuddly-coins-fold.md @@ -0,0 +1,6 @@ +--- +"@jspsych/plugin-audio-button-response": patch +"@jspsych/plugin-audio-slider-response": patch +--- + +Fixed negative response times being recorded by ensuring if the AudioContext object exists, startTime is recorded with respect to that. diff --git a/packages/plugin-audio-button-response/src/index.ts b/packages/plugin-audio-button-response/src/index.ts index 954c7b6437..96ad72ecf7 100644 --- a/packages/plugin-audio-button-response/src/index.ts +++ b/packages/plugin-audio-button-response/src/index.ts @@ -147,8 +147,6 @@ class AudioButtonResponsePlugin implements JsPsychPlugin { } async trial(display_element: HTMLElement, trial: TrialType, on_load: () => void) { - // hold the .resolve() function from the Promise that ends the trial - this.trial_complete; this.params = trial; this.display = display_element; // setup stimulus @@ -217,9 +215,6 @@ class AudioButtonResponsePlugin implements JsPsychPlugin { this.disable_buttons(); } - // start time - this.startTime = performance.now(); - // end trial if time limit is set if (trial.trial_duration !== null) { this.jsPsych.pluginAPI.setTimeout(() => { @@ -229,9 +224,17 @@ class AudioButtonResponsePlugin implements JsPsychPlugin { on_load(); + // start time + this.startTime = performance.now(); + if (this.context !== null) { + this.startTime = this.context.currentTime; + } + + // start audio this.audio.play(); return new Promise((resolve) => { + // hold the .resolve() function from the Promise that ends the trial this.trial_complete = resolve; }); } diff --git a/packages/plugin-audio-slider-response/src/index.ts b/packages/plugin-audio-slider-response/src/index.ts index 2a803a74c2..f43e376f94 100644 --- a/packages/plugin-audio-slider-response/src/index.ts +++ b/packages/plugin-audio-slider-response/src/index.ts @@ -156,8 +156,6 @@ class AudioSliderResponsePlugin implements JsPsychPlugin { } async trial(display_element: HTMLElement, trial: TrialType, on_load: () => void) { - // record webaudio context start time - this.startTime; this.params = trial; this.display = display_element; // for storing data related to response @@ -325,7 +323,12 @@ class AudioSliderResponsePlugin implements JsPsychPlugin { } }); + //record start time this.startTime = performance.now(); + // record webaudio context start time + if (this.context !== null) { + this.startTime = this.context.currentTime; + } // start audio this.audio.play();