Skip to content

Commit

Permalink
Remove mixer sanitation
Browse files Browse the repository at this point in the history
  • Loading branch information
sakertooth committed Jun 16, 2024
1 parent 01ffa95 commit acfd40a
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 123 deletions.
11 changes: 0 additions & 11 deletions include/MixHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ bool useNaNHandler();

void setNaNHandler( bool use );

bool sanitize( sampleFrame * src, int frames );

/*! \brief Add samples from src to dst */
void add( sampleFrame* dst, const sampleFrame* src, int frames );

Expand All @@ -60,15 +58,6 @@ void addMultipliedByBuffer( sampleFrame* dst, const sampleFrame* src, float coef
/*! \brief Add samples from src multiplied by coeffSrc and coeffSrcBuf to dst */
void addMultipliedByBuffers( sampleFrame* dst, const sampleFrame* src, ValueBuffer * coeffSrcBuf1, ValueBuffer * coeffSrcBuf2, int frames );

/*! \brief Same as addMultiplied, but sanitize output (strip out infs/nans) */
void addSanitizedMultiplied( sampleFrame* dst, const sampleFrame* src, float coeffSrc, int frames );

/*! \brief Add samples from src multiplied by coeffSrc and coeffSrcBuf to dst - sanitized version */
void addSanitizedMultipliedByBuffer( sampleFrame* dst, const sampleFrame* src, float coeffSrc, ValueBuffer * coeffSrcBuf, int frames );

/*! \brief Add samples from src multiplied by coeffSrc and coeffSrcBuf to dst - sanitized version */
void addSanitizedMultipliedByBuffers( sampleFrame* dst, const sampleFrame* src, ValueBuffer * coeffSrcBuf1, ValueBuffer * coeffSrcBuf2, int frames );

/*! \brief Add samples from src multiplied by coeffSrcLeft/coeffSrcRight to dst */
void addMultipliedStereo( sampleFrame* dst, const sampleFrame* src, float coeffSrcLeft, float coeffSrcRight, int frames );

Expand Down
3 changes: 0 additions & 3 deletions src/core/EffectChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,12 @@ bool EffectChain::processAudioBuffer( sampleFrame * _buf, const fpp_t _frames, b
return false;
}

MixHelpers::sanitize( _buf, _frames );

bool moreEffects = false;
for (const auto& effect : m_effects)
{
if (hasInputNoise || effect->isRunning())
{
moreEffects |= effect->processAudioBuffer(_buf, _frames);
MixHelpers::sanitize(_buf, _frames);
}
}

Expand Down
104 changes: 0 additions & 104 deletions src/core/MixHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,46 +89,6 @@ void setNaNHandler( bool use )
s_NaNHandler = use;
}

/*! \brief Function for sanitizing a buffer of infs/nans - returns true if those are found */
bool sanitize( sampleFrame * src, int frames )
{
if( !useNaNHandler() )
{
return false;
}

bool found = false;
for( int f = 0; f < frames; ++f )
{
for( int c = 0; c < 2; ++c )
{
if( std::isinf( src[f][c] ) || std::isnan( src[f][c] ) )
{
#ifdef LMMS_DEBUG
// TODO don't use printf here
printf("Bad data, clearing buffer. frame: ");
printf("%d: value %f\n", f, src[f][c]);
#endif
for( int f = 0; f < frames; ++f )
{
for( int c = 0; c < 2; ++c )
{
src[f][c] = 0.0f;
}
}
found = true;
return found;
}
else
{
src[f][c] = std::clamp(src[f][c], -1000.0f, 1000.0f);
}
}
}
return found;
}


