문제

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?

도움이 되었습니까?

해결책

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";
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top