-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
592 lines (495 loc) · 17.2 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#include "..\h\p33FJ256GP506.h"
#include "..\h\WM8510CodecDrv.h"
#include "..\h\sask.h"
#include "..\h\SFMDrv.h"
#include "..\h\G711.h"
_FGS(GWRP_OFF & GCP_OFF);
_FOSCSEL(FNOSC_FRC);
_FOSC(FCKSM_CSECMD & OSCIOFNC_ON & POSCMD_NONE);
_FWDT(FWDTEN_OFF);
/* FRAME_SIZE - Size of each audio frame
* SPEECH_SEGMENT_SIZE - Size of intro speech segment
* WRITE_START_ADDRESS - Serial Flash Memory write address
* */
#define FRAME_SIZE 128
#define SPEECH_SEGMENT_SIZE 98049L
#define WRITE_START_ADDRESS 0x20000
#define PI 3.1416
/* Allocate memory for buffers and drivers
* codecBuffer - Buffer used by the codec driver
* samples - buffer contained raw audio data
* encodedSamples - buffer containing G.711 encoded data
* decodedSamples - buffer containing G.711 decoded datat
* flashMemoryBuffer - buffer used by the SFM driver
* */
int codecBuffer [WM8510DRV_DRV_BUFFER_SIZE];
int samples [FRAME_SIZE];
char encodedSamples [FRAME_SIZE];
int decodedSamples [FRAME_SIZE];
/* Instantiate the drivers structures
* and create handles.
* */
WM8510Handle codec;
WM8510Handle * codecHandle = &codec;
long address;
/* flags
* record - if set means recording
* playback - if set mean playback
* erasedBeforeRecord - means SFM eras complete before record
* */
int record;
int sign(double z){
int result;
if (z > 0) {
result = 1;
} else if (z == 0) {
result = 0;
} else {
result = -1;
}
return result;
}
int max_abs_int( int *arr ){
int abs_Mayor = 0;
int sampleIndex;
for (sampleIndex = 0; sampleIndex < FRAME_SIZE; sampleIndex++) {
int actual_abs = fabs(arr[sampleIndex]);
abs_Mayor = abs_Mayor < actual_abs ? actual_abs : abs_Mayor;
}
return abs_Mayor;
}
int max_abs_double( double arr[] ){
int abs_Mayor = 0;
int sampleIndex;
for (sampleIndex = 0; sampleIndex < FRAME_SIZE; sampleIndex++) {
int actual_abs = fabs(arr[sampleIndex]);
abs_Mayor = abs_Mayor < actual_abs ? actual_abs : abs_Mayor;
}
return abs_Mayor;
}
void fuzz_effect( int * decodedSamples){
double q[FRAME_SIZE], z[FRAME_SIZE], y[FRAME_SIZE];
int gain = 6;
float mix = 1;
int sampleIndex;
double maxAbsDecodedSample = max_abs_int(decodedSamples);
for(sampleIndex = 0; sampleIndex < FRAME_SIZE; sampleIndex++) {
q[sampleIndex] = decodedSamples[sampleIndex] * gain/maxAbsDecodedSample;
z[sampleIndex] = sign(-q[sampleIndex]) * (1- exp(sign(-q[sampleIndex]) * q[sampleIndex]));
}
double maxAbsZ = max_abs_double(z);
for(sampleIndex = 0; sampleIndex < FRAME_SIZE; sampleIndex++) {
y[sampleIndex] = mix*z[sampleIndex]*maxAbsDecodedSample/maxAbsZ + (1 -mix)*decodedSamples[sampleIndex];
}
double maxAbsY = max_abs_double(y);
for(sampleIndex = 0; sampleIndex < FRAME_SIZE; sampleIndex++) {
decodedSamples[sampleIndex] = y[sampleIndex]*maxAbsDecodedSample/maxAbsY;
}
}
double tremolo_effect2(double trem_triangular, int *ptr_subida, int *decodedSamples){
int sampleIndex;
double delta = 5e-4;
double maxf = 0.5;
double minf = -0.5;
for(sampleIndex = 0; sampleIndex < FRAME_SIZE ; sampleIndex++){
decodedSamples[sampleIndex] = trem_triangular*decodedSamples[sampleIndex];
if(*ptr_subida == 1){
trem_triangular += delta;
} else {
trem_triangular -= delta;
}
if(trem_triangular >= maxf){
*ptr_subida = 0;
} else if(trem_triangular <= minf){
*ptr_subida = 1;
}
}
return trem_triangular;
}
double wah_wah_effect(double wah_triangular, int *ptr_subida, int * decodedSamples, double *last_yb, double *last_yh, double *last_yl, int *first_time_wah_wah_function){
double minf = 2000;
double maxf = 5000;
double Fw = 2000;
double Fs = 44100;
double delta = 0.1;//Fw/Fs;//Fw/Fs=.045351-> (3000-500)/delta = 55125
double damp = 0.05;
double F1;
int sampleIndex;
double yh[FRAME_SIZE], yb[FRAME_SIZE], yl[FRAME_SIZE];
double Q1;
F1 = 2*sin(PI*wah_triangular/Fs);
Q1 = 2*damp;
if(*first_time_wah_wah_function == 1){
//**** INICIO CASO 1
//CASO DEL PRIMER VALOR DLE FRAME TOMA EL DEL FRAME ANTERIOR
yh[0] = decodedSamples[0];
yb[0] = F1*yh[0];
yl[0] = F1*yb[0];
//***END CASO 1
*first_time_wah_wah_function = 0;
} else {
yh[0] = decodedSamples[0] - (*last_yl) - Q1*(*last_yb);
yb[0] = F1*yh[0] + (*last_yb);
yl[0] = F1*yb[0] + (*last_yl);
}
if(*ptr_subida == 1){
wah_triangular += delta;
} else {
wah_triangular -= delta;
}
if(wah_triangular >= maxf){
*ptr_subida = 0;
} else if(wah_triangular <= minf){
*ptr_subida = 1;
}
for(sampleIndex = 1; sampleIndex < FRAME_SIZE ; sampleIndex++){
yh[sampleIndex] = decodedSamples[sampleIndex] - yl[sampleIndex-1] - Q1*yb[sampleIndex -1];
yb[sampleIndex] = F1*yh[sampleIndex] + yb[sampleIndex -1];
yl[sampleIndex] = F1*yb[sampleIndex] + yl[sampleIndex -1];
F1 = 2*sin(PI*wah_triangular/Fs);
if(*ptr_subida == 1){
wah_triangular += delta;
} else {
wah_triangular -= delta;
}
if(wah_triangular >= maxf){
*ptr_subida = 0;
} else if(wah_triangular <= minf){
*ptr_subida = 1;
}
}
double max_yb = max_abs_double(yb);
for(sampleIndex = 0; sampleIndex < FRAME_SIZE; sampleIndex++) {
decodedSamples[sampleIndex] = yb[sampleIndex];//max_yb;
//Revisar si es necesaria la asignacion del ultimo valor en last_y*
if(sampleIndex == FRAME_SIZE -1){
*last_yb=yb[sampleIndex];
*last_yh=yh[sampleIndex];
*last_yl=yl[sampleIndex];
}
}
return wah_triangular;
}
double phaser_effect(double phaser_triangular, int *ptr_subida, int * decodedSamples, double *last_yb, double *last_yh, double *last_yl, int *first_time_phaser_function){
double minf = 500;
double maxf = 2000;
double minf2= 5000;
double maxf2= 20000;
double Fw = 2000;
double Fs = 44100;
double delta = 0.16;//Fw/Fs;//Fw/Fs=.045351-> (3000-500)/delta = 55125
double damp = 0.05;
double F1;
int sampleIndex;
double yh[FRAME_SIZE], yb[FRAME_SIZE], yl[FRAME_SIZE];
double Q1;
F1 = 2*sin(PI*phaser_triangular/Fs);
Q1 = 2*damp;
if(*first_time_phaser_function == 1){
//**** INICIO CASO 1
//CASO DEL PRIMER VALOR DLE FRAME TOMA EL DEL FRAME ANTERIOR
yh[0] = decodedSamples[0];
yb[0] = F1*yh[0];
yl[0] = F1*yb[0];
//***END CASO 1
*first_time_phaser_function = 0;
} else {
yh[0] = decodedSamples[0] - (*last_yl) - Q1*(*last_yb);
yb[0] = F1*yh[0] + (*last_yb);
yl[0] = F1*yb[0] + (*last_yl);
}
if(*ptr_subida == 1){
phaser_triangular += delta;
} else {
phaser_triangular -= delta;
}
if(phaser_triangular >= maxf){
*ptr_subida = 0;
} else if(phaser_triangular <= minf){
*ptr_subida = 1;
}
for(sampleIndex = 1; sampleIndex < FRAME_SIZE ; sampleIndex++){
yh[sampleIndex] = decodedSamples[sampleIndex] - yl[sampleIndex-1] - Q1*yb[sampleIndex -1];
yb[sampleIndex] = F1*yh[sampleIndex] + yb[sampleIndex -1];
yl[sampleIndex] = F1*yb[sampleIndex] + yl[sampleIndex -1];
F1 = 2*sin(PI*phaser_triangular/Fs);
if(*ptr_subida == 1){
if(phaser_triangular == maxf){
phaser_triangular = minf2;
}
phaser_triangular += delta;
} else {
if(phaser_triangular == minf2){
phaser_triangular = maxf;
}
phaser_triangular -= delta;
}
if(phaser_triangular >= maxf2){
*ptr_subida = 0;
} else if(phaser_triangular <= minf){
*ptr_subida = 1;
}
}
double max_yb = max_abs_double(yb);
for(sampleIndex = 0; sampleIndex < FRAME_SIZE; sampleIndex++) {
decodedSamples[sampleIndex] = yb[sampleIndex];//max_yb;
//Revisar si es necesaria la asignacion del ultimo valor en last_y*
if(sampleIndex == FRAME_SIZE -1){
*last_yb=yb[sampleIndex];
*last_yh=yh[sampleIndex];
*last_yl=yl[sampleIndex];
}
}
return phaser_triangular;
}
void flanger_effect(int * decodedSamples, int *first_time_flanger_function){
double max_time_delay = 0.003; //3ms max delay in seconds
int rate = 1; //rate of flange in hz
int max_sample_delay;
int sampleIndex;
int Fs=44100;
//x,bits; // asignarle valor
double y[FRAME_SIZE];
double amp = 0.7;
double flan[FRAME_SIZE], flan_sample[FRAME_SIZE];
double sin_ref[FRAME_SIZE];
double cur_sin,cur_delay;
if(*first_time_flanger_function == 1){
//**** INICIO CASO 1
//CASO DEL MAX SAMPLE DELAY,
for (int i = 0; i < round(max_time_delay*Fs); i++){
decodedSamples
}
yh[0] = decodedSamples[0];
yb[0] = F1*yh[0];
yl[0] = F1*yb[0];
//***END CASO 1
*first_time_flanger_function = 0;
}
for(sampleIndex=0; sampleIndex < FRAME_SIZE; sampleIndex++){
sin_ref[sampleIndex]= (sin(2*PI*sampleIndex*(rate/Fs)));
max_sample_delay = round(max_time_delay*Fs);
y[sampleIndex] = 0;
}
for(sampleIndex=0;sampleIndex <max_sample_delay; sampleIndex++){
y[max_sample_delay] = flan[max_sample_delay];
}
for (sampleIndex = (max_sample_delay+1); sampleIndex < (max_sample_delay+1); sampleIndex++){
cur_sin= abs(sin_ref[sampleIndex]);
cur_delay = ceil(cur_sin*max_sample_delay);
y[sampleIndex]= ((amp*flan[sampleIndex])+(ampl*(flan[sampleIndex-cur_delay])));
}
}
void tremolo_effect( int *decodedSamples){
int Fc = 1;
double alpha = .8;
int sampleIndex;
int Fs = 5000;
double trem[FRAME_SIZE], trem_Sample[FRAME_SIZE];
for(sampleIndex = 0; sampleIndex < FRAME_SIZE; sampleIndex++){
trem[sampleIndex] = 1 + alpha*sin(2*PI*sampleIndex*(Fc/Fs));
}
for(sampleIndex = 0; sampleIndex < FRAME_SIZE ; sampleIndex++){
trem_Sample[sampleIndex] = trem[sampleIndex]*decodedSamples[sampleIndex];
}
for(sampleIndex = 0; sampleIndex < FRAME_SIZE ; sampleIndex++){
decodedSamples[sampleIndex] = trem_Sample[sampleIndex];
}
}
//Primera parte del Reverb
/*
//Shroeder Effect
#include <stdio.h>
double reverb_shroeder(double reverb_shroeder, int* decodedSamples, int *contReverb){
//Number of allpass filters
// Variables y parametros a utilizar:
// C | MATLAB
// decodedSamples | x = the input signal
// numbF | n = the number of allpass filters
// gain | g = the gain of the allpass filters (this should be less than 1 for stability)
// delayL | d = a vector which contains the delay length of each allpass filter
// gainFactor | k = the gain factor of the direct signal
// decodedSamples | y = the output signal
// b | b = the numerator coefficients of the transfer function
// a | a = the denominator coefficients of the transfer function
// Fs |
double Fs= 44100;
int numbF = 6;
double gain = 0.9;
double delayL; //puede ser necesario cambiarlo a long
double gainFactor = 0.2;
double bits;
double clockPic = 12*pow(10,6);
double a,b;
contReverb++;
if (contReverb==1000)//Sera experimetnal, faltan probar otros valores
delayL = round(0.05*rand([1,numbF])*Fs);// revisar la matriz que tiene como parametro
}
void allpass_filter(){
//b=[g zeros(1,d-1) 1];
//a=[1 zeros(1,d-1) g];
//Colocar los valores de a y b que posteriormente se utilizaran en allpass filter
int arrB[delayL+2];
int arrA[delayL+2];
int i;
for (i=0;i<delayL+2;i++){
if (i==0){
arrB[i]=gain;
}
if (i<=delayL){
arrB=0;
}
if (i==delayL+1){
arrB=1;
}
}
for (i=0;i<delayL+2;i++){
if (i==0){
arrA[i]=1;
}
if (i<=delayL){
arrA=0;
}
if (i==delayL+1){
arrA=gain;
}
}
}
void series_coefficients(int *arrA, int *arrB, double *a,double *b){
//Esto no es así, pero era para dejar una idea de como más o menos debe de ir
a = conv(arrA[0],arrA[1]);
b = conv(arrB[0],arrB[1]);
}
*/
int main(void)
{
long address = 0;
int record = 0;
int selector_efecto = 0;
///tremolo2
double trem_triangular = -0.5;
int trem_subida = 1;
///tremolo2
///wahwah
double wah_triangular = 500;
int wah_subida = 1;
double last_wah_wah_yb, last_wah_wah_yh, last_wah_wah_yl;
int first_time_wah_wah_function = 1;
///wahwah
///phaser
double phaser_triangular = 500;
int phaser_subida = 1;
int first_time_phaser_function = 1;
double last_phaser_yb, last_phaser_yh, last_phaser_yl;
//Phaser
/* Configure Oscillator to operate the device at 40MHz.
* Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
* Fosc= 700Mhz for 7.37M input clock */
PLLFBD=41; /* M=39 */
CLKDIVbits.PLLPOST=0; /* N1=2 */
CLKDIVbits.PLLPRE=0; /* N2=2 */
OSCTUN=0;
__builtin_write_OSCCONH(0x01); /* Initiate Clock Switch to FRC with PLL*/
__builtin_write_OSCCONL(0x01);
while (OSCCONbits.COSC != 0b01); /* Wait for Clock switch to occur */
while(!OSCCONbits.LOCK);
/* Intialize the board and the drivers */
SASKInit();
WM8510Init(codecHandle,codecBuffer);
/* Start Audio input and output function */
WM8510Start(codecHandle);
/* Configure codec for 8K operation */
WM8510SampleRate8KConfig(codecHandle);
/* Main processing loop. Executed for every input and
* output frame */
while(1) {
/*Obtain Audio Samples */
while(WM8510IsReadBusy(codecHandle));
WM8510Read(codecHandle,samples,FRAME_SIZE);
G711Lin2Ulaw(samples,encodedSamples,FRAME_SIZE);
/* Decode the samples */
G711Ulaw2Lin (encodedSamples,decodedSamples, FRAME_SIZE);
/* Wait till the codec is available for a new frame */
while(WM8510IsWriteBusy(codecHandle));
switch(selector_efecto) {
case 0:
// prenden los leds
RED_LED=SASK_LED_OFF;
YELLOW_LED=SASK_LED_OFF;
GREEN_LED=SASK_LED_OFF;
// se activa el clean
break;
case 1:
// prenden los leds
RED_LED=SASK_LED_OFF;
YELLOW_LED=SASK_LED_OFF;
GREEN_LED=SASK_LED_ON;
// Se activa el efecto de tremolo
tremolo_effect(decodedSamples);
break;
case 2:
// prenden los leds
RED_LED=SASK_LED_OFF;
YELLOW_LED=SASK_LED_ON;
GREEN_LED=SASK_LED_OFF;
// Se activa el efecto de tremolo2
trem_triangular = tremolo_effect2(trem_triangular, &trem_subida, decodedSamples);
break;
case 3:
// prenden los leds
RED_LED=SASK_LED_OFF;
YELLOW_LED=SASK_LED_ON;
GREEN_LED=SASK_LED_ON;
// Se activa el efecto de fuzz
fuzz_effect(decodedSamples);
break;
case 4:
// prenden los leds
RED_LED=SASK_LED_ON;
YELLOW_LED=SASK_LED_OFF;
GREEN_LED=SASK_LED_OFF;
// Se activa el efecto de wah wah
wah_triangular = wah_wah_effect(wah_triangular, &wah_subida, decodedSamples, &last_wah_wah_yb, &last_wah_wah_yh, &last_wah_wah_yl, &first_time_wah_wah_function);
break;
case 5:
// prenden los leds
RED_LED=SASK_LED_ON;
YELLOW_LED=SASK_LED_OFF;
GREEN_LED=SASK_LED_ON;
phaser_triangular = phaser_effect(phaser_triangular, &phaser_subida, decodedSamples, &last_phaser_yb, &last_phaser_yh, &last_phaser_yl, &first_time_phaser_function);
// Se activa el efecto de wah wah
break;
case 6:
// prenden los leds
RED_LED=SASK_LED_ON;
YELLOW_LED=SASK_LED_ON;
GREEN_LED=SASK_LED_OFF;
// Se activa el efecto de wah wah
break;
case 7:
// prenden los leds
RED_LED=SASK_LED_ON;
YELLOW_LED=SASK_LED_ON;
GREEN_LED=SASK_LED_ON;
// Se activa el efecto de wah wah
break;
default:
break;
}
/* Write the frame to the output */
WM8510Write (codecHandle,decodedSamples,FRAME_SIZE);
if(CheckSwitchS1()){
selector_efecto--;
}
if(CheckSwitchS2()){
selector_efecto++;
}
// rango de 0-7 para selector de efectos
if (selector_efecto < 0 ) {
selector_efecto = 7;
}
if (selector_efecto > 7 ) {
selector_efecto = 0;
}
}
}