SpectrumAnalyzer.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * SpectrumAnalyzer.cpp - spectrum analyzer effect plugin
  3. *
  4. * Copyright (c) 2008-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #include "SpectrumAnalyzer.h"
  25. #include "embed.cpp"
  26. extern "C"
  27. {
  28. Plugin::Descriptor PLUGIN_EXPORT spectrumanalyzer_plugin_descriptor =
  29. {
  30. STRINGIFY( PLUGIN_NAME ),
  31. "Spectrum Analyzer",
  32. QT_TRANSLATE_NOOP( "pluginBrowser", "Graphical spectrum analyzer plugin" ),
  33. "Tobias Doerffel <tobydox/at/users.sf.net>",
  34. 0x0100,
  35. Plugin::Effect,
  36. new PluginPixmapLoader( "logo" ),
  37. NULL,
  38. NULL
  39. } ;
  40. }
  41. SpectrumAnalyzer::SpectrumAnalyzer( Model * _parent,
  42. const Descriptor::SubPluginFeatures::Key * _key ) :
  43. Effect( &spectrumanalyzer_plugin_descriptor, _parent, _key ),
  44. m_saControls( this ),
  45. m_framesFilledUp( 0 ),
  46. m_energy( 0 )
  47. {
  48. memset( m_buffer, 0, sizeof( m_buffer ) );
  49. m_specBuf = (fftwf_complex *) fftwf_malloc( ( FFT_BUFFER_SIZE + 1 ) * sizeof( fftwf_complex ) );
  50. m_fftPlan = fftwf_plan_dft_r2c_1d( FFT_BUFFER_SIZE*2, m_buffer, m_specBuf, FFTW_MEASURE );
  51. }
  52. SpectrumAnalyzer::~SpectrumAnalyzer()
  53. {
  54. fftwf_destroy_plan( m_fftPlan );
  55. fftwf_free( m_specBuf );
  56. }
  57. bool SpectrumAnalyzer::processAudioBuffer( sampleFrame* _buf, const fpp_t _frames )
  58. {
  59. if( !isEnabled() || !isRunning () )
  60. {
  61. return false;
  62. }
  63. if( !m_saControls.isViewVisible() )
  64. {
  65. return true;
  66. }
  67. fpp_t f = 0;
  68. if( _frames > FFT_BUFFER_SIZE )
  69. {
  70. m_framesFilledUp = 0;
  71. f = _frames - FFT_BUFFER_SIZE;
  72. }
  73. const int cm = m_saControls.m_channelMode.value();
  74. switch( cm )
  75. {
  76. case MergeChannels:
  77. for( ; f < _frames; ++f )
  78. {
  79. m_buffer[m_framesFilledUp] =
  80. ( _buf[f][0] + _buf[f][1] ) * 0.5;
  81. ++m_framesFilledUp;
  82. }
  83. break;
  84. case LeftChannel:
  85. for( ; f < _frames; ++f )
  86. {
  87. m_buffer[m_framesFilledUp] = _buf[f][0];
  88. ++m_framesFilledUp;
  89. }
  90. break;
  91. case RightChannel:
  92. for( ; f < _frames; ++f )
  93. {
  94. m_buffer[m_framesFilledUp] = _buf[f][1];
  95. ++m_framesFilledUp;
  96. }
  97. break;
  98. }
  99. if( m_framesFilledUp < FFT_BUFFER_SIZE )
  100. {
  101. return isRunning();
  102. }
  103. // hanming( m_buffer, FFT_BUFFER_SIZE, HAMMING );
  104. const sample_rate_t sr = Engine::mixer()->processingSampleRate();
  105. const int LOWEST_FREQ = 0;
  106. const int HIGHEST_FREQ = sr / 2;
  107. fftwf_execute( m_fftPlan );
  108. absspec( m_specBuf, m_absSpecBuf, FFT_BUFFER_SIZE+1 );
  109. if( m_saControls.m_linearSpec.value() )
  110. {
  111. compressbands( m_absSpecBuf, m_bands, FFT_BUFFER_SIZE+1,
  112. MAX_BANDS,
  113. (int)(LOWEST_FREQ*(FFT_BUFFER_SIZE+1)/(float)(sr/2)),
  114. (int)(HIGHEST_FREQ*(FFT_BUFFER_SIZE+1)/(float)(sr/2)));
  115. m_energy = maximum( m_bands, MAX_BANDS ) / maximum( m_buffer, FFT_BUFFER_SIZE );
  116. }
  117. else
  118. {
  119. calc13octaveband31( m_absSpecBuf, m_bands, FFT_BUFFER_SIZE+1, sr/2.0 );
  120. m_energy = signalpower( m_buffer, FFT_BUFFER_SIZE ) / maximum( m_buffer, FFT_BUFFER_SIZE );
  121. }
  122. m_framesFilledUp = 0;
  123. checkGate( 1 );
  124. return isRunning();
  125. }
  126. extern "C"
  127. {
  128. // necessary for getting instance out of shared lib
  129. Plugin * PLUGIN_EXPORT lmms_plugin_main( Model* parent, void* data )
  130. {
  131. return new SpectrumAnalyzer( parent, static_cast<const Plugin::Descriptor::SubPluginFeatures::Key *>( data ) );
  132. }
  133. }