-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.c
64 lines (57 loc) · 3.84 KB
/
audio.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
#include "audio.h"
#include <avr/pgmspace.h>
//const uint8_t dac_out_lut[256] /*__attribute__((aligned(256)))*/ PROGMEM = {0, 0, 0, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 21, 21, 22, 22, 23, 23, 24, 25, 25, 26, 26, 27, 27, 28, 29, 29, 30, 30, 31, 32, 32, 33, 34, 34, 35, 36, 36, 37, 37, 38, 39, 39, 40, 41, 41, 42, 43, 44, 44, 45, 46, 46, 47, 48, 48, 49, 50, 51, 51, 52, 53, 54, 54, 55, 56, 57, 57, 58, 59, 60, 60, 61, 62, 63, 64, 65, 65, 66, 67, 68, 69, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 120, 121, 122, 123, 124, 125, 127, 128, 129, 130, 131, 132, 134, 135, 136, 137, 139, 140, 141, 142, 144, 145, 146, 148, 149, 150, 152, 153, 154, 156, 157, 158, 160, 161, 162, 164, 165, 167, 168, 169, 171, 172, 174, 175, 177, 178, 180, 181, 183, 184, 186, 187, 189, 190, 192, 193, 195, 197, 198, 200, 201, 203, 205, 206, 208, 210, 211, 213, 215, 216, 218, 220, 222, 223, 225, 227, 229, 230, 232, 234, 236, 238, 239, 241, 243, 245, 247, 249, 251, 253, 255};
const int8_t sin_lut[256] /*__attribute__((aligned(256)))*/ PROGMEM = {0,3,6,9,12,15,18,21,24,28,31,34,37,40,43,46,48,51,54,57,60,63,65,68,71,73,76,78,81,83,85,88,90,92,94,96,98,100,102,104,106,108,109,111,112,114,115,117,118,119,120,121,122,123,124,124,125,126,126,127,127,127,127,127,127,127,127,127,127,127,126,126,125,124,124,123,122,121,120,119,118,117,115,114,112,111,109,108,106,104,102,100,98,96,94,92,90,88,85,83,81,78,76,73,71,68,65,63,60,57,54,51,48,46,43,40,37,34,31,28,24,21,18,15,12,9,6,3,0,-3,-6,-9,-12,-15,-18,-21,-24,-28,-31,-34,-37,-40,-43,-46,-48,-51,-54,-57,-60,-63,-65,-68,-71,-73,-76,-78,-81,-83,-85,-88,-90,-92,-94,-96,-98,-100,-102,-104,-106,-108,-109,-111,-112,-114,-115,-117,-118,-119,-120,-121,-122,-123,-124,-124,-125,-126,-126,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-127,-126,-126,-125,-124,-124,-123,-122,-121,-120,-119,-118,-117,-115,-114,-112,-111,-109,-108,-106,-104,-102,-100,-98,-96,-94,-92,-90,-88,-85,-83,-81,-78,-76,-73,-71,-68,-65,-63,-60,-57,-54,-51,-48,-46,-43,-40,-37,-34,-31,-28,-24,-21,-18,-15,-12,-9,-6,-3};
volatile uint8_t pcm_a[256], pcm_b[256];
volatile uint8_t dac_out_scale = 255;
volatile uint8_t dac_out_val = 0;
volatile struct channel_t channels[NUM_CHANNELS];
void audio_init() {
cli();
GPIOR0 = 93, GPIOR1 = 13;
PORTA.DIRSET = (1 << 6) | (1 << 5);
PORTA.OUTSET = 1 << 6;
DAC0.CTRLA = 1 | (1 << 6);
PORTA.DIRSET = 1 << 5;
for(int i = 0; i < NUM_CHANNELS; i++) {
channels[i].wave = 0;
channels[i].amplitude = 0;
}
for(int i = 0; i < 256; i++) {
if(i < 128) pcm_a[i] = i * 2;
else pcm_a[i] = 255;
uint8_t t = i + 64;
pcm_b[i] = 0;
if(i < 128) pcm_b[i] += 127;
if(t < 128) pcm_b[i] += 127;
}
audio_dac_vref(DACREF_0V55);
audio_mute(1);
TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV8_gc | TCA_SINGLE_ENABLE_bm;
TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm;
TCA0.SINGLE.CTRLB = TCA_SINGLE_WGMODE_NORMAL_gc;
TCA0.SINGLE.EVCTRL &= ~(TCA_SINGLE_CNTEI_bm);
TCA0.SINGLE.PER = INTERRUPT_PER(SAMPLE_RATE, 8, F_CPU);
sei();
}
void audio_mute(uint8_t mute) {
if(mute) PORTA.OUTCLR = 1 << 5;
else PORTA.OUTSET = 1 << 5;
}
void audio_dac_vref(dac_vref_t vref) {
switch(vref) {
case DACREF_0V55:
VREF_CTRLA = VREF_DAC0REFSEL_0V55_gc;
break;
case DACREF_1V5:
VREF_CTRLA = VREF_DAC0REFSEL_1V5_gc;
break;
case DACREF_2V5:
VREF_CTRLA = VREF_DAC0REFSEL_2V5_gc;
break;
case DACREF_4V34:
VREF_CTRLA = VREF_DAC0REFSEL_4V34_gc;
break;
}
VREF_CTRLB |= VREF_DAC0REFEN_bm;
}