«Granular Sampling GUI Demo 2» by Bruno Ruviaro

on 09 Sep'13 04:51 in guisynthesis techniquesgranular sampling

Graphical interface to experiment with granular sampling (multiple sound files at a time).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// ************************************
// Granular Synthesis Demo (GUI)
// Patch 2 - Granular Sampling with Multiple Files
// Bruno Ruviaro, 2013-08-21
// ************************************

/*

Use the "add files" button to load several wave or aif files.
This granulator needs mono files. If you open a stereo file,
only the left channel will be actually used.

Transp: rate of transposition in semitones.
Duration: duration of individual samples (sequential: "one sample every X seconds").
Pan: distribution of samples in the stereo field (left / right).
Amplitude: amplitude of individual samples.
Reverse: probability of a sample to be played backwards (0-100%).
Rests: probability of rests to occur (0-100%).
Overlap: smaller numbers are more 'staccato', bigger numbers more 'legato'.

Samples will be chosen randomly from any of the loaded sound files.

How to start: select all (ctrl + A), then evaluate (ctrl + enter).
(on a Mac, use the command key instead of control)

*/

s.waitForBoot({

	var win, subwin, openButton, startButton, durSlider, transpSlider, ampSlider, panSlider, restSlider, reverseSlider, overlapSlider, displayFileName, transpToRate, bufferList, staticTextList, rateLo, rateHi, durLo, durHi, panLo, panHi, ampLo, ampHi, restProb, reverseProb, overlap, pattern, player;

	// Init values
	rateLo = 1; rateHi = 1;
	durLo = 0.1; durHi = 0.2;
	panLo = 0; panHi = 0;
	ampLo = 0.3; ampHi = 0.4;
	restProb = 0.1;
	overlap = 1;
	reverseProb = 0;

	Window.closeAll;


	staticTextList = List.new;

	// Main window
	win = Window.new("Granular Sampling - Sequential, Multiple Files", Rect(50, 50, 600, 560), false).front;
	win.background = Color.grey(0.1, 0.9);
	win.onClose = { player.stop };


	// Sub view to group all sliders
	subwin = CompositeView.new(win, Rect(20, 220, 560, 325))
	// .background_(Color.red(0.4))
	;
	subwin.decorator = FlowLayout(subwin.bounds, margin: 0@0, gap: 5@10);

	// FUNCTIONS

	// Convert transpSlider values (in semitones)
	// to rate values for TGrains (1 = no transp):
	transpToRate = {arg transp; transp.linexp(-24, 24, 0.25, 4)};

	// Display file names on random places on window
	displayFileName = {arg fileName;
		var left, top, width, height;
		width = 150;
		height = 30;
		left = rrand(0, win.bounds.width-width);
		top = rrand(0, 220-height);

		staticTextList.add(
			StaticText.new(win, Rect(left, top, width, height))
			.string_(fileName.asString)
			.stringColor_(Color.gray);
		);

		openButton.front;
	};


	openButton = Button.new(win, Rect(240, 90, 120, 30))
	.states_([["add files", Color.black, Color.gray]])
	.action_({
		// Stop player if it's running:
		if(player.isNil, {"do nothing"}, { player.stop; player = nil; });
		// Let user select files:
		Dialog.openPanel(
			okFunc: {arg pathList;
				pathList.do({arg path;
					bufferList.add(Buffer.readChannel(s, path, channels: [0]));
					displayFileName.value(path.split.last);
				});
			},
			cancelFunc: { "cancelled".postln },
			multipleSelection: true);
	});


	transpSlider = EZRanger(
		parent: subwin,
		bounds: 560@30,
		label: "Transp  ",
		controlSpec: ControlSpec(
			minval:	-24, // two octaves below
			maxval: 24, // two octaves above
			warp: 'lin',
			step: 1, // step by semitones
			units: " ST"),
		action: {arg v;
			rateLo = transpToRate.value(v.lo);
			rateHi = transpToRate.value(v.hi)
		},
		initVal: [0, 0],
		labelWidth: 60,
		unitWidth: 30)
	.setColors(Color.grey,Color.white, Color.grey(0.7),Color.grey, Color.white, Color.yellow);

	durSlider = EZRanger(
		parent: subwin,
		bounds: 560@30,
		label: "Duration  ",
		controlSpec: ControlSpec(
			minval:	0.05,
			maxval: 2,
			warp: 'exp',
			step: 0.01,
			units: "sec"),
		action: {arg v;
			durLo = v.lo;
			durHi = v.hi;
		},
		initVal: [durLo, durHi],
		labelWidth: 70,
		unitWidth: 30)
	.setColors(Color.grey,Color.white, Color.grey(0.7),Color.grey, Color.white, Color.yellow);

	panSlider = EZRanger(
		parent: subwin,
		bounds: 560@30,
		label: "Pan     ",
		controlSpec: ControlSpec(
			minval:	-1,
			maxval: 1,
			warp: 'lin',
			step: 0.1,
			units: "L/R"),
		action: {arg v;
			panLo = v.lo;
			panHi = v.hi;
		},
		initVal: [panLo, panHi],
		labelWidth: 60,
		unitWidth: 30)
	.setColors(Color.grey,Color.white, Color.grey(0.7),Color.grey, Color.white, Color.yellow);


	ampSlider = EZRanger(
		parent: subwin,
		bounds: 560@30,
		label: "Amplitude ",
		controlSpec: ControlSpec(
			minval:	0.0,
			maxval: 1,
			warp: 'lin',
			step: 0.01,
			units: "amp"),
		action: {arg v;
			ampLo = v.lo;
			ampHi = v.hi;
		},
		initVal: [ampLo, ampHi],
		labelWidth: 78,
		unitWidth: 35)
	.setColors(Color.grey,Color.white, Color.grey(0.7),Color.grey, Color.white, Color.yellow);


	reverseSlider = EZSlider(
		parent: subwin,
		bounds: 560@30,
		label: "Reverse ",
		controlSpec: ControlSpec(
			minval:	0,
			maxval: 100,
			warp: 'lin',
			step: 1,
			units: "%"),
		action: {arg v;
			reverseProb = v.value/100;
		},
		initVal: reverseProb,
		labelWidth: 60,
		unitWidth: 35)
	.setColors(Color.grey,Color.white, Color.grey(0.7),Color.grey, Color.white, Color.yellow);

	restSlider = EZSlider(
		parent: subwin,
		bounds: 560@30,
		label: "Rests  ",
		controlSpec: ControlSpec(
			minval:	0,
			maxval: 100,
			warp: 'lin',
			step: 1,
			units: "%"),
		action: {arg v;
			restProb = v.value/100;
		},
		initVal: 0.0,
		labelWidth: 50,
		unitWidth: 35)
	.setColors(Color.grey,Color.white, Color.grey(0.7),Color.grey, Color.white, Color.yellow);

	overlapSlider = EZSlider(
		parent: subwin,
		bounds: 560@30,
		label: "Overlap ",
		controlSpec: ControlSpec(
			minval:	0.1,
			maxval: 2,
			warp: 'lin',
			step: 0.1),
		action: {arg v;
			overlap = v.value;
		},
		initVal: overlap,
		labelWidth: 63,
	/*unitWidth: 35*/)
	.setColors(Color.grey,Color.white, Color.grey(0.7),Color.grey, Color.white, Color.yellow);


	// Start button
	startButton = Button.new(subwin, 560@50)
	.states_([["START", Color.black, Color.gray], ["STOP", Color.black, Color.gray]])
	.action_({arg button;
		if(button.value==1,
			{ player = pattern.play;
				("Now sampling from " ++ bufferList.size ++ " sound files").postln;
				/*bufferList.postln;
				[rateLo, rateHi].postln;
				[durLo, durHi].postln;
				[ampLo, ampHi].postln;
				"".postln;*/
			},
			{ player.stop; player = nil; });
	});

	/////////////////
	// SynthDef
	/////////////////

	SynthDef("grain-asr", {arg bufnum, rate = 1, reverse = 1, startPos = 0, gate = 1, att = 0.1, rel = 0.1, amp = 1, pan = 0;
		var env, snd;
		env = EnvGen.kr(Env.asr(att, amp, rel), gate, doneAction: 2);
		snd = PlayBuf.ar(1, bufnum, rate: rate * reverse, startPos: startPos);
		snd = snd * env;
		Out.ar(0, Pan2.ar(snd, pan));
	}).add;

	// Buffer list

	bufferList = List.newClear(1); // Prand won't like if initial list is empty

	// Pattern

	pattern = Pbind(
		\instrument, "grain-asr",
		\buffer, Prand(bufferList, inf),
		\bufnum, Pfunc({arg evt; evt.at(\buffer).bufnum}),
		\startPos, Pwhite(0, Pfunc({arg evt; evt.at(\buffer).numFrames})),
		\dur, Pwhite(Pfunc({durLo}), Pfunc({durHi})),
		\makeRest, Pif(Pfunc({ restProb.coin }), Rest, 709),
		\rate, Pwhite(Pfunc({rateLo}), Pfunc({rateHi})),
		\reverse, Pif(Pfunc({ reverseProb.coin }), -1, 1),
		\amp, Pwhite(Pfunc({ampLo}), Pfunc({ampHi})),
		\pan, Pwhite(Pfunc({panLo}), Pfunc({panHi})),
		\legato, Pfunc({overlap})
	);

	bufferList.pop; // remove 'nil', we only want actual buffers here

}); // end of block
raw 7699 chars (focus & ctrl+a+c to copy)
reception
comments