-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo_intr.c
306 lines (297 loc) · 8.99 KB
/
echo_intr.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
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
// echo_intr.c
#include "audio.h"
volatile int16_t audio_chR=0;
volatile int16_t audio_chL=0;
static int emode = 0u;
static int pflag = 0u;
static int ButtonPressCheckFlag = 1;
#define DELAY_BUF_SIZE 16000
#define DELAY_BUF_FLANG 800
#define DELAY_BUF_SB 2000
#define DELAY_BUF_CH1 2500
#define DELAY_BUF_CH2 3000
#define GAIN 0.6f
#define LOOP_SIZE 1024
double sine_table[LOOP_SIZE] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,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={1, 1, 1, 1, 1, 1, 0.5, 0.5, 0.5};
static int sine_ptr = 0;
int16_t buffer[DELAY_BUF_SIZE];
int16_t buffer2[DELAY_BUF_SB];
int16_t buffer3[DELAY_BUF_CH1];
int16_t buffer4[DELAY_BUF_CH2];
int16_t buf_ptr = 0;
int16_t buf_ptr2 = 0;
int16_t buf_ptr3 = 0;
int16_t buf_ptr4 = 0;
void I2S_HANDLER(void) {
int16_t delayed_sample;
int16_t audio_out_chL = 0;
int16_t audio_out_chR = 0;
int flangerflag = 0 ;
float vargain=0.1;
audio_IN = i2s_rx();
audio_chL = (audio_IN & 0x0000FFFF);
audio_chR = ((audio_IN >>16)& 0x0000FFFF);
//delayed_sample = buffer[buf_ptr];
switch(emode)
{
case 0: // Echo
delayed_sample = buffer[buf_ptr];
audio_out_chL = delayed_sample + audio_chL;
buffer[buf_ptr] = audio_chL + delayed_sample*GAIN;
buf_ptr = (buf_ptr+1)%DELAY_BUF_SIZE;
audio_OUT = ((audio_chR<<16 & 0xFFFF0000)) + (audio_out_chL& 0x0000FFFF);
break;
case 1: // Flanger
/*if(flangerflag==0)
{
flangerflag = 1;
delayed_sample = buffer[buf_ptr];
audio_out_chL = delayed_sample + audio_chL;
buffer[buf_ptr] = audio_chL + delayed_sample;
buf_ptr = (buf_ptr+1)%DELAY_BUF_SB;
//audio_OUT = ((audio_chR<<16 & 0xFFFF0000)) + (audio_out_chL& 0x0000FFFF);
}
else
{
flangerflag = 0;
delayed_sample = buffer2[buf_ptr2];
audio_out_chL = delayed_sample + audio_chL;
buffer2[buf_ptr2] = audio_chL + delayed_sample*GAIN;
buf_ptr2 = (buf_ptr2+1)%DELAY_BUF_FLANG;
//audio_OUT = ((audio_chR<<16 & 0xFFFF0000)) + (audio_out_chL& 0x0000FFFF);
} */
delayed_sample = buffer[buf_ptr];
audio_out_chL = delayed_sample + audio_chL;
buffer[buf_ptr] = audio_chL + delayed_sample*GAIN;
buf_ptr = (buf_ptr+1)%DELAY_BUF_SIZE;
audio_OUT = ((audio_chR<<16 & 0xFFFF0000)) + (audio_out_chL& 0x0000FFFF);
break;
case 2: // Tremelo
delayed_sample = buffer[buf_ptr];
audio_out_chL = delayed_sample*sine_table[sine_ptr];
audio_chR = delayed_sample*sine_table[sine_ptr];
buffer[buf_ptr] = audio_chL ;
buf_ptr = (buf_ptr+1)%DELAY_BUF_SIZE;
//--audio_out_chL = audio_chL*vargain;
//--vargain= vargain+2;
sine_ptr = (sine_ptr+1) % LOOP_SIZE;
audio_OUT = ((audio_chR<<16 & 0xFFFF0000)) + (audio_out_chL& 0x0000FFFF);
//if (vargain>=10)
//{ vargain =0.1;}
break;
case 3: //slapback
delayed_sample = buffer2[buf_ptr2];
audio_out_chL = delayed_sample + audio_chL;
buffer2[buf_ptr2] = audio_chL + delayed_sample*0.4;
buf_ptr2 = (buf_ptr2+1)%DELAY_BUF_CH1;
audio_OUT = ((audio_chR<<16 & 0xFFFF0000)) + (audio_out_chL& 0x0000FFFF);
break;
case 4: //Chorus Audio
delayed_sample = buffer2[buf_ptr2]+buffer3[buf_ptr3];
audio_out_chL = delayed_sample + audio_chL;
buffer2[buf_ptr2] = audio_chL + delayed_sample*0.4;
buffer3[buf_ptr3] = audio_chL + delayed_sample*0.4;
buf_ptr2 = (buf_ptr2+1)%DELAY_BUF_SB;
buf_ptr3 = (buf_ptr3+1)%DELAY_BUF_CH1;
audio_OUT = ((audio_chR<<16 & 0xFFFF0000)) + (audio_out_chL& 0x0000FFFF);
break;
case 5: //Pan Audio
if(pflag==0u) {
delayed_sample = buffer[buf_ptr];
audio_out_chL = 0.1*delayed_sample;
audio_out_chR = 1.5*delayed_sample;
buffer[buf_ptr] = audio_chL;
buf_ptr = (buf_ptr+1)%DELAY_BUF_SIZE;
audio_OUT = ((audio_out_chR<<16 & 0xFFFF0000)) + (audio_out_chL& 0x00000000);
pflag =1u;
}
else if(pflag == 1u){
delayed_sample = buffer[buf_ptr];
audio_out_chL = delayed_sample;
buffer[buf_ptr] = audio_chL;
buf_ptr = (buf_ptr+1)%DELAY_BUF_SIZE;
audio_OUT = ((audio_out_chR<<16 & 0xFFFF0000)) + (audio_out_chL& 0x0000FFFF);
pflag = 2u;
}
else if(pflag ==2u){
//Pan Audio 2
delayed_sample = buffer[buf_ptr];
audio_out_chL = 1.5*delayed_sample;
audio_out_chR = 0.1*delayed_sample;
buffer[buf_ptr] = audio_chL;
buf_ptr = (buf_ptr+1)%DELAY_BUF_SIZE;
audio_OUT = ((audio_out_chR<<16 & 0x00000000)) + (audio_out_chL& 0x0000FFFF);
pflag =0u;
}
break;
case 7: //Reverb
delayed_sample = buffer[buf_ptr]+buffer2[buf_ptr2];
audio_out_chL = delayed_sample + audio_chL;
buffer[buf_ptr] = (audio_chL + delayed_sample*GAIN);
buf_ptr = (buf_ptr+1)%DELAY_BUF_SIZE;
buffer2[buf_ptr2] = delayed_sample*0;
buf_ptr2 = (buf_ptr2+1)%DELAY_BUF_SB;
audio_OUT = (((audio_chR)<<16 & 0xFFFF0000)) + ((audio_out_chL) & 0x0000FFFF);
break;
}
i2s_tx(audio_OUT);
}
int main(void)
{
audio_init ( hz48000, dmic_in, intr, I2S_HANDLER);
gpio_set_mode(BUTTON,Input );
gpio_set_mode(LEDR,Output );
gpio_set_mode(LEDG,Output );
gpio_set_mode(LEDB,Output );
while(1){
switch (emode)
{
case 0 : // Echo
gpio_set(LEDR,1u);
if (!gpio_get(BUTTON))
{
if (1 == ButtonPressCheckFlag)
{
ButtonPressCheckFlag = 0;
emode =1;
}
else {ButtonPressCheckFlag = 1;}
}
break;
case 1: // Flanger
gpio_set(LEDG,1u);
if (!gpio_get(BUTTON))
{
if (1 == ButtonPressCheckFlag)
{
ButtonPressCheckFlag = 0;
emode =2;
}
else {ButtonPressCheckFlag = 1;}
}
delay_ms(1000*sine_table[sine_ptr]);
break;
case 2: // Tremelo
gpio_set(LEDB,1u);
if (!gpio_get(BUTTON))
{
if (1 == ButtonPressCheckFlag)
{
ButtonPressCheckFlag = 0;
emode =3;
}
else {ButtonPressCheckFlag = 1;}
}
break;
case 3: //slapback
if (!gpio_get(BUTTON))
{
if (1 == ButtonPressCheckFlag)
{
ButtonPressCheckFlag = 0;
emode =4;
}
else {ButtonPressCheckFlag = 1;}
}
break;
case 4: //Chorus
if (!gpio_get(BUTTON))
{
if (1 == ButtonPressCheckFlag)
{
ButtonPressCheckFlag = 0;
emode =5;
}
else {ButtonPressCheckFlag = 1;}
}
delay_cycles(3);
break;
case 5: //Pan Audio 1
if (!gpio_get(BUTTON))
{
if (1 == ButtonPressCheckFlag)
{
ButtonPressCheckFlag = 0;
emode =6;
}
else {ButtonPressCheckFlag = 1;}
}
break;
case 6: //Pan Audio 2
if (!gpio_get(BUTTON))
{
if (1 == ButtonPressCheckFlag)
{
ButtonPressCheckFlag = 0;
emode =7;
}
else {ButtonPressCheckFlag = 1;}
}
break;
case 7: //Reverb
if (!gpio_get(BUTTON))
{
if (1 == ButtonPressCheckFlag)
{
ButtonPressCheckFlag = 0;
emode =0;
}
else {ButtonPressCheckFlag = 1;}
}
break;
}
}
}