-
Notifications
You must be signed in to change notification settings - Fork 0
/
Serendipity.scd
301 lines (209 loc) · 6.13 KB
/
Serendipity.scd
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
(
{
var baseDir= thisProcess.nowExecutingPath.dirname.standardizePath;
var lamePath= baseDir ++ "/bin/lame.exe".standardizePath;
var featuresPath= baseDir ++ "/features/".standardizePath;
var wavsPath= baseDir ++ "/wavs/".standardizePath;
s = Server.local;
s.boot;
Buffer.freeAll;
//avoid loading features if they are still loaded from previous execution
if(l.notNil,"Features are still in RAM, dont load from disk right now.".warn,{
"Begin gathering track features.".underlined.postln;
l = PathName(baseDir++"/tracks/").files.collect({arg file, i;
var tfd,bsfd,bffd,gfd,rfd,t,genre,bs, bf,r;
var track = file.fileNameWithoutExtension;
var basename = featuresPath ++ track;
tfd = File(basename ++ ".tempo","r");
bsfd = File(basename ++ ".beat_secs","r");
bffd = File(basename ++ ".beat_frames","r");
gfd = File(basename ++ ".genre","r");
rfd = File(basename ++ ".rmse","r");
format("--> Attempting to gather % features...",track).post;
t = tfd.readAllString.asFloat;
bs = bsfd.readAllString.split($\n).asFloat;
bf = bffd.readAllString.split($\n).asInt;
genre = gfd.readAllString.replace("\n","").replace("\r","");
r = rfd.readAllString.split($\n).asFloat;
tfd.close;
bsfd.close;
bffd.close;
gfd.close;
rfd.close;
d = Dictionary.new;
d.add(\tempo -> t);
d.add(\originalFile -> file);
d.add(\genre -> genre);
d.add(\beat_secs -> bs);
d.add(\beat_frames -> bf);
d.add(\rmse -> r);
// segment analisys function
// si el RMS es mayor a tanto y múltiplo de 4, lo selecciono
// y podemos usar segmentos sólo en un rango de RMS (para beats_skip)
// armar una librería de segmentos por archivo:
// RMS, CROMA Y/O CENTROIDE ESPECTRAL, para empezar
// luego vamos ordenando los segmentos por similitud
// y repetimos hasta lograr 15 minutos
// segment analisys function
d.add(\beat_start -> (16 * [1,2,4,8].choose));
d.add(\beats_length -> (4 * [1,2].choose));
d.add(\loop_times -> 4);
"ok.".postln;
//return dictionary
d;
});
"Done with features query.".underlined.postln;
});
//reject tracks without genre
l = l.reject({arg item, i;
item.at(\genre).size==0;
});
g = Set.new;
l.do({arg item, i;
var genre = item.at(\genre);
if(g.includes(genre) ,
{},
{ g.add(genre)});
});
//selects a genre
h = g.choose;
//selects matching genre tracks
o = l.select({arg item, i;
item.at(\genre) == h;
});
o.sortBy(\tempo);
t = (o.collect({arg item, i;
item.at(\tempo);
}).sum/o.size).round(10);
format("\nSelected genre: %\nAverage tempo : %\nTracks matched: %\n\n",h,t, o.size).postln;
"Convert to wav all selected mp3 files".underlined.postln;
o.do{ arg item,i;
var input= item.at(\originalFile).fullPath.standardizePath;
var output = wavsPath++item.at(\originalFile).fileNameWithoutExtension ++".wav";
if(File.exists(output),{
format("--> File % already in WAV format.",item.at(\originalFile).fileName).postln;
},{
var cmd = format("% --decode --silent \"%\" \"%\"", lamePath, input, output);
format("--> Converting % to %",item.at(\originalFile).fileName, output).postln;
//using this instead because it is blocking
cmd.unixCmdGetStdOut;
});
//adds wav file path to dictionary
item.add(\wavfile->output);
//adds a playback rate
item.add(\playback_rate-> (t/item.at(\tempo)) );
};
"All files converted".underlined.postln;
"Creating main out bus".postln;
~mainBus = Bus.audio(s, 2);
"Defining play buf synth def".postln;
SynthDef(\playBuf,{ arg out = 0, bufnum,rate=1, trigger=1,startpos=0,loop=0;
Out.ar( out,
PlayBuf.ar(2, bufnum, rate,trigger,startpos,loop,2)
)
}).add;
"Defining compresor synth def".postln;
SynthDef(\Compre, {arg out = 0, in, gate=1;
var sig, env;
sig = In.ar(in, 2);
// main effects here
// compressor
sig = Compander.ar(sig, sig,
thresh: 0.9,
slopeBelow: 1,
slopeAbove: 0.5,
clampTime: 0.01,
relaxTime: 0.01
);
Compander.ar(sig, sig,
thresh: 0.9,
slopeBelow: 1,
slopeAbove: 0.1,
clampTime: 0.01,
relaxTime: 0.01
);
env = EnvGen.ar(Env.asr(0.1,1,10),gate,doneAction:2);
// ---
Out.ar(out, sig*env);
}).add;
"Waiting server to be on sync".postln;
{s.sync};
"Declaring main compresor on main bus".postln;
~mainCompre = Synth(\Compre, [\out, 0,\in, ~mainBus]);
//esperar unos segundos antes de disparar esto
1.wait;
//SystemClock.sched(30, { ~mainCompre.release(); ~mainRoutine.stop; "release".postln});
"Loading buffers".underlined.postln;
// read a soundfile
b = o.collect{ arg item,i;
var bb;
var startFrame, endFrame, numFrames;
startFrame = item.at(\beat_frames)[item.at(\beat_start)]*1024-1023;
endFrame = item.at(\beat_frames)[item.at(\beat_start)+item.at(\beats_length)]*1024-1023;
numFrames = endFrame - startFrame - 1;
bb = Buffer.read(s, item.at(\wavfile), startFrame, numFrames);
{s.sync;}.fork; //should wait?
bb;
};
"Buffers loaded".underlined.postln;
}.fork;
)
s.scope
(
x = Synth(\playBuf,[
\out, ~mainBus,
\bufnum, b[1].bufnum,
\rate, o[1].at(\playback_rate),
\loop,1
]);
y = Synth(\playBuf,[
\out, ~mainBus,
\bufnum, b[2].bufnum,
\rate, o[2].at(\playback_rate),
\loop,1
]);
)
x.set(\trigger,1);
x.set(\trigger,-1);
x.free;
y.free;
(
{
o.do({ arg item, i;
x = Synth(\playBuf,[
\out, ~mainBus,
\bufnum, b[i].bufnum
]);
1.wait;
x.free;
s.sync; //evita referencias perdidas
});
}.fork;
)
(
{
//start 15min set. Sched release stage (14x60+50segs from now). Release 10 sec. WE NEED TO END OPENSEMBLE
//start music
o.do({ arg item, i;
var skip = item.at(\beats_skip);
var last = item.at(\beats_last);
var duration = (item.at(\beat_secs)[skip+last-1] - item.at(\beat_secs)[skip-1]);
//repeat
//item.at(\loop_times).postln;
//duration = duration * item.at(\loop_times);
//repeat, puede hacerse mejor
item.at(\loop_times).do {
x = Synth(\playBuf,[
\out, ~mainBus,
\bufnum, b[i].bufnum,
\startpos, item.at(\beat_frames)[item.at(\beats_skip)]
]);
duration.wait;
x.free;
s.sync; //evita referencias perdidas
};
"play fill start".postln;
//~playfill.value; //esto es asincrónico
})}.fork;
)
CmdPeriod.run