Skip to content

Instantly share code, notes, and snippets.

@eleses
Created June 17, 2020 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eleses/0720ba5af1f1361308f75f1076aba8be to your computer and use it in GitHub Desktop.
Save eleses/0720ba5af1f1361308f75f1076aba8be to your computer and use it in GitHub Desktop.
Cleanups dupe fix, beta 1
CleanupThunk : AbstractFunction {
var function, value;
*new { arg function;
^super.newCopyArgs(function)
}
value { |... args| ^this.prEvaluate(\valueArray, args) }
valueArray { |... args| ^this.prEvaluate(\valueArray, *args) }
valueEnvir { |... args| ^this.prEvaluate(\valueArrayEnvir, args) }
valueArrayEnvir { |... args| ^this.prEvaluate(\valueArrayEnvir, *args) }
prEvaluate { |selector, args|
^value ?? {
value = function.performList(selector, args);
function = nil;
value
}
}
didEvaluate { ^function.isNil }
clear { function = nil }
}
+ EventStreamCleanup {
copy {
^this.class.new.functions_(this.functions.copy)
}
addFunction { |event, function |
if(event.isKindOf(Dictionary)) {
function = CleanupThunk(function); // hjh
functions.add(function);
event[\addToCleanup] = event[\addToCleanup].add(function);
};
^function; // fix Artic
}
update { | event |
if(event.isKindOf(Dictionary)) {
functions.addAll(event[\addToNodeCleanup]);
functions.addAll(event[\addToCleanup]);
functions = functions.reject(_.didEvaluate);
};
^event
}
exit { | event, freeNodes = true |
if(event.isKindOf(Dictionary)) {
this.update(event);
if(functions.notEmpty) {
functions.do(_.value(freeNodes) );
functions = functions.reject(_.didEvaluate); // prolly a bit pointless
if(functions.size != 0) { ["EXIT", functions].postln; }
};
this.clear;
};
^event
}
}
+ PmonoStream {
prInitNode {
event.use {
if (~id.notNil) {
~type = \monoSet;
id = ~id;
} {
// If the event is a rest, no node would be created
// so there is no need to add a cleanup function
// (for a node that will never exist). If a later
// event is not a rest, ~id will be nil and the
// cleanup will be set at that time.
if(event.isRest.not) {
~type = \monoNote;
~instrument = pattern.synthName;
currentCleanupFunc = cleanup.addFunction(event, { | flag |
if (flag) { (id: id, server: server, type: \off,
hasGate: hasGate,
schedBundleArray: schedBundleArray,
schedBundle: schedBundle).play
};
currentCleanupFunc = nil;
});
};
};
// this should happen whether or not ~id is nil
~updatePmono = { | argID, argServer |
id = argID;
server = argServer;
schedBundleArray = ~schedBundleArray;
schedBundle = ~schedBundle;
};
};
}
}
+ PmonoArticStream {
embedInStream { |inevent|
var sustain, rearticulating = true, oldStyleStop = false;
inevent ?? { ^nil.yield };
this.prInit(inevent);
if(pattern.respondsTo(\oldStyleStop)) {
oldStyleStop = pattern.oldStyleStop;
};
//("oldStyleStop = " ++ oldStyleStop).postln;
loop {
if(this.prDoStreams) {
if(rearticulating and: { event.isRest.not }) {
event[\id] = nil;
this.prInitNode;
rearticulating = false;
};
sustain = event.use { ~sustain.value };
if(sustain.notNil and: { sustain < event.delta }) {
//event[\removeFromCleanup] = event[\removeFromCleanup].add(currentCleanupFunc);
var thunkMaybeCopy = currentCleanupFunc;
if(oldStyleStop) {
thunkMaybeCopy = currentCleanupFunc.copy;
currentCleanupFunc.clear; // old behavior, thunkCopy not cleared
};
thisThread.clock.sched(sustain, {
thunkMaybeCopy.value(true);
rearticulating = true;
});
};
cleanup.update(event);
inevent = event.yield;
this.prSetNextEvent(inevent);
} {
^cleanup.exit(inevent)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment