I’ve been learning SC for some days now and I’m really loving it, wicked app.
However, I am trying to figure how I can run a code block just once, on Server Boot / Reboot, so I don’t need to make this manually using cmd + enter. Any idea? I am reading documentation and watching so many tutorial as hours in a day, but I couldn’t find an example of it.
Thanks a lot in advance!
(
// How can we execute this code only once, on server boot or reboot?
Buffer.freeAll;
~audioFiles = Array.new;
~audioDir = PathName("/Users/Blablabla/Samples/");
~audioDir.entries.do({
| path |
~audioFiles = ~audioFiles.add(Buffer.read(s, path.fullPath));
});
~audioFiles.size;
)
You can use s.waitForBoot{ …place your code here …} provided your Server is named ‘s’, which it is by default. If for some reason you renamed the default server to anything other than ‘s’ you can use Server.default.waitForBoot instead.
WaitForBoot is a routine and works like this: if the server is not running it will start the server, then run the code. If the server is already running it will just run the code inside the function. If you need to wait for something to happen on the server before proceeding (like instantiating a Synth, which takes a tiny amount of time) you can insert ‘s.sync’ which tells sclang to wait for an operation on the server to complete before moving on.
Thank you, very appreciated. I’ve tried with s.waitForBoot(), but for some reason I can’t get it to work, probably I am missing something pretty basic. I’m still getting familiarized with the SC syntax.
I am trying with this simple example right now, expecting to see the “do something” string as soon as I boot the server using cmd + B, but I can’t see that in the post window. Should I ?
However if I select the code manually and then cmd + enter, that works.
Do you want this to be evaluated as soon as you open the editor and start the language? If this is the case, put this code on startup.scd, you can find it on menu File.
Thank you, this setup is per sc project or global?
Basically what I am trying to archive is as soon as I open my SC File and boot (or reboot) the server, a specific code block is executed , I am using it for some buffer variables . Otherwise if I try to play my SynthDef, buffers are not found (logically). (Except I manually trig that block before of playing the SynthDef).
It’s global. If you want this just for one project, you need to evaluate it. I’m not aware of a way to evaluate a Document when it’s opened on ScIDE. Also, there isn’t the concept of “workspace” when you open different folders. The SCIDE don’t see this.
Well you could put ServerBoot stuff in your project SC file instead but I don’t really see how that helps unless you are for some reason rebooting the server a lot.
As rule of thumb, any code not in your startup.scd file will need to be evaluated manually somehow… (although I do remember some header comment that would automatically run the entire file when it was opened I can’t find it now…?)
After recompiling sclang, evaluate the following block, or copy it to startup.scd, which can be accessed via “Open startup file” from the File menu:
(
ServerBoot.add{
Buffer.freeAll; // I am not sure if it is necessary.
~soundFolder = thisProcess.nowExecutingPath;// +/+ "BW2 Exports";
~soundFolder.postln;
// I would save the SCD file containing this code block in a parent folder of the sound file folder, but thisProcess..... does not work here. Why?
~soundFolder = Platform.resourceDir +/+ "sounds";
~soundFolder.postln;
~soundFiles = SoundFile.collect(~soundFolder +/+ "*");
~buffers = ~soundFiles.collect { |aFile|
aFile.postln;
Buffer.read(s, aFile.path)
};
s.scope;
}
)