int main(void) { ex_sask_init(); /*Initialise Audio input and output function*/ ADCChannelInit(pADCChannelHandle, adcBuffer); OCPWMInit(pOCPWMHandle, ocPWMBuffer); /*Start Audio input and output function*/ ADCChannelStart(pADCChannelHandle); OCPWMStart(pOCPWMHandle); /*Initialising variables that are needed for calculating the peak frequency*/ int peakFrequency = 0; int peakFrequencyBin = 0; while (1) { /*Wait till the ADC has a new frame available*/ while (ADCChannelIsBusy(pADCChannelHandle)); /*Read in the Audio Samples from the ADC*/ ADCChannelRead(pADCChannelHandle, frctAudioIn, FRAME_SIZE); /*Computing the FFT of the raw signal*/ fourierTransform(FRAME_SIZE, compX, frctAudioIn); /*Removing the negative part of the FFT output (imaginary part) by zeroing it out*/ filterNegativeFreq(FRAME_SIZE, compXfiltered, compX); /*Compute the square magnitude of the complex FFT output array so we have a Real output vetor*/ SquareMagnitudeCplx(FRAME_SIZE / 2, &compXfiltered[0], &compXfiltered[0].real); /*Find the frequency Bin ( = index into the SigCmpx[] array) that has the largest energy*/ /*i.e., the largest spectral component*/ VectorMax(FRAME_SIZE / 2, &compXfiltered[0].real, &peakFrequencyBin); /*Compute the frequency (in Hz) of the largest spectral component*/ peakFrequency = peakFrequencyBin * (8000 / FRAME_SIZE); /*Uses the peak frequency variable to turn on the corresponding LEDs*/ turnOnLEDs(peakFrequency); /* Used for checking whether the user wants to record */ if (CheckSwitchS1() == PRESSED) { if (switchState == 0) { //startRecording(); Code works, but doesn't record much, only a fraction of a second } else if (switchState == 1) { int i = 0; for (i; i < (FRAME_SIZE)*5; i++) { frctAudioOut[i] = 0; } switchState = 0; firstPartExecutedDecision = 0; } } /* Switch two is just used to toggle between different modes */ if (CheckSwitchS2() == PRESSED) { if (switchState2 == 0) { switchState2 = 1; } else if (switchState2 == 1) { switchState2 = 2; } else if (switchState2 == 2) { switchState2 = 0; } } /* Checking whether the peak frequency is above 800 and if so calling the function playPureSignal() */ if (peakFrequency >= 800) { playPureSignal(peakFrequency); } else if (switchState2 != 2) { int i = 0; for (i; i < FRAME_SIZE; i++) { if (switchState2 == 0) { frctAudioOut[i] = frctAudioIn[i]; //Used for the mode that allows for outputting of the original signal } else if (switchState2 == 1) { frctAudioOut[i] = 0; //Used for the mode that requests silence if the threshold has not been met } } } if (switchState2 == 2) { int i = 0; for (i; i < FRAME_SIZE; i++) { frctAudioOut[i] = 0; } } /* Outputting the pure signal or original sound or silence, depending on what's inside frctAudioOut*/ while (OCPWMIsBusy(pOCPWMHandle)); OCPWMWrite(pOCPWMHandle, frctAudioOut, FRAME_SIZE); } /*============================================================================= | Function used for recording the input signal, a snapshot of the frame | is stored inside the frctAudioOut array, this is done five times as that | was the largest possible size for frctAudioOut array as anything larger would | lead to exhausting the memory, the function records correctly but the recording | is not long enough, hence the code was retired as it requires more work. *===========================================================================*/ void startRecording() { if (firstPartExecutedDecision == 0) { int i = 0; for (i; i < FRAME_SIZE; i++) { frctAudioOut[i] = frctAudioIn[i]*0.5; } firstPartExecutedDecision = 1; } if (firstPartExecutedDecision == 1) { int i = FRAME_SIZE; int iFrameSize = 0; for (i; i < (FRAME_SIZE)*2; i++) { frctAudioOut[i] = frctAudioIn[iFrameSize]*0.5; iFrameSize++; } firstPartExecutedDecision = 2; } if (firstPartExecutedDecision == 2) { int i = (FRAME_SIZE)*2; int iFrameSize = 0; for (i; i < (FRAME_SIZE)*3; i++) { frctAudioOut[i] = frctAudioIn[iFrameSize]*0.5; iFrameSize++; } firstPartExecutedDecision = 3; } if (firstPartExecutedDecision == 3) { int i = (FRAME_SIZE)*3; int iFrameSize = 0; for (i; i < (FRAME_SIZE)*4; i++) { frctAudioOut[i] = frctAudioIn[iFrameSize]*0.5; iFrameSize++; } firstPartExecutedDecision = 4; } if (firstPartExecutedDecision == 4) { int i = (FRAME_SIZE)*4; int iFrameSize = 0; for (i; i < (FRAME_SIZE)*5; i++) { frctAudioOut[i] = frctAudioIn[iFrameSize]*0.5; iFrameSize++; } firstPartExecutedDecision = -1; switchState = 1; } } /*============================================================================= | Function used to generate the pure signal using the createSimpleSignal() | function available in the modulate.h header - this function uses the peak | frequency handed through the parameter call to decide what signal to generate. | The output signal is stored inside the frctAudioOut array which is then | later used for the OCPWMWrite() function to output the sound. *===========================================================================*/ void playPureSignal(int peakFrequency) { int simpleSignalFrequency = 0; if (peakFrequency <= 1600) { simpleSignalFrequency = 500; createSimpleSignal(simpleSignalFrequency, FRAME_SIZE, frctAudioOut); } else if (peakFrequency <= 2400) { simpleSignalFrequency = 800; createSimpleSignal(simpleSignalFrequency, FRAME_SIZE, frctAudioOut); } else if (peakFrequency <= 3200) { simpleSignalFrequency = 1000; createSimpleSignal(simpleSignalFrequency, FRAME_SIZE, frctAudioOut); } else if (peakFrequency <= 4000) { simpleSignalFrequency = 1500; createSimpleSignal(simpleSignalFrequency, FRAME_SIZE, frctAudioOut); } }
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; } } }