-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCODE
296 lines (252 loc) · 9.64 KB
/
CODE
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
/*
_ ____ _ ____ _ ____ _ ____
/ \ / _ \ / |/ _ \/ \ /\/ __\/ \/ _ \
| | | / \| | || / \|| | ||| \/|| || / \|
| |_/\| |-|| /\_| || |-||| \_/|| /| || |-||
\____/\_/ \| \____/\_/ \|\____/\_/\_\\_/\_/ \|
AUDUINO con Arduino UNO 2017
Creado por la Jauria
Revisión 1 - Alex Vargas Benanburg.
// Auduino, the Lo-Fi granular synthesiser
//
// by Peter Knight, Tinker.it http://tinker.it
//
// Help: http://code.google.com/p/tinkerit/wiki/Auduino
// More help: http://groups.google.com/group/auduino
//***********************************************************************
//* modified by la jauría on Nov 20th by changing the pentatonic *
//* scale to a random fibonacci scale and adding 2 arppegiators and *
//* 3 beat controls. *
//***********************************************************************
*/
// Analog in 0: Grain 1 pitch
// Analog in 1: Grain 2 decay
// Analog in 2: Grain 1 decay
// Analog in 3: Grain 2 pitch
// Analog in 4: Grain repetition frequency
//
//Digital out, 11: Arp HIGH
//Digital out, 10: Arp LOW
//Digital out, 9: Beat selector,
//Digital out, 8: Arp HIGH beat random selector
//Digital out, 7: Arp LOW, beat random selector
//
//
// Digital 3: Audio out (Digital 11 on ATmega8)
//
// Changelog:
// 19 Nov 2008: Added support for ATmega8 boards
// 21 Mar 2009: Added support for ATmega328 boards
// 7 Apr 2009: Fixed interrupt vector for ATmega328 boards
// 8 Apr 2009: Added support for ATmega1280 boards (Arduino Mega)
// 20 Nov 2016 : Added 2 random arpeggiators
#include <avr/io.h>
#include <avr/interrupt.h>
uint16_t syncPhaseAcc;
uint16_t syncPhaseInc;
uint16_t grainPhaseAcc;
uint16_t grainPhaseInc;
uint16_t grainAmp;
uint8_t grainDecay;
uint16_t grain2PhaseAcc;
uint16_t grain2PhaseInc;
uint16_t grain2Amp;
uint8_t grain2Decay;
// Map Analogue channels
#define SYNC_CONTROL (4 )
#define GRAIN_FREQ_CONTROL (0)
#define GRAIN_DECAY_CONTROL (2)
#define GRAIN2_FREQ_CONTROL (3)
#define GRAIN2_DECAY_CONTROL (1)
#define numero (5)
// random arpegiartor switches
int buttonPin1 = 11;
int buttonPin2 = 10;
int buttonPin3 = 9;
int buttonPin4 = 8;
int buttonPin5 = 7;
// Changing these will also requires rewriting audioOn()
#if defined(__AVR_ATmega8__)
//
// On old ATmega8 boards.
// Output is on pin 11
//
#define LED_PIN 13
#define LED_PORT PORTB
#define LED_BIT 5
#define PWM_PIN 11
#define PWM_VALUE OCR2
#define PWM_INTERRUPT TIMER2_OVF_vect
#elif defined(__AVR_ATmega1280__)
//
// On the Arduino Mega
// Output is on pin 3
//
#define LED_PIN 13
#define LED_PORT PORTB
#define LED_BIT 7
#define PWM_PIN 3
#define PWM_VALUE OCR3C
#define PWM_INTERRUPT TIMER3_OVF_vect
#else
//
// For modern ATmega168 and ATmega328 boards
// Output is on pin 3
//
#define PWM_PIN 3
#define PWM_VALUE OCR2B
#define LED_PIN 13
#define LED_PORT PORTB
#define LED_BIT 5
#define PWM_INTERRUPT TIMER2_OVF_vect
#endif
// Smooth logarithmic mapping
//
uint16_t antilogTable[] = {
64830, 64132, 63441, 62757, 62081, 61413, 60751, 60097, 59449, 58809, 58176, 57549, 56929, 56316, 55709, 55109,
54515, 53928, 53347, 52773, 52204, 51642, 51085, 50535, 49991, 49452, 48920, 48393, 47871, 47356, 46846, 46341,
45842, 45348, 44859, 44376, 43898, 43425, 42958, 42495, 42037, 41584, 41136, 40693, 40255, 39821, 39392, 38968,
38548, 38133, 37722, 37316, 36914, 36516, 36123, 35734, 35349, 34968, 34591, 34219, 33850, 33486, 33125, 32768
};
uint16_t mapPhaseInc(uint16_t input) {
return (antilogTable[input & 0x3f]) >> (input >> 6);
}
// Stepped chromatic mapping
//
uint16_t midiTable[] = {
17, 18, 19, 20, 22, 23, 24, 26, 27, 29, 31, 32, 34, 36, 38, 41, 43, 46, 48, 51, 54, 58, 61, 65, 69, 73,
77, 82, 86, 92, 97, 103, 109, 115, 122, 129, 137, 145, 154, 163, 173, 183, 194, 206, 218, 231,
244, 259, 274, 291, 308, 326, 346, 366, 388, 411, 435, 461, 489, 518, 549, 581, 616, 652, 691,
732, 776, 822, 871, 923, 978, 1036, 1097, 1163, 1232, 1305, 1383, 1465, 1552, 1644, 1742,
1845, 1955, 2071, 2195, 2325, 2463, 2610, 2765, 2930, 3104, 3288, 3484, 3691, 3910, 4143,
4389, 4650, 4927, 5220, 5530, 5859, 6207, 6577, 6968, 7382, 7821, 8286, 8779, 9301, 9854,
10440, 11060, 11718, 12415, 13153, 13935, 14764, 15642, 16572, 17557, 18601, 19708, 20879,
22121, 23436, 24830, 26306
};
uint16_t mapMidi(uint16_t input) {
return (midiTable[(1023 - input) >> 3]);
}
// Stepped Pentatonic mapping
//
uint16_t pentatonicTable[432] = {
10943, 7, 10940, 10946, 1, 610, 237, 2, 10804, 59, 8, 10571, 34, 10338, 17, 9961, 9351, 55, 10943, 25, 38, 470, 2444, 237, 4041,
10943, 7, 10940, 12, 10927, 3, 847, 1457, 10914, 5, 4041, 1, 610, 10715, 2, 10946, 10571, 34, 12, 237, 3, 847, 1457, 10914, 5, 9351, 55, 10943, 25, 38, 470, 2444, 237, 8,
5, 0, 35, 144, 93, 8364, 6767, 9, 233, 377, 1597, 2584, 987, 4181, 6765, 10945, 89, 13, 10859, 21, 10893, 2, 10804, 59, 8, 10571, 34, 10338, 17, 9961, 9351, 55, 10943, 25, 38, 470, 2444, 237, 1,
10338, 17, 9961, 25, 2, 10804, 59, 8, 10935, 13, 10859, 21, 10893, 144, 93, 8364, 6767, 9, 233, 377, 1597, 2584, 987, 4181, 6765, 10945, 89, 2, 10943, 7, 10940, 10946, 1, 610, 237,
237, 7, 4041, 12, 377, 3, 847, 1457, 0, 5, 41870, 1, 610, 10715, 2, 0, 10571, 34, 12, 0, 3, 847, 1457, 1, 5, 9351, 55, 10943, 25, 38, 470, 2444, 237, 4041, 2444, 237, 4041,
5, 610, 21, 144, 93, 8364, 6767, 9, 233, 377, 1597, 2584, 987, 4181, 6765, 10945, 89, 13, 10859, 21, 10893, 8, 10571, 34, 10338, 17, 9961, 9351, 55, 10943, 25, 38, 470,
13, 17, 9961, 25, 2, 5, 59, 8, 10935, 13, 10859, 21, 10893, 144, 93, 8, 6767, 9, 233, 377, 1597, 2584, 987, 4181, 6765, 10945, 89, 2, 10943, 7, 10940, 10946, 1, 610, 237, 2, 10804, 59,
10943, 7, 10940, 12, 10927, 3, 847, 1457, 10914, 5, 4041, 1, 610, 10715, 2, 10946, 10571, 34, 12, 237, 3, 847, 1457, 10914, 5, 9351, 55, 10943, 25, 38, 470, 2444, 237, 4041,
5, 0, 35, 144, 93, 34, 12, 0, 3, 847, 1457, 1, 5, 9351, 55, 10943, 25, 38, 470, 2444, 237, 4041, 34, 10338, 17, 9961, 9351, 55, 10943, 25, 38, 470, 2444, 237, 4041, 13, 10859, 21, 10893,
5, 610, 21, 144, 93, 8364, 6767, 9, 233, 377, 1597, 2584, 987, 4181, 6765, 10945, 89, 13, 10859, 21, 10893, 8364, 6767, 9, 233, 377, 1597, 2584, 987, 4181, 6765, 10945, 89,
10338, 17, 9961, 25, 2, 10804, 59, 8, 10935, 13, 10859, 21, 10893, 144, 93, 8364, 6767, 9, 233, 377, 1597, 2584, 987, 4181, 6765, 10945, 89, 2, 10943, 7, 10940, 10946, 1, 610, 237, 2, 10804, 59, 8, 10571,
237, 7, 4041, 12, 377, 3, 847, 1457, 0, 5, 41870, 10571, 13, 17, 9961, 25, 2, 5, 59, 8, 10935, 13, 10859, 21, 10893, 144, 93, 8, 6767, 9, 233, 377, 1597, 2584, 987, 4181, 6765, 10945, 89, 2, 1, 610, 10715, 2, 0
};
uint16_t mapPentatonic(uint16_t input) {
uint8_t value = (1023 - input) / (1024 / 431);
return (pentatonicTable[value]);
}
void audioOn() {
#if defined(__AVR_ATmega8__)
// ATmega8 has different registers
TCCR2 = _BV(WGM20) | _BV(COM21) | _BV(CS20);
TIMSK = _BV(TOIE2);
#elif defined(__AVR_ATmega1280__)
TCCR3A = _BV(COM3C1) | _BV(WGM30);
TCCR3B = _BV(CS30);
TIMSK3 = _BV(TOIE3);
#else
// Set up PWM to 31.25kHz, phase accurate
TCCR2A = _BV(COM2B1) | _BV(WGM20);
TCCR2B = _BV(CS20);
TIMSK2 = _BV(TOIE2);
#endif
}
int randNumber1;
int randtempo;
int randtempo1;
int tempo;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int retraso;
unsigned long tEspera = 0;
void setup() {
pinMode(PWM_PIN, OUTPUT);
audioOn();
pinMode(LED_PIN, OUTPUT);
}
float p = -PI;
float q = -PI;
float r = -PI;
float s = -PI;
float t = -PI;
void loop() {
int amplitud = analogRead(SYNC_CONTROL);
syncPhaseInc = mapPentatonic((sin(p) * amplitud/2) + 512); // lee el analog read
grainPhaseInc = mapPhaseInc(analogRead(GRAIN_FREQ_CONTROL) + sin(q) * 4) / 2;
grainDecay = (analogRead(GRAIN_DECAY_CONTROL) + sin(r) * 4) / 8 ;
grain2PhaseInc = ( (analogRead(GRAIN2_FREQ_CONTROL) + sin(s) * 4 ) / 2);
grain2PhaseInc += sin(s); // hace una oscilacion sobre la senal.
grain2Decay = (analogRead(GRAIN2_DECAY_CONTROL) + sin(t) * 4) / 4;
grain2Decay += sin(t) * 2; // hace una oscilacion sobre la senal.
if (millis() - tEspera >50) {
tEspera = millis();
t += 0.001; // amplitud
if (t > PI) {
r = -PI;
}
p += 0.0001; // amplitud
if (p > PI) {
p = -PI;
}
q += 0.001; // amplitud
if (q > PI) {
q = -PI;
}
r += 0.001; // amplitud
if (r > PI) {
r = -PI;
}
s += 0.001; // amplitud
if (s > PI) {
s = -PI;
}
}
}
SIGNAL(PWM_INTERRUPT)
{
uint8_t value;
uint16_t output;
syncPhaseAcc += syncPhaseInc;
if (syncPhaseAcc < syncPhaseInc) {
// Time to start the next grain
grainPhaseAcc = 0;
grainAmp = 0x7fff;
grain2PhaseAcc = 0;
grain2Amp = 0x7fff;
LED_PORT ^= 1 << LED_BIT; // Faster than using digitalWrite
}
// Increment the phase of the grain oscillators
grainPhaseAcc += grainPhaseInc;
grain2PhaseAcc += grain2PhaseInc;
// Convert phase into a triangle wave
value = (grainPhaseAcc >> 7) & 0xff;
if (grainPhaseAcc & 0x8000) value = ~value;
// Multiply by current grain amplitude to get sample
output = value * (grainAmp >> 8);
// Repeat for second grain
value = (grain2PhaseAcc >> 7) & 0xff;
if (grain2PhaseAcc & 0x8000) value = ~value;
output += value * (grain2Amp >> 8);
// Make the grain amplitudes decay by a factor every sample (exponential decay)
grainAmp -= (grainAmp >> 8) * grainDecay;
grain2Amp -= (grain2Amp >> 8) * grain2Decay;
// Scale output to the available range, clipping if necessary
output >>= 9;
if (output > 255) output = 255;
// Output to PWM (this is faster than using analogWrite)
PWM_VALUE = output;
}