-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
165 lines (142 loc) · 4.63 KB
/
main.c
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
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <math.h>
#define nop() __asm__ __volatile__("nop"::)
#define SET_BIT(port, bit) port |= (uint8_t)(1 << (bit))
#define CLEAR_BIT(port, bit) port &= (uint8_t)(~(1 << (bit)))
uint8_t rand() {
static unsigned int val;
val = val * 1103515245U + 12345U;
return val >> 8;
}
uint8_t adc_read(uint8_t channel) {
ADMUX = channel | (1 << ADLAR);
ADCSRA |= (1 << ADSC);
while((ADCSRA & (1 << ADSC)))
;
return ADCH;
}
#define PITCH_PORT ((0 << MUX3) | (0 << MUX2) | (0 << MUX1) | (0 << MUX0))
#define TEMPO_PORT ((0 << MUX3) | (0 << MUX2) | (0 << MUX1) | (1 << MUX0))
#define ARPEG_PORT ((0 << MUX3) | (0 << MUX2) | (1 << MUX1) | (0 << MUX0))
#define TEMPO_LED PORTB1
struct Oscillator {
uint16_t value;
uint16_t frequencyControl;
uint8_t volume;
uint8_t overflowCount;
uint8_t *pacTable;
};
#define OSCILLATOR_COUNT 2
struct Oscillator oscillators[OSCILLATOR_COUNT];
ISR(TIMER0_OVF_vect) {
uint16_t sum = 0;
oscillators[0].value += oscillators[0].frequencyControl;
sum += oscillators[0].pacTable[oscillators[0].value >> 8] *
oscillators[0].volume;
uint16_t oldValue = oscillators[1].value;
oscillators[1].value += oscillators[1].frequencyControl;
if ((oscillators[1].value ^ oldValue) & 0x1000)
oscillators[1].overflowCount++;
sum += oscillators[1].pacTable[oscillators[1].overflowCount] *
oscillators[1].volume;
sum /= OSCILLATOR_COUNT;
sum >>= 7;
if (sum > 255) sum = 255;
OCR0A = sum;
}
uint8_t randomTable[256];
uint8_t sinTable[256];
#define LOOP_LENGTH 128
uint8_t loop[LOOP_LENGTH * 2];
int main() {
wdt_disable();
for (int i = 0 ; i < OSCILLATOR_COUNT ; ++i) {
oscillators[i].value = 0;
oscillators[i].frequencyControl = 0;
oscillators[i].volume = 0x7f;
oscillators[i].overflowCount = 0;
}
for (int i = 0 ; i < 256 ; i += 2) {
float u1 = rand() / 255.0f;
float u2 = rand() / 255.0f;
// Box-Muller transform
float nat1 = sqrtf(-2 * log(u1)) * cosf(2 * M_PI * u2);
float nat2 = sqrtf(-2 * log(u1)) * sinf(2 * M_PI * u2);
int val1 = nat1 * 64 + 127;
if (val1 < 0) val1 = 0;
if (val1 > 255) val1 = 255;
int val2 = nat2 * 64 + 127;
if (val2 < 0) val2 = 0;
if (val2 > 255) val2 = 255;
randomTable[i] = val1;
randomTable[i + 1] = val2;
}
oscillators[1].pacTable = randomTable;
for (int i = 0 ; i < 256 ; ++i) {
sinTable[i] = (uint8_t)(sinf(i * M_PI / 128.0f) * 127.0f + 127.0f);
}
oscillators[0].pacTable = sinTable;
DDRB = 0xff;
DDRD = (1 << DDD6);
PORTB = 0xff;
// Set pins connected to buttons as inputs
CLEAR_BIT(DDRD, DDD0);
CLEAR_BIT(DDRD, DDD1);
// Enable internal pull-up resistors on buttons
SET_BIT(PORTD, PORTD0);
SET_BIT(PORTD, PORTD1);
// Disable digital input buffers on ADC channels.
DIDR0 = (1 << ADC2D) | (1 << ADC1D) | (1 << ADC0D);
// Enable ADC
ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
// Clock 0, output A in fast PWM mode
TCCR0A = (1 << COM0A1) | (0 << COM0A0) | (0 << COM0B1) | (0 << COM0B0) |
(1 << WGM01) | (1 << WGM00);
// continued, set clock on, no prescaling
TCCR0B = (0 << WGM02) | (0 << CS02) | (0 << CS01) || (1 << CS00);
// Start at 50% cycle
OCR0A = 0x7f;
TIFR0 = TIFR0;
// Enable counter overflow interrupt
TIMSK0 = (0 << OCIE0B) || (0 << OCIE0A) || (1 << TOIE0);
sei();
int loopPosition = 0;
while (1) {
if (loopPosition == 0)
PORTB = (0 << PORTB2) | (0 << PORTB1) | (0 << PORTB0);
else if (loopPosition == LOOP_LENGTH / 4)
PORTB = (1 << PORTB2) | (1 << PORTB1) | (0 << PORTB0);
else if (loopPosition == LOOP_LENGTH / 2)
PORTB = (1 << PORTB2) | (1 << PORTB1) | (0 << PORTB0);
else if (loopPosition == LOOP_LENGTH / 2 + LOOP_LENGTH / 4)
PORTB = (1 << PORTB2) | (1 << PORTB1) | (0 << PORTB0);
else
PORTB = (1 << PORTB2) | (1 << PORTB1) | (1 << PORTB0);
uint8_t pitch = adc_read(PITCH_PORT);
uint8_t arpeg = adc_read(ARPEG_PORT);
if (!(PIND & (1 << PIND0))) {
if (loopPosition & 1) {
loop[loopPosition] = (pitch >> 1) + (arpeg >> 2) + 32;
}
else {
loop[loopPosition] = (pitch >> 1) + 32;
}
}
if (!(PIND & (1 << PIND1))) {
loop[loopPosition + LOOP_LENGTH] = pitch;
}
oscillators[0].frequencyControl = loop[loopPosition] << 4;
oscillators[1].frequencyControl = loop[loopPosition + LOOP_LENGTH] << 4;
uint8_t tempo = adc_read(TEMPO_PORT);
for (uint16_t i = 0 ; i < 65535 / (tempo + 16) ; ++i) {
for (int j = 0 ; j < 128 ; ++j) {
nop();
}
}
++loopPosition;
if (loopPosition == LOOP_LENGTH) loopPosition = 0;
}
}