6

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?

David
  • 15,750
  • 22
  • 90
  • 150

1 Answers1

14

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";
Dan Stowell
  • 4,618
  • 2
  • 20
  • 30
  • Once again, thanks for both the solution and the explanation. – David Sep 22 '13 at 11:23
  • One caveat to this: `thisProcess.nowExecutingPath` only works on the IDE and the `scel` environments interactively. All others (e.g. scvim) return `nil`. – ZNK Dec 30 '18 at 07:49
  • To get around the issue I mentioned above, there is the `-d` flag when calling `sclang`, which specifies the runtime directory. If you run `sclang -d $(pwd)` in the same directory as your scripts/sounds, then you'll be able to have a relatively portable project. – ZNK Dec 30 '18 at 08:09