151 lines
4.4 KiB
Python
151 lines
4.4 KiB
Python
|
|
|
|
Granulator {
|
|
var <buflen, <inb, <outb, <>posb, <triggerb, <pitchb, <rectriggerb, <modb, <buffer, <recorder, <grains, <triggersynth, <pitchsynth, server, mode, record, gain, mix, speed, trigger, size, blur, dust, slope, back, chorus, detune, pitch, pan, track, jitter;
|
|
|
|
*init {
|
|
SynthDef(
|
|
\grain_record,
|
|
{
|
|
arg in = 2, trigger = 0, buffer = 0, mix = 0.5, record = 0.0;
|
|
var insig = record * In.ar(in, 1), trigsig = In.kr(trigger, 1);
|
|
RecordBuf.ar(insig, buffer, 0, mix, 1 - mix, loop: 1, trigger: trigsig)
|
|
}
|
|
).add();
|
|
|
|
SynthDef(
|
|
\grain_play,
|
|
{
|
|
arg out=0, buffer, blen, pitchb, triggerb, posb, rectriggerb, modb, size=0.1, amp=0.5, pan=0, track=0.25, jitter=0, blur=0.0;
|
|
var pitch, trigger, pos, pans, grains;
|
|
trigger = In.kr(triggerb);
|
|
pitch = In.kr(pitchb);
|
|
pos = Wrap.kr(In.kr(posb, 1) + WhiteNoise.kr(blur), 0, 1);
|
|
pans = pan + WhiteNoise.kr(jitter) + (track * (In.kr(posb, 1) - 1));
|
|
grains = TGrains.ar(2, trigger, buffer, pitch, pos * blen, size, pans, amp);
|
|
Out.ar(out, grains);
|
|
}
|
|
).add();
|
|
|
|
SynthDef(
|
|
\grain_pitch,
|
|
{
|
|
arg out, posb, triggerb, track=1, dir=1, detune=0.0, chorus=0, harmonics=2, pitch=0;
|
|
var tracking, base, chor, det, csig, dsig;
|
|
csig = Latch.kr(WhiteNoise.kr(), In.kr(triggerb));
|
|
dsig = Latch.kr(WhiteNoise.kr(), In.kr(triggerb));
|
|
tracking = Schmidt.kr(Slope.kr(posb), 0, 0) * 2 - 1;
|
|
base = 2.pow(pitch) * dir * (track * tracking + (1 - track));
|
|
det = detune * dsig + 1;
|
|
chor = chorus * harmonics.pow((csig * 2).round) + (1 - chorus);
|
|
Out.kr(out, base * chor * det);
|
|
}
|
|
).add();
|
|
|
|
SynthDef(
|
|
\grain_trigger,
|
|
{
|
|
arg out, freq=120, dust=0;
|
|
Out.kr(out, (Impulse.kr(freq) * (1 - dust)) + (Dust.kr(freq) * dust));
|
|
}
|
|
).add();
|
|
}
|
|
|
|
*new { | buflen, inb, outb, posb, rectriggerb |
|
|
^super.new.init(buflen, inb, outb, posb, rectriggerb);
|
|
}
|
|
|
|
// note that the default values here have been hand-picked to match the TouchOSC
|
|
// defaults, which is Bad.
|
|
|
|
init { | abuflen, ainb, aoutb, aposb, arectriggerb |
|
|
buflen = abuflen;
|
|
inb = ainb;
|
|
outb = aoutb;
|
|
posb = aposb;
|
|
rectriggerb = arectriggerb;
|
|
server = Server.default;
|
|
mode = 0;
|
|
blur = 0;
|
|
size = 0.1;
|
|
trigger = 120;
|
|
dust = 0;
|
|
back = 0;
|
|
slope = 0;
|
|
chorus = 0;
|
|
detune = 0;
|
|
pitch = 0;
|
|
mix = 0.25;
|
|
pan = 0;
|
|
track = 0.5;
|
|
jitter = 0.25;
|
|
triggerb = Bus.control(server, 1);
|
|
pitchb = Bus.control(server, 1);
|
|
buffer = Buffer.alloc(server, server.sampleRate * buflen, 1);
|
|
recorder = Synth.new(\grain_record, [
|
|
\in, inb,
|
|
\buffer, buffer,
|
|
\record, 0,
|
|
\trigger, rectriggerb
|
|
], server, \addToTail);
|
|
triggersynth = Synth.new(\grain_trigger, [ \out, triggerb ]);
|
|
pitchsynth = Synth.new(\grain_pitch, [ \out, pitchb, \triggerb, triggerb, \posb, posb ]);
|
|
grains = Synth.new(
|
|
\grain_play,
|
|
[
|
|
\out, outb,
|
|
\buffer, buffer,
|
|
\blen, buflen,
|
|
\posb, posb,
|
|
\triggerb, triggerb,
|
|
\pitchb, pitchb,
|
|
\modb, modb,
|
|
\size, 0.1
|
|
],
|
|
server);
|
|
}
|
|
|
|
reset { | newbuflen |
|
|
var newbuf = Buffer.alloc(server, server.sampleRate * newbuflen, 1), oldbuf = buffer;
|
|
recorder.set(\buffer, newbuf);
|
|
grains.set(\buffer, newbuf);
|
|
buffer = newbuf;
|
|
oldbuf.free;
|
|
}
|
|
|
|
mode { ^mode }
|
|
record { ^record }
|
|
mix { ^mix }
|
|
gain { ^gain }
|
|
speed { ^speed }
|
|
trigger { ^trigger }
|
|
size { ^size }
|
|
blur { ^blur }
|
|
chorus { ^chorus }
|
|
detune { ^detune }
|
|
pitch { ^pitch }
|
|
dust { ^dust }
|
|
slope { ^slope }
|
|
back { ^back }
|
|
pan { ^pan }
|
|
track { ^track }
|
|
jitter { ^jitter }
|
|
|
|
mode_ { | v | mode = v; } // todo - playback mode in this class?
|
|
record_ { | v | record = v; recorder.set(\record, v);}
|
|
mix_ { | v | mix = v; recorder.set(\mix, v); }
|
|
gain_ { | v | gain = v; grains.set(\amp, v); }
|
|
size_ { | v | size = v; grains.set(\size, v); }
|
|
pan_ { | v | pan = v; grains.set(\pan, v); }
|
|
track_ { | v | track = v; grains.set(\track, v); }
|
|
jitter_ { | v | jitter = v; grains.set(\jitter, v); }
|
|
blur_ { | v | blur = v; grains.set(\blur, v); }
|
|
trigger_ { | v | trigger = v; triggersynth.set(\freq, v); }
|
|
dust_ { | v | dust = v; triggersynth.set(\dust, v); }
|
|
slope_ { | v | slope = v; pitchsynth.set(\track, v); }
|
|
back_ { | v | back = v; pitchsynth.set(\dir, if( v > 0, { -1 }, { 1 })); }
|
|
chorus_ { | v | chorus = v; pitchsynth.set(\chorus, v); }
|
|
detune_ { | v | detune = v; pitchsynth.set(\detune, v); }
|
|
pitch_ { | v | pitch = v; pitchsynth.set(\pitch, v); }
|
|
|
|
} |