SpectrumAnalyzerControlDialog.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * SpectrumAnalyzerControlDialog.cpp - view for spectrum analyzer
  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 <cmath>
  25. #include <QLayout>
  26. #include <QPainter>
  27. #include "SpectrumAnalyzer.h"
  28. #include "MainWindow.h"
  29. #include "GuiApplication.h"
  30. #include "LedCheckbox.h"
  31. #include "embed.h"
  32. static inline void darken( QImage& img, int x, int y, int w, int h )
  33. {
  34. int imgWidth = img.width();
  35. QRgb * base = ( (QRgb *) img.bits() ) + y*imgWidth + x;
  36. for( int y = 0; y < h; ++y )
  37. {
  38. QRgb * d = base + y*imgWidth;
  39. for( int x = 0; x < w; ++x )
  40. {
  41. // shift each color component by 1 bit and set alpha
  42. // to 0xff
  43. d[x] = ( ( d[x] >> 1 ) & 0x7f7f7f7f ) | 0xff000000;
  44. }
  45. }
  46. }
  47. class SpectrumView : public QWidget
  48. {
  49. public:
  50. SpectrumView( SpectrumAnalyzer* s, QWidget * _parent ) :
  51. QWidget( _parent ),
  52. m_sa( s ),
  53. m_backgroundPlain( PLUGIN_NAME::getIconPixmap( "spectrum_background_plain" ).toImage() ),
  54. m_background( PLUGIN_NAME::getIconPixmap( "spectrum_background" ).toImage() )
  55. {
  56. setFixedSize( 249, 151 );
  57. connect( gui->mainWindow(), SIGNAL( periodicUpdate() ), this, SLOT( update() ) );
  58. setAttribute( Qt::WA_OpaquePaintEvent, true );
  59. }
  60. virtual ~SpectrumView()
  61. {
  62. }
  63. virtual void paintEvent( QPaintEvent* event )
  64. {
  65. QPainter p( this );
  66. QImage i = m_sa->m_saControls.m_linearSpec.value() ?
  67. m_backgroundPlain : m_background;
  68. const float e = m_sa->m_energy;
  69. if( e <= 0 )
  70. {
  71. darken( i, 0, 0, i.width(), i.height() );
  72. p.drawImage( 0, 0, i );
  73. return;
  74. }
  75. const bool lin_y = m_sa->m_saControls.m_linearYAxis.value();
  76. float * b = m_sa->m_bands;
  77. const int LOWER_Y = -60; // dB
  78. int h;
  79. const int fh = height();
  80. if( m_sa->m_saControls.m_linearSpec.value() )
  81. {
  82. if( lin_y )
  83. {
  84. for( int x = 0; x < MAX_BANDS; ++x, ++b )
  85. {
  86. h = fh * 2.0 / 3.0 * (*b / e );
  87. if( h < 0 ) h = 0; else if( h >= fh ) continue;
  88. darken( i, x, 0, 1, fh-h );
  89. }
  90. }
  91. else
  92. {
  93. for( int x = 0; x < MAX_BANDS; ++x, ++b )
  94. {
  95. h = (int)( fh * 2.0 / 3.0 * (20*(log10( *b / e ) ) - LOWER_Y ) / (-LOWER_Y ) );
  96. if( h < 0 ) h = 0; else if( h >= fh ) continue;
  97. darken( i, x, 0, 1, fh-h );
  98. }
  99. }
  100. }
  101. else
  102. {
  103. if( lin_y )
  104. {
  105. for( int x = 0; x < 31; ++x, ++b )
  106. {
  107. h = fh * 2.0 / 3.0 * ( 1.2 * *b / e );
  108. if( h < 0 ) h = 0; else if( h >= fh ) continue; else h = ( h / 3 ) * 3;
  109. darken( i, x*8, 0, 8, fh-h );
  110. }
  111. }
  112. else
  113. {
  114. for( int x = 0; x < 31; ++x, ++b )
  115. {
  116. h = (int)( fh * 2.0 / 3.0 * (20*(log10( *b / e ) ) - LOWER_Y ) / (-LOWER_Y ) );
  117. if( h < 0 ) h = 0; else if( h >= fh ) continue; else h = ( h / 3 ) * 3;
  118. darken( i, x*8, 0, 8, fh-h );
  119. }
  120. }
  121. darken( i, 31*8, 0, 1, fh );
  122. }
  123. p.drawImage( 0, 0, i );
  124. }
  125. private:
  126. SpectrumAnalyzer * m_sa;
  127. QImage m_backgroundPlain;
  128. QImage m_background;
  129. } ;
  130. SpectrumAnalyzerControlDialog::SpectrumAnalyzerControlDialog( SpectrumAnalyzerControls* controls ) :
  131. EffectControlDialog( controls ),
  132. m_controls( controls ),
  133. m_logXAxis( PLUGIN_NAME::getIconPixmap( "log_x_axis" ) ),
  134. m_logYAxis( PLUGIN_NAME::getIconPixmap( "log_y_axis" ) )
  135. {
  136. setAutoFillBackground( true );
  137. QPalette pal;
  138. pal.setBrush( backgroundRole(), PLUGIN_NAME::getIconPixmap( "background" ) );
  139. setFixedSize( 293, 205 );
  140. setPalette( pal );
  141. /* QVBoxLayout * l = new QVBoxLayout( this );*/
  142. SpectrumView* v = new SpectrumView( controls->m_effect, this );
  143. v->move( 34, 10 );
  144. LedCheckBox * lin_spec = new LedCheckBox( tr( "Linear spectrum" ), this );
  145. lin_spec->move( 32, 182 );
  146. lin_spec->setModel( &controls->m_linearSpec );
  147. LedCheckBox * lin_y = new LedCheckBox( tr( "Linear Y axis" ), this );
  148. lin_y->move( 137, 182 );
  149. lin_y->setModel( &controls->m_linearYAxis );
  150. connect( &controls->m_linearSpec, SIGNAL( dataChanged() ), this, SLOT( update() ) );
  151. connect( &controls->m_linearYAxis, SIGNAL( dataChanged() ), this, SLOT( update() ) );
  152. /* l->addWidget( v );
  153. l->addWidget( lin_spec );
  154. l->addWidget( lin_y );*/
  155. }
  156. void SpectrumAnalyzerControlDialog::paintEvent( QPaintEvent * )
  157. {
  158. QPainter p( this );
  159. if( !m_controls->m_linearSpec.value() )
  160. {
  161. p.drawPixmap( 33, 165, m_logXAxis );
  162. }
  163. if( !m_controls->m_linearYAxis.value() )
  164. {
  165. p.drawPixmap( 10, 29, m_logYAxis);
  166. }
  167. }