Is there a way in a Supercollider source code file to include another source code file from disk, like the "include"-statement does in C++?
I assume there must be a way to do it, but I haven't managed to find any example by googling.
Is there a way in a Supercollider source code file to include another source code file from disk, like the "include"-statement does in C++?
I assume there must be a way to do it, but I haven't managed to find any example by googling.
Supercollider appears to be able to execute code from external files, though whether this is an include-into-the-current-namespace or something-run-in-a-new-environment I don't know:
http://doc.sccode.org/Classes/String.html#-load
A recursive grep for "loadPath" should turn up examples:
// ===================================================================== // FILE: utilityFunctions.scd // =====================================================================
/*This file contains some utility functions.
You can 'import' these functions using executeFile.
NONE of the vars in the 'imported' file end up
in the scope of the file doing the 'importing'
'import' this with something like:
var utils = this.executeFile(path-to-this-file);
If BOTH FILES are in the same directory you can do this:
var utils = this.executeFile(Document.current.dir ++ "/utilityFunctions.scd");
Then you can refer to the functions like this:
utils[\fmtInt].(1234567); // this returns "1,234,567"
You could also do this:
var fmtInt = utils[\fmtInt];
fmtInit.(7654321); // this returns "7,654,321"
*/
var fmtInt = { arg num, sep=",", groupSize=3;
var str = num.asInteger.asString;
var len = str.size;
var numSeps = ((len-1)/groupSize).floor.asInteger;
var firstSep = len - (numSeps*groupSize);
var stop = len - groupSize;
var out=str;
firstSep.forBy(stop, groupSize,
{|pos, itr|
var ins = pos + itr;
out = out.insert(ins, sep);
});
out; // return result
};
var readStr0 = {arg file, max=20;
var zeroByte = 0.asAscii; // zero byte
var str = "";
block {|break|
max.do {|i|
var char = file.getChar;
if (char == zeroByte) { break.value(999); };
str = str++char;
};
};
str; // return result
};
var functions = Dictionary.new(2);
functions.add(\fmtInt -> fmtInt);
functions.add(\readStr0 -> readStr0);
functions; // return the dictionary of functions
// ===================================================================== // END OF FILE: utilityFunctions.scd // =====================================================================
// ===================================================================== // FILE: testUtilityFunctions.scd // =====================================================================
/*TestUtilityFunctions*/
(
// this assumes the test file is in the same dir as the code file
var dir = Document.current.dir;
var name = "/utilityFunctions.scd";
var path = dir ++ name;
var utils = this.executeFile(path);
var f1 = utils[\fmtInt];
var f2 = utils[\readStr0];
var d1 = f1.def;
var a1 = d1.argNames;
postf("\nTesting the (import of) the functions in file: %\n", name);
postf("utils is: %\n", utils);
postf("f1 should be a Function, it is: %\n", f1);
postf("f2 should be a Function, it is: %\n", f2);
postf("d1 should be a FunctionDef, it is: %\n", d1);
postf("a1 should be the args of f1, it is: %\n", a1);
postln("Check that names did not bleed in from imported file:");
postf("~functions should be nil, it is: %\n", ~functions);
postf("~fmtInt should be nil, it is: %\n", ~fmtInt);
postln("try one of the functions:");
postf("f1.(12345) is: %\n", f1.(12345));
postf("utils[\fmtInt].(5678) is: %\n", utils[\fmtInt].(5678));
postln("'functions.postln;' should return: ERROR: Variable 'functions' not defined.");
/functions.postln;/
"\n";
)
// ======================================================================== // END OF FILE: testUtilityFunctions.scd // ========================================================================