123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- /*
- * CrossoverEQ.cpp - A native 4-band Crossover Equalizer
- * good for simulating tonestacks or simple peakless (flat-band) equalization
- *
- * Copyright (c) 2014 Vesa Kivimäki <contact/dot/diizy/at/nbl/dot/fi>
- * Copyright (c) 2006-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
- *
- * This file is part of LMMS - https://lmms.io
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public
- * License along with this program (see COPYING); if not, write to the
- * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301 USA.
- *
- */
-
- #include "CrossoverEQ.h"
- #include "lmms_math.h"
- #include "embed.h"
- #include "plugin_export.h"
- extern "C"
- {
- Plugin::Descriptor PLUGIN_EXPORT crossovereq_plugin_descriptor =
- {
- STRINGIFY( PLUGIN_NAME ),
- "Crossover Equalizer",
- QT_TRANSLATE_NOOP( "PluginBrowser", "A 4-band Crossover Equalizer" ),
- "Vesa Kivimäki <contact/dot/diizy/at/nbl/dot/fi>",
- 0x0100,
- Plugin::Effect,
- new PluginPixmapLoader( "logo" ),
- NULL,
- NULL
- };
- }
- CrossoverEQEffect::CrossoverEQEffect( Model* parent, const Descriptor::SubPluginFeatures::Key* key ) :
- Effect( &crossovereq_plugin_descriptor, parent, key ),
- m_controls( this ),
- m_sampleRate( Engine::mixer()->processingSampleRate() ),
- m_lp1( m_sampleRate ),
- m_lp2( m_sampleRate ),
- m_lp3( m_sampleRate ),
- m_hp2( m_sampleRate ),
- m_hp3( m_sampleRate ),
- m_hp4( m_sampleRate ),
- m_needsUpdate( true )
- {
- m_tmp1 = MM_ALLOC( sampleFrame, Engine::mixer()->framesPerPeriod() );
- m_tmp2 = MM_ALLOC( sampleFrame, Engine::mixer()->framesPerPeriod() );
- m_work = MM_ALLOC( sampleFrame, Engine::mixer()->framesPerPeriod() );
- }
- CrossoverEQEffect::~CrossoverEQEffect()
- {
- MM_FREE( m_tmp1 );
- MM_FREE( m_tmp2 );
- MM_FREE( m_work );
- }
- void CrossoverEQEffect::sampleRateChanged()
- {
- m_sampleRate = Engine::mixer()->processingSampleRate();
- m_lp1.setSampleRate( m_sampleRate );
- m_lp2.setSampleRate( m_sampleRate );
- m_lp3.setSampleRate( m_sampleRate );
- m_hp2.setSampleRate( m_sampleRate );
- m_hp3.setSampleRate( m_sampleRate );
- m_hp4.setSampleRate( m_sampleRate );
- m_needsUpdate = true;
- }
- bool CrossoverEQEffect::processAudioBuffer( sampleFrame* buf, const fpp_t frames )
- {
- if( !isEnabled() || !isRunning () )
- {
- return( false );
- }
-
- // filters update
- if( m_needsUpdate || m_controls.m_xover12.isValueChanged() )
- {
- m_lp1.setLowpass( m_controls.m_xover12.value() );
- m_hp2.setHighpass( m_controls.m_xover12.value() );
- }
- if( m_needsUpdate || m_controls.m_xover23.isValueChanged() )
- {
- m_lp2.setLowpass( m_controls.m_xover23.value() );
- m_hp3.setHighpass( m_controls.m_xover23.value() );
- }
- if( m_needsUpdate || m_controls.m_xover34.isValueChanged() )
- {
- m_lp3.setLowpass( m_controls.m_xover34.value() );
- m_hp4.setHighpass( m_controls.m_xover34.value() );
- }
-
- // gain values update
- if( m_needsUpdate || m_controls.m_gain1.isValueChanged() )
- {
- m_gain1 = dbfsToAmp( m_controls.m_gain1.value() );
- }
- if( m_needsUpdate || m_controls.m_gain2.isValueChanged() )
- {
- m_gain2 = dbfsToAmp( m_controls.m_gain2.value() );
- }
- if( m_needsUpdate || m_controls.m_gain3.isValueChanged() )
- {
- m_gain3 = dbfsToAmp( m_controls.m_gain3.value() );
- }
- if( m_needsUpdate || m_controls.m_gain4.isValueChanged() )
- {
- m_gain4 = dbfsToAmp( m_controls.m_gain4.value() );
- }
-
- // mute values update
- const bool mute1 = m_controls.m_mute1.value();
- const bool mute2 = m_controls.m_mute2.value();
- const bool mute3 = m_controls.m_mute3.value();
- const bool mute4 = m_controls.m_mute4.value();
-
- m_needsUpdate = false;
-
- memset( m_work, 0, sizeof( sampleFrame ) * frames );
-
- // run temp bands
- for( int f = 0; f < frames; ++f )
- {
- m_tmp1[f][0] = m_lp2.update( buf[f][0], 0 );
- m_tmp1[f][1] = m_lp2.update( buf[f][1], 1 );
- m_tmp2[f][0] = m_hp3.update( buf[f][0], 0 );
- m_tmp2[f][1] = m_hp3.update( buf[f][1], 1 );
- }
- // run band 1
- if( mute1 )
- {
- for( int f = 0; f < frames; ++f )
- {
- m_work[f][0] += m_lp1.update( m_tmp1[f][0], 0 ) * m_gain1;
- m_work[f][1] += m_lp1.update( m_tmp1[f][1], 1 ) * m_gain1;
- }
- }
-
- // run band 2
- if( mute2 )
- {
- for( int f = 0; f < frames; ++f )
- {
- m_work[f][0] += m_hp2.update( m_tmp1[f][0], 0 ) * m_gain2;
- m_work[f][1] += m_hp2.update( m_tmp1[f][1], 1 ) * m_gain2;
- }
- }
-
- // run band 3
- if( mute3 )
- {
- for( int f = 0; f < frames; ++f )
- {
- m_work[f][0] += m_lp3.update( m_tmp2[f][0], 0 ) * m_gain3;
- m_work[f][1] += m_lp3.update( m_tmp2[f][1], 1 ) * m_gain3;
- }
- }
-
- // run band 4
- if( mute4 )
- {
- for( int f = 0; f < frames; ++f )
- {
- m_work[f][0] += m_hp4.update( m_tmp2[f][0], 0 ) * m_gain4;
- m_work[f][1] += m_hp4.update( m_tmp2[f][1], 1 ) * m_gain4;
- }
- }
-
- const float d = dryLevel();
- const float w = wetLevel();
- double outSum = 0.0;
- for( int f = 0; f < frames; ++f )
- {
- buf[f][0] = d * buf[f][0] + w * m_work[f][0];
- buf[f][1] = d * buf[f][1] + w * m_work[f][1];
- outSum += buf[f][0] * buf[f][0] + buf[f][1] * buf[f][1];
- }
-
- checkGate( outSum / frames );
-
- return isRunning();
- }
- void CrossoverEQEffect::clearFilterHistories()
- {
- m_lp1.clearHistory();
- m_lp2.clearHistory();
- m_lp3.clearHistory();
- m_hp2.clearHistory();
- m_hp3.clearHistory();
- m_hp4.clearHistory();
- }
- extern "C"
- {
- // necessary for getting instance out of shared lib
- PLUGIN_EXPORT Plugin * lmms_plugin_main( Model* parent, void* data )
- {
- return new CrossoverEQEffect( parent, static_cast<const Plugin::Descriptor::SubPluginFeatures::Key *>( data ) );
- }
- }
|