fix audio offset with negative numbers

This commit is contained in:
Ruben van de Ven 2021-12-22 12:19:00 +01:00
parent 9a8509e56d
commit 1e9bc1f471

View file

@ -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