-
Notifications
You must be signed in to change notification settings - Fork 0
/
Markers.sc
265 lines (263 loc) · 5.47 KB
/
Markers.sc
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
// Markers.sc
// FIX: maybe just delete Markers and rename MarkerSpec to Markers for simplicity(?)
MarkerSpec { // actual list of points.
var >points, <>text, <>color, <>type, <>total;
// 'total' should be the total number of frames of the source sound file, so that a percent can be made.
*new {
arg points, text="", color=Color.blue;
^super.newCopyArgs(points, text, color, \manual).init;
}
*extrapolate {
arg points, text="", color=Color.blue;
^super.newCopyArgs(points, text, color, \extrapolate).init;
}
*series {
arg points, text="", color=Color.blue;
^super.newCopyArgs(points, text, color, \series).init;
}
*manual {
arg points, text="", color=Color.blue;
^super.newCopyArgs(points, text, color, \manual).init;
}
init {
if(points.isNil, {
points = [];
});
}
storeOn {
| stream |
stream << "MarkerSpec." << this.type << "(" << points.cs << ", " << text.cs << ", " << color.cs << ")" << if(total.isNil, "", ".total_(" ++ total.asString ++ ")");
}
at { // should return a single "section" - an array of 2 numbers
| index se=nil |
var list = this.points.sort;
if(se.isNil, {
^[list[index], list[index+1]];
}, {
if(se==0, {
^list[index];
}, {
^list[index+se];
});
});
}
start {
| index |
^this.at(index, 0);
}
end {
| index |
^this.at(index, 1);
}
points {
switch(this.type,
\manual, {
^points.sort;
},
\extrapolate, {
var diff = [], avg;
points.stutter.drop(1).drop(-1).doAdjacentPairs({
| a b |
diff = diff ++ [b-a];
});
avg = diff.sum/diff.size;
^(points[0], points[0]+avg .. points.last);
},
\series, {
^(points[0], points[1] .. points[2]);
},
);
}
marks {
^this.points;
}
forPattern { // output for use in patterns, for sending the array of points to a synth.
^[this.points.asArray];
}
sndp { // same as above, but uses percentages (0..1) instead of frame numbers, for my SndP pseudo-UGen.
^[this.points.asArray/this.total];
}
add {
| frame |
if(points.includes(frame).not, { // don't add dupes cuz that's dumb!
points = points.add(frame);
});
}
mark {
| frame |
this.add(frame);
}
remove {
| frame |
points.remove(frame);
}
unmark {
| frame |
this.remove(frame);
}
}
Markers { // several different MarkerSpecs combined. use this class for convenience.
var <>markersList, markers;
*new {
| ... specs |
^super.new.init(*specs);
}
init {
| ... specs |
if(specs.size == 0, {
markersList = [MarkerSpec.new];
}, {
markersList = [];
specs.do({
| sp |
this.add(sp);
});
});
}
storeArgs {
^markersList;
}
add {
| spec |
markersList = markersList ++ [spec];
markers = nil;
}
delete {
| index |
markersList = markersList.removeAt(index);
markers = nil;
}
at {
| n |
^markersList[n];
}
prMakeMarkers {
var res = [];
markersList.do {
| marker n |
var marks = switch(marker.class,
MarkerSpec, {
marker.points;
},
Event, {
case(
{ marker[\start].notNil }, {
(marker[\start], marker[\second] .. marker[\end]);
},
{ }, {
});
});
if(marks.notNil, {
marks = marks.collect({
| mrk |
(frame: mrk, color: marker.color, type: n, text: marker.text);
});
});
res = res ++ marks;
};
markers = res.sortBy(\frame);
}
markers {
this.prMakeMarkers;
^markers.asArray;
}
points {
^this.markers.collect(_[\frame]);
}
sf {
^[this.points++[this.total]];
}
getMarkerToLeft {
| frame |
var marks = this.markers;
if((marks.size == 0) or: {frame == 0}, {
^(frame: 0);
}, {
var iogt = marks.collect(_[\frame]).indexOfGreaterThan(frame);
if(iogt.isNil, {
^marks[marks.size-1];
}, {
if(iogt == 0, {
^(frame:0);
}, {
^marks.clipAt(iogt-1);
});
});
});
}
getMarkerToRight {
| frame |
var marks = this.markers;
if((marks.size == 0) or: {frame == -1}, {
^(frame: -1);
}, {
var iogt = marks.collect(_[\frame]).indexOfGreaterThan(frame);
if(iogt.isNil, {
^(frame: -1);
}, {
^marks.clipAt(iogt);
});
});
}
getMarkersSurrounding {
| frame num=0 | // num is how many markers to skip on each side. if 0, select the 2 nearest markers. if 1, select the 2 second-nearest markers, etc.
var marks = this.markers;
^[this.getMarkerToLeft(frame), this.getMarkerToRight(frame)]; // FIX: need to take num into account.
// if(marks.size == 0, {
// ^[(frame:0), (frame:-1)];
// }, {
// var iogt = marks.collect(_[\frame]).indexOfGreaterThan(frame);
// if(iogt.isNil, { // there is no marker to our right.
// ^[marks.clipAt(iogt-(1+num)), (frame:-1)];
// }, { // there is a marker to our right
// if(iogt == 0, { // there is no marker to our left.
// ^[(frame:0), marks.clipAt(iogt+num)];
// }, {
// ^[marks.clipAt(iogt-(1+num)), marks.clipAt(iogt+num)];
// });
// });
// });
}
getMarkersInRange {
| start end |
^this.markers.select({
| mrk |
(mrk[\frame] >= start) && {mrk[\frame] <= end};
});
}
mark {
| frame type=0 |
markersList[type].mark(frame);
}
unmark {
| frame type |
if(type.isNil, {
markersList.do({
| markersspec |
markersspec.remove(frame);
});
}, {
markersList[type].remove(frame);
});
}
unmarkInRange {
| start end |
this.getMarkersInRange(start, end).do({
| mrk |
this.unmark(mrk[\frame], mrk[\type]);
});
}
total_ {
| num |
this.markersList.do({
| m |
m.total_(num);
});
}
total {
^this.markersList[0].total;
}
sndp {
^this.markersList[0].sndp;
}
}