-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMEM.ino
331 lines (242 loc) · 6.55 KB
/
MEM.ino
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
PROGMEM prog_uint16_t clearTo[]={
877, 0, 0,0, 0,128,0,1022, 13 , 65,48};
/*
//13 = 1011 - sync ON, shiftDir OFF,tuned ON,repeat ON,
red
sampleRate, crush, start, end // loop
green
loopLen, shift, attack, release // normal-random
blue
rate, amt, shape, resolution // dest
*/
// variable names
//page 0
//osc mod-freq amt
//atck decay mod type // 2+2+3
#define PAGE_G 0
#define PAGE_B 1
#define VARIABLES_PER_PAGE 4
PROGMEM prog_char labels[]={
"rcargmse"};
#define RATE 0 //1024 - 10 R
#define CRUSH 1 //127 - 7 C
#define ATTACK 2 // 127 - 7 A
#define RELEASE 3 // 127 - 7 R
// =41
#define LOOP_LENGTH 4 // 127 - 7 L
#define SHIFT_SPEED 5 // 127 - 7 S
#define START 6 //1024 - 10 S
#define END 7 //1024 - 10 E
//#define MODE 8 // 1 - 1 M
#define SETTING 8 // 7 - 4 L
// = 29
#define SAMPLE_NAME_1 9 // 127 - 7
#define SAMPLE_NAME_2 10 // 127 - 7
//#define SAMPLE_NAME_3 12 // 127 - 7
// = 14
/*
#define LFO_RATE 10 // 127 -7 R
#define LFO_AMT 11 // 127 -7 A
#define LFO_SHAPE 12 // 7 -3 S
#define LFO_RES 13 // 15 - 4 R
#define LFO_DEST 14 // 3 -2 D
*/
// =23
// 41+29+14 = 84 /8 = 10,5 =>11
#define KNOB_BITS 10
PROGMEM prog_uint16_t maxValue[]={
1023,127,127,127, 127,255,1023,1023,63 ,127,127}; //const //127,127,7,15,3
#define NUMBER_OF_VARIABLES 11
#define NUMBER_OF_BYTES 12 //11
#define NUMBER_OF_PRESETS 6
#define NUMBER_OF_BANKS 9
#define PRESET_SIZE 84 //66 in real
#define NUMBER_OF_SOUNDS 6
#define CHANNEL_BYTE 1023
const unsigned char variableDepth[NUMBER_OF_VARIABLES]={
10,7,7,7, 7,8,10,10,8, 7,7};
int maxVal(int _maxVal){
return pgm_read_word_near(maxValue+_maxVal);
}
const unsigned char byteCoordinate[NUMBER_OF_VARIABLES]={
0, 1, 2, 3, 3, 4, 5, 7, 8, 9, 10}
;
/*
unsigned char byteCoordinate[NUMBER_OF_VARIABLES]={
0, 2, 3, 4, 5, 6, 7, 9, 11, 12, 13}
;
*/
const unsigned char bitCoordinate[NUMBER_OF_VARIABLES]={
0, 2, 1, 0, 7, 6, 6, 0, 2, 2, 1};
/*
int buffer[NUMBER_OF_SOUNDS][NUMBER_OF_VARIABLES];
unsigned char bufferP[NUMBER_OF_STEPS];
*/
unsigned char variable[NUMBER_OF_SOUNDS][NUMBER_OF_BYTES];
/*
unsigned char variableCop[NUMBER_OF_SOUNDS][NUMBER_OF_BYTES];
void copyPreset(){
for(int i=0;i<NUMBER_OF_SOUNDS;i++){
for(int j=0;j<NUMBER_OF_BYTES;j++){
variableCop[i][j]=variable[i][j];
}
}
}
void pastePreset(){
for(int i=0;i<NUMBER_OF_SOUNDS;i++){
for(int j=0;j<NUMBER_OF_BYTES;j++){
variable[i][j]=variableCop[i][j];
}
}
}
*/
void initMem(){
//calculateBitDepth();
loadPreset(currentBank,currentPreset);
}
void calculateBitDepth(){
/*
for(int i=0;i<NUMBER_OF_VARIABLES;i++){ // calculate bitDepth according to the maximum value
int x=0;
while(maxVal(i)-pow(2,x)>=0) x++;
variableDepth[i]=x ;
}
*/
/*
for(int i=0;i<NUMBER_OF_VARIABLES;i++){
int sum=0;
for(int j=0;j<i;j++){
sum+=variableDepth[j];
}
byteCoordinate[i]=sum/8;
bitCoordinate[i]=sum%8;
Serial.print( bitCoordinate[i]);
Serial.print(", ");
}
*/
}
int getVar(unsigned char _SOUND, unsigned char _VARIABLE){
int _value=0;
unsigned char byteShift=0;
unsigned char _bitCoordinate=0;
for(int i=0;i<variableDepth[_VARIABLE];i++){
if((bitCoordinate[_VARIABLE]+i)>15){
byteShift=2;
_bitCoordinate = i-(16-bitCoordinate[_VARIABLE]);
}
else if((bitCoordinate[_VARIABLE]+i)>7){
byteShift=1;
_bitCoordinate = i-(8-bitCoordinate[_VARIABLE]);//bitCount;
}
else {
_bitCoordinate=bitCoordinate[_VARIABLE]+i;
}
boolean bitState=bitRead(variable[_SOUND][byteCoordinate[_VARIABLE]+byteShift],_bitCoordinate);
bitWrite(_value,i,bitState);
}
return _value;
}
void setVar(unsigned char _SOUND, unsigned char _VARIABLE, int _value){
unsigned char byteShift=0;
unsigned char _bitCoordinate;
for(int i=0;i<variableDepth[_VARIABLE];i++){
if((bitCoordinate[_VARIABLE]+i)>15){
byteShift=2;
_bitCoordinate = i-(16-bitCoordinate[_VARIABLE]);
}
else if((bitCoordinate[_VARIABLE]+i)>7){
byteShift=1;
_bitCoordinate = i-(8-bitCoordinate[_VARIABLE]);
}
else {
_bitCoordinate=bitCoordinate[_VARIABLE]+i;
}
boolean bitState=bitRead(_value,i);
bitWrite(variable[_SOUND][byteCoordinate[_VARIABLE]+byteShift],_bitCoordinate,bitState);
}
}
char presetName[8]="p00.txt";
boolean storePreset(unsigned char _bank,unsigned char _preset) {
// Serial.end();
// Serial.begin(9600);
noSound();
//clearBuffer();
presetName[2]=_preset+49;
presetName[1]=_bank+48;
if (!file.open(&root,presetName, O_WRITE | O_CREAT)) {
return false;
}
for (int j = 0; j < NUMBER_OF_SOUNDS; j++) {
for (int k = 0; k < NUMBER_OF_BYTES; k++) {
file.write(variable[j][k]);
}
}
file.close();
// clearIndexes();
return true;
}
void loadPreset(unsigned char _bank,unsigned char _preset) {
if(_preset>=NUMBER_OF_PRESETS) _preset=NUMBER_OF_PRESETS-1;
if(_bank>=200) _bank=0;
if(_bank>=NUMBER_OF_BANKS) _bank=NUMBER_OF_BANKS;
//if(_bank<=0) _bank=0;
presetName[2]=_preset+49;
presetName[1]=_bank+48;
currentPreset=_preset;
currentBank=_bank;
noSound();
clearBuffer();
if(!file.open(&root, presetName, O_READ)){
clearPreset();
storePreset(_bank,_preset);
file.open(&root, presetName, O_READ);
}
for (int j = 0; j < NUMBER_OF_SOUNDS; j++) {
for (int k = 0; k < NUMBER_OF_BYTES; k++) {
variable[j][k]=file.read();
}
}
file.close();
hw.freezeAllKnobs();
showForWhile("pr "),hw.setDot(1,true),hw.displayChar(presetName[2],3),hw.displayChar(presetName[1],2), clearIndexes();
}
void clearMemmory(){
for(int i=0;i<NUMBER_OF_BANKS;i++){
for(int j=0;j<NUMBER_OF_PRESETS;j++){
loadPreset(i,j);
clearPreset();
storePreset(i,j);
// hw.updateDisplay() ;
}
}
}
void clearPreset(){
for(int i=0;i<NUMBER_OF_SOUNDS;i++){
clearSound(i);
}
}
void clearSound(unsigned char i){
for(int j=0;j<NUMBER_OF_VARIABLES;j++){
setVar(i,j, pgm_read_word_near(clearTo+j));
}
}
boolean inBetween(int val1,int val2,int inBet){
boolean retVal;
if(val1>=val2){
if(inBet<=val1 && inBet>=val2) retVal=true;
else retVal=false;
}
else if(val1<val2){
if(inBet>=val1 && inBet<=val2) retVal=true;
else retVal=false;
}
return retVal;
}
int scale(int _value, unsigned char _originalBits, unsigned char _endBits){
if(_originalBits >= _endBits) return _value >> (_originalBits - _endBits);
else return _value << (_endBits - _originalBits);
}
long myMap(long x, long in_max, long out_max)
{
return (x) * (out_max) / (in_max);
}