make webaudio optional #508

This commit is contained in:
Josh de Leeuw 2018-02-18 16:19:09 -05:00
parent 7fa09da61f
commit f3d5a5043d
5 changed files with 19 additions and 8 deletions

View File

@ -308,6 +308,7 @@ show_preload_progress_bar | boolean | If true, then a progress bar is displayed
preload_audio | array | An array of audio files to preload before starting the experiment.
preload_images | array | An array of image files to preload before starting the experiment.
max_load_time | numeric | The maximum number of milliseconds to wait for content to preload. If the wait time is exceeded an error message is displayed and the experiment stops. The default value is 60 seconds.
use_webaudio | boolean | If false, then jsPsych will not attempt to use the WebAudio API for audio playback. Instead, HTML5 Audio objects will be used. The WebAudio API offers more precise control over the timing of audio events, and should be used when possible. The default value is true.
default_iti | numeric | The default inter-trial interval in ms. The default value if none is specified is 0ms.
Possible values for the exclusions parameter above.

View File

@ -28,6 +28,7 @@
jsPsych.init({
timeline: timeline,
use_webaudio: false,
on_finish: function(){jsPsych.data.displayData();}
});

View File

@ -32,6 +32,7 @@
jsPsych.init({
timeline: [trial_1, trial_2, trial_3],
use_webaudio: false,
on_finish: function() {
jsPsych.data.displayData();
},

View File

@ -17,6 +17,7 @@
jsPsych.init({
timeline: [trial_1],
use_webaudio: false,
on_finish: function() {
jsPsych.data.displayData();
},

View File

@ -77,6 +77,7 @@ window.jsPsych = (function() {
},
'preload_images': [],
'preload_audio': [],
'use_webaudio': true,
'exclusions': {},
'show_progress_bar': false,
'auto_update_progress_bar': true,
@ -138,6 +139,9 @@ window.jsPsych = (function() {
timeline: opts.timeline
});
// initialize audio context based on options and browser capabilities
jsPsych.pluginAPI.initAudio();
// below code resets event listeners that may have lingered from
// a previous incomplete experiment loaded in same DOM.
jsPsych.pluginAPI.reset(opts.display_element);
@ -2097,16 +2101,19 @@ jsPsych.pluginAPI = (function() {
}
// audio //
// temporary patch for Safari
if (typeof window !== 'undefined' && window.hasOwnProperty('webkitAudioContext') && !window.hasOwnProperty('AudioContext')) {
window.AudioContext = webkitAudioContext;
}
// end patch
var context = (typeof window !== 'undefined' && typeof window.AudioContext !== 'undefined') ? new AudioContext() : null;
var context = null;
var audio_buffers = [];
module.initAudio = function(){
// temporary patch for Safari
if (typeof window !== 'undefined' && window.hasOwnProperty('webkitAudioContext') && !window.hasOwnProperty('AudioContext')) {
window.AudioContext = webkitAudioContext;
}
// end patch
context = (typeof window !== 'undefined' && typeof window.AudioContext !== 'undefined' && jsPsych.initSettings().use_webaudio == true) ? new AudioContext() : null;
}
module.audioContext = function(){
return context;
}