I am using the music metadata-browser npm package to retrieve audio metadata from files. This library is being used in an Electron and React desktop app.
To get the metadata of audio files, (and add it to the redux state), I use the following function to get metadata and add the file to redux state. This method works well with small amounts of data, but gets really slow (obviously) as more audio files are given to be processed. Is there a better way I can process these files? Not sure if javascript has any worker/job techniques I could use.
import * as mm from 'music-metadata-browser';
const addFile = (filePath, file, dispatch, ext) => {
if (Buffer.isBuffer(file)) {
mm.parseBuffer(file, ext)
.then(metadata => {
const libraryEntry = createLibraryEntry(filePath, metadata);
dispatch({ type: constants.ADD_FILE, libraryEntry, totalFiles });
});
}
else {
mm.parseBlob(file, ext)
.then((metadata) => {
const libraryEntry = createLibraryEntry(filePath, metadata);
dispatch({ type: constants.ADD_FILE, libraryEntry, totalFiles });
});
}
};
const processFiles = (files, dirPath, dispatch) => {
files.map((file, index) => {
const parsedFile = file.split('.');
const format = parsedFile[parsedFile.length - 1];
const filePath = `${dirPath}/${file}`;
const isDirectory = fs.lstatSync(filePath).isDirectory();
if (isDirectory) {
fs.readdir(filePath, (err, files) => {processFiles(files, filePath, dispatch);});
}
else if (isValidFormat(format)) {
fs.readFile(filePath, (err, file) => {
addFile(filePath, Buffer(file), dispatch, format);
});
}
return;
});
};
Thanks for the help.