Commit dea59947 authored by sagnowski's avatar sagnowski
Browse files

Clean up LFE delay compensation

parent 08136bc6
Loading
Loading
Loading
Loading
Loading
+26 −43
Original line number Diff line number Diff line
@@ -6349,19 +6349,21 @@ static ivas_error renderLfeToBinaural(
#endif
    IVAS_REND_AudioBuffer outAudio )
{
    int16_t i;
#ifdef FIX_194_LFE_DELAY_EXTREND
    int16_t ear_idx;
#endif
    int16_t lfe_idx;
#ifdef SPLIT_REND_WITH_HEAD_ROT
    int16_t pose_idx, num_poses;
#endif
    float gain;
#ifdef FIX_194_LFE_DELAY_EXTREND
    float *tmpLfeBuffer;
#endif
    int16_t ear_idx;
    float tmpLfeBuffer[MAX_BUFFER_LENGTH_PER_CHANNEL];
    int16_t frame_size, num_cpy_smpl_cur_frame, num_cpy_smpl_prev_frame;
    const float *lfeInput;
    float *writePtr;
#else
    int16_t i;
    float *readPtr, *writePtr;
#endif

#ifdef SPLIT_REND_WITH_HEAD_ROT
    assert( ( getAudioConfigType( outConfig ) == IVAS_REND_AUDIO_CONFIG_TYPE_BINAURAL ) && "Must be binaural output" );
@@ -6388,35 +6390,21 @@ static ivas_error renderLfeToBinaural(
    }

#ifdef FIX_194_LFE_DELAY_EXTREND
    /*  Render LFE first to a temporary buffer to avoid applying gain multiple times */
    tmpLfeBuffer = malloc( mcInput->base.inputBuffer.config.numSamplesPerChannel * sizeof( float ) );
    if ( tmpLfeBuffer == NULL )
    {
        return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Failed to allocate memory for temporary LFE to Binaural buffer" );
    }

    /* 1. read from the LFE delay buffer into temp buffer
       2. read remaining samples from the input LFE channel into temp buffer */
    readPtr = mcInput->lfeDelayBuffer;
    writePtr = tmpLfeBuffer;
    for ( i = 0; i < mcInput->binauralDelaySmp; i++ )
    {
        *writePtr++ = gain * ( *readPtr++ );
    }
    readPtr = getSmplPtr( mcInput->base.inputBuffer, lfe_idx, 0 );
    for ( ; i < ( mcInput->base.inputBuffer.config.numSamplesPerChannel - mcInput->binauralDelaySmp ); i++ )
    {
        *writePtr++ = gain * ( *readPtr++ );
    }

    /* save leftover samples in the LFE delay buffer for next frame */
    writePtr = mcInput->lfeDelayBuffer;
    for ( i = 0; i < mcInput->binauralDelaySmp; i++ )
    {
        *writePtr++ = gain * ( *readPtr++ );
    }
    /* --- Prepare LFE signal to be added to binaural output --- */
    lfeInput = getSmplPtr( mcInput->base.inputBuffer, lfe_idx, 0 );
    frame_size = mcInput->base.inputBuffer.config.numSamplesPerChannel;
    num_cpy_smpl_prev_frame = mcInput->binauralDelaySmp;
    num_cpy_smpl_cur_frame = frame_size - num_cpy_smpl_prev_frame;
    /* Assuming LFE should be delayed by less that the duration of one frame */
    assert( mcInput->binauralDelaySmp < frame_size );
    /* Get delayed LFE signal from previous frame, apply gain and save in tmp buffer */
    v_multc( mcInput->lfeDelayBuffer, gain, tmpLfeBuffer, num_cpy_smpl_prev_frame );
    /* Continue filling tmp buffer, now with LFE signal from current frame */
    v_multc( lfeInput, gain, tmpLfeBuffer + num_cpy_smpl_prev_frame, num_cpy_smpl_cur_frame );
    /* Save remaining LFE samples of current frame for next frame */
    mvr2r( lfeInput + num_cpy_smpl_cur_frame, mcInput->lfeDelayBuffer, num_cpy_smpl_prev_frame );
#endif

#endif /* FIX_194_LFE_DELAY_EXTREND */
#ifdef SPLIT_REND_WITH_HEAD_ROT
    /* Copy LFE to left and right binaural channels for all poses */
    if ( mcInput->base.ctx.pSplitRendWrapper != NULL )
@@ -6433,9 +6421,8 @@ static ivas_error renderLfeToBinaural(
#ifdef FIX_194_LFE_DELAY_EXTREND
        for ( ear_idx = 0; ear_idx < BINAURAL_CHANNELS; ++ear_idx )
        {
            mvr2r( tmpLfeBuffer,
                   getSmplPtr( outAudio, pose_idx * BINAURAL_CHANNELS + ear_idx, 0 ),
                   mcInput->base.inputBuffer.config.numSamplesPerChannel );
            writePtr = getSmplPtr( outAudio, pose_idx * BINAURAL_CHANNELS + ear_idx, 0 );
            v_add( writePtr, tmpLfeBuffer, writePtr, frame_size );
        }
#else
        readPtr = getSmplPtr( mcInput->base.inputBuffer, lfe_idx, 0 );
@@ -6458,9 +6445,8 @@ static ivas_error renderLfeToBinaural(
#ifdef FIX_194_LFE_DELAY_EXTREND
    for ( ear_idx = 0; ear_idx < BINAURAL_CHANNELS; ++ear_idx )
    {
        mvr2r( tmpLfeBuffer,
               getSmplPtr( outAudio, ear_idx, 0 ),
               mcInput->base.inputBuffer.config.numSamplesPerChannel );
        writePtr = getSmplPtr( outAudio, pose_idx * BINAURAL_CHANNELS + ear_idx, 0 );
        v_add( writePtr, tmpLfeBuffer, writePtr, frame_size );
    }
#else
    readPtr = getSmplPtr( mcInput->base.inputBuffer, lfe_idx, 0 );
@@ -6478,9 +6464,6 @@ static ivas_error renderLfeToBinaural(
    }
#endif /* FIX_194_LFE_DELAY_EXTREND */
#endif /* SPLIT_REND_WITH_HEAD_ROT */
#ifdef FIX_194_LFE_DELAY_EXTREND
    free( tmpLfeBuffer );
#endif /* FIX_194_LFE_DELAY_EXTREND */

    pop_wmops();