Pproto provide a proto event for an event stream


superclass: Pattern


*new(makeFunction, patternOrPatternArray, cleanupFunction)


Pproto uses the makeFunction to allocate resources (buffers, buses, groups) and create a protoEvent that makes those resources available to a pattern. It is fully compatible with non-realtime synthesis using render. 


makeFunction  "makes" the protoEvent (i.e.protoEvent is currentEnvironment).  Typically, it defines and yields a sequence of events that create the needed resources using the following eventTypes:


\allocRead load a file from ~path

starting at ~firstFileFrame

reading ~numFrames sample frames

\cue cue a file for DiskIn, with ~bufferSize frames

\table load ~amps directly into a buffer

\sine1 generate a buffer from ~amps

\sine2 generate a buffer from ~freqs, ~amps

\sine3 generate a buffer from ~freqs, ~amps, ~pahses

\cheby generate a waveshape buffer from ~amps

\audioBus allocate ~channels consecutive audio buses

\controlBus allocate ~channels consecutive control buses

\on create a synth


Note: these eventTypes will allocate their own buffers and buses unless they are specified.  To support this, the key \bufNum is used rather than \bufnum which has a default value assigned. 


When Pproto ends, these eventTypes will respond to the cleanup call by deallocating any resources they have allocated. Do not assume your buffers, buses etc. will exist after Pproto stops!


The function yields each event.  That event is then performed with possible modifications by enclosing patterns and the player (either an EventStreamPlayer or a ScoreStreamPlayer).  The resultant event is returned to the function where it can be assigned to a key within the protoEvent.


The patternarray is played using Pfpar, a variant of Ppar that ends when any of its subpatterns end.  In this way,

you can use Pproto to create effects that can be controlled by a pattern that runs in parallel with the note generating pattern and ends together with that note generating pattern (see example 0 below).


A cleanupFunction that deallocates resources when the pattern ends or is stopped is automatically created. It  can be replaced with a user defined cleanup if needed.  This function receives two arguments: proto, the prototype event, and flag, which is set false if all nodes have been freed already by CmdPeriod.



Example 0, using an effect with parallel control.


(

SynthDef(\echo, { arg out=0, maxdtime=0.2, dtime=0.2, decay=2, gate=1;

var env, in;

env = Linen.kr(gate, 0.05, 1, 5, 2);

in = In.ar(out, 2);

XOut.ar(out, env, CombL.ar(in * env, maxdtime, Lag.kr(dtime, 4), decay, 1, in));

}, [\ir, \ir, 0.1, 0.1, 0]).add;



SynthDef(\fm, { arg out=0,  freq, index, decay=2, gate=1;

var env, in;

env = Linen.kr(gate, 0.05, 1, 5, 2);

in = In.ar(out, 2);

XOut.ar(out, env, SinOsc.ar(freq, in * index));

}).add;



Pproto({

~fsynth = ( type: \on, instrument: \fm, freq: 4, index: 1, addAction: 1, db: -30).yield;

~fControl = [\set, ~fsynth[\id], ~fsynth[\msgFunc] ];

~synth = ( type: \on, instrument: \echo, addAction: 1).yield;

~sControl = [\set, ~synth[\id], ~synth[\msgFunc] ];


}, [ 

Pbind(*[

#[type, id, msgFunc], Pkey(\fControl),


freq: Pseg([0,1], 10).linexp(0,1, 0.1, 1000),

index: Pseg([0,1], 10).linexp(0,1, 0.1, 100),

dur:  0.1,

]),

Pbind(*[

#[type, id, msgFunc], Pkey(\sControl),

dtime: Pwhite(0,0.2),

decay: Pwhite(0.1,2),

dur:  1,

]),

Pbind(*[

instrument: \default,

freq: Pwhite(1,16) * 100,

dur: 0.2,

db: 0

])

]

).play

)


Example 1, loading and granulating a sound file.



(

SynthDef(\help_playbuf, { | out=0, bufnum = 0, rate = 1, startPos = 0, amp = 0.1, sustain = 1, pan = 0, loop = 1|

var audio;

rate = rate * BufRateScale.kr(bufnum);

startPos = startPos * BufFrames.kr(bufnum);

audio = BufRd.ar(1, bufnum, Phasor.ar(0, rate, startPos, BufFrames.ir(bufnum)), 1, 1);

audio = EnvGen.ar(Env.sine, 1, timeScale: sustain, doneAction: 2) * audio;

audio = Pan2.ar(audio, pan, amp);

OffsetOut.ar(out, audio);

}).add;


a = Pproto({

~newgroup = (type: \group).yield;

~sf1 =  SoundFile("sounds/a11wlk01-44_1.aiff").asEvent.yield;

// (type: \on).yield

},

Pbind(*[

instrument: \help_playbuf,

dur: Pseg([0,0,0.25,0.5, 0.75, 1],10).linexp(0,1,0.01,2),

legato: 4,

startPos: Pn(Pseg([0,1], 20), inf),

rate: Pwhite(1, 1).midiratio,

loop: 0,

group: Pkey(\newgroup),

bufnum: Pkey(\sf1)

])

);

a.play;

)


//a.render("sounds/test.aif", 40)

//SoundFile.openRead("sounds/test.aif").play


Example 2, loading a waveform buffer and modifying it in performance


(

SynthDef(\osc,{ arg out=0, bufnum=0, numbufs = 8, sustain = 1, freq = 500, amp = 0.1, pan = 0;

var audio;

audio = Osc.ar(bufnum, freq);

audio = EnvGen.ar(Env.linen(0.01, 0.90,0.9), 1, timeScale: sustain, doneAction: 2) * audio;

audio = Pan2.ar(audio, pan, amp);

OffsetOut.ar(out, audio);

}).add;


b = Pproto({

~bufnum = (type: \sine1, amps: 1.0/[1,2,3,4,5,6] ).yield;

},

Ppar([

Pbind(*[

instrument: \osc,

freq: Pwhite(1, 16) * 100,

detune: Pfunc { Array.fill(3.rand + 1, {3.0.rand}) },

dur: Prand([2,2,2.5,1],10),

db: Pn(Pstep([-10, -20, -20, -15, -20, -20, -20], 0.5) ),

legato: Pwhite(0.0,1).linexp(0,1,0.1, 3)

]),

Pbind(*[

type: \sine1, 

amps: Pseg(Pfunc{ | ev | Array.fill(10, {1.0.rand}) }, 1),

numOvertones: Pseg(Pwhite(0, 9), 10).asInteger,

amps: Pfunc{ | ev | ev[\amps].copyRange(0, ev[\numOvertones]) },

dur: 0.05,

bufNum: Pkey(\bufnum)

])

])

);

b.play

)


//b.render("sounds/test.aif", 40)

//SoundFile.openRead("sounds/test.aif").play


Example 3, loading a waveshaper buffer and modifying it in performance


(

SynthDef(\shaper,{ arg out=0, bufnum=0, numbufs = 8, sustain = 1, freq = 500, amp = 0.1, pan = 0;

var audio;

audio = SinOsc.ar(freq);

audio = EnvGen.ar(Env.linen(0.4, 0.50,0.9), 1, timeScale: sustain, doneAction: 2) * audio;

audio = Shaper.ar(bufnum, audio);

audio = Pan2.ar(audio, pan, amp);

OffsetOut.ar(out, LeakDC.ar(audio));

}).add;


c = Pproto({

~bufnum = (type: \cheby, amps: 1.0/[1,2,3,4,5,6] ).yield;

},

Ppar([

Pbind(*[

instrument: \shaper,

freq: Pwhite(1, 16) * 100,

detune: Pfunc { Array.fill(3.rand + 1, {3.0.rand}) },

dur: Prand([2,2,2.5,1],inf),

db: Pn(Pstep([-10, -20, -20, -15, -20, -20, -20], 0.5) ),

legato: Pwhite(0.0,1).linexp(0,1,1.1, 5)

]),

Pbind(*[

type: \cheby, 

amps: Pseg(Pfunc{ | ev | Array.fill(10, {1.0.rand}) }, 4),

dur: 0.05

])

])

);

c.play

)

//c.render("sounds/test.aif", 40)

//SoundFile("sounds/test.aif").play

//