«Granular Sampling GUI Demo 1» by Bruno Ruviaro

on 09 Sep'13 04:50 in guisynthesis techniquesgranular synthesissampling

Graphical interface to experiment with granular sampling (one sound file 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// ************************************
// Granular Synthesis Demo (GUI)
// Patch 1 - Granular Sampling
// Bruno Ruviaro, 2013-08-20
// ************************************

/*

Use the "Open New File" button to load a WAVE or AIFF file.
This granulator needs a mono file. If you open a stereo file,
only the left channel will be actually used (though you will
see both channels displayed on the Sound File View).

Trigger: number of grains being triggered per second.
Transp: rate of transposition in semitones.
grainDur: duration of individual grains.
Pan: distribution of grains in the stereo field (left / right).
grainAmp: amplitude of individual grains.
Reverse: probability of a grain to be played backwards (0-100%).

Grains will be chosen randomly from anywhere within
the selected portion of the sound file. You can select
portions of the sound file in two different ways:
a) directly on the waveform (click and drag);
b) using the Selection Slider just below the waveform display.

You can also zoom in and out the waveform
with Shift + Right Click + Mouse Up/Down
(Note: the selection slider will not follow the zoom.
The slider always reflects the position of current
selection in regard to the total duration of the file)

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

If you want to have several windows open to granulate different sounds,
comment out the lines Window.closeAll and Buffer.freeAll

*/

s.waitForBoot({
	var win, soundFile, soundFileView, subwin, centerPosSlider, centerPosInSeconds, triggerSlider, transpSlider, transpToRate, durSlider, panSlider, ampSlider, reverseSlider, buffer, synth, startButton, openButton, selectionSpec;


	// FUNCTIONS

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

	// Convert from centerPosSlider values (0-1)
	// to actual sound file position in seconds:
	centerPosInSeconds = {
		[
			centerPosSlider.lo.linlin(0, 1, 0, soundFile.duration),
			centerPosSlider.hi.linlin(0, 1, 0, soundFile.duration)
		] // returns an array [lo, hi]
	};

	Window.closeAll;
	Buffer.freeAll;

	// Main window
	win = Window.new("Granular Sampling", Rect(50, 50, 600, 580), false).front;
	win.background = Color.grey(0.1, 0.9);
	win.onClose = {s.freeAll};

	// Sound File View
	soundFileView = SoundFileView.new(win, Rect(30, 20, 540, 200))
	// .soundfile_(soundFile)
	// .read(0, soundFile.numFrames)
	.gridOn_(false);

	// What to do when user selects portion of sound file directly
	// (i.e., on waveform, not using slider)
	soundFileView.mouseUpAction = {arg view;
		var loFrames, hiFrames, loSlider, hiSlider;
		loFrames = view.selection(0)[0];
		hiFrames = view.selection(0)[1] + loFrames;
		loSlider = selectionSpec.unmap(loFrames);
		hiSlider = selectionSpec.unmap(hiFrames);
		2.do{centerPosSlider.setSpanActive(loSlider, hiSlider)}; // 2.do = hack...
	};

	// Open Button
	openButton = Button.new(win, Rect(460, 20, 110, 30))
	.states_([["open new file", Color.black, Color.gray]])
	.action_({

		"HELLO".postln;
		// Stop whatever is playing
		s.freeAll;

		startButton.value = 0;

		Dialog.openPanel(
			okFunc: { |path|
				soundFile = SoundFile.new;
				soundFile.openRead(path);
				// Load sound into buffer
				buffer = Buffer.readChannel(s, path, channels: [0]);
				// Display sound on View
				soundFileView.soundfile_(soundFile);
				soundFileView.read(0, soundFile.numFrames);
				// ControlSpec (slider 0-1 <=> numFrames)
				selectionSpec = ControlSpec(0, soundFile.numFrames);
				// selectionSpec.postln;
				// Set initial selection on View
				soundFileView.setSelection(0, selectionSpec.map([0.1, 0.2]));
				// Update slider
				soundFileView.mouseUpAction.value(soundFileView);
			},
			cancelFunc: {"cancelled"}
		);



	});


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

	centerPosSlider = RangeSlider(subwin, 560@50)
	.lo_(0.1)
	.hi_(0.3)
	.action_({ |v|
		var lo, hi, size;
		lo = selectionSpec.map(v.lo);
		hi = selectionSpec.map(v.hi);
		size = hi - lo;
		soundFileView.setSelection(0, [lo, size]);
		if(startButton.value==1, {synth.set(
			\centerPosLo, centerPosInSeconds.value[0],
			\centerPosHi, centerPosInSeconds.value[1])});
		// ["uau", v.lo, v.hi, lo, hi].postln;
	});

	triggerSlider = EZRanger(
		parent: subwin,
		bounds: 560@30,
		label: "Trigger  ",
		controlSpec: ControlSpec(
			minval:	0.5,
			maxval: 50,
			warp: 'exp',
			step: 0.1,
			units: " t/s"),
		action: {|v|
			if(startButton.value==1, {synth.set(\triggerLo, v.lo, \triggerHi, v.hi)})},
		initVal: [1, 2],
		labelWidth: 60,
		unitWidth: 30)
	.setColors(Color.grey,Color.white, Color.grey(0.7),Color.grey, Color.white, Color.yellow);

	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: {|v|
			if(startButton.value==1, {
				synth.set(
					\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: "grainDur  ",
		controlSpec: ControlSpec(
			minval:	0.1,
			maxval: 2,
			warp: 'lin',
			step: 0.1,
			units: "sec"),
		action: {|v|
			if(startButton.value==1, {synth.set(\durLo, v.lo, \durHi, v.hi)})},
		initVal: [0, 0],
		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: {|v|
			if(startButton.value==1, {synth.set(\panLo, v.lo, \panHi, v.hi)})},
		initVal: [0, 0],
		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: "grainAmp ",
		controlSpec: ControlSpec(
			minval:	0.0,
			maxval: 1,
			warp: 'lin',
			step: 0.01,
			units: "amp"),
		action: {|v|
			if(startButton.value==1, {synth.set(\ampLo, v.lo, \ampHi, v.hi)})},
		initVal: [0.2, 0.4],
		labelWidth: 73,
		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: {|v|
			if(startButton.value==1, {synth.set(\reverseProb, v.value/100)});
		},
		initVal: 0.0,
		labelWidth: 63,
		unitWidth: 35)
	.setColors(Color.grey,Color.white, Color.grey(0.7),Color.grey, Color.white, Color.yellow);

	startButton = Button.new(subwin, 560@40)
	.states_([["START"], ["STOP", Color.black, Color.gray]])
	.action_({arg button;
		if(button.value==1,
			{
				synth = Synth("granular-sampling", [
					\triggerLo, triggerSlider.lo,
					\triggerHi, triggerSlider.hi,
					\rateLo, transpToRate.value(transpSlider.lo),
					\rateHi, transpToRate.value(transpSlider.hi),
					\centerPosLo, centerPosInSeconds.value[0],
					\centerPosHi, centerPosInSeconds.value[1],
					\durLo, durSlider.lo,
					\durHi, durSlider.hi,
					\panLo, panSlider.lo,
					\panHi, panSlider.hi,
					\ampLo, ampSlider.lo,
					\ampHi, ampSlider.hi,
					\reverseProb, reverseSlider.value,
					\bufnum, buffer.bufnum]);
			},
			{synth.free});
	});


	// SynthDef
	SynthDef("granular-sampling", {
		arg triggerLo, triggerHi, rateLo, rateHi, centerPosLo, centerPosHi, durLo, durHi, panLo, panHi, ampLo, ampHi, reverseProb, bufnum;

		var trig, trigFreqMess, rate, centerPos, dur, pan, amp, coin, reverse, snd;
		// var bufdur = BufDur.kr(buffer);

		trigFreqMess = LFNoise2.kr(12).range(0.5, 1);
		trig = Impulse.kr(LFNoise0.kr(trigFreqMess).range(triggerLo, triggerHi));

		rate = Dwhite(rateLo, rateHi);
		centerPos = Dwhite(centerPosLo, centerPosHi);
		dur = Dwhite(durLo, durHi);
		pan = Dwhite(panLo, panHi);
		amp = Dwhite(ampLo, ampHi);
		coin = CoinGate.kr(reverseProb, trig);
		reverse = Select.kr(coin, [1, -1]);
		// reverse.poll(trig);

		Demand.kr(trig, 0, [rate, centerPos, dur, pan, amp]);

		snd = TGrains.ar(
			numChannels: 2,
			trigger: trig,
			bufnum: bufnum,
			rate: rate * reverse,
			centerPos: centerPos,
			dur: dur,
			pan: pan,
			amp: amp);

		Out.ar(0, snd);

	}).add;

}); // end of block
raw 9156 chars (focus & ctrl+a+c to copy)
reception
comments
alln4tural user 31 May'15 09:53

this is cool, thanks for sharing

Martin Horst user 09 Dec'16 15:41

dear Bruno,

thanks for the code. Unfortunately I am getting on SC3.8 Windows 10 the error:

unmatched '}' in file 'selected text' line 1 char 1 ERROR: syntax error, unexpected BADTOKEN, expecting $end in file 'selected text' line 1 char 1:

}); // end of block ^

would you know how to fix? thanks a lot!

M

kustota user 04 Feb'19 16:01

so amazing on drum loops

Bruno Ruviaro user 13 Nov'19 22:05

@Martin only now saw your message here, not sure what the error was. Did you select all (control + A) before running the code with control + Enter?

giy.hands user 14 Nov'21 06:34

This code shows useful ways of doing with granular synthesis. Thanks!