Skip to content

Instantly share code, notes, and snippets.

@MrAntex
Created December 21, 2022 22:40
Show Gist options
  • Save MrAntex/36d37db862fe94572a47dc8ae898c699 to your computer and use it in GitHub Desktop.
Save MrAntex/36d37db862fe94572a47dc8ae898c699 to your computer and use it in GitHub Desktop.

Revisions

  1. MrAntex created this gist Dec 21, 2022.
    124 changes: 124 additions & 0 deletions audio_lab_software.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,124 @@

    // Filtering function using 2 for loops instead of one with a modulo because it turned out to be way more efficient
    // indexVal is the position of the current sample, it decreases each loop but staying in [0;currentSize]
    void filter(){

    for(int w = indexVal ; w < currentSize ; w++){
    toReturn[0] += currentCoeffs[w-indexVal] * bufferL[w];
    toReturn[1] += currentCoeffs[w-indexVal] * bufferR[w];
    }

    for(int l = 0 ; l < indexVal ; l++){
    toReturn[0] += currentCoeffs[currentSize-indexVal+l] * bufferL[l];
    toReturn[1] += currentCoeffs[currentSize-indexVal+l] * bufferR[l];
    }


    }

    // Changing the used filter by changing the used coefficients and the number of them (currentSize)
    void applyFilter(int filter){
    switch(filter){
    case 0: // Basic HP
    currentCoeffs = HP;
    currentSize = N_HP;
    break;

    case 1: // Basic LP
    currentCoeffs = LP;
    currentSize = N_LP;
    break;

    case 2: // None
    currentCoeffs = None;
    currentSize = N_None;
    break;

    case 3: // LP 2
    currentCoeffs = LP2;
    currentSize = N_LP2;
    break;

    case 99:
    currentCoeffs = F99;
    currentSize = N_F99;
    break;
    }

    indexVal = currentSize-1;
    }




    int main()
    {

    init_platform();

    print("Started!\n\r");

    AudioInitialize(SCU_TIMER_ID, AUDIO_IIC_ID, AUDIO_CTRL_BASEADDR);

    initialize_FIFO(AUDIO_FIFO);
    initialize_FIFO(FIR_FIFO);

    applyFilter(99);



    int SampleL, SampleR;


    int j=0;
    int time_now=0;
    int time_func = 0;
    int t = 0;

    int tim_now_filter = 0;

    int currentFilter = 0;

    int indexVal = currentSize-1;



    while (1){

    if(j%2==0){ // Every 2 loops
    t = Xil_In32(GLOBAL_TMR_BASEADDR + GTIMER_COUNTER_LOWER_OFFSET) - time_now;
    }

    int newSample[] = {(int) I2SFifoRead(AUDIO_FIFO), (int) I2SFifoRead(AUDIO_FIFO)};

    bufferL[indexVal] = newSample[0];
    bufferR[indexVal] = newSample[1];

    toReturn[0] = 0;
    toReturn[1] = 0;

    time_now = Xil_In32(GLOBAL_TMR_BASEADDR + GTIMER_COUNTER_LOWER_OFFSET);

    filter();

    time_func = Xil_In32(GLOBAL_TMR_BASEADDR + GTIMER_COUNTER_LOWER_OFFSET) - time_now;


    if(j%100000 == 0){ // Every 2-3 seconds
    xil_printf("Filter : %d, Loop : %d\n",time_func, t);
    j = 0;
    }


    I2SFifoWrite(AUDIO_FIFO, toReturn[0]);
    I2SFifoWrite(AUDIO_FIFO, toReturn[1]);

    j++;

    // Updating indexVal
    indexVal = indexVal == 0 ? currentSize-1 : (indexVal-1)%currentSize;
    }

    cleanup_platform();
    return 0;
    }