Skip to content

Commit

Permalink
Audio Recording Folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Allcharles committed Feb 25, 2019
1 parent acfd702 commit e9e4628
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/html/analysisForm.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@

<div class="group" id="audio" style="display: none">
<div class="question-fail">
<p class="question-text">Select Audio Files</p>
<p class="question-text">Select Recordings Folder</p>
<a onclick="getAudio()">
<div class="question-button-fail">Select Files</div>
<div class="question-button-fail">Select Folder</div>
</a>
</div>
<div class="group-content">
Expand Down
146 changes: 118 additions & 28 deletions src/js/controller.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
const electron = require("electron");
const { app } = electron.remote;
const dialog = electron.remote.dialog;
const SUPPORTED_AUDIO_FORMATS = ["wav"];
/** All ffmpeg supported audio formats */
const SUPPORTED_AUDIO_FORMATS = [
".wav",
".mp3",
".pcm",
".aiff",
".aac",
".ogg",
".wma",
".flac",
".alac",
".mwa"
];
const IS_WINDOWS = process.platform === "win32";
const AP_LOCATION = IS_WINDOWS
? "C:/AP"
Expand Down Expand Up @@ -298,7 +310,7 @@ function getFilename(filePath) {
}

/**
* Returns the index of the last / or \ depending on the filepath
* Returns the index of the last / or \ depending on the file path
* @param {string} filePath File path
* @returns {int} Index position of last / or \
*/
Expand Down Expand Up @@ -523,55 +535,133 @@ function getAudio() {
process.dlopen = () => {
throw new Error("Load native module is not safe");
};

//Open file selector dialog
dialog.showOpenDialog(
{
properties: ["openFile", "multiSelections"],
filters: [{ name: "Audio", extensions: SUPPORTED_AUDIO_FORMATS }],
title: "Select Audio Files"
properties: ["openDirectory", "multiSelections"],
title: "Select Audio Recordings Folder"
},
function(filePaths) {
document.querySelector("#audiospinner").style.display = "none";
function(folders) {
findAudioFiles(folders, SUPPORTED_AUDIO_FORMATS);
}
);
}

if (filePaths === undefined || filePaths.length == 0) {
failure("audio");
/**
* Finds all files inside a folder recursively which match a list of extension types
* @param {[string]} extensions File extensions to find
* @returns {[string]} List of all files matching file extension
*/
function findAudioFiles(folders, extensions = [""]) {
//Parallel Recursive Search (https://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search)
var fs = require("fs");
var path = require("path");
var walk = function(dir, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var pending = list.length;
if (!pending) return done(null, results);
list.forEach(function(file) {
file = path.resolve(dir, file);

document.querySelector("#audio .group-content p").style.display =
"inherit";
document.querySelector("#audio .group-content ul").style.display =
"none";
//Determine if file is a directory
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
//Check folder recursively
walk(file, function(err, res) {
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
//Check file extension is found
extensions.some(function(extension) {
if (file.substr(file.length - extension.length) === extension) {
console.log("Extension Match: " + extension);
results.push(file);
return true;
}
});

audioFiles = [];
} else {
success("audio");
if (!--pending) done(null, results);
}
});
});
});
};

document.querySelector("#audio .group-content p").style.display =
"none";
document.querySelector("#audio .group-content ul").style.display =
"inherit";
if (folders === undefined) {
failure("audio");

audioFiles = filePaths;
document.querySelector("#audiospinner").style.display = "none";
document.querySelector("#audio .group-content p").style.display = "inherit";
document.querySelector("#audio .group-content ul").style.display = "none";
return;
}

updateAudio();
}
//TODO Parallelise this so that load times are faster for large amounts of files
var count = 0;
var maxCount = folders.length;
audioFiles = [];

updateAnalyseButton();
}
);
for (let i = 0; i < maxCount; i++) {
walk(folders[i], function(err, results) {
audioFiles = audioFiles.concat(results);
count++;

if (count == maxCount) {
document.querySelector("#audiospinner").style.display = "none";

if (audioFiles.length == 0) {
failure("audio");

document.querySelector("#audio .group-content p").style.display =
"inherit";
document.querySelector("#audio .group-content ul").style.display =
"none";
} else {
success("audio");

document.querySelector("#audio .group-content p").style.display =
"none";
document.querySelector("#audio .group-content ul").style.display =
"inherit";

updateAudio();
}

updateAnalyseButton();
}
});
}
}

/**
*
* Updates the Select Recordings Folder list of selected audio files
*/
function updateAudio() {
//Display list of files
var files = document.querySelector("#audio .group-content ul");
files.innerHTML = "";
files.innerHTML +=
"<li><b>Number of Loaded Files: " + audioFiles.length + "</b></li>";

//Storing files and then outputting to html is far faster
var temp = "";
//Add files to list
for (var file = 0; file < audioFiles.length; file++) {
files.innerHTML += '<li class="files">' + audioFiles[file] + "</li>";
temp += '<li class="files">' + audioFiles[file] + "</li>";

//Push periodically to reduce ram burden
if (file % 1000 == 0) {
files.innerHTML += temp;
temp = "";
}
}

//Final push to html
files.innerHTML += temp;
}

/**
Expand Down

1 comment on commit e9e4628

@Allcharles
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial fix to #10, however future enhancements will follow.

Please sign in to comment.