remove audio buffer time, fixes #476

This commit is contained in:
Josh de Leeuw 2017-12-06 12:57:15 -05:00
parent de9dff4e4a
commit fd78450ecf
4 changed files with 29 additions and 14 deletions

View File

@ -172,7 +172,7 @@ Gets an AudioBuffer that can be played with the WebAudio API or an Audio object
var source = context.createBufferSource(); var source = context.createBufferSource();
source.buffer = jsPsych.pluginAPI.getAudioBuffer(trial.stimulus); source.buffer = jsPsych.pluginAPI.getAudioBuffer(trial.stimulus);
source.connect(context.destination); source.connect(context.destination);
startTime = context.currentTime + 0.1; startTime = context.currentTime;
source.start(startTime); source.start(startTime);
``` ```

View File

@ -161,7 +161,7 @@ jsPsych.plugins["audio-button-response"] = (function() {
// function to end trial when it is time // function to end trial when it is time
function end_trial() { function end_trial() {
// stop the audio file if it is playing // stop the audio file if it is playing
// remove end event listeners if they exist // remove end event listeners if they exist
if(context !== null){ if(context !== null){
@ -194,7 +194,7 @@ jsPsych.plugins["audio-button-response"] = (function() {
// start audio // start audio
if(context !== null){ if(context !== null){
startTime = context.currentTime + 0.1; startTime = context.currentTime;
source.start(startTime); source.start(startTime);
} else { } else {
audio.play(); audio.play();

View File

@ -142,7 +142,7 @@ jsPsych.plugins["audio-keyboard-response"] = (function() {
// start audio // start audio
if(context !== null){ if(context !== null){
startTime = context.currentTime + 0.1; startTime = context.currentTime;
source.start(startTime); source.start(startTime);
} else { } else {
audio.play(); audio.play();

View File

@ -41,7 +41,7 @@ jsPsych.plugins['audio-slider-response'] = (function() {
button_label: { button_label: {
type: jsPsych.plugins.parameterType.STRING, type: jsPsych.plugins.parameterType.STRING,
pretty_name: 'Button label', pretty_name: 'Button label',
default: undefined, default: 'Continue',
array: false, array: false,
description: 'Label of the button to advance.' description: 'Label of the button to advance.'
}, },
@ -72,7 +72,7 @@ jsPsych.plugins['audio-slider-response'] = (function() {
} }
} }
plugin.trial = function(display_element, trial) { plugin.trial = function(display_element, trial) {
// setup stimulus // setup stimulus
var context = jsPsych.pluginAPI.audioContext(); var context = jsPsych.pluginAPI.audioContext();
@ -97,8 +97,7 @@ jsPsych.plugins['audio-slider-response'] = (function() {
} }
var html = '<div id="jspsych-audio-slider-response-wrapper" style="margin: 100px 0px;">'; var html = '<div id="jspsych-audio-slider-response-wrapper" style="margin: 100px 0px;">';
html += '<div id="jspsych-audio-slider-response-stimulus"><img src="' + trial.stimulus + '"></div>'; html += '<div class="jspsych-audio-slider-response-container" style="position:relative;">';
html += '<div class="jspsych-audio-slider-response-container" style="position:relative;">';
html += '<input type="range" min="'+trial.min+'" max="'+trial.max+'" step="'+trial.step+'" style="width: 100%;" id="jspsych-audio-slider-response-response"></input>'; html += '<input type="range" min="'+trial.min+'" max="'+trial.max+'" step="'+trial.step+'" style="width: 100%;" id="jspsych-audio-slider-response-response"></input>';
html += '<div>' html += '<div>'
for(var j=0; j < trial.labels.length; j++){ for(var j=0; j < trial.labels.length; j++){
@ -127,7 +126,12 @@ jsPsych.plugins['audio-slider-response'] = (function() {
display_element.querySelector('#jspsych-audio-slider-response-next').addEventListener('click', function() { display_element.querySelector('#jspsych-audio-slider-response-next').addEventListener('click', function() {
// measure response time // measure response time
var endTime = (new Date()).getTime(); var endTime = (new Date()).getTime();
response.rt = endTime - startTime; var rt = endTime - startTime;
if(context !== null){
endTime = context.currentTime;
rt = Math.round((endTime - startTime) * 1000);
}
response.rt = rt;
response.response = display_element.querySelector('#jspsych-audio-slider-response-response').value; response.response = display_element.querySelector('#jspsych-audio-slider-response-response').value;
if(trial.response_ends_trial){ if(trial.response_ends_trial){
@ -142,6 +146,14 @@ jsPsych.plugins['audio-slider-response'] = (function() {
jsPsych.pluginAPI.clearAllTimeouts(); jsPsych.pluginAPI.clearAllTimeouts();
if(context !== null){
source.stop();
source.onended = function() { }
} else {
audio.pause();
audio.removeEventListener('ended', end_trial);
}
// save data // save data
var trialdata = { var trialdata = {
"rt": response.rt, "rt": response.rt,
@ -154,10 +166,13 @@ jsPsych.plugins['audio-slider-response'] = (function() {
jsPsych.finishTrial(trialdata); jsPsych.finishTrial(trialdata);
} }
if (trial.stimulus_duration > 0) { var startTime = (new Date()).getTime();
jsPsych.pluginAPI.setTimeout(function() { // start audio
display_element.querySelector('#jspsych-audio-slider-response-stimulus').style.visibility = 'hidden'; if(context !== null){
}, trial.stimulus_duration); startTime = context.currentTime;
source.start(startTime);
} else {
audio.play();
} }
// end trial if trial_duration is set // end trial if trial_duration is set
@ -167,7 +182,7 @@ jsPsych.plugins['audio-slider-response'] = (function() {
}, trial.trial_duration); }, trial.trial_duration);
} }
var startTime = (new Date()).getTime();
}; };
return plugin; return plugin;