Double Buffering 32-bit Audio (Aliasing problem)
Hi,
I'm developing an audio device which will process incoming audio signals into a distorted output.
Since there is a series of processes that will be executed, I thought it would be useful to use "double buffering" to process the collected audio, while simultaneously fetching new data in the background using DMA.
Below is the essence of what I'm doing. My understanding is that this is a pretty basic way to go about it: Do processing when buffer is half-full, and then again when buffer is full.
#define AUDIO_BUFFER_SIZE 128
#define AUDIO_BUFFER_SIZE_HALF (uint32_t)(AUDIO_BUFFER_SIZE * 0.5f)
int32_t audio_rx[AUDIO_BUFFER_SIZE];
int32_t audio_tx[AUDIO_BUFFER_SIZE];
static volatile int32_t *audio_rx_p;
static volatile int32_t *audio_tx_p = &audio_tx[0];
void Audio_Transmit(){
for(uint8_t n = 0; n < (AUDIO_BUFFER_SIZE_HALF - 1); n += 2){
//left channel
audio_tx_p[n] = FLOAT_TO_INT32 * Audio_Process(INT32_TO_FLOAT * audio_rx_p[n]);
//right channel
audio_tx_p[n + 1] = FLOAT_TO_INT32 * Audio_Process(INT32_TO_FLOAT * audio_rx_p[n + 1]);
}
audio_ready_flag = AUDIO_DATA_FREE;
}
void HAL_SAI_TxHalfCpltCallback(SAI_HandleTypeDef *hi2s){
audio_rx_p = &audio_rx[0];
audio_tx_p = &audio_tx[0];
audio_ready_flag = AUDIO_DATA_READY;
}
void HAL_SAI_TxCpltCallback(SAI_HandleTypeDef *hi2s){
audio_rx_p = &audio_rx[AUDIO_BUFFER_SIZE_HALF];
audio_tx_p = &audio_tx[AUDIO_BUFFER_SIZE_HALF];
audio_ready_flag = AUDIO_DATA_READY;
}
My issue is that when I set the buffer size larger than 128 elements, the output audio faces some serious aliasing. What previously sounded like a modern audio interface, suddenly turns into a Gameboy Advanced.
Any ideas as to what may be the culprit?
My intuition is that if double buffering is done correctly, increasing the buffer size should just create a delay between the input and output audio, since the data to be processed becomes increasingly large (if you've ever used an audio interface for recording, you probably know what I'm talking about).
Thankful for any help I can receive on this issue!
Br,
Max
