Pattern control by external device

Control of parameters by MIDI or HID

To get pattern values from a HID device, use the PhidKey or PhidSlot pattern.

For MIDI, the best approach is to save an incoming value into a variable, and then use Pfunc to access the variable for each event.

(

~legato = 1;

c = CCResponder({ |src, chan, num, value|

~legato = value.linlin(0, 127, 0.1, 2.5)

}, num: 1); // num: 1 means modwheel

)

(

p = Pbind(

\degree, Pwhite(-7, 12, inf),

\dur, Pwrand([0.25, Pn(0.125, 2)], #[0.8, 0.2], inf),

\legato, Pfunc { ~legato } // retrieves value set by MIDI control

).play;

)

p.stop;

c.remove;

If Pfunc { } is bothersome in the Pbind, a PatternProxy or Pdefn could also serve the purpose.

(

~legato = PatternProxy(1);

c = CCResponder({ |src, chan, num, value|

~legato.source = value.linlin(0, 127, 0.1, 2.5)

}, num: 1);

)

(

p = Pbind(

\degree, Pwhite(-7, 12, inf),

\dur, Pwrand([0.25, Pn(0.125, 2)], #[0.8, 0.2], inf),

\legato, ~legato

).play;

)

p.stop;

c.remove;

Triggering patterns by external control

Issuing 'play' to a pattern can occur in an action function for many different kinds of objects: GUI, MIDI, OSCresponder, HID actions. This allows triggering patterns from a variety of interfaces.

It's very unlikely that an action function would be triggered exactly in sync with a clock. If the pattern being played needs to run in time with other patterns, use the 'quant' argument to control its starting time (see Quant).

Triggering a pattern by a GUI

(

var pattern = Pbind(

\degree, Pseries(7, Pwhite(1, 3, inf) * Prand(#[-1, 1], inf), inf).fold(0, 14)

+ Prand(#[[0, -2, -4], [0, -3, -5], [0, -2, -5], [0, -1, -4]], inf),

\dur, Pwrand(#[1, 0.5], #[0.8, 0.2], inf)

),

player, window;

window = Window.new("Pattern trigger", Rect(5, 100, 150, 100))

// onClose is fairly important

// without it, closing the window could leave the pattern playing

.onClose_({ player.stop });

Button.new(window, Rect(5, 5, 140, 90))

.states_([["Pattern GO"], ["Pattern STOP"]])

.font_(Font.new("Helvetica", 18))

.action_({ |button|

if(button.value == 1 and: { player.isNil or: { player.isPlaying.not } }) {

player = pattern.play;

} {

player.stop;

button.value = 0;

};

});

window.front;

)

p.stop;

Triggering a pattern by MIDI

(

var pattern = Pbind(

\degree, Pseries(7, Pwhite(1, 3, inf) * Prand(#[-1, 1], inf), inf).fold(0, 14)

+ Prand(#[[0, -2, -4], [0, -3, -5], [0, -2, -5], [0, -1, -4]], inf),

\dur, Pwrand(#[1, 0.5], #[0.8, 0.2], inf)

),

player;

~noteOnResp = NoteOnResponder({

if(player.isNil or: { player.isPlaying.not }) {

player = pattern.play;

} {

player.stop;

};

// num: 60 limits this responder to listen to middle-C only

// but it will pick up that note from any port, any channel

}, num: 60);

)

// when done

~noteOnResp.remove;

Triggering a pattern by signal amplitude

Triggering a pattern based on audio amplitude is a bit trickier -- not because it's harder to play the pattern, but because identifying when the trigger should happen is more involved. The most straightforward way in SuperCollider is to use the Amplitude UGen to get the volume of the input signal and compare it to a threshold. Volume can fluctuate rapidly, so the 'releaseTime' argument of Amplitude is set to a high value. This makes the measured amplitude fall more slowly toward the baseline, preventing triggers from being sent too close together.

The actual threshold depends on the incoming signal, so the example pops up a quick and dirty window to see the measured amplitude and set the threshold and decay accordingly. The synth listens by default to the first hardware input bus, but you can change it the following in the code to use a different input bus:

inbus: s.options.numOutputBusChannels

In this configuration, the first trigger starts the pattern and the second trigger stops it. You might want the pattern to play while the input signal is above the threshold, and stop when the signal drops to a quieter level. The comparison amp >= thresh can send a trigger only when the signal goes from softer to lower, so if we want the pattern to stop when the signal becomes quiet, we need to send a trigger when crossing the threshold in both directions.

var amp = Amplitude.kr(In.ar(inbus, 1), attackTime: 0.01, releaseTime: decay),

trig = HPZ1.kr(amp >= thresh);

SendTrig.kr(trig.abs, 1, trig);

HPZ1 is positive if its input rises and negative if it falls. Triggering based on the absolute value, then, sends the trigger on any change. The client responding to the trigger might need to know the direction of change, so we send HPZ1's value back and the client can decide which action to take based on the sign of this value.

For this example, triggers are measured only when the signal rises above the threshold.

(

var pattern = Pbind(

\degree, Pseries(7, Pwhite(1, 3, inf) * Prand(#[-1, 1], inf), inf).fold(0, 14)

+ Prand(#[[0, -2, -4], [0, -3, -5], [0, -2, -5], [0, -1, -4]], inf),

\dur, Pwrand(#[1, 0.5], #[0.8, 0.2], inf)

),

player;

// Quicky GUI to tune threshold and decay times

~w = Window("threshold setting", Rect(15, 100, 300, 100))

.onClose_({

~ampSynth.free;

~ampUpdater.remove;

~oscTrigResp.remove;

player.stop;

});

~w.view.decorator = FlowLayout(~w.view.bounds, 2@2, 2@2);

~ampView = EZSlider(~w, 295@20, "amplitude", \amp, labelWidth: 80, numberWidth: 60);

~ampView.sliderView.canFocus_(false).enabled_(false);

~ampView.numberView.canFocus_(false).enabled_(false);

StaticText(~w, 295@5).background_(Color.gray);

~threshView = EZSlider(~w, 295@30, "threshold", \amp, action: { |ez|

~ampSynth.set(\thresh, ez.value);

}, initVal: 0.4, labelWidth: 80, numberWidth: 60);

~decayView = EZSlider(~w, 295@30, "decay", #[0.1, 100, \exp], action: { |ez|

~ampSynth.set(\decay, ez.value);

}, initVal: 80.0, labelWidth: 80, numberWidth: 60);

~w.front;

~ampSynth = SynthDef(\ampSynth, { |inbus, thresh = 0.8, decay = 1|

var amp = Amplitude.kr(In.ar(inbus, 1), attackTime: 0.01, releaseTime: decay);

// this trigger (id==0) is to update the gui only

SendTrig.kr(Impulse.kr(10), 0, amp);

// this trigger gets sent only when amplitude crosses threshold

SendTrig.kr(amp >= thresh, 1, 1);

}).play(args: [inbus: s.options.numOutputBusChannels, thresh: ~threshView.value, decay: ~decayView.value]);

~ampUpdater = OSCpathResponder(s.addr, ['/tr', ~ampSynth.nodeID, 0], { |time, resp, msg|

defer { ~ampView.value = msg[3] }

}).add;

~oscTrigResp = OSCpathResponder(s.addr, ['/tr', ~ampSynth.nodeID, 1], { |time, resp, msg|

if(player.isNil or: { player.isPlaying.not }) {

player = pattern.play;

} {

player.stop;

};

}).add;

)

Previous: PG_Cookbook02_Manipulating_Patterns

Next: PG_Cookbook04_Sending_MIDI