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

livecodable dsp #309

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions hacks/livecoding-dsp.scd
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*

livecode DSP graphs!

(see livecoding-dsp.tidal for the corresponding Tidal code)

since OSC messages can contain arbitrary strings, we can put sclang code in
them and .interpret it to build synth graphs on the fly.

!!! WARNING !!!
this is a huge security hole if you configure SuperDirt to listen on a non-
loopback network interface - anyone who can send you OSC will be able to
execute arbitrary code on your system.

*/

(
// first, create an event diversion to build temporary synthdefs from sclang
// code received with the key 'dsp'.
~dirt.orbits.do { |o|
o.defaultParentEvent[\play] = { |dirtEvent|
// run in a routine so we can wait for server sync
Routine {
if(~dsp.notNil) {
// generate temporary synthdef name. by default, these run from
// 'temp__0' to 'temp__511' and then loop back, so old names
// eventually get reused and we dont accumulate synthdefs
// indefinitely.
~dspSynthDef = SystemSynthDefs.generateTempName;

// build the synthdef. this synth will run after conventional
// SuperDirt synths specified with 's' (e.g. dirt_sample), and
// can process their output!
SynthDef(~dspSynthDef, { |out|
var in, sig;
// wrap the code to be interpreted in a function definition
// to provide two special variables:
//
// - out: output (and input) bus
// - in: input signal from the previous synth
//
// everything else is accessible via the event, e.g. ~freq.
//
// the newline before the closing bracket allows the synth
// code to include single-line comments.
in = In.ar(out, ~numChannels);
sig = "{ |out, in| %\n}"
.format(~dsp.asString).interpret.(out, in);
sig = DirtPan.ar(sig, ~numChannels, ~pan);
ReplaceOut.ar(out, sig);
}).add;

// wait for the server to finish adding the synthdef. this will
// eat into the time buffer provided by our latency setting - in
// testing done by pulu, we typically spend about 20ms waiting.
~server.sync;

// adjust the latency value to compensate for the time spent
// syncing.
~latency = ~timeStamp - thisThread.seconds;
};

// play synths. the synthdef name stored in ~dspSynthDef will
// activate the 'dirt-live-dsp' module, defined below.
//
// to bypass SuperDirt's default event playback path, we need to
// return non-nil from here, which .playSynths does,
dirtEvent.playSynths;
}.();
};
};

// define the module which will play our temporary synthdefs.
~dirt.addModule('dirt-live-dsp', { |dirtEvent|
dirtEvent.sendSynth(~dspSynthDef,
[
freq: ~freq,
out: ~out
]
);
}, { ~dspSynthDef.notNil });

~dirt.orderModules(['sound', 'dirt-live-dsp']);

// even if our livecoded dsp doesnt use an input signal, a conventional synth
// needs to be specified in 's', otherwise Tidal will not send the event at all.
// thus, it is convenient to have a 'silence' synthdef.
SynthDef(\silence, { |out|
Out.ar(out, Silent.ar(~dirt.numChannels));
}).add;
)
43 changes: 43 additions & 0 deletions hacks/livecoding-dsp.tidal
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{-

livecode DSP graphs!

(see livecoding-dsp.scd for the corresponding SuperCollider code)

-}


-- define a couple of helpers:

dsp' code = s "silence" # pS "dsp" code
dsp code = dsp' (pure code)

-- and write some patterns:

-- basic synth with some sclang randomness
d1
$ note "<<7 <10 5>> <3!2 -5> 2 0>*<8!3 16 16>*16"
# dsp "VarSaw.ar(~freq, rrand(0.0, 1.0), rrand(0.0, 1.0))"
# attack 0.001 # release (rangex 0.1 0.5 rand)
# delay 0.5 # delayt (3.0/16) # lock 1 # delayfb 0.5
# gain 0.8 # pan rand

-- sampler fx
d2
$ n "0@2 1@2 2@2 3 4 5 6 7 8 2@2 10 4"
# dsp "(in + DelayC.ar(in, 0.01, (~cycle % 2pi).sin.linexp(-1, 1, 0.001, 0.01)) * 3).scurve"
# s "amencutup" -- needs to go after dsp or it will be overridden by the default "silence"
# gain 1.1

-- patterned synths and string concatenation
d3 $ let
addFilter code = "RLPF.ar(" ++ code ++ ", exprand(400,8000), 0.2)"
synths =
[ ("saw", pure $ addFilter "Saw.ar(~freq)")
, ("sqr", pure $ addFilter "Pulse.ar(~freq)")
]
in id
$ note ("[0@2 -12@3 0@3]*2" - 24)
# dsp' (inhabit synths "<saw sqr!2>*8")
# release 0.3
# gain 1.1