struct AddOp
{
void operator()( sampleFrame& dst, const sampleFrame& src ) const
Expand Down Expand Up @@ -212,70 +172,6 @@ void addMultipliedByBuffers( sampleFrame* dst, const sampleFrame* src, ValueBuff

}

void addSanitizedMultipliedByBuffer( sampleFrame* dst, const sampleFrame* src, float coeffSrc, ValueBuffer * coeffSrcBuf, int frames )
{
if ( !useNaNHandler() )
{
addMultipliedByBuffer( dst, src, coeffSrc, coeffSrcBuf,
frames );
return;
}

for( int f = 0; f < frames; ++f )
{
dst[f][0] += ( std::isinf( src[f][0] ) || std::isnan( src[f][0] ) ) ? 0.0f : src[f][0] * coeffSrc * coeffSrcBuf->values()[f];
dst[f][1] += ( std::isinf( src[f][1] ) || std::isnan( src[f][1] ) ) ? 0.0f : src[f][1] * coeffSrc * coeffSrcBuf->values()[f];
}
}

void addSanitizedMultipliedByBuffers( sampleFrame* dst, const sampleFrame* src, ValueBuffer * coeffSrcBuf1, ValueBuffer * coeffSrcBuf2, int frames )
{
if ( !useNaNHandler() )
{
addMultipliedByBuffers( dst, src, coeffSrcBuf1, coeffSrcBuf2,
frames );
return;
}

for( int f = 0; f < frames; ++f )
{
dst[f][0] += ( std::isinf( src[f][0] ) || std::isnan( src[f][0] ) )
? 0.0f
: src[f][0] * coeffSrcBuf1->values()[f] * coeffSrcBuf2->values()[f];
dst[f][1] += ( std::isinf( src[f][1] ) || std::isnan( src[f][1] ) )
? 0.0f
: src[f][1] * coeffSrcBuf1->values()[f] * coeffSrcBuf2->values()[f];
}

}


struct AddSanitizedMultipliedOp
{
AddSanitizedMultipliedOp( float coeff ) : m_coeff( coeff ) { }

void operator()( sampleFrame& dst, const sampleFrame& src ) const
{
dst[0] += ( std::isinf( src[0] ) || std::isnan( src[0] ) ) ? 0.0f : src[0] * m_coeff;
dst[1] += ( std::isinf( src[1] ) || std::isnan( src[1] ) ) ? 0.0f : src[1] * m_coeff;
}

const float m_coeff;
};

void addSanitizedMultiplied( sampleFrame* dst, const sampleFrame* src, float coeffSrc, int frames )
{
if ( !useNaNHandler() )
{
addMultiplied( dst, src, coeffSrc, frames );
return;
}

run<>( dst, src, frames, AddSanitizedMultipliedOp(coeffSrc) );
}



struct AddMultipliedStereoOp
{
AddMultipliedStereoOp( float coeffLeft, float coeffRight )
Expand Down
10 changes: 5 additions & 5 deletions src/core/Mixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,21 @@ void MixerChannel::doProcessing()
if( ! volBuf && ! sendBuf ) // neither volume nor send has sample-exact data...
{
const float v = sender->m_volumeModel.value() * sendModel->value();
MixHelpers::addSanitizedMultiplied( m_buffer, ch_buf, v, fpp );
MixHelpers::addMultiplied(m_buffer, ch_buf, v, fpp);
}
else if( volBuf && sendBuf ) // both volume and send have sample-exact data
{
MixHelpers::addSanitizedMultipliedByBuffers( m_buffer, ch_buf, volBuf, sendBuf, fpp );
MixHelpers::addMultipliedByBuffers(m_buffer, ch_buf, volBuf, sendBuf, fpp);
}
else if( volBuf ) // volume has sample-exact data but send does not
{
const float v = sendModel->value();
MixHelpers::addSanitizedMultipliedByBuffer( m_buffer, ch_buf, v, volBuf, fpp );
MixHelpers::addMultipliedByBuffer(m_buffer, ch_buf, v, volBuf, fpp);
}
else // vice versa
{
const float v = sender->m_volumeModel.value();
MixHelpers::addSanitizedMultipliedByBuffer( m_buffer, ch_buf, v, sendBuf, fpp );
MixHelpers::addMultipliedByBuffer(m_buffer, ch_buf, v, sendBuf, fpp);
}
m_hasInput = true;
}
Expand Down Expand Up @@ -679,7 +679,7 @@ void Mixer::masterMix( sampleFrame * _buf )
const float v = volBuf
? 1.0f
: m_mixerChannels[0]->m_volumeModel.value();
MixHelpers::addSanitizedMultiplied( _buf, m_mixerChannels[0]->m_buffer, v, fpp );
MixHelpers::addMultiplied(_buf, m_mixerChannels[0]->m_buffer, v, fpp);

// clear all channel buffers and
// reset channel process state
Expand Down

0 comments on commit acfd40a

Please sign in to comment.