Skip to content

Commit

Permalink
Clear the cache on Model change to prevent issues with cached file sa…
Browse files Browse the repository at this point in the history
…mple rates
  • Loading branch information
Mattk70 committed Jan 27, 2024
1 parent f52f22e commit 7b83a1d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
13 changes: 7 additions & 6 deletions js/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -1476,7 +1476,7 @@ window.onload = async () => {
speciesThreshold: config.speciesThreshold
});
loadModel();
worker.postMessage({ action: 'clear-cache' })
//worker.postMessage({ action: 'clear-cache' })
// New users - show the tour
if (!config.seenTour) {
setTimeout(prepTour, 2000)
Expand Down Expand Up @@ -2191,7 +2191,7 @@ speciesThreshold.addEventListener('change', () =>{
worker.postMessage({ action: 'update-list', list: config.list })
})

const loadModel = () => {
const loadModel = ({clearCache = true} = {}) => {
t0_warmup = Date.now();
worker.postMessage({
action: 'load-model',
Expand All @@ -2200,7 +2200,8 @@ const loadModel = () => {
batchSize: config[config.backend].batchSize,
warmup: config.warmup,
threads: config[config.backend].threads,
backend: config.backend
backend: config.backend,
clearCache: clearCache
});
}

Expand Down Expand Up @@ -2253,7 +2254,7 @@ const handleBackendChange = (e) => {
updatePrefs();
// restart wavesurfer regions to set new maxLength
initRegion();
loadModel();
loadModel({clearCache: false});
}

const backend = document.getElementsByName('backend');
Expand Down Expand Up @@ -3472,7 +3473,7 @@ batchSizeSlider.addEventListener('input', (e) => {
})
batchSizeSlider.addEventListener('change', (e) => {
config[config.backend].batchSize = BATCH_SIZE_LIST[e.target.value];
loadModel();
loadModel({clearCache: false});
updatePrefs();
// Reset region maxLength
initRegion();
Expand Down Expand Up @@ -3960,7 +3961,7 @@ ThreadSlider.addEventListener('input', () => {
});
ThreadSlider.addEventListener('change', () => {
config[config.backend].threads = ThreadSlider.valueAsNumber;
loadModel();
loadModel({clearCache: false});
updatePrefs();
});

Expand Down
15 changes: 10 additions & 5 deletions js/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ const clearCache = async (fileCache, sizeLimitInGB) => {
const requiredSpace = sizeLimitInGB * 1024 ** 3;
// If Full, clear at least 25% of cache, so we're not doing this too frequently
if (size > requiredSpace) {
// while (size > requiredSpace && canBeRemovedFromCache.length) {
while (canBeRemovedFromCache.length > 1) {
const file = canBeRemovedFromCache.shift();
const proxy = metadata[file].proxy;
Expand Down Expand Up @@ -251,9 +250,10 @@ break;
case "chart": {await onChartRequest(args);
break;
}
case "clear-cache": {CACHE_LOCATION = p.join(TEMP, "chirpity");
fs.existsSync(CACHE_LOCATION) || fs.mkdirSync(CACHE_LOCATION);
await clearCache(CACHE_LOCATION, 0);
case "clear-cache": {
CACHE_LOCATION = p.join(TEMP, "chirpity");
fs.existsSync(CACHE_LOCATION) || fs.mkdirSync(CACHE_LOCATION);
await clearCache(CACHE_LOCATION, 0);
break;
}
case "convert-dataset": {convertSpecsFromExistingSpecs();
Expand Down Expand Up @@ -314,7 +314,12 @@ break;
event: "spawning"
});
sampleRate = args.model === "v2.4" ? 48_000 : 24_000;

// Since models have different sample rates, we need to clear the cache of
// files that have been resampled for a different model, and reset metadata
CACHE_LOCATION = p.join(TEMP, "chirpity");
DEBUG && console.log('clear cache', args.clearCache, 'location', CACHE_LOCATION)
args.clearCache && ipcRenderer.invoke('clear-cache', CACHE_LOCATION)
metadata = {}
BATCH_SIZE = parseInt(args.batchSize);
setAudioContext(sampleRate);
memoryDB = undefined;
Expand Down
6 changes: 6 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,9 @@ ipcMain.handle('saveFile', (event, arg) => {
});
mainWindow.webContents.send('saveFile', { message: 'file saved!' });
});

ipcMain.handle('clear-cache', (event, filePath) => {
// console.log("cache location:", event, filePath);
// return
clearCache(filePath)
})
2 changes: 1 addition & 1 deletion preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ contextBridge.exposeInMainWorld('electron', {
getPath: () => ipcRenderer.invoke('getPath'),
getTemp: () => ipcRenderer.invoke('getTemp'),
getVersion: () => ipcRenderer.invoke('getVersion'),
getAudio: () => ipcRenderer.invoke('getAudio'),
getAudio: () => ipcRenderer.invoke('getAudio')
});

contextBridge.exposeInMainWorld('module', {
Expand Down

0 comments on commit 7b83a1d

Please sign in to comment.