Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I provide a relative path to Buffer.read?

So I want to load a sound file into a buffer using a relative path. (I keep my stuff under version control and don't want to make any assumptions about where someone might clone the repo on their file system.)

So, I initially assumed that if I provided a path that looked relative then SC would interpret it as being relative to the location of the file being executed:

Buffer.read(s, "Samples/HiHats1.hihat2.wav");

But SC couldn't find the file. So I looked up asAbsolutePath and tried that:

Buffer.read(s, "Samples/HiHats1.hihat2.wav".asAbsolutePath);

This doesn't work either because SC can't work out the absolute path from my relative one.

"Samples/HiHats1.hihat2.wav".asAbsolutePath

...actually returns a nonexistent location on my filesystem. Hurrah!

Can anyone make any suggestion as to how I might achieve this troublesome task?

like image 661
David Avatar asked Sep 21 '13 20:09

David


1 Answers

SuperCollider handles relative paths just fine - just like a lot of software, it resolves them relative to the current working directory. Usually this is relative to the executable, not the script file, hence the confusion.

What's the current working directory in your case? You can find out by running

     File.getcwd

(This is what .asAbsolutePath uses.) The cwd depends on your OS - on some platforms it's not a particularly handy default. You can't rely on it being anything in particular.

In your case, if you want the path to be resolved relative to the location of your script or class, then you need to do that yourself. For example:

     // Use the path of whatever file is currently executing:
     var samplePath = thisProcess.nowExecutingPath.dirname +/+ "Samples/HiHats1.hihat2.wav";

     // Use the path of the class file for the class you're coding
     var samplePath = this.class.filenameSymbol.asString.dirname +/+ "Samples/HiHats1.hihat2.wav";
like image 174
Dan Stowell Avatar answered Jan 01 '23 07:01

Dan Stowell