Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Evert-R committed Oct 24, 2023
1 parent f9e9472 commit 5e27c56
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 16 deletions.
2 changes: 1 addition & 1 deletion backend/experiment/actions/playback.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Playback(BaseAction):
- play_config: define to override the following values:
- play_method:
- 'BUFFER': Use webaudio buffers. (recommended for stimuli up to 45s)
- 'HTML': Use the HTML tag. (recommended for stimuli longer 45s)
- 'HTML': Use the HTML tag. (recommended for stimuli longer than 45s)
- 'EXTERNAL': Use for externally hosted audio files. Web-audio api will be disabled
- ready_time: time before presentation of sound
- timeout_after_playback: pause in ms after playback has finished
Expand Down
4 changes: 4 additions & 0 deletions backend/experiment/actions/song_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def __init__(self, section, key, result_id, title=None, config=None, instruction
- section: section to be played during the round
- title: title of the page
- config: optional settings to override the default config
- play_method:
- 'BUFFER': Use webaudio buffers. (recommended for stimuli up to 45s)
- 'HTML': Use the HTML tag. (recommended for stimuli longer than 45s)
- 'EXTERNAL': Use for externally hosted audio files. Web-audio api will be disabled
- instructions: optional instructions to override the default instructions
- buttons: optional button labels to override the default labels
'''
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/components/Preload/Preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ const Preload = ({ instruction, pageTitle, duration, sections, playConfig, onNex
};

// Audio preloader
useEffect(() => {
console.log('preload');
useEffect(() => {
if (playConfig.play_method === 'BUFFER') {

// Use Web-audio and preload sections in buffers
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/util/audioControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ export const playAudio = (playConfig, section) => {
let latency = 0;

if (playConfig.play_method === 'BUFFER') {
console.log(`Play buffer ${section.id} (local)`);

// Determine latency for current audio device
latency = webAudio.getTotalLatency()
// Play audio
webAudio.playBufferFrom(section.id, Math.max(0, playConfig.playhead || 0));

return latency
} else {
console.log('Play HTML audo')

// Only initialize webaudio if section is hosted local
if (playConfig.play_method !== 'EXTERNAL') {
Expand Down
16 changes: 5 additions & 11 deletions frontend/src/util/webAudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@ export const initWebAudioListener = () => {
};

// init HTML audio element in webaudio context and connect track to destination (output)
export const initWebAudio = () => {
console.log('Init HTML webaudio')
export const initWebAudio = () => {
if (track === undefined) {
track = audioContext.createMediaElementSource(window.audio);
track.connect(audioContext.destination);
}
}

// Change HTML audio element crossorigin attribute for playing external files
export const closeWebAudio = () => {
console.log('close webaudio for html - external audio')
export const closeWebAudio = () => {
window.audio.removeAttribute('crossOrigin');
window.audio.crossorigin = "use-credentials";
}
Expand All @@ -41,19 +39,16 @@ export const closeWebAudio = () => {
export const getTotalLatency = () => {
let baseLatency = audioContext.baseLatency;
let outputLatency = audioContext.outputLatency;
console.log(`Baselatency : ${baseLatency} Type: ${typeof baseLatency}`);
console.log(`Outputlatency : ${outputLatency} Type: ${typeof outputLatency}`);

// Check if the response is a valid number
if (isNaN(baseLatency)) {
baseLatency = 0;
}
if (isNaN(outputLatency)) {
outputLatency = 0
outputLatency = 0;
}

let totalLatency = (baseLatency + outputLatency) * 1000;
console.log(`Compensate for total Latency of: ${totalLatency}ms`);
let totalLatency = (baseLatency + outputLatency) * 1000;
return totalLatency;
}

Expand Down Expand Up @@ -83,8 +78,7 @@ export const loadBuffer = async (id, src, canPlay) => {
.then(buffer => audioContext.decodeAudioData(buffer))
// store buffer in buffers object
.then(decodedData => {
buffers[id] = decodedData;
console.log(buffers);
buffers[id] = decodedData;
canPlay();
});
}
Expand Down

0 comments on commit 5e27c56

Please sign in to comment.