Skip to content

Commit

Permalink
Clamp position for seekTo between 0 & 1
Browse files Browse the repository at this point in the history
Error message for getDuration
limit saved check to 25_000 to prevent too many variables error
  • Loading branch information
Mattk70 committed Apr 18, 2024
1 parent 4fc730a commit 808b166
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
35 changes: 19 additions & 16 deletions js/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ function zoomSpec(direction) {
}
}
// Keep playhead at same time in file
position = (timeNow - bufferBegin) / windowLength;
position = clamp((timeNow - bufferBegin) / windowLength, 0, 1);
// adjust region start time to new window start time
let region = getRegion();
if (region) {
Expand Down Expand Up @@ -623,7 +623,7 @@ function customAnalysisAllMenu(saved){
<span class="shortcut float-end">Ctrl+A</span>`;
enableMenuItem(['reanalyseAll']);
} else {
analyseAllMenu.innerHTML = `<span class="material-symbols-outlined">upload_file</span> Analyse All Open Files
analyseAllMenu.innerHTML = `<span class="material-symbols-outlined">search</span> Analyse All Open Files
<span class="shortcut float-end">Ctrl+A</span>`;
disableMenuItem(['reanalyseAll']);
}
Expand Down Expand Up @@ -830,7 +830,7 @@ async function onOpenFiles(args) {
resetDiagnostics();
// Store the file list and Load First audio file
fileList = args.filePaths;
fileList.length > 1 && worker.postMessage({action: 'check-all-files-saved', files: fileList});
fileList.length > 1 && fileList.length < 25_000 && worker.postMessage({action: 'check-all-files-saved', files: fileList});
STATE.openFiles = args.filePaths;
// Sort file by time created (the oldest first):
if (fileList.length > 1) {
Expand Down Expand Up @@ -1186,6 +1186,9 @@ async function resultClick(e) {
}
}

function clamp(value, min, max){
return Math.min(Math.max(value, min), max);
}

const loadResultRegion = ({ file = '', start = 0, end = 3, label = '' } = {}) => {
start = parseFloat(start);
Expand All @@ -1194,7 +1197,7 @@ const loadResultRegion = ({ file = '', start = 0, end = 3, label = '' } = {}) =>
if (windowLength <= 3.5) windowLength = 6;
bufferBegin = Math.max(0, start - (windowLength / 2) + 1.5)
const region = { start: Math.max(start - bufferBegin, 0), end: end - bufferBegin, label: label };
const position = wavesurfer.getCurrentTime() / windowLength;
const position = clamp(wavesurfer.getCurrentTime() / windowLength, 0, 1);
postBufferUpdate({ file: file, begin: bufferBegin, position: position, region: region })
}

Expand Down Expand Up @@ -2410,7 +2413,7 @@ function onChartData(args) {
setTimelinePreferences();
if (fileLoaded) {
// Reload wavesurfer with the new timeline
const position = wavesurfer.getCurrentTime() / windowLength;
const position = clamp(wavesurfer.getCurrentTime() / windowLength, 0, 1);
postBufferUpdate({ begin: bufferBegin, position: position })
}
updatePrefs('config.json', config)
Expand Down Expand Up @@ -2516,7 +2519,7 @@ function onChartData(args) {
},
PageUp: function () {
if (currentBuffer) {
const position = wavesurfer.getCurrentTime() / windowLength;
const position = clamp(wavesurfer.getCurrentTime() / windowLength, 0, 1);
bufferBegin = Math.max(0, bufferBegin - windowLength);
postBufferUpdate({ begin: bufferBegin, position: position })
}
Expand All @@ -2532,7 +2535,7 @@ function onChartData(args) {
},
PageDown: function () {
if (currentBuffer) {
const position = wavesurfer.getCurrentTime() / windowLength;
const position = clamp(wavesurfer.getCurrentTime() / windowLength, 0, 1);
bufferBegin = Math.min(bufferBegin + windowLength, currentFileDuration - windowLength);
postBufferUpdate({ begin: bufferBegin, position: position })
}
Expand All @@ -2550,7 +2553,7 @@ function onChartData(args) {
const skip = windowLength / 100;
if (currentBuffer) {
wavesurfer.skipBackward(skip);
const position = wavesurfer.getCurrentTime() / windowLength;
const position = clamp(wavesurfer.getCurrentTime() / windowLength, 0, 1);
if (wavesurfer.getCurrentTime() < skip && bufferBegin > 0) {
bufferBegin -= skip;
postBufferUpdate({ begin: bufferBegin, position: position })
Expand All @@ -2561,7 +2564,7 @@ function onChartData(args) {
const skip = windowLength / 100;
if (wavesurfer) {
wavesurfer.skipForward(skip);
const position = Math.max(wavesurfer.getCurrentTime() / windowLength, 1);
const position = clamp(wavesurfer.getCurrentTime() / windowLength, 0, 1);
if (wavesurfer.getCurrentTime() > windowLength - skip) {
bufferBegin = Math.min(currentFileDuration - windowLength, bufferBegin += skip)
postBufferUpdate({ begin: bufferBegin, position: position })
Expand All @@ -2572,7 +2575,7 @@ function onChartData(args) {
if (e.shiftKey) {
if (wavesurfer.spectrogram.fftSamples > 64) {
wavesurfer.spectrogram.fftSamples /= 2;
const position = wavesurfer.getCurrentTime() / windowLength;
const position = clamp(wavesurfer.getCurrentTime() / windowLength, 0, 1);
postBufferUpdate({ begin: bufferBegin, position: position, region: getRegion(), goToRegion: false })
console.log(wavesurfer.spectrogram.fftSamples);
}
Expand All @@ -2584,7 +2587,7 @@ function onChartData(args) {
if (e.shiftKey) {
if (wavesurfer.spectrogram.fftSamples > 64) {
wavesurfer.spectrogram.fftSamples /= 2;
const position = wavesurfer.getCurrentTime() / windowLength;
const position = clamp(wavesurfer.getCurrentTime() / windowLength, 0, 1);
postBufferUpdate({ begin: bufferBegin, position: position, region: getRegion(), goToRegion: false })
console.log(wavesurfer.spectrogram.fftSamples);
}
Expand All @@ -2596,7 +2599,7 @@ function onChartData(args) {
if (e.shiftKey) {
if (wavesurfer.spectrogram.fftSamples <= 2048) {
wavesurfer.spectrogram.fftSamples *= 2;
const position = wavesurfer.getCurrentTime() / windowLength;
const position = clamp(wavesurfer.getCurrentTime() / windowLength, 0, 1);
postBufferUpdate({ begin: bufferBegin, position: position, region: getRegion(), goToRegion: false })
console.log(wavesurfer.spectrogram.fftSamples);
}
Expand All @@ -2608,7 +2611,7 @@ function onChartData(args) {
if (e.shiftKey) {
if (wavesurfer.spectrogram.fftSamples <= 2048) {
wavesurfer.spectrogram.fftSamples *= 2;
const position = wavesurfer.getCurrentTime() / windowLength;
const position = clamp(wavesurfer.getCurrentTime() / windowLength, 0, 1);
postBufferUpdate({ begin: bufferBegin, position: position, region: getRegion(), goToRegion: false })
console.log(wavesurfer.spectrogram.fftSamples);
}
Expand Down Expand Up @@ -4014,7 +4017,7 @@ function onChartData(args) {
// High pass threshold
const showFilterEffect = () => {
if (fileLoaded) {
const position = wavesurfer.getCurrentTime() / windowLength;
const position = clamp(wavesurfer.getCurrentTime() / windowLength, 0, 1);
postBufferUpdate({ begin: bufferBegin, position: position, region: getRegion() })
}
}
Expand Down Expand Up @@ -4369,15 +4372,15 @@ DOM.gain.addEventListener('input', () => {
DOM.gainAdjustment.textContent = element.value + 'dB'; //.toString();
config.audio.gain = element.value;
worker.postMessage({action:'update-state', audio: config.audio})
const position = wavesurfer.getCurrentTime() / windowLength;
const position = clamp(wavesurfer.getCurrentTime() / windowLength, 0, 1);
fileLoaded &&
postBufferUpdate({ begin: bufferBegin, position: position, region: getRegion(), goToRegion: false })
break;
}
case 'normalise': {
config.audio.normalise = element.checked;
worker.postMessage({action:'update-state', audio: config.audio})
const position = wavesurfer.getCurrentTime() / windowLength;
const position = clamp(wavesurfer.getCurrentTime() / windowLength, 0, 1);
fileLoaded &&
postBufferUpdate({ begin: bufferBegin, position: position, region: getRegion(), goToRegion: false })
break;
Expand Down
6 changes: 5 additions & 1 deletion js/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ function onAbort({

const getDuration = async (src) => {
let audio;
return new Promise(function (resolve) {
return new Promise(function (resolve, reject) {
audio = new Audio();
audio.src = src.replace(/#/g, '%23'); // allow hash in the path (https://github.com/Mattk70/Chirpity-Electron/issues/98)
audio.addEventListener("loadedmetadata", function () {
Expand All @@ -939,6 +939,10 @@ const getDuration = async (src) => {

resolve(duration);
});
audio.addEventListener('error', (error) => {
UI.postMessage({event: 'generate-alert', message: 'Unable to decode file metatada'})
reject(error)
})
});
}

Expand Down

0 comments on commit 808b166

Please sign in to comment.