New SC user, some questions

Hi guys,

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.

1 Like

Hi Thor_Madsen,

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.

s.waitForBoot({
	"do something".postln;
})

Thanks once more again.

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.

1 Like

Hi smoge,

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).

Does that makes sense?

Thank you!

(
Buffer.freeAll;
~audioFiles = Array.new;
~audioDir = PathName("/Users/TheUser/Downloads/BW2 Exports");

~audioDir.entries.do({
	| path |
	~audioFiles = ~audioFiles.add(Buffer.read(s, path.fullPath));
});
s.scope;
)

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.

1 Like

Check out ServerBoot

https://doc.sccode.org/Classes/ServerBoot.html

In your case I would open startup.scd (File > open startup file) and write

ServerBoot.add({
  // your code here
});

Then your code will run whenever you boot server e.g. with cmd- B

1 Like

That’s also global, I’m not sure is what he wants.

For a beginner, it would not be a bad idea to separate the projects and remember what you’re evaluating on each one. I think.

1 Like

Oh yeah I missed that.

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…?)

1 Like

Hey smoge, Eric.

Everything properly noted. Thank a lot for the info.

I will evaluate it manually, I don’t need to do this very often anyway, only if I change the samples directory, so it’s ok.

I will keep reading and practicing.

Thanks once more again and have a nice weekend!

I am not sure if @lABl want the followings:

  1. 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;
    }
    )
    
  2. Evaluate the following block:

    (
    ~test = {
    	s.reboot;
    	s.doWhenBooted {
    		~soundFolder.postln;
    		~soundFiles.postln;
    		~buffers.postln;
    		~buffers.do { |aBuffer| 
    			aBuffer.query;
    			aBuffer.play;
    			2.wait
    		}
    	}
    }
    )
    
  3. Evaluate the following code two or three times as a test:

    ~test.()
    
1 Like

Thank you prko! I will check this out today later.
Cheers