-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathADDAC_PulseGen.h
executable file
·312 lines (207 loc) · 8.3 KB
/
ADDAC_PulseGen.h
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
309
310
311
312
/*
* ADDAC_PulseGen
* whitney pulse generator - pusleGen
*
*/
#ifndef ADDAC_PulseGen_h
#define ADDAC_PulseGen_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include <WProgram.h>
#endif
#include "ADDAC_PVector.h"
#include "ADDAC_Timer.h"
#include "ADDAC_Lin2Log.h"
#include "ADDAC_Euclidean.h"
#include "ADDAC_Comparator.h"
#define widthS 1000
#define heightS 1000
#define numB 48
#define numSlots 6
class ADDAC_Points{
public:
ADDAC_PVector origin;
ADDAC_PVector pos;
ADDAC_PVector vel;
bool activated;
int slot;
int position;
int pulsePerCluster;
int pulsePerClusterOld;
unsigned int beat_holder;
int slotSize;
float inc;
float factor;
float offSet;
float speed;
ADDAC_Timer timer;
ADDAC_Lin2Log ltl;
ADDAC_Points(){}
ADDAC_Points(int _position){
origin.set(widthS/2, heightS/2);
pos.set(widthS/2, heightS/2);
vel.set(0, 0);
position=(_position);
speed=1000.0f;
slotSize=numB/numSlots;
pulsePerCluster=8;
pulsePerClusterOld=0;
factor = 0.01f;
offSet=48;
speed=0.05f;
if (_position<slotSize) slot = 6;
else if (_position<(slotSize)*2) slot = 5;
else if (_position<(slotSize)*3) slot = 4;
else if (_position<(slotSize)*4) slot = 3;
else if (_position<(slotSize)*5) slot = 2;
else if (_position<(slotSize)*6) slot = 1;
}
void update(int _pulsePerCluster, float _speed, float _factor, float _offset){
pulsePerCluster=constrain(_pulsePerCluster,1,8);
speed=ltl.calc(_speed,0.60f)+0.001f;
factor=_factor;
offSet=_offset;
pos.x = position*6.0f * cos(inc) + widthS/2;
pos.y = position*6.0f * sin(inc) + heightS/2;
float temp = ((float)(abs(offSet-position)+1.0f)/numB)/10.0f+1.0f;
float temp2 = (temp-1.0f)*(factor)*100.0f+1.0f;
//original-
//inc+=(temp*temp2)*speed;
inc+=(temp*temp2)*speed;
checkAngle(pos);
if(pulsePerCluster!=pulsePerClusterOld){
beat_holder = euclid(8,pulsePerCluster);
pulsePerClusterOld=pulsePerCluster;
activated = false;
}
}
void checkAngle( ADDAC_PVector _angleToCheck) {
// float angle = atan2(origin.x-_angleToCheck.x,origin.y-_angleToCheck.y);
//
// float tresh = 0.055f;
//
unsigned int readBit = bitRead (beat_holder,position%slotSize);
// if(angle+tresh > -PI/2 && angle-tresh < -PI/2 && (readBit==1))
// activated = 1;
if(inc>=2*PI){
inc=inc-(2*PI);
if((readBit==1))
activated = true;
}
else activated = false;
}
// Euclid calculation function
unsigned int euclid(int n, int k){ // inputs: n=total, k=beats, o = offset
int pauses = n-k;
int pulses = k;
int per_pulse = pauses/k;
int remainder = pauses%pulses;
unsigned int workbeat[n];
unsigned int outbeat;
unsigned int working;
int workbeat_count=n;
int a;
int b;
int trim_count;
for (int a=0;a<n;a++){ // Populate workbeat with unsorted pulses and pauses
if (a<pulses){
workbeat[a] = 1;
}else {
workbeat [a] = 0;
}
}
if (per_pulse>0 && remainder <2){ // Handle easy cases where there is no or only one remainer
for (int a=0;a<pulses;a++){
for (int b=workbeat_count-1; b>workbeat_count-per_pulse-1;b--){
workbeat[a] = ConcatBin (workbeat[a], workbeat[b]);
}
workbeat_count = workbeat_count-per_pulse;
}
outbeat = 0; // Concatenate workbeat into outbeat - according to workbeat_count
for (int a=0;a < workbeat_count;a++){
outbeat = ConcatBin(outbeat,workbeat[a]);
}
return outbeat;
}else {
int groupa = pulses;
int groupb = pauses;
int iteration=0;
if (groupb<=1){
}
while(groupb>1){ //main recursive loop
if (groupa>groupb){ // more Group A than Group B
int a_remainder = groupa-groupb; // what will be left of groupa once groupB is interleaved
trim_count = 0;
for (int a=0; a<groupa-a_remainder;a++){ //count through the matching sets of A, ignoring remaindered
workbeat[a] = ConcatBin (workbeat[a], workbeat[workbeat_count-1-a]);
trim_count++;
}
workbeat_count = workbeat_count-trim_count;
groupa=groupb;
groupb=a_remainder;
}else if (groupb>groupa){ // More Group B than Group A
int b_remainder = groupb-groupa; // what will be left of group once group A is interleaved
trim_count=0;
for (int a = workbeat_count-1;a>=groupa+b_remainder;a--){ //count from right back through the Bs
workbeat[workbeat_count-a-1] = ConcatBin (workbeat[workbeat_count-a-1], workbeat[a]);
trim_count++;
}
workbeat_count = workbeat_count-trim_count;
groupb=b_remainder;
}else if (groupa == groupb){ // groupa = groupb
trim_count=0;
for (int a=0;a<groupa;a++){
workbeat[a] = ConcatBin (workbeat[a],workbeat[workbeat_count-1-a]);
trim_count++;
}
workbeat_count = workbeat_count-trim_count;
groupb=0;
}else {
// Serial.println("ERROR");
}
iteration++;
}
outbeat = 0; // Concatenate workbeat into outbeat - according to workbeat_count
for (int a=0;a < workbeat_count;a++){
outbeat = ConcatBin(outbeat,workbeat[a]);
}
// Serial.println(outbeat,BIN);
return outbeat;
}
}
// Function to find the binary length of a number by counting bitwise
int findlength(unsigned int bnry){
boolean lengthfound = false;
int length=1; // no number can have a length of zero - single 0 has a length of one, but no 1s for the sytem to count
for (int q=32;q>=0;q--){
int r=bitRead(bnry,q);
if(r==1 && lengthfound == false){
length=q+1;
lengthfound = true;
}
}
return length;
}
// Function to concatenate two binary numbers bitwise
unsigned int ConcatBin(unsigned int bina, unsigned int binb){
int binb_len=findlength(binb);
unsigned int sum=(bina<<binb_len);
sum = sum | binb;
return sum;
}
};
class ADDAC_PulseGen{
public:
ADDAC_PulseGen();
void setup();
void update(int pulsePerCluster, float _speed, float _factor, float _offset);
void checkSlots();
void reset();
ADDAC_Points point[numB];
ADDAC_Timer tm;
ADDAC_Comparator sl0;
bool slot[6];
int mute, muteOld; //1-8
};
#endif