About using multiple filters for the same piece of audio

Do you know if this code can be optimized? 1. I was trying to use different bandpass filters for the same audio, and I found that it didn’t work for me to use only 31 filters on one readpointer, so I had to copy a lot of audio buffers out and then use the filters on them separately, I’m not sure if that’s correct.

void QUAD_CorrAudioProcessor::dsp(const std::array<const float*, 4>& channelsData,juce::AudioBuffer<float>& buffer,int channel_1,int channel_2)
{
    
    for (int i = 0; i < 31; ++i)
    {
        left_Data_Copy[i].setSize(1, buffer.getNumSamples());
        left_Data_Copy[i].copyFrom(0, 0, channelsData[channel_1], buffer.getNumSamples());
        right_Data_Copy[i].setSize(1, buffer.getNumSamples());
        right_Data_Copy[i].copyFrom(0, 0, channelsData[channel_2], buffer.getNumSamples());
    }
    float* left_Data_Copy_Data_1 = left_Data_Copy[band::band1].getWritePointer(0);
    float* left_Data_Copy_Data_2 = left_Data_Copy[band::band2].getWritePointer(0);
    float* left_Data_Copy_Data_3 = left_Data_Copy[band::band3].getWritePointer(0);
    float* left_Data_Copy_Data_4 = left_Data_Copy[band::band4].getWritePointer(0);
    float* left_Data_Copy_Data_5 = left_Data_Copy[band::band5].getWritePointer(0);
    float* left_Data_Copy_Data_6 = left_Data_Copy[band::band6].getWritePointer(0);
    float* left_Data_Copy_Data_7 = left_Data_Copy[band::band7].getWritePointer(0);
    float* left_Data_Copy_Data_8 = left_Data_Copy[band::band8].getWritePointer(0);
    float* left_Data_Copy_Data_9 = left_Data_Copy[band::band9].getWritePointer(0);
    float* left_Data_Copy_Data_10 = left_Data_Copy[band::band10].getWritePointer(0);
    float* left_Data_Copy_Data_11 = left_Data_Copy[band::band11].getWritePointer(0);
    float* left_Data_Copy_Data_12 = left_Data_Copy[band::band12].getWritePointer(0);
    float* left_Data_Copy_Data_13 = left_Data_Copy[band::band13].getWritePointer(0);
    float* left_Data_Copy_Data_14 = left_Data_Copy[band::band14].getWritePointer(0);
    float* left_Data_Copy_Data_15 = left_Data_Copy[band::band15].getWritePointer(0);
    float* left_Data_Copy_Data_16 = left_Data_Copy[band::band16].getWritePointer(0);
    float* left_Data_Copy_Data_17 = left_Data_Copy[band::band17].getWritePointer(0);
    float* left_Data_Copy_Data_18 = left_Data_Copy[band::band18].getWritePointer(0);
    float* left_Data_Copy_Data_19 = left_Data_Copy[band::band19].getWritePointer(0);
    float* left_Data_Copy_Data_20 = left_Data_Copy[band::band20].getWritePointer(0);
    float* left_Data_Copy_Data_21 = left_Data_Copy[band::band21].getWritePointer(0);
    float* left_Data_Copy_Data_22 = left_Data_Copy[band::band22].getWritePointer(0);
    float* left_Data_Copy_Data_23 = left_Data_Copy[band::band23].getWritePointer(0);
    float* left_Data_Copy_Data_24 = left_Data_Copy[band::band24].getWritePointer(0);
    float* left_Data_Copy_Data_25 = left_Data_Copy[band::band25].getWritePointer(0);
    float* left_Data_Copy_Data_26 = left_Data_Copy[band::band26].getWritePointer(0);
    float* left_Data_Copy_Data_27 = left_Data_Copy[band::band27].getWritePointer(0);
    float* left_Data_Copy_Data_28 = left_Data_Copy[band::band28].getWritePointer(0);
    float* left_Data_Copy_Data_29 = left_Data_Copy[band::band29].getWritePointer(0);
    float* left_Data_Copy_Data_30 = left_Data_Copy[band::band30].getWritePointer(0);
    float* left_Data_Copy_Data_31 = left_Data_Copy[band::band31].getWritePointer(0);
    
    float* right_Data_Copy_Data_1 = right_Data_Copy[band::band1].getWritePointer(0);
    float* right_Data_Copy_Data_2 = right_Data_Copy[band::band2].getWritePointer(0);
    float* right_Data_Copy_Data_3 = right_Data_Copy[band::band3].getWritePointer(0);
    float* right_Data_Copy_Data_4 = right_Data_Copy[band::band4].getWritePointer(0);
    float* right_Data_Copy_Data_5 = right_Data_Copy[band::band5].getWritePointer(0);
    float* right_Data_Copy_Data_6 = right_Data_Copy[band::band6].getWritePointer(0);
    float* right_Data_Copy_Data_7 = right_Data_Copy[band::band7].getWritePointer(0);
    float* right_Data_Copy_Data_8 = right_Data_Copy[band::band8].getWritePointer(0);
    float* right_Data_Copy_Data_9 = right_Data_Copy[band::band9].getWritePointer(0);
    float* right_Data_Copy_Data_10 = right_Data_Copy[band::band10].getWritePointer(0);
    float* right_Data_Copy_Data_11 = right_Data_Copy[band::band11].getWritePointer(0);
    float* right_Data_Copy_Data_12 = right_Data_Copy[band::band12].getWritePointer(0);
    float* right_Data_Copy_Data_13 = right_Data_Copy[band::band13].getWritePointer(0);
    float* right_Data_Copy_Data_14 = right_Data_Copy[band::band14].getWritePointer(0);
    float* right_Data_Copy_Data_15 = right_Data_Copy[band::band15].getWritePointer(0);
    float* right_Data_Copy_Data_16 = right_Data_Copy[band::band16].getWritePointer(0);
    float* right_Data_Copy_Data_17 = right_Data_Copy[band::band17].getWritePointer(0);
    float* right_Data_Copy_Data_18 = right_Data_Copy[band::band18].getWritePointer(0);
    float* right_Data_Copy_Data_19 = right_Data_Copy[band::band19].getWritePointer(0);
    float* right_Data_Copy_Data_20 = right_Data_Copy[band::band20].getWritePointer(0);
    float* right_Data_Copy_Data_21 = right_Data_Copy[band::band21].getWritePointer(0);
    float* right_Data_Copy_Data_22 = right_Data_Copy[band::band22].getWritePointer(0);
    float* right_Data_Copy_Data_23 = right_Data_Copy[band::band23].getWritePointer(0);
    float* right_Data_Copy_Data_24 = right_Data_Copy[band::band24].getWritePointer(0);
    float* right_Data_Copy_Data_25 = right_Data_Copy[band::band25].getWritePointer(0);
    float* right_Data_Copy_Data_26 = right_Data_Copy[band::band26].getWritePointer(0);
    float* right_Data_Copy_Data_27 = right_Data_Copy[band::band27].getWritePointer(0);
    float* right_Data_Copy_Data_28 = right_Data_Copy[band::band28].getWritePointer(0);
    float* right_Data_Copy_Data_29 = right_Data_Copy[band::band29].getWritePointer(0);
    float* right_Data_Copy_Data_30 = right_Data_Copy[band::band30].getWritePointer(0);
    float* right_Data_Copy_Data_31 = right_Data_Copy[band::band31].getWritePointer(0);
    
    
    for (int i = 0; i < buffer.getNumSamples(); ++i)
    {
        left_Data_Copy_Data_1[i] = filter[0].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_1[i] = filter[1].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_2[i] = filter[2].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_2[i] = filter[3].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_3[i] = filter[4].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_3[i] = filter[5].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_4[i] = filter[6].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_4[i] = filter[7].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_5[i] = filter[8].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_5[i] = filter[9].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_6[i] = filter[10].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_6[i] = filter[11].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_7[i] = filter[12].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_7[i] = filter[13].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_8[i] = filter[14].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_8[i] = filter[15].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_9[i] = filter[16].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_9[i] = filter[17].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_10[i] = filter[18].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_10[i] = filter[19].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_11[i] = filter[20].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_11[i] = filter[21].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_12[i] = filter[22].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_12[i] = filter[23].processSample(channelsData[channel::right][i]);
        
        
        left_Data_Copy_Data_13[i] = filter[24].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_13[i] = filter[25].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_14[i] = filter[26].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_14[i] = filter[27].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_15[i] = filter[28].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_15[i] = filter[29].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_16[i] = filter[30].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_16[i] = filter[31].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_17[i] = filter[32].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_17[i] = filter[33].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_18[i] = filter[34].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_18[i] = filter[35].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_19[i] = filter[36].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_19[i] = filter[37].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_20[i] = filter[38].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_20[i] = filter[39].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_21[i] = filter[40].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_21[i] = filter[41].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_22[i] = filter[42].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_22[i] = filter[43].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_23[i] = filter[44].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_23[i] = filter[45].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_24[i] = filter[46].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_24[i] = filter[47].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_25[i] = filter[48].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_25[i] = filter[49].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_26[i] = filter[50].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_26[i] = filter[51].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_27[i] = filter[52].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_27[i] = filter[53].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_28[i] = filter[54].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_28[i] = filter[55].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_29[i] = filter[56].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_29[i] = filter[57].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_30[i] = filter[58].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_30[i] = filter[59].processSample(channelsData[channel::right][i]);
        
        left_Data_Copy_Data_31[i] = filter[60].processSample(channelsData[channel::left][i]);
        right_Data_Copy_Data_31[i] = filter[61].processSample(channelsData[channel::right][i]);
    }
  • If you want to apply these filters in parallel (31 parallel filters actually shock me), I am afraid that you have to copy the audio-buffer each time before you apply a filter. But instead of writing all things in that way, why not create an array of filters/buffers to handle these?
  • NEVER call AudioBuffer.setSize() in audio thread unless you explicitly set avoidReallocating=true
1 Like

Now the main CPU usage is these 31 filters, I just want to apply different filters on the same audio 31 times, so hard!