From 1e9bc1f471582c89f4aab97596246ab584cf9982 Mon Sep 17 00:00:00 2001 From: Ruben van de Ven Date: Wed, 22 Dec 2021 12:19:00 +0100 Subject: [PATCH] fix audio offset with negative numbers --- www/annotate.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/www/annotate.js b/www/annotate.js index 19fa815..7ce4b77 100644 --- a/www/annotate.js +++ b/www/annotate.js @@ -380,7 +380,7 @@ class Annotator { start: [this.currentTime, this.duration], connect: true, range: { - 'min': 0, + 'min': this.audioOffset < 0 ? this.audioOffset * 1000 : 0, 'max': this.duration }, tooltips: [ @@ -422,7 +422,8 @@ class Annotator { if (metadata) { this.annotations = metadata.annotations; this.audioFile = metadata.hasOwnProperty('audio') ? metadata.audio.file : null; - this.audioOffset = metadata.hasOwnProperty('audio') ? metadata.audio.offset : 0; + this.audioOffset = metadata.hasOwnProperty('audio') ? Number.parseFloat(metadata.audio.offset) : 0; + this.currentTime = this.audioOffset < 0 ? this.audioOffset * 1000 : 0; // // load any saved metadata } @@ -449,11 +450,16 @@ class Annotator { this.formatter = wNumb({ decimals: 2, edit: (time) => { + let neg = ""; + if(time < 0) { + neg = "-"; + time *= -1; + } const s = Math.floor(time / 1000); const minutes = Math.floor(s / 60); const seconds = s - minutes * 60; const ms = Math.floor((time / 1000 - s) * 1000); - return `${minutes}:${seconds}:${ms}`; + return `${neg}${minutes}:${seconds}:${ms}`; } }); @@ -531,9 +537,10 @@ class Annotator { } setAudioOffset(audioOffset) { - this.audioOffset = audioOffset; + this.audioOffset = Number.parseFloat(audioOffset); // TODO update playhead // TODO update this.duration + this.setUpAnnotator(); // if offset is negative, annotator starts at negative time this.updateState(); } @@ -542,7 +549,7 @@ class Annotator { * @returns float */ getAudioTime(time) { - return Number.parseFloat(time) + (this.audioOffset * 1000 ?? 0); + return Number.parseFloat(time) - (this.audioOffset * 1000 ?? 0); } /** @@ -558,14 +565,13 @@ class Annotator { const t_start = this.getAudioTime(t_in); // in ms const t_diff = t_out - t_in; // in ms - console.log('set time', t_in, t_start, typeof t_start, typeof t_in, t_start < 0); this.audioEl.pause(); if (t_start < 0) { if (t_diff <= t_start * -1) { - console.log('no audio playback in segment', t_start, t_diff); + console.debug('no audio playback in segment', t_start, t_diff); } else { - console.log('huh?', t_start, t_diff); + console.debug('delay audio playback', t_start, t_diff); // a negative audiooffset delays playback from the start // this.audioStartTimeout = setTimeout((e) => this.audioEl.play(), t*-1000); this.audioStartTimeout = setTimeout((e) => { this.audioEl.currentTime = 0 }, t_start * -1); // triggers play with "seeked" event