1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
Function : AbstractFunction {
var <def, context;
// a Function is what you get when you write a FunctionDef in your code.
// it consists of the function's code and the variables in its defining context
*new { ^this.shouldNotImplement(thisMethod) }
isFunction { ^true }
isClosed { ^def.sourceCode.notNil }
archiveAsCompileString { ^true }
archiveAsObject { ^true }
checkCanArchive { if (def.sourceCode.isNil) { "cannot archive open Functions".warn } }
storeOn { arg stream;
var args;
stream << (def.sourceCode ?? {
args = def.argumentString;
"{ %\"open Function\" }".format(if(args.notNil) { "| % | ".format(args) } { "" })
})
}
shallowCopy { ^this }
choose { ^this.value }
update { |obj, what ... args| this.value(obj, what, *args) }
// evaluation
value { arg ... args;
_FunctionValue
// evaluate a function with args
^this.primitiveFailed
}
valueArray { arg ... args;
_FunctionValueArray
// evaluate a function, if the last argument is an array it will be
// expanded into separate args.
^this.primitiveFailed
}
valueEnvir { arg ... args;
_FunctionValueEnvir
// evaluate a function with args.
// unsupplied argument names are looked up in the currentEnvironment
^this.primitiveFailed
}
valueArrayEnvir { arg ... args;
_FunctionValueArrayEnvir
// evaluate a function, if the last argument is an array it will be
// expanded into separate args.
// unsupplied argument names are looked up in the currentEnvironment
^this.primitiveFailed
}
functionPerformList { arg selector, arglist;
_ObjectPerformList;
^this.primitiveFailed
}
valueWithEnvir { arg envir;
var prototypeFrame;
if(envir.isNil) { ^this.value };
prototypeFrame = def.prototypeFrame.copy;
def.argNames.do { |name,i|
var val = envir[name];
val !? { prototypeFrame[i] = val };
};
// postf("argNames: % prototypeFrame: %\n", def.argNames, prototypeFrame);
// evaluate a function, using arguments from the supplied environment
// slightly faster than valueEnvir and does not replace the currentEnvironment
^this.valueArray(prototypeFrame)
}
performWithEnvir { |selector, envir|
if(selector === \value) { ^this.valueWithEnvir(envir) };
^super.performWithEnvir(selector, envir)
}
performKeyValuePairs { |selector, pairs|
var envir;
if(selector !== \value) {
^this.superPerform(\performKeyValuePairs, pairs)
};
envir = this.def.makeEnvirFromArgs;
envir.putPairs(pairs);
^this.valueWithEnvir(envir)
}
numArgs { ^def.numArgs } // return number of arguments to the function
numVars { ^def.numVars } // return number of variables in the function
varArgs { ^def.varArgs } // return boolean whether function has ellipsis argument
loop {
// loop is supported magically by the compiler,
// thus it can be implemented in terms of itself
loop { this.value };
}
block {
^this.value {|val| ^val };
}
// block {
// var result;
// try {
// result = this.value #{|val| Break(val).throw };
// }{|error|
// if (error.class == Break) {
// ^error.value
// }{
// error.throw
// }
// }
// ^result
// }
asRoutine {
^Routine.new(this)
}
dup { arg n = 2;
^Array.fill(n, this)
}
sum { arg n = 2;
var sum = 0;
n.do {|i| sum = sum + this.value(i) };
^sum
}
defer { arg delta;
if (delta.isNil and: {this.canCallOS}) {
this.value
}{
AppClock.sched(delta ? 0, { this.value; nil })
}
}
thunk { ^Thunk(this) }
// Pattern support
transformEvent { arg event;
^this.value(event)
}
// ControlView support
set { arg ... args; ^this.valueArray(args) }
get { arg prevVal; ^prevVal }
fork { arg clock, quant, stackSize;
^Routine(this, stackSize).play(clock, quant);
}
forkIfNeeded { arg clock, quant, stackSize;
if(thisThread.isKindOf(Routine), this, { ^this.fork(clock, quant, stackSize) });
^thisThread;
}
awake { arg beats, seconds, clock;
var time = seconds; // prevent optimization
^this.value(beats, seconds, clock)
}
cmdPeriod { this.value }
bench { arg print = true;
var dt;
var t0 = Main.elapsedTime;
this.value;
dt = Main.elapsedTime - t0;
if (print) { Post << "time to run: " << dt << " seconds.\n"; }
^dt
}
protect { arg handler;
var result;
result = this.prTry;
if (result.isException) {
handler.value(result);
result.throw;
}{
handler.value; // argument should be nil if there was no exception.
^result
};
}
try { arg handler;
var result = this.prTry;
if (result.isException) { ^handler.value(result); }
{ ^result }
}
prTry {
var result, thread = thisThread;
var next = thread.exceptionHandler,
wasInProtectedFunc = Exception.inProtectedFunction;
thread.exceptionHandler = {|error|
thread.exceptionHandler = next; // pop
^error
};
Exception.inProtectedFunction = true;
result = this.value;
Exception.inProtectedFunction = wasInProtectedFunc;
thread.exceptionHandler = next; // pop
^result
}
handleError { arg error; ^this.value(error) }
case { arg ... cases;
cases = [this] ++ cases;
cases.pairsDo { | test, trueFunc |
if (test.value) { ^trueFunc.value };
};
if (cases.size.odd) { ^cases.last.value };
^nil
}
r { ^Routine(this) }
p { ^Prout(this) }
matchItem { arg item;
^this.value(item)
}
// scale suppoert
performDegreeToKey { arg scaleDegree, stepsPerOctave = 12, accidental = 0;
^this.value(scaleDegree, stepsPerOctave, accidental)
}
// multichannel expand function return values
flop {
if(def.argNames.isNil) { ^this };
^{ |... args| args.flop.collect(this.valueArray(_)) }
}
envirFlop {
var func = this.makeFlopFunc;
^{ |... args|
func.valueArrayEnvir(args).collect(this.valueArray(_))
}
}
makeFlopFunc {
if(def.argNames.isNil) { ^this };
^interpret(
"#{ arg " ++ " " ++ def.argumentString(true) ++ "; "
++ "[ " ++ def.argumentString(false) ++ " ].flop };"
)
}
// attach the function to a specific environment
inEnvir { |envir|
envir ?? { envir = currentEnvironment };
^{ |... args| envir.use({ this.valueArray(args) }) }
}
asBuffer { |duration = 0.01, target, action, fadeTime = (0)|
var buffer, def, synth, name, numChannels, rate, server;
target = target.asTarget;
server = target.server;
name = this.hash.asString;
def = SynthDef(name, { |bufnum|
var val = this.value;
if(val.isValidUGenInput.not) {
val.dump;
Error("reading signal failed: % is no valid UGen input".format(val)).throw
};
val = UGen.replaceZeroesWithSilence(val.asArray);
rate = val.rate;
if(rate == \audio) { // convert mixed rate outputs:
val = val.collect { |x| if(x.rate != \audio) { K2A.ar(x) } { x } }
};
numChannels = val.size.max(1);
if(fadeTime > 0) {
val = val * EnvGen.kr(Env.linen(fadeTime, duration - (2 * fadeTime), fadeTime))
};
RecordBuf.perform(RecordBuf.methodSelectorForRate(rate), val, bufnum, loop: 0);
Line.perform(Line.methodSelectorForRate(rate), dur: duration, doneAction: 2);
});
buffer = Buffer.new(server);
Routine.run {
var numFrames, running;
running = server.serverRunning;
if(running.not) { server.bootSync; 1.wait };
numFrames = duration * server.sampleRate;
if(rate == \control) { numFrames = numFrames / server.options.blockSize };
buffer.numFrames = numFrames.asInteger;
buffer.numChannels = numChannels;
buffer = buffer.alloc(numFrames, numChannels);
server.sync;
def.send(server);
server.sync;
synth = Synth(name, [\bufnum, buffer], target, \addAfter);
OSCFunc({
action.value(buffer);
server.sendMsg("/d_free", name);
}, '/n_end', server.addr, nil, [synth.nodeID]).oneShot;
};
^buffer
}
loadToFloatArray { |duration = 0.01, target, action|
this.asBuffer(duration, target, { |buffer|
buffer.loadToFloatArray(action: { |array|
action.value(array, buffer);
buffer.free
})
})
}
getToFloatArray { |duration = 0.01, target, action, wait = 0.01, timeout = 3|
this.asBuffer(duration, target, { |buffer|
buffer.getToFloatArray(0, wait: wait, action: { |array|
action.value(array, buffer);
buffer.free
})
})
}
}
Thunk : AbstractFunction {
// a thunk is an unevaluated value.
// it gets evaluated once and then always returns that value.
// also known as a "promise" in Scheme.
// thunks have no arguments.
var function, value;
*new { arg function;
^super.newCopyArgs(function)
}
value {
^value ?? { value = function.value; function = nil; value }
}
valueArray { ^this.value }
valueEnvir { ^this.value }
valueArrayEnvir { ^this.value }
}
UGenThunk : Thunk {
var ugens;
value {
var res;
ugens = ugens ?? { IdentityDictionary.new };
res = ugens.at(UGen.buildSynthDef);
if(res.isNil) { res = super.value; ugens.put(UGen.buildSynthDef, res) };
^res
}
}
|