EqSpectrumView.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* eqspectrumview.cpp - implementation of EqSpectrumView class.
  2. *
  3. * Copyright (c) 2014-2017, David French <dave/dot/french3/at/googlemail/dot/com>
  4. *
  5. * This file is part of LMMS - https://lmms.io
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public
  17. * License along with this program (see COPYING); if not, write to the
  18. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19. * Boston, MA 02110-1301 USA.
  20. *
  21. */
  22. #include "EqSpectrumView.h"
  23. #include "AudioEngine.h"
  24. #include "Engine.h"
  25. #include "EqCurve.h"
  26. #include "GuiApplication.h"
  27. #include "MainWindow.h"
  28. EqAnalyser::EqAnalyser() :
  29. m_framesFilledUp ( 0 ),
  30. m_energy ( 0 ),
  31. m_sampleRate ( 1 ),
  32. m_active ( true )
  33. {
  34. m_inProgress=false;
  35. m_specBuf = ( fftwf_complex * ) fftwf_malloc( ( FFT_BUFFER_SIZE + 1 ) * sizeof( fftwf_complex ) );
  36. m_fftPlan = fftwf_plan_dft_r2c_1d( FFT_BUFFER_SIZE*2, m_buffer, m_specBuf, FFTW_MEASURE );
  37. //initialize Blackman-Harris window, constants taken from
  38. //https://en.wikipedia.org/wiki/Window_function#A_list_of_window_functions
  39. const float a0 = 0.35875;
  40. const float a1 = 0.48829;
  41. const float a2 = 0.14128;
  42. const float a3 = 0.01168;
  43. for (int i = 0; i < FFT_BUFFER_SIZE; i++)
  44. {
  45. m_fftWindow[i] = (a0 - a1 * cos(2 * F_PI * i / ((float)FFT_BUFFER_SIZE - 1.0))
  46. + a2 * cos(4 * F_PI * i / ((float)FFT_BUFFER_SIZE - 1.0))
  47. - a3 * cos(6 * F_PI * i / ((float)FFT_BUFFER_SIZE - 1.0)));
  48. }
  49. clear();
  50. }
  51. EqAnalyser::~EqAnalyser()
  52. {
  53. fftwf_destroy_plan( m_fftPlan );
  54. fftwf_free( m_specBuf );
  55. }
  56. void EqAnalyser::analyze( sampleFrame *buf, const fpp_t frames )
  57. {
  58. //only analyse if the view is visible
  59. if ( m_active )
  60. {
  61. m_inProgress=true;
  62. const int FFT_BUFFER_SIZE = 2048;
  63. fpp_t f = 0;
  64. if( frames > FFT_BUFFER_SIZE )
  65. {
  66. m_framesFilledUp = 0;
  67. f = frames - FFT_BUFFER_SIZE;
  68. }
  69. // meger channels
  70. for( ; f < frames; ++f )
  71. {
  72. m_buffer[m_framesFilledUp] =
  73. ( buf[f][0] + buf[f][1] ) * 0.5;
  74. ++m_framesFilledUp;
  75. }
  76. if( m_framesFilledUp < FFT_BUFFER_SIZE )
  77. {
  78. m_inProgress = false;
  79. return;
  80. }
  81. m_sampleRate = Engine::audioEngine()->processingSampleRate();
  82. const int LOWEST_FREQ = 0;
  83. const int HIGHEST_FREQ = m_sampleRate / 2;
  84. //apply FFT window
  85. for( int i = 0; i < FFT_BUFFER_SIZE; i++ )
  86. {
  87. m_buffer[i] = m_buffer[i] * m_fftWindow[i];
  88. }
  89. fftwf_execute( m_fftPlan );
  90. absspec( m_specBuf, m_absSpecBuf, FFT_BUFFER_SIZE+1 );
  91. compressbands( m_absSpecBuf, m_bands, FFT_BUFFER_SIZE+1,
  92. MAX_BANDS,
  93. ( int )( LOWEST_FREQ * ( FFT_BUFFER_SIZE + 1 ) / ( float )( m_sampleRate / 2 ) ),
  94. ( int )( HIGHEST_FREQ * ( FFT_BUFFER_SIZE + 1) / ( float )( m_sampleRate / 2 ) ) );
  95. m_energy = maximum( m_bands, MAX_BANDS ) / maximum( m_buffer, FFT_BUFFER_SIZE );
  96. m_framesFilledUp = 0;
  97. m_inProgress = false;
  98. m_active = false;
  99. }
  100. }
  101. float EqAnalyser::getEnergy() const
  102. {
  103. return m_energy;
  104. }
  105. int EqAnalyser::getSampleRate() const
  106. {
  107. return m_sampleRate;
  108. }
  109. bool EqAnalyser::getActive() const
  110. {
  111. return m_active;
  112. }
  113. void EqAnalyser::setActive(bool active)
  114. {
  115. m_active = active;
  116. }
  117. bool EqAnalyser::getInProgress()
  118. {
  119. return m_inProgress;
  120. }
  121. void EqAnalyser::clear()
  122. {
  123. m_framesFilledUp = 0;
  124. m_energy = 0;
  125. memset( m_buffer, 0, sizeof( m_buffer ) );
  126. memset( m_bands, 0, sizeof( m_bands ) );
  127. }
  128. EqSpectrumView::EqSpectrumView(EqAnalyser *b, QWidget *_parent) :
  129. QWidget( _parent ),
  130. m_analyser( b ),
  131. m_periodicalUpdate( false )
  132. {
  133. setFixedSize( 450, 200 );
  134. connect( getGUI()->mainWindow(), SIGNAL( periodicUpdate() ), this, SLOT( periodicalUpdate() ) );
  135. setAttribute( Qt::WA_TranslucentBackground, true );
  136. m_skipBands = MAX_BANDS * 0.5;
  137. float totalLength = log10( 20000 );
  138. m_pixelsPerUnitWidth = width( ) / totalLength ;
  139. m_scale = 1.5;
  140. m_color = QColor( 255, 255, 255, 255 );
  141. for ( int i = 0 ; i < MAX_BANDS ; i++ )
  142. {
  143. m_bandHeight.append( 0 );
  144. }
  145. }
  146. void EqSpectrumView::paintEvent(QPaintEvent *event)
  147. {
  148. const float energy = m_analyser->getEnergy();
  149. if( energy <= 0 && m_peakSum <= 0 )
  150. {
  151. //dont draw anything
  152. return;
  153. }
  154. const int fh = height();
  155. const int LOWER_Y = -36; // dB
  156. QPainter painter( this );
  157. painter.setPen( QPen( m_color, 1, Qt::SolidLine, Qt::RoundCap, Qt::BevelJoin ) );
  158. painter.setRenderHint(QPainter::Antialiasing, true);
  159. if( m_analyser->getInProgress() || m_periodicalUpdate == false )
  160. {
  161. //only paint the cached path
  162. painter.fillPath( m_path, QBrush( m_color ) );
  163. return;
  164. }
  165. m_periodicalUpdate = false;
  166. //Now we calculate the path
  167. m_path = QPainterPath();
  168. float *bands = m_analyser->m_bands;
  169. float peak;
  170. m_path.moveTo( 0, height() );
  171. m_peakSum = 0;
  172. const float fallOff = 1.07;
  173. for( int x = 0; x < MAX_BANDS; ++x, ++bands )
  174. {
  175. peak = ( fh * 2.0 / 3.0 * ( 20 * ( log10( *bands / energy ) ) - LOWER_Y ) / ( - LOWER_Y ) );
  176. if( peak < 0 )
  177. {
  178. peak = 0;
  179. }
  180. else if( peak >= fh )
  181. {
  182. continue;
  183. }
  184. if ( peak > m_bandHeight[x] )
  185. {
  186. m_bandHeight[x] = peak;
  187. }
  188. else
  189. {
  190. m_bandHeight[x] = m_bandHeight[x] / fallOff;
  191. }
  192. if( m_bandHeight[x] < 0 )
  193. {
  194. m_bandHeight[x] = 0;
  195. }
  196. m_path.lineTo( EqHandle::freqToXPixel( bandToFreq( x ), width() ), fh - m_bandHeight[x] );
  197. m_peakSum += m_bandHeight[x];
  198. }
  199. m_path.lineTo( width(), height() );
  200. m_path.closeSubpath();
  201. painter.fillPath( m_path, QBrush( m_color ) );
  202. painter.drawPath( m_path );
  203. }
  204. QColor EqSpectrumView::getColor() const
  205. {
  206. return m_color;
  207. }
  208. void EqSpectrumView::setColor( const QColor &value )
  209. {
  210. m_color = value;
  211. }
  212. float EqSpectrumView::bandToFreq( int index )
  213. {
  214. return index * m_analyser->getSampleRate() / ( MAX_BANDS * 2 );
  215. }
  216. void EqSpectrumView::periodicalUpdate()
  217. {
  218. m_periodicalUpdate = true;
  219. m_analyser->setActive( isVisible() );
  220. update();
  221. }