-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdspD8_v1-5.ino
287 lines (241 loc) · 7.03 KB
/
dspD8_v1-5.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
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "tones.h"
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
// Standard Arduino Pins
#define digitalPinToPortReg(P) \
(((P) >= 0 && (P) <= 7) ? &PORTD : (((P) >= 8 && (P) <= 13) ? &PORTB : &PORTC))
#define digitalPinToDDRReg(P) \
(((P) >= 0 && (P) <= 7) ? &DDRD : (((P) >= 8 && (P) <= 13) ? &DDRB : &DDRC))
#define digitalPinToPINReg(P) \
(((P) >= 0 && (P) <= 7) ? &PIND : (((P) >= 8 && (P) <= 13) ? &PINB : &PINC))
#define digitalPinToBit(P) \
(((P) >= 0 && (P) <= 7) ? (P) : (((P) >= 8 && (P) <= 13) ? (P) - 8 : (P) - 14))
#define digitalReadFast(P) bitRead(*digitalPinToPINReg(P), digitalPinToBit(P))
#define digitalWriteFast(P, V) bitWrite(*digitalPinToPortReg(P), digitalPinToBit(P), (V))
const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
//--------- Ringbuf parameters ----------
uint8_t Ringbuffer[256];
uint8_t RingWrite = 0;
uint8_t RingRead = 0;
volatile uint8_t RingCount = 0;
const int LFilterPin = A4;
const int VolPin = A3;
float vol = 1.0;
float alpha = 0.1;
int16_t filteredOutput = 0;
//-----------------------------------------
ISR(TIMER1_COMPA_vect)
{
//------------------- Ringbuffer handler -------------------------
if (RingCount)
{ // If entry in FIFO..
int16_t rawOutput = Ringbuffer[RingRead++];
//filteredOutput = (alpha * rawOutput) + ((1 - alpha) * filteredOutput);
//amplifiedOutput = filteredOutput * vol;
OCR2A = rawOutput * 0.5; // Output LSB of 16-bit DAC
//uncomment this for the amplified out
//OCR2A = amplifiedOutput;
RingCount--;
}
//-----------------------------------------------------------------
}
// Define the array of arrays
const uint8_t *const Tones[8] PROGMEM = {tone_0, tone_1, tone_2, tone_3, tone_4, tone_5, tone_6, tone_7};
int buttonPins[8] = {2, 3, 4, 5, 6, 7, 8, 9}; // or byte?
bool currentButtonStates[8] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
bool lastButtonStates[8] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
const int pitchPin = A0;
uint8_t phaccs[8];
uint8_t pitches[8] = {1, 1, 1, 1, 1, 1, 1, 1};
uint16_t samplelens[8] = {1014, 1014, 1014, 1014, 1014, 1014, 1014, 1014};
uint16_t samplecnts[8];
uint16_t samplepnts[8];
uint8_t oldPORTB;
uint8_t oldPORTD;
int16_t total;
uint8_t divider;
uint8_t MUX = 0;
char ser = ' ';
void setup()
{
Serial.begin(9600);
OSCCAL = 0xFF;
// Drumtrigger inputs
for (int i = 0; i < 8; i++)
{ // byte
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(pitchPin, INPUT);
// pinMode(LFilterPin, INPUT);
// 8-bit PWM DAC pin
pinMode(11, OUTPUT);
// Set up Timer 1 to send a sample every interrupt.
cli();
// Set CTC mode
// Have to set OCR1A *after*, otherwise it gets reset to 0!
TCCR1B = (TCCR1B & ~_BV(WGM13)) | _BV(WGM12);
TCCR1A = TCCR1A & ~(_BV(WGM11) | _BV(WGM10));
// No prescaler
TCCR1B = (TCCR1B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10);
// Set the compare register (OCR1A).
// OCR1A is a 16-bit register, so we have to do this with
// interrupts disabled to be safe.
// OCR1A = F_CPU / SAMPLE_RATE;
// Enable interrupt when TCNT1 == OCR1A
TIMSK1 |= _BV(OCIE1A);
OCR1A = 400; // 40KHz Samplefreq
// Set up Timer 2 to do pulse width modulation on D11
// Use internal clock (datasheet p.160)
ASSR &= ~(_BV(EXCLK) | _BV(AS2));
// Set fast PWM mode (p.157)
TCCR2A |= _BV(WGM21) | _BV(WGM20);
TCCR2B &= ~_BV(WGM22);
// Do non-inverting PWM on pin OC2A (p.155)
// On the Arduino this is pin 11.
TCCR2A = (TCCR2A | _BV(COM2A1)) & ~_BV(COM2A0);
TCCR2A &= ~(_BV(COM2B1) | _BV(COM2B0));
// No prescaler (p.158)
TCCR2B = (TCCR2B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10);
// Set initial pulse width to the first sample.
OCR2A = 128;
// set timer0 interrupt at 61Hz
TCCR0A = 0; // set entire TCCR0A register to 0
TCCR0B = 0; // same for TCCR0B
TCNT0 = 0; // initialize counter value to 0
// set compare match register for 62hz increments
OCR0A = 255; // = 61Hz
// turn on CTC mode
TCCR0A |= (1 << WGM01);
// Set CS01 and CS00 bits for prescaler 1024
TCCR0B |= (1 << CS02) | (0 << CS01) | (1 << CS00); // 1024 prescaler
TIMSK0 = 0;
// set up the ADC
ADCSRA &= ~PS_128; // remove bits set by Arduino library
// Choose prescaler PS_128.
ADCSRA |= PS_128;
ADMUX = 64;
sbi(ADCSRA, ADSC);
sei();
}
void loop()
{
// int value = analogRead(LFilterPin);
// alpha = map(value, 0, 1023, 1, 99) / 100.0;
//------ Add current sample word to ringbuffer FIFO --------------------
for (int i = 0; i < 8; i++)
{
if (currentButtonStates[0] == LOW)
{
//Serial.print(0);
Serial.println(" pressed");}
}
if (RingCount < 255)
{ // if space in ringbuffer
total = 0;
// we can't wrap the following a for loop, for some reason when we do the samples get corrupted, or we have side-effects/compiler optimising issues
if (currentButtonStates[0] == LOW)
{
phaccs[0] += pitches[0];
if (phaccs[0] & 128)
{
phaccs[0] &= 127;
samplepnts[0]++;
}
total += (pgm_read_byte_near(Tones[0] + samplepnts[0]) - 128);
}
if (currentButtonStates[1] == LOW)
{
phaccs[1] += pitches[1];
if (phaccs[1] & 128)
{
phaccs[1] &= 127;
samplepnts[1]++;
}
total += (pgm_read_byte_near(Tones[1] + samplepnts[1]) - 128);
}
if (currentButtonStates[2] == LOW)
{
phaccs[2] += pitches[2];
if (phaccs[2] & 128)
{
phaccs[2] &= 127;
samplepnts[2]++;
}
total += (pgm_read_byte_near(Tones[2] + samplepnts[2]) - 128);
}
if (currentButtonStates[3] == LOW)
{
phaccs[3] += pitches[3];
if (phaccs[3] & 128)
{
phaccs[3] &= 127;
samplepnts[3]++;
}
total += (pgm_read_byte_near(Tones[3] + samplepnts[3]) - 128);
}
if (currentButtonStates[4] == LOW)
{
phaccs[4] += pitches[4];
if (phaccs[4] & 128)
{
phaccs[4] &= 127;
samplepnts[4]++;
}
total += (pgm_read_byte_near(Tones[4] + samplepnts[4]) - 128);
}
if (currentButtonStates[5] == LOW)
{
phaccs[5] += pitches[5];
if (phaccs[5] & 128)
{
phaccs[5] &= 127;
samplepnts[5]++;
}
total += (pgm_read_byte_near(Tones[5] + samplepnts[5]) - 128);
}
if (currentButtonStates[6] == LOW)
{
phaccs[6] += pitches[6];
if (phaccs[6] & 128)
{
phaccs[6] &= 127;
samplepnts[6]++;
}
total += (pgm_read_byte_near(Tones[6] + samplepnts[6]) - 128);
}
if (currentButtonStates[7] == LOW)
{
phaccs[7] += pitches[7];
if (phaccs[7] & 128)
{
phaccs[7] &= 127;
samplepnts[7]++;
}
total += (pgm_read_byte_near(Tones[7] + samplepnts[7]) - 128);
}
total >>= 1;
if (!(PINB & 4))
total >>= 1;
total += 128;
if (total > 255)
total = 255;
for (int i = 0; i < 8; i++) {
currentButtonStates[i] = digitalRead(buttonPins[i]);}
// if (lastButtonStates[i] == HIGH && currentButtonStates[i] == LOW) {
// trigger(i);
// }
// lastButtonStates[i] = currentButtonStates[i];
// }
cli();
Ringbuffer[RingWrite] = total;
RingWrite++;
RingCount++;
sei();
}}