From d35df8ee7b4dfd23bd748485cd271face09afe5f Mon Sep 17 00:00:00 2001 From: Lukas W Date: Sun, 25 Aug 2019 23:47:03 +0200 Subject: [PATCH] Move BufferPool::clear to MixHelpers and rewrite * Move out of BufferPool because it's not related to memory allocation * Rewrite because memset() is not portable because it sets all bytes to zero which depends on the platform's float representation of 0.0 to be all zero bytes --- include/BufferPool.h | 7 ------- include/MixHelpers.h | 5 +++++ src/core/BufferPool.cpp | 14 -------------- src/core/FxMixer.cpp | 6 +++--- src/core/MixHelpers.cpp | 16 ++++++++++++++++ src/core/Mixer.cpp | 7 ++++--- src/core/Oscillator.cpp | 4 ++-- src/core/PlayHandle.cpp | 5 +++-- src/core/RemotePlugin.cpp | 8 ++++---- src/core/audio/AudioPort.cpp | 2 +- src/gui/widgets/VisualizationWidget.cpp | 4 ++-- 11 files changed, 40 insertions(+), 38 deletions(-) diff --git a/include/BufferPool.h b/include/BufferPool.h index 6d5cd08e2..9a552faae 100644 --- a/include/BufferPool.h +++ b/include/BufferPool.h @@ -34,12 +34,5 @@ class LMMS_EXPORT BufferPool public: static void init( fpp_t framesPerPeriod ); static sampleFrame * acquire(); - // audio-buffer-mgm - static void clear( sampleFrame * ab, const f_cnt_t frames, - const f_cnt_t offset = 0 ); -#ifndef LMMS_DISABLE_SURROUND - static void clear( surroundSampleFrame * ab, const f_cnt_t frames, - const f_cnt_t offset = 0 ); -#endif static void release( sampleFrame * buf ); }; diff --git a/include/MixHelpers.h b/include/MixHelpers.h index 872319f82..2bef776c5 100644 --- a/include/MixHelpers.h +++ b/include/MixHelpers.h @@ -37,6 +37,11 @@ bool useNaNHandler(); void setNaNHandler( bool use ); +void clear(sampleFrame * src, int frames); +#ifndef LMMS_DISABLE_SURROUND +void clear(surroundSampleFrame * src, int frames); +#endif + bool sanitize( sampleFrame * src, int frames ); /*! \brief Add samples from src to dst */ diff --git a/src/core/BufferPool.cpp b/src/core/BufferPool.cpp index 72dc60a55..4e52e3826 100644 --- a/src/core/BufferPool.cpp +++ b/src/core/BufferPool.cpp @@ -42,20 +42,6 @@ sampleFrame * BufferPool::acquire() return reinterpret_cast(pool->allocate()); } -void BufferPool::clear( sampleFrame *ab, const f_cnt_t frames, const f_cnt_t offset ) -{ - memset( ab + offset, 0, sizeof( *ab ) * frames ); -} - -#ifndef LMMS_DISABLE_SURROUND -void BufferPool::clear( surroundSampleFrame * ab, const f_cnt_t frames, - const f_cnt_t offset ) -{ - memset( ab + offset, 0, sizeof( *ab ) * frames ); -} -#endif - - void BufferPool::release( sampleFrame * buf ) { pool->deallocate(buf); diff --git a/src/core/FxMixer.cpp b/src/core/FxMixer.cpp index 7d33e1af1..4eb44ab89 100644 --- a/src/core/FxMixer.cpp +++ b/src/core/FxMixer.cpp @@ -74,7 +74,7 @@ FxChannel::FxChannel( int idx, Model * _parent ) : m_queued( false ), m_dependenciesMet(0) { - BufferPool::clear( m_buffer, Engine::mixer()->framesPerPeriod() ); + MixHelpers::clear( m_buffer, Engine::mixer()->framesPerPeriod() ); } @@ -585,7 +585,7 @@ void FxMixer::mixToChannel( const sampleFrame * _buf, fx_ch_t _ch ) void FxMixer::prepareMasterMix() { - BufferPool::clear( m_fxChannels[0]->m_buffer, + MixHelpers::clear( m_fxChannels[0]->m_buffer, Engine::mixer()->framesPerPeriod() ); } @@ -658,7 +658,7 @@ void FxMixer::masterMix( sampleFrame * _buf ) // reset channel process state for( int i = 0; i < numChannels(); ++i) { - BufferPool::clear( m_fxChannels[i]->m_buffer, + MixHelpers::clear( m_fxChannels[i]->m_buffer, Engine::mixer()->framesPerPeriod() ); m_fxChannels[i]->reset(); m_fxChannels[i]->m_queued = false; diff --git a/src/core/MixHelpers.cpp b/src/core/MixHelpers.cpp index de1c5cfc3..7b5ca4851 100644 --- a/src/core/MixHelpers.cpp +++ b/src/core/MixHelpers.cpp @@ -86,6 +86,22 @@ void setNaNHandler( bool use ) s_NaNHandler = use; } +void clear(sampleFrame * src, int frames) +{ + for (sampleFrame* frame = src; frame < src + frames; frame++) { + (*frame)[0] = (*frame)[1] = 0.0; + } +} + +#ifndef LMMS_DISABLE_SURROUND +void clear(surroundSampleFrame *src, int frames) +{ + for (surroundSampleFrame* frame = src; frame < src + frames; frame++) { + (*frame)[0] = (*frame)[1] = 0.0; + } +} +#endif + /*! \brief Function for sanitizing a buffer of infs/nans - returns true if those are found */ bool sanitize( sampleFrame * src, int frames ) { diff --git a/src/core/Mixer.cpp b/src/core/Mixer.cpp index 1cf063569..19bfe7758 100644 --- a/src/core/Mixer.cpp +++ b/src/core/Mixer.cpp @@ -37,6 +37,7 @@ #include "ConfigManager.h" #include "SamplePlayHandle.h" #include "Memory.h" +#include "MixHelpers.h" #include "BufferPool.h" // platform-specific audio-interface-classes @@ -97,7 +98,7 @@ Mixer::Mixer( bool renderOnly ) : m_inputBufferFrames[i] = 0; m_inputBufferSize[i] = DEFAULT_BUFFER_SIZE * 100; m_inputBuffer[i] = new sampleFrame[ DEFAULT_BUFFER_SIZE * 100 ]; - BufferPool::clear( m_inputBuffer[i], m_inputBufferSize[i] ); + MixHelpers::clear( m_inputBuffer[i], m_inputBufferSize[i] ); } // determine FIFO size and number of frames per period @@ -140,7 +141,7 @@ Mixer::Mixer( bool renderOnly ) : { m_readBuf = alloc.allocate( m_framesPerPeriod ); - BufferPool::clear( m_readBuf, m_framesPerPeriod ); + MixHelpers::clear( m_readBuf, m_framesPerPeriod ); m_bufferPool.push_back( m_readBuf ); } @@ -416,7 +417,7 @@ const surroundSampleFrame * Mixer::renderNextBuffer() m_readBuf = m_bufferPool[m_readBuffer]; // clear last audio-buffer - BufferPool::clear( m_writeBuf, m_framesPerPeriod ); + MixHelpers::clear( m_writeBuf, m_framesPerPeriod ); // prepare master mix (clear internal buffers etc.) FxMixer * fxMixer = Engine::fxMixer(); diff --git a/src/core/Oscillator.cpp b/src/core/Oscillator.cpp index bb06d9804..1437c789c 100644 --- a/src/core/Oscillator.cpp +++ b/src/core/Oscillator.cpp @@ -24,8 +24,8 @@ #include "Oscillator.h" -#include "BufferPool.h" #include "Engine.h" +#include "MixHelpers.h" #include "Mixer.h" #include "AutomatableModel.h" @@ -59,7 +59,7 @@ void Oscillator::update( sampleFrame * _ab, const fpp_t _frames, { if( m_freq >= Engine::mixer()->processingSampleRate() / 2 ) { - BufferPool::clear( _ab, _frames ); + MixHelpers::clear( _ab, _frames ); return; } if( m_subOsc != NULL ) diff --git a/src/core/PlayHandle.cpp b/src/core/PlayHandle.cpp index 67ddb5e35..04a1e5b73 100644 --- a/src/core/PlayHandle.cpp +++ b/src/core/PlayHandle.cpp @@ -22,10 +22,11 @@ * */ -#include "PlayHandle.h" #include "BufferPool.h" +#include "PlayHandle.h" #include "Engine.h" #include "Mixer.h" +#include "MixHelpers.h" #include #include @@ -54,7 +55,7 @@ void PlayHandle::doProcessing() if( m_usesBuffer ) { m_bufferReleased = false; - BufferPool::clear(m_playHandleBuffer, Engine::mixer()->framesPerPeriod()); + MixHelpers::clear(m_playHandleBuffer, Engine::mixer()->framesPerPeriod()); play( buffer() ); } else diff --git a/src/core/RemotePlugin.cpp b/src/core/RemotePlugin.cpp index 82bec2671..4121177ed 100644 --- a/src/core/RemotePlugin.cpp +++ b/src/core/RemotePlugin.cpp @@ -28,9 +28,9 @@ #include #endif -#include "BufferPool.h" #include "RemotePlugin.h" #include "Mixer.h" +#include "MixHelpers.h" #include "Engine.h" #include @@ -287,7 +287,7 @@ bool RemotePlugin::process( const sampleFrame * _in_buf, { if( _out_buf != NULL ) { - BufferPool::clear( _out_buf, frames ); + MixHelpers::clear( _out_buf, frames ); } return false; } @@ -306,7 +306,7 @@ bool RemotePlugin::process( const sampleFrame * _in_buf, } if( _out_buf != NULL ) { - BufferPool::clear( _out_buf, frames ); + MixHelpers::clear( _out_buf, frames ); } return false; } @@ -380,7 +380,7 @@ bool RemotePlugin::process( const sampleFrame * _in_buf, sampleFrame * o = (sampleFrame *) ( m_shm + m_inputCount*frames ); // clear buffer, if plugin didn't fill up both channels - BufferPool::clear( _out_buf, frames ); + MixHelpers::clear( _out_buf, frames ); for( ch_cnt_t ch = 0; ch < qMin( DEFAULT_CHANNELS, outputs ); ++ch ) diff --git a/src/core/audio/AudioPort.cpp b/src/core/audio/AudioPort.cpp index 91bd2d9d9..90d95cc9f 100644 --- a/src/core/audio/AudioPort.cpp +++ b/src/core/audio/AudioPort.cpp @@ -111,7 +111,7 @@ void AudioPort::doProcessing() const fpp_t fpp = Engine::mixer()->framesPerPeriod(); // clear the buffer - BufferPool::clear( m_portBuffer, fpp ); + MixHelpers::clear( m_portBuffer, fpp ); //qDebug( "Playhandles: %d", m_playHandles.size() ); for( PlayHandle * ph : m_playHandles ) // now we mix all playhandle buffers into the audioport buffer diff --git a/src/gui/widgets/VisualizationWidget.cpp b/src/gui/widgets/VisualizationWidget.cpp index 2d933ec80..0e8406755 100644 --- a/src/gui/widgets/VisualizationWidget.cpp +++ b/src/gui/widgets/VisualizationWidget.cpp @@ -31,11 +31,11 @@ #include "gui_templates.h" #include "MainWindow.h" #include "Mixer.h" +#include "MixHelpers.h" #include "Engine.h" #include "ToolTip.h" #include "Song.h" -#include "BufferPool.h" VisualizationWidget::VisualizationWidget( const QPixmap & _bg, QWidget * _p, @@ -55,7 +55,7 @@ VisualizationWidget::VisualizationWidget( const QPixmap & _bg, QWidget * _p, const fpp_t frames = Engine::mixer()->framesPerPeriod(); m_buffer = new sampleFrame[frames]; - BufferPool::clear( m_buffer, frames ); + MixHelpers::clear( m_buffer, frames ); ToolTip::add( this, tr( "Oscilloscope" ) );