/* * spectrum_analyzer.cpp - spectrum analyzer plugin * * Copyright (c) 2008 Tobias Doerffel * * This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net * * 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 "spectrum_analyzer.h" #undef SINGLE_SOURCE_COMPILE #include "embed.cpp" extern "C" { plugin::descriptor PLUGIN_EXPORT spectrumanalyzer_plugin_descriptor = { STRINGIFY_PLUGIN_NAME( PLUGIN_NAME ), "Spectrum Analyzer", QT_TRANSLATE_NOOP( "pluginBrowser", "plugin for using arbitrary VST-effects " "inside LMMS." ), "Tobias Doerffel ", 0x0100, plugin::Effect, new pluginPixmapLoader( "logo" ), NULL } ; } spectrumAnalyzer::spectrumAnalyzer( model * _parent, const descriptor::subPluginFeatures::key * _key ) : effect( &spectrumanalyzer_plugin_descriptor, _parent, _key ), m_saControls( this ), m_framesFilledUp( 0 ), m_energy( 0 ) { m_specBuf = (fftwf_complex *) fftwf_malloc( ( BUFFER_SIZE + 1 ) * sizeof( fftwf_complex ) ); m_fftPlan = fftwf_plan_dft_r2c_1d( BUFFER_SIZE*2, m_buffer, m_specBuf, FFTW_MEASURE ); } spectrumAnalyzer::~spectrumAnalyzer() { fftwf_destroy_plan( m_fftPlan ); fftwf_free( m_specBuf ); } enum WINDOWS { KAISER=1, RECTANGLE, HANNING, HAMMING }; /* returns biggest value from abs_spectrum[spec_size] array returns -1 on error */ float maximum(float *abs_spectrum, unsigned int spec_size) { float maxi=0; unsigned int i; if ( abs_spectrum==NULL ) return -1; if (spec_size<=0) return -1; for ( i=0; imaxi ) maxi=abs_spectrum[i]; } return maxi; } /* apply hanning or hamming window to channel returns -1 on error */ int hanming(float *timebuffer, int length, int type) { int i; float alpha; if ( (timebuffer==NULL)||(length<=0) ) return -1; switch (type) { case HAMMING: alpha=0.54; break; case HANNING: default: alpha=0.5; break; } for ( i=0; i num_new returns 0 on success, else -1 */ int compressbands(float *absspec_buffer, float *compressedband, int num_old, int num_new, int bottom, int top) { float ratio; int i, usefromold; float j; float j_min, j_max; if ( (absspec_buffer==NULL)||(compressedband==NULL) ) return -1; if ( num_old=num_old ) top=num_old-1; usefromold=num_old-(num_old-top)-bottom; ratio=(float)usefromold/(float)num_new; // foreach new subband for ( i=0; i BUFFER_SIZE ) { m_framesFilledUp = 0; f = _frames - BUFFER_SIZE; } const int cm = m_saControls.m_channelMode.value(); switch( cm ) { case MergeChannels: for( ; f < _frames; ++f ) { m_buffer[m_framesFilledUp] = ( _buf[f][0] + _buf[f][1] ) * 0.5; ++m_framesFilledUp; } break; case LeftChannel: for( ; f < _frames; ++f ) { m_buffer[m_framesFilledUp] = _buf[f][0]; ++m_framesFilledUp; } break; case RightChannel: for( ; f < _frames; ++f ) { m_buffer[m_framesFilledUp] = _buf[f][1]; ++m_framesFilledUp; } break; } if( m_framesFilledUp < BUFFER_SIZE ) { return( isRunning() ); } // hanming( m_buffer, BUFFER_SIZE, HAMMING ); const sample_rate_t sr = engine::getMixer()->processingSampleRate(); const int LOWEST_FREQ = 0; const int HIGHEST_FREQ = sr / 2; fftwf_execute( m_fftPlan ); absspec( m_specBuf, m_absSpecBuf, BUFFER_SIZE+1 ); if( m_saControls.m_linearSpec.value() ) { compressbands( m_absSpecBuf, m_bands, BUFFER_SIZE+1, MAX_BANDS, LOWEST_FREQ*(BUFFER_SIZE+1)/(float)(sr/2), HIGHEST_FREQ*(BUFFER_SIZE+1)/(float)(sr/2) ); m_energy = maximum( m_bands, MAX_BANDS ) / maximum( m_buffer, BUFFER_SIZE ); } else { calc13octaveband31( m_absSpecBuf, m_bands, BUFFER_SIZE+1, sr/2.0); m_energy = signalpower( m_buffer, BUFFER_SIZE ) / maximum( m_buffer, BUFFER_SIZE ); } m_framesFilledUp = 0; checkGate( 0 ); return( isRunning() ); } extern "C" { // neccessary for getting instance out of shared lib plugin * PLUGIN_EXPORT lmms_plugin_main( model * _parent, void * _data ) { return( new spectrumAnalyzer( _parent, static_cast( _data ) ) ); } }