0

I´m trying to read 2 Files with FATFS from my SD via SDIO and mix the 16 bit PCM audio data. It works but The Output Sound is while playing 2 files slower and doesn´t sound good.

I use I2S to send the Data to an IC. My Buffersize is 4096. without mixing the 2 Buffers the Audio Quality is fine. When I´m using the example to mix the Buffers which i have found online you can hear a lot of noise.

Any Ideas?

my code:

volatile uint16_t* signal_play_buff = NULL;
uint16_t* signal_read_buff = NULL;
uint16_t* signal_read_buff2 = NULL;

volatile uint16_t signal_buff1[4096];
volatile uint16_t signal_buff2[4096];

HAL_StatusTypeDef hal_res;
    int nsamples = sizeof(signal_buff1) / sizeof(signal_buff1[0]);
    hal_res = HAL_I2S_Transmit_IT(&hi2s2, (uint16_t*)signal_buff1, nsamples);
    if(hal_res != HAL_OK) {
        UART_Printf("I2S - HAL_I2S_Transmit failed, hal_res = %d!\r\n", hal_res);
        f_close(&file);
        return -12;
    }
        FIL file2;
        FRESULT res2 = f_open(&file2, "over.raw", FA_READ);

        while(bytesRead >= sizeof(signal_buff1)) {
                if(!read_next_chunk) {
                    continue;
                }

                read_next_chunk = false;

                res = f_read(&file, (uint8_t*)signal_read_buff, sizeof(signal_buff1), &bytesRead);
                res2 = f_read(&file2, (uint8_t*)signal_buff3, sizeof(signal_buff3), &bytesRead2);

                for(int i = 0; i < sizeof(signal_buff1) /2; i++){
                    uint16_t a = signal_read_buff[i]; // first sample
                    uint16_t b = signal_buff3[i]; // second sample
                    uint16_t m; // mixed result will go here

                    // Pick the equation
                    if ((a < 32768) || (b < 32768)) {
                        // Viktor's first equation when both sources are "quiet"
                        // (i.e. less than middle of the dynamic range)
                        m = a * b / 32768;
                    } else {
                        // Viktor's second equation when one or both sources are loud
                        m = 2 * (a + b) - (a * b) / 32768 - 65536;
                    }

                    if (m == 65536) m = 65535;
                    signal_read_buff[i] = m;
                }

                if(res != FR_OK) {
                    UART_Printf("f_read() failed, res = %d\r\n", res);
                    f_close(&file);
                    return -13;
                }

            }

            end_of_file_reached = true;   
    res = f_close(&file);
    if(res != FR_OK) {
        UART_Printf("f_close() failed, res = %d\r\n", res);
        return -14;
    }

    return 0;
}

I2S Callback to send next Buffer

void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s) {
    if(end_of_file_reached)
        return;

    volatile uint16_t* temp = signal_play_buff;
    signal_play_buff = signal_read_buff;
    signal_read_buff = temp;

    int nsamples = sizeof(signal_buff1) / sizeof(signal_buff1[0]);
    HAL_I2S_Transmit_IT(&hi2s2, (uint16_t*)signal_play_buff, nsamples);
    read_next_chunk = true;
}
Jonas Bock
  • 11
  • 2
  • 1
    Can you share your code? Are you reading a sample from each file, summing them then writing to output and repeating, or operating on larger buffers? Are you just summing the samples, or adjusting levels too? How are you 'playing' the audio out? – Colin Apr 17 '20 at 12:41
  • i added my Code in the Question. I´m reading 2 Buffers (each 4096) and then trying to add them. – Jonas Bock Apr 29 '20 at 13:34

0 Answers0