Skip to content

Instantly share code, notes, and snippets.

@scztt
Last active April 25, 2025 02:30
Show Gist options
  • Save scztt/b8a97ee5399a40f1f35a7dfbcaad4130 to your computer and use it in GitHub Desktop.
Save scztt/b8a97ee5399a40f1f35a7dfbcaad4130 to your computer and use it in GitHub Desktop.
+Event {
*composeEventType {
|type, func, parentEvent, composeType|
var composeEvent = partialEvents.playerEvent.parentTypes[composeType] ?? {()};
if (parentEvent.notNil) {
parentEvent = parentEvent.collect {
|value, key|
if (value.isKindOf(Function)) {
value.value(_, composeEvent[key]);
} {
value
}
};
} {
parentEvent = parentEvent.copy;
};
Event.addEventType(
type,
{
|server|
var eventTypes = ~eventTypes;
var parentPlayFunc = eventTypes[composeType] ?? { eventTypes[\note] };
func
!? {
func.value(server, {
|...args|
parentPlayFunc.value(server, *args)
})
}
?? {
|...args|
parentPlayFunc.value(server, *args)
};
},
parentEvent
)
}
}
Pmonophonic : Pattern {
*initClass {
Class.initClassTree(Event);
Event.composeEventType(
\monoUpdate,
composeType: \set,
parentEvent: (updateGroup:false),
func: {
|server, set|
var bundle;
if (~updateGroup.value) {
bundle = [
62 /* \n_order */,
Node.actionNumberFor(~addAction),
~group.value,
] ++ ~id;
bundle = bundle.asOSCArgArray;
~schedBundleArray.(~lag, ~timingOffset, server, bundle, ~latency);
};
set.();
}
)
}
embedInStream {
|inEvent|
var id, offEvent;
var event;
var cleanup = EventStreamCleanup(),
cleanupFunc;
var group, addAction;
var synthDefHash, newSynthDefHash, monoId, newMonoId;
cleanupFunc = CallOnce {
"mono cleanup".postln;
offEvent.do(_.play)
};
while {inEvent.notNil} {
offEvent !? { offEvent.do(_.play); offEvent = nil };
event = inEvent.copy;
cleanup.update(event);
cleanup.addFunction(event, cleanupFunc);
event.putAll((
gate: 1,
sendGate: false,
callback: event[\callback].addFunc({
id = ~id.debug("starting mono");
group = ~group.value;
addAction = ~addAction.value;
offEvent = offEvent.add((
type: ~offEventType ?? { \off },
id: ~id,
server: ~server,
hasGate: ~hasGate,
schedBundleArray: ~schedBundleArray,
schedBundle: ~schedBundle,
callback: { "mono off: %".format(~id).postln; },
));
})
));
synthDefHash = event[\instrument].asSynthDesc.identityHash;
monoId = event.use{ ~monoId.value };
while {
inEvent = event.yield;
inEvent.notNil
and: {
newSynthDefHash = event[\instrument].asSynthDesc.identityHash;
newSynthDefHash == synthDefHash
}
and: {
newMonoId = event.use{ ~monoId.value };
newMonoId == monoId;
}
and: {
event.use { ~restart.value.asBoolean.not }
}
} {
synthDefHash = newSynthDefHash;
monoId = newMonoId;
event = inEvent.copy;
cleanup.update(event);
cleanup.addFunction(event, cleanupFunc);
event.putAll((
type: \monoUpdate,
id: id,
updateGroup: {
~group = ~group.value;
~addAction = ~addAction.value;
(group.asGroup != ~group.asGroup)
or: { addAction != ~addAction }
},
gate: 1,
args: event.keys.includes(\args).if({ event[\args] }, {[]})
));
};
};
^cleanup.exit(event ? inEvent);
}
}
+Pbind {
*mono {
|...pairs, kwpairs|
^Pmonophonic() <> Pbind.performArgs(\new, pairs, kwpairs)
}
mono {
^Pmonophonic() <> this
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment