«panflute sampler» by nicolaariutti

on 20 Oct'19 13:42 in samplersamples

A slightly modified version of my sampler (https://scsynth.org/t/sorting-list-of-dictionaries-by-keys/705, https://scsynth.org/t/dictionary-and-array-inside-patterns/747, https://scsynth.org/t/orchestral-texture-v1/994/3).

I made it to test it with some beautiful pan pipe sound files recorded by timohanes (@ https://freesound.org/people/timohanes/packs/4159/ ) and released under CC0.

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
/*
* A test on creating a sampler for some beautiful pan pipe sound files
* recorded by timohanes (@ https://freesound.org/people/timohanes/packs/4159/ )
* and released under CC0.
*/

// first of all start the server
s.boot;

// the following dictionary will contain all information
// related to the instruments like:
// * sample buffers
// * corresponding MIDI notes
// * lowest MIDI of the recoded samples
~panflute_sampler = Dictionary.new();

// the path where the samples are stored
~samples_path = "path-to-the-samples-folder";
// following global contains a string which is contained in all the samples names
~file_name_body = "__timohanes__pan-pipe-";


// fill the dictionary with ordered data from the samples
(
var tmp_buffers;
var tmp_array;
var path;

// 1. load samples from a folder extracting only one channel from the stereo files
path = PathName(~sample_path);
path.entries.do{
	|item|
	if( item.fileName.contains(),
		{
			// load only a single channel from the audiofile (MONO)
			tmp_buffers = tmp_buffers.add( Buffer.readChannel(s, item.fullPath, channels:1));
		},{
			//do nothing
		}
	);
};


// 2. create an array of dictionaries
tmp_array = Array.newClear();

tmp_buffers.do{
	|item, index|
	var d = Dictionary.new;
	var string = PathName(item.path).fileNameWithoutExtension;
	var output = string.findRegexp("[abcdefgABCDEFG]#?[0123456789]");
	if( output.isEmpty.not, {
		var noteNameAll = output[0][1];
		var octNumber = noteNameAll.rotate(1)[0].asString.asInteger;
		var noteName = noteNameAll[0].asString;
		var isSharp = noteNameAll.contains("#"); // boolean
		//[noteNameAll, noteName, octNumber, isSharp].postln;
		var midiNumber = (octNumber +1) * 12;
		switch( noteName,
			"c", { midiNumber = midiNumber+0; },
			"d", { midiNumber = midiNumber+2; },
			"e", { midiNumber = midiNumber+4; },
			"f", { midiNumber = midiNumber+5; },
			"g", { midiNumber = midiNumber+7; },
			"a", { midiNumber = midiNumber+9; },
			"b", { midiNumber = midiNumber+11; },
		);
		if(isSharp, {midiNumber = midiNumber + 1;});
		[noteNameAll, noteName, isSharp, octNumber, midiNumber].postln;
		d.add(\midi -> midiNumber.asInteger);
		d.add(\note -> noteNameAll);
		d.add(\buffer -> item);
		tmp_array = tmp_array.add(d);
	},
	{
		output.postln;
	});
};

tmp_array.sortBy(\midi);

~panflute_sampler.put( "lowest_note", tmp_array[0][\midi] );
~panflute_sampler.put(   "buffers", Array.newClear() );
~panflute_sampler.put( "midinotes", Array.newClear() );

tmp_array.do{
	|item, index|
	[index, item.values].postln;
	~panflute_sampler.put( "buffers",   ~panflute_sampler.at("buffers").add(item[\buffer]) );
	~panflute_sampler.put( "midinotes", ~panflute_sampler.at("midinotes").add(item[\midi]) );
};
)

// print something to check if the dictionary has been
// succesfully created and filled with data
~panflute_sampler.at("lowest_note");
~panflute_sampler.at("buffers").do({|item| item.postln});
~panflute_sampler.at("midinotes").do({|item| item.postln});


// define some synth to play the samples

// SIMPLE PLAYER
// A synth to play the sample as it is.
(
SynthDef(\simple_player, {
	|
	out=0, amp=0.5, gate=1, buf, rate=1.0, pan=0.0,
	atk=0.01, dcy=0.2, sus=0.7, rel=0.1
	|
	var sig, env;
	env = EnvGen.ar(Env.adsr(atk, dcy, sus, rel), gate, doneAction:2);
	sig = PlayBuf.ar(1, buf, BufRateScale.ir(buf)*rate, 1, doneAction:0);
	sig = sig * amp * env;
	sig = HPF.ar(sig, 400);
	sig = LeakDC.ar(sig);
	Out.ar(out, Pan2.ar(sig, pan));
}).add;
)


// GRAIN PLAYER
// a synth to play buffers in a granular fashion
(
SynthDef(\grain_player, {
	|
	out=0, gate=1, amp=0.9, buf, rate=1, pan=0.0,
	atk=5, dcy=0.2, sus=0.7, rel=5
	|
    var sig, env;

	var density = LFNoise0.kr(25).range(1, 5);
	var trigger = Impulse.kr( density );
	var pos = 0.5 + TRand.kr(trigger, -0.35, 0.35);
	var length = 1 + TRand.kr(trigger, 0.25, 0.35);

	env = EnvGen.kr(Env.adsr(atk, dcy, sus, rel), gate, doneAction:2);

    sig = GrainBuf.ar(2,
		trigger,
		length,
		buf,
		rate,
		pos,
		2,
		pan: pan);

	sig = sig * amp * env;
	sig = HPF.ar(sig, 400);
	sig = LeakDC.ar(sig);
	Out.ar(out, sig);
}).add;
)


// HYBRID PLAYER
// A synth which make use of the initial section of the sample in order to get a sharper attack,
// then it moves automatically to the sustain section (made with a granular Ugen)
// to make the tail of the sound indipendent from the sample duration.
(
SynthDef(\hybrid_player, {
	|
	out=0, gate=1, amp=0.9, buf, rate=1, pan=0.0,
	atk=5, dcy=0.1, sus=0.7, rel=5
	|
    var sig, sig1, sig2, env;

	var density = LFNoise0.kr(25).range(1, 5);
	var trigger = Impulse.kr( density );
	var pos = 0.5 + TRand.kr(trigger, -0.35, 0.35);
	var length = 1 + TRand.kr(trigger, 0.25, 0.35);

	env = EnvGen.kr(Env.adsr(atk, dcy, sus, rel), gate, doneAction:2);

	sig1 = PlayBuf.ar(1, buf, BufRateScale.ir(buf)*rate, 1, doneAction:0);

    sig2 = GrainBuf.ar(2,
		trigger,
		length,
		buf,
		rate,
		pos,
		2,
		pan: 0);

	sig = SelectX.ar(Line.kr(0.0, 1.0, 0.2), [sig1, sig2]);

	sig = sig * amp * env;
	//sig = HPF.ar(sig, 400);
	sig = LeakDC.ar(sig);
	Out.ar(out, Pan2.ar(sig, pan));
}).add;
)



// Play with these synths and samples using some Pbind.

(
Pbindef(\panflute_pattern,
	\index, Pfunc { |e| ((e.use{ ~midinote.()} - ~panflute_sampler.at("lowest_note")-1)/3).floor },
	\buf, Pindex(~panflute_sampler.at("buffers"), Pkey(\index)),
	\rate, (Pfunc{ |e| e.use {~midinote.()}} - Pindex(~panflute_sampler.at("midinotes"), Pkey(\index))).midiratio,
	\instrument, Prand([\simple_sampler, \grain_sampler], inf),
	\scale, Scale.major,
	\octave, 4,
	\degree, Pseq([1,2,3,4,5,6,7,8]-1, inf),
	\amp, Pwhite(0.3, 0.5, inf),
	\dur, 1,
	\atk, 0.01,
	\rel, 0.1,
	\legato, 1,
	\out, 0,
	\pan, Pwhite(-0.2, 0.2, inf)
);
)

(
Pbindef(\panflute_pattern,
	\index, Pfunc { |e| ((e.use{ ~midinote.()} - ~panflute_sampler.at("lowest_note")-1)/3).floor },
	\buf, Pindex(~panflute_sampler.at("buffers"), Pkey(\index)),
	\rate, (Pfunc{ |e| e.use {~midinote.()}} - Pindex(~panflute_sampler.at("midinotes"), Pkey(\index))).midiratio,
	\instrument, \hybrid_sampler,
	\scale, Scale.minor,
	\octave, 4,
	\degree, Pseq([0,1,2,3,4,5,6,7], inf),
	\amp, 0.3, //Pwhite(0.3, 0.5, inf),
	\dur, 10,
	\atk, 0.1,
	\dcy, 0.2,
	\sus, 0.7,
	\rel, 0.2,
	\legato, 1,
	\out, 0,
	\pan, Pwhite(-0.2, 0.2, inf)
);
)

(
Pbindef(\panflute_pattern,
	\index, Pfunc { |e| ((e.use{ ~midinote.()} - ~panflute_sampler.at("lowest_note")-1)/3).floor },
	\buf, Pindex(~panflute_sampler.at("buffers"), Pkey(\index)),
	\rate, (Pfunc{ |e| e.use {~midinote.()}} - Pindex(~panflute_sampler.at("midinotes"), Pkey(\index))).midiratio,
	\instrument, \hybrid_sampler,
	\scale, Scale.minorPentatonic,
	\octave, Pwrand([2,3,4], [0.5,1,3].normalizeSum, inf),
	\degree, Prand([0,1,2,3,4,5,6,7], inf),
	\amp, Pwhite(0.3, 0.5, inf),
	\dur, Prand([0.25, 0.5, 1], inf),
	\atk, 0.1,
	\dcy, 0.2,
	\sus, 0.7,
	\rel, 0.2,
	\legato, 1,
	\out, 0,
	\pan, Pwhite(-0.2, 0.2, inf)
);
)

Pbindef(\panflute_pattern).play;
Pbindef(\panflute_pattern).stop;
raw 7235 chars (focus & ctrl+a+c to copy)
reception
comments