所以我想使用相对路径将声音文件加载到缓冲区中.(我将我的东西保留在版本控制之下,并且不想对某人可能在其文件系统上克隆repo的位置做出任何假设.)
所以,我最初假设如果我提供了一个看起来相对的路径,那么SC会把它解释为相对于正在执行的文件的位置:
Buffer.read(s, "Samples/HiHats1.hihat2.wav");
Run Code Online (Sandbox Code Playgroud)
但SC无法找到该文件.所以我抬头asAbsolutePath试了一下:
Buffer.read(s, "Samples/HiHats1.hihat2.wav".asAbsolutePath);
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为SC无法计算出我的相对路径的绝对路径.
"Samples/HiHats1.hihat2.wav".asAbsolutePath
Run Code Online (Sandbox Code Playgroud)
...实际上在我的文件系统上返回一个不存在的位置.欢呼!
任何人都可以就如何实现这个麻烦的任务提出任何建议吗?
Dan*_*ell 11
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
Run Code Online (Sandbox Code Playgroud)
(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.
在您的情况下,如果您希望相对于脚本或类的位置解析路径,那么您需要自己执行此操作.例如:
// 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";
Run Code Online (Sandbox Code Playgroud)