Skip to content

Commit

Permalink
Merge pull request #3348 from vzhang03/audio-player
Browse files Browse the repository at this point in the history
Fixed negative rt bug in audio-button and audio-slider plugin.
  • Loading branch information
jodeleeuw authored Jul 17, 2024
2 parents f56b894 + 5552e48 commit e374226
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/cuddly-coins-fold.md
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 8 additions & 5 deletions packages/plugin-audio-button-response/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ class AudioButtonResponsePlugin implements JsPsychPlugin<Info> {
}

async trial(display_element: HTMLElement, trial: TrialType<Info>, 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
Expand Down Expand Up @@ -217,9 +215,6 @@ class AudioButtonResponsePlugin implements JsPsychPlugin<Info> {
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(() => {
Expand All @@ -229,9 +224,17 @@ class AudioButtonResponsePlugin implements JsPsychPlugin<Info> {

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;
});
}
Expand Down
7 changes: 5 additions & 2 deletions packages/plugin-audio-slider-response/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ class AudioSliderResponsePlugin implements JsPsychPlugin<Info> {
}

async trial(display_element: HTMLElement, trial: TrialType<Info>, on_load: () => void) {
// record webaudio context start time
this.startTime;
this.params = trial;
this.display = display_element;
// for storing data related to response
Expand Down Expand Up @@ -325,7 +323,12 @@ class AudioSliderResponsePlugin implements JsPsychPlugin<Info> {
}
});

//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();
Expand Down

0 comments on commit e374226

Please sign in to comment.