Skip to content

Instantly share code, notes, and snippets.

@atsushieno
Created March 6, 2020 08:19
Show Gist options
  • Save atsushieno/2414203413e8188ad734cd5ad72281e7 to your computer and use it in GitHub Desktop.
Save atsushieno/2414203413e8188ad734cd5ad72281e7 to your computer and use it in GitHub Desktop.
flatten-sfz.js - resolve #include paths in .sfz and make sfz files loadable on SFZ tools that do not support ARIA extensions.
const fs = require('fs');
const path = require('path');
main();
function main() {
var args = process.argv.slice(2);
var sfzDirPath = args.length == 0 ? process.cwd() : args[0];
console.log("Path: " + sfzDirPath);
var dir = fs.opendirSync(sfzDirPath);
var ent = null;
while ((ent = dir.readSync()) != null) {
console.log(ent);
if (ent.isFile && ent.name.toLowerCase().endsWith(".sfz") && !ent.name.endsWith(".flatten.sfz")) {
var fileRooted = path.join(sfzDirPath, ent.name);
var content = readSfzFlatten(fileRooted);
fs.writeFileSync(fileRooted.substring(0, fileRooted.length - 4) + ".flatten.sfz", content);
}
}
}
function readSfzFlatten(filePath)
{
var sfz = fs.readFileSync(filePath);
var result = "";
var lines = sfz.toString().split('\n');
for (var line in lines) {
var s = lines [line];
var iLen = "#include".length;
if (s.startsWith("#include")) {
if (s.length <= iLen || (s[iLen] != ' ' && s[iLen] != '\t')) {
console.log ("Illegal inclusion specifier at line " + (line + 1));
continue;
}
var includeSpec = s.substring ("#include".length + 1).trim();
if (includeSpec.length < 2 || includeSpec[0] != '"' || includeSpec[includeSpec.length - 1] != '"') {
console.log ("Illegal inclusion specifier at line " + (line + 1));
continue;
}
includeSpec = includeSpec.substring(1, includeSpec.length - 1);
console.log("Include: " + includeSpec);
var includePath = path.join(path.dirname(filePath), includeSpec);
result += readSfzFlatten(includePath);
} else
result += s;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment