-
Notifications
You must be signed in to change notification settings - Fork 0
/
MorduleBuffer.sc
143 lines (125 loc) · 2.31 KB
/
MorduleBuffer.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
MorduleBuffer {
var <buffer;
var <keys;
var <channels;
var <nilIndex;
*new {
arg keys = [], channels = 1, nilIndex = true;
var mb = super.new();
mb.nilIndex = nilIndex;
mb.channels = channels;
mb.keys = keys;
^ mb;
}
channels_ {
arg num;
if (this.testBufferLocked.not) {
channels = num;
} {
"Can't change MorduleBuffer channel count after buffer is allocated".warn;
};
^ this;
}
keys_ {
arg a_keys;
keys = [];
if (nilIndex) {
// keys.add(\nil)
};
keys = keys.addAll(a_keys);
^ this;
}
nilIndex_ {
arg a_nilIndex;
nilIndex = a_nilIndex.asBoolean();
^ this;
}
ensureBuffer {
if (this.testBufferLocked.not) {
buffer = LocalBuf(keys.size, channels).clear;
};
^ this;
}
indexOf {
arg key, force = true;
var i = keys.indexOf(key);
if (i == nil && force) {
("MorduleBuffer key not found: " ++ key).error;
};
^ i;
}
read {
arg index, mul = 1, add = 0;
this.ensureBuffer();
^ BufRd.kr(channels, buffer, index, interpolation: 1).madd(mul, add);
}
readKey {
arg key, mul = 1, add =0;
var index = this.indexOf(key);
^ this.read(index, mul, add);
}
write {
arg index, v;
this.ensureBuffer();
v = v.asArray.wrapExtend(channels);
BufWr.kr(v, buffer, index);
^ this;
}
writeKey {
arg key, v;
var index = this.indexOf(key);
^ this.write(index, v);
}
insert {
arg index, v;
this.write(index, v + this.read(index));
^ this;
}
insertKey {
arg key, v;
var index = this.indexOf(key);
^ this.insert(index, v);
}
tap {
arg index, mul = 1, add = 0;
var v = this.read(index, mul, add);
this.write(index, 0);
^ v;
}
tapKey {
arg key, mul = 1, add = 0;
var index = this.indexOf(key);
^ this.tap(index, mul, add);
}
selectIndex {
arg index, subset = nil;
var subkeys;
subset = subset.defaultWhenNil(keys);
subkeys = subset.collect {
arg key;
this.indexOf(key);
};
^ Select.kr(index, subkeys);
}
add {
arg key;
if (this.testBufferLocked.not) {
keys.add(key);
} {
"Can't change MorduleBuffer keys after buffer is allocated".warn;
};
^ this;
}
remove {
arg key;
if (this.testBufferLocked.not) {
keys.remove(key);
} {
"Can't change MorduleBuffer keys after buffer is allocated".warn;
};
^ this;
}
testBufferLocked {
^ buffer.isNil.not;
}
}