Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

making Synthdef work in many threads #6073

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 15 additions & 15 deletions SCClassLibrary/Common/Audio/BasicOpsUGen.sc
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ BinaryOpUGen : BasicOpUGen {

if (b.isKindOf(UnaryOpUGen) and: { b.operator == 'neg' and: { b.descendants.size == 1 } }) {
// a + b.neg -> a - b
buildSynthDef.removeUGen(b);
UGen.buildSynthDef.removeUGen(b);
replacement = a - b.inputs[0];
// this is the first time the dependants logic appears. It's repeated below.
// We will remove 'this' from the synthdef, and replace it with 'replacement'.
Expand All @@ -186,7 +186,7 @@ BinaryOpUGen : BasicOpUGen {

if (a.isKindOf(UnaryOpUGen) and: { a.operator == 'neg' and: { a.descendants.size == 1 } }) {
// a.neg + b -> b - a
buildSynthDef.removeUGen(a);
UGen.buildSynthDef.removeUGen(a);
replacement = b - a.inputs[0];
replacement.descendants = descendants;
this.optimizeUpdateDescendants(replacement, a);
Expand All @@ -204,14 +204,14 @@ BinaryOpUGen : BasicOpUGen {
and: { a.descendants.size == 1 }})
{
if (MulAdd.canBeMulAdd(a.inputs[0], a.inputs[1], b)) {
buildSynthDef.removeUGen(a);
UGen.buildSynthDef.removeUGen(a);
replacement = MulAdd.new(a.inputs[0], a.inputs[1], b).descendants_(descendants);
this.optimizeUpdateDescendants(replacement, a);
^replacement
};

if (MulAdd.canBeMulAdd(a.inputs[1], a.inputs[0], b)) {
buildSynthDef.removeUGen(a);
UGen.buildSynthDef.removeUGen(a);
replacement = MulAdd.new(a.inputs[1], a.inputs[0], b).descendants_(descendants);
this.optimizeUpdateDescendants(replacement, a);
^replacement
Expand All @@ -222,14 +222,14 @@ BinaryOpUGen : BasicOpUGen {
and: { b.descendants.size == 1 }})
{
if (MulAdd.canBeMulAdd(b.inputs[0], b.inputs[1], a)) {
buildSynthDef.removeUGen(b);
UGen.buildSynthDef.removeUGen(b);
replacement = MulAdd.new(b.inputs[0], b.inputs[1], a).descendants_(descendants);
this.optimizeUpdateDescendants(replacement, b);
^replacement
};

if (MulAdd.canBeMulAdd(b.inputs[1], b.inputs[0], a)) {
buildSynthDef.removeUGen(b);
UGen.buildSynthDef.removeUGen(b);
replacement = MulAdd.new(b.inputs[1], b.inputs[0], a).descendants_(descendants);
this.optimizeUpdateDescendants(replacement, b);
^replacement
Expand All @@ -245,7 +245,7 @@ BinaryOpUGen : BasicOpUGen {

if (a.isKindOf(BinaryOpUGen) and: { a.operator == '+'
and: { a.descendants.size == 1 }}) {
buildSynthDef.removeUGen(a);
UGen.buildSynthDef.removeUGen(a);
if(a === b) {
replacement = Sum4(a.inputs[0], a.inputs[0], a.inputs[1], a.inputs[1])
.descendants_(descendants);
Expand All @@ -258,7 +258,7 @@ BinaryOpUGen : BasicOpUGen {

if (b.isKindOf(BinaryOpUGen) and: { b.operator == '+'
and: { b.descendants.size == 1 }}) {
buildSynthDef.removeUGen(b);
UGen.buildSynthDef.removeUGen(b);
// we do not need the a === b check here
// if a === b, then the 'a' branch above must have handled it
replacement = Sum3(b.inputs[0], b.inputs[1], a).descendants_(descendants);
Expand All @@ -275,14 +275,14 @@ BinaryOpUGen : BasicOpUGen {
if(a.rate == \demand or: { b.rate == \demand }) { ^nil };

if (a.isKindOf(Sum3) and: { a.descendants.size == 1 }) {
buildSynthDef.removeUGen(a);
UGen.buildSynthDef.removeUGen(a);
replacement = Sum4(a.inputs[0], a.inputs[1], a.inputs[2], b).descendants_(descendants);
this.optimizeUpdateDescendants(replacement, a);
^replacement;
};

if (b.isKindOf(Sum3) and: { b.descendants.size == 1 }) {
buildSynthDef.removeUGen(b);
UGen.buildSynthDef.removeUGen(b);
replacement = Sum4(b.inputs[0], b.inputs[1], b.inputs[2], a).descendants_(descendants);
this.optimizeUpdateDescendants(replacement, b);
^replacement;
Expand All @@ -296,7 +296,7 @@ BinaryOpUGen : BasicOpUGen {

if (b.isKindOf(UnaryOpUGen) and: { b.operator == 'neg' and: { b.descendants.size == 1 } }) {
// a - b.neg -> a + b
buildSynthDef.removeUGen(b);
UGen.buildSynthDef.removeUGen(b);

replacement = BinaryOpUGen('+', a, b.inputs[0]);
replacement.descendants = descendants;
Expand Down Expand Up @@ -352,13 +352,13 @@ BinaryOpUGen : BasicOpUGen {
#aa, bb = a.inputs;
if (b.isKindOf(SimpleNumber)) {
if (aa.isKindOf(SimpleNumber)) {
buildSynthDef.removeUGen(a);
UGen.buildSynthDef.removeUGen(a);
this.inputs[0] = aa.perform(operator, b);
this.inputs[1] = bb;
^this
};
if (bb.isKindOf(SimpleNumber)) {
buildSynthDef.removeUGen(a);
UGen.buildSynthDef.removeUGen(a);
this.inputs[0] = bb.perform(operator, b);
this.inputs[1] = aa;
^this
Expand All @@ -379,13 +379,13 @@ BinaryOpUGen : BasicOpUGen {
#cc, dd = b.inputs;
if (a.isKindOf(SimpleNumber)) {
if (cc.isKindOf(SimpleNumber)) {
buildSynthDef.removeUGen(b);
UGen.buildSynthDef.removeUGen(b);
this.inputs[0] = a.perform(operator, cc);
this.inputs[1] = dd;
^this
};
if (dd.isKindOf(SimpleNumber)) {
buildSynthDef.removeUGen(b);
UGen.buildSynthDef.removeUGen(b);
this.inputs[0] = a.perform(operator, dd);
this.inputs[1] = cc;
^this
Expand Down
2 changes: 1 addition & 1 deletion SCClassLibrary/Common/Audio/FFT.sc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

WidthFirstUGen : UGen {
addToSynth {
synthDef = buildSynthDef;
synthDef = UGen.buildSynthDef;
if (synthDef.notNil, {
synthDef.addUGen(this);
synthDef.widthFirstUGens = synthDef.widthFirstUGens.add(this);
Expand Down
10 changes: 5 additions & 5 deletions SCClassLibrary/Common/Audio/FFTUnpacking.sc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ PV_ChainUGen : WidthFirstUGen {
addCopiesIfNeeded {
var directDescendants, frames, buf, copy;
// find UGens that have me as an input
directDescendants = buildSynthDef.children.select ({ |child|
directDescendants = UGen.buildSynthDef.children.select ({ |child|
var inputs;
child.isKindOf(PV_Copy).not and: { child.isKindOf(WidthFirstUGen) } and: {
inputs = child.inputs;
Expand All @@ -100,10 +100,10 @@ PV_ChainUGen : WidthFirstUGen {
copy = PV_Copy(this, buf);
copy.widthFirstAntecedents = widthFirstAntecedents ++ [buf];
desc.inputs[j] = copy;
buildSynthDef.children = buildSynthDef.children.drop(-3).insert(this.synthIndex + 1, frames);
buildSynthDef.children = buildSynthDef.children.insert(this.synthIndex + 2, buf);
buildSynthDef.children = buildSynthDef.children.insert(this.synthIndex + 3, copy);
buildSynthDef.indexUGens;
UGen.buildSynthDef.children = UGen.buildSynthDef.children.drop(-3).insert(this.synthIndex + 1, frames);
UGen.buildSynthDef.children = UGen.buildSynthDef.children.insert(this.synthIndex + 2, buf);
UGen.buildSynthDef.children = UGen.buildSynthDef.children.insert(this.synthIndex + 3, copy);
UGen.buildSynthDef.indexUGens;
});
});
});
Expand Down
31 changes: 21 additions & 10 deletions SCClassLibrary/Common/Audio/SynthDef.sc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

SynthDef {
classvar threadSingletonManager;
var <>name, <>func;

var <>controls, <>controlNames; // ugens add to this
Expand All @@ -21,21 +23,28 @@ SynthDef {
classvar <synthDefDir;
classvar <>warnAboutLargeSynthDefs = false;

*getActiveSingleton { ^threadSingletonManager.getActive }
*impl_getSingletonManager {^threadSingletonManager;}

*synthDefDir_ { arg dir;
if (dir.last.isPathSeparator.not )
{ dir = dir ++ thisProcess.platform.pathSeparator };
synthDefDir = dir;
}

*initClass {
threadSingletonManager = ThreadSingletonManager(SynthDef);
synthDefDir = Platform.userAppSupportDir ++ "/synthdefs/";
// Ensure exists:
synthDefDir.mkdir;
}

*new { arg name, ugenGraphFunc, rates, prependArgs, variants, metadata;
^super.newCopyArgs(name.asSymbol).variants_(variants).metadata_(metadata ?? {()}).children_(Array.new(64))
.build(ugenGraphFunc, rates, prependArgs)
^super.newCopyArgs(name.asSymbol)
.variants_(variants)
.metadata_(metadata ?? {()})
.children_(Array.new(64))
.build(ugenGraphFunc, rates, prependArgs)
}

storeArgs { ^[name, func] }
Expand All @@ -48,16 +57,19 @@ SynthDef {
func = ugenGraphFunc;
this.class.changed(\synthDefReady, this);
} {
UGen.buildSynthDef = nil;
threadSingletonManager.removeActive(this);
}
}

*wrap { arg func, rates, prependArgs;
if (UGen.buildSynthDef.isNil) {
"SynthDef.wrap should be called inside a SynthDef ugenGraphFunc.\n".error;
^0
};
^UGen.buildSynthDef.buildUgenGraph(func, rates, prependArgs);
try {
^threadSingletonManager.getActive.buildUgenGraph(func, rates, prependArgs)
} { |ex|
if(ex.isKindOf(ErrorThreadSingletonInstance)) {
"SynthDef.wrap should be called inside a SynthDef ugenGraphFunc.\n".error;
^0
} { ex.throw }
}
}

//only write if no file exists
Expand All @@ -69,7 +81,7 @@ SynthDef {
}

initBuild {
UGen.buildSynthDef = this;
threadSingletonManager.setActive(this);
constants = Dictionary.new;
constantSet = Set.new;
controls = nil;
Expand Down Expand Up @@ -281,7 +293,6 @@ SynthDef {
// re-sort graph. reindex.
this.topologicalSort;
this.indexUGens;
UGen.buildSynthDef = nil;
}

addCopiesIfNeeded {
Expand Down