Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
5.2KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2009 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. #include "Meter_Module.H"
  19. #include "DPM.H"
  20. #include <FL/Fl.H>
  21. #include <FL/Fl_Single_Window.H>
  22. #include <FL/Fl_Scalepack.H>
  23. #include "JACK/Port.H"
  24. #include <math.h>
  25. #include "FL/test_press.H"
  26. const float METER_UPDATE_FREQ = 0.1f;
  27. Meter_Module::Meter_Module ( )
  28. : Module ( 50, 100, name() )
  29. {
  30. box( FL_THIN_UP_FRAME );
  31. dpm_pack = new Fl_Scalepack( x(), y(), w(), h() );
  32. dpm_pack->type( FL_HORIZONTAL );
  33. control_value = 0;
  34. color( FL_BLACK );
  35. end();
  36. Port p( this, Port::OUTPUT, Port::CONTROL, "dB level" );
  37. p.hints.type = Port::Hints::LOGARITHMIC;
  38. p.hints.ranged = true;
  39. p.hints.maximum = 6.0f;
  40. p.hints.minimum = -70.0f;
  41. p.hints.dimensions = 1;
  42. p.connect_to( new float[1] );
  43. p.control_value_no_callback( -70.0f );
  44. add_port( p );
  45. Fl::add_timeout( METER_UPDATE_FREQ, update_cb, this );
  46. log_create();
  47. }
  48. Meter_Module::~Meter_Module ( )
  49. {
  50. if ( control_value )
  51. delete[] control_value;
  52. Fl::remove_timeout( update_cb, this );
  53. log_destroy();
  54. }
  55. void
  56. Meter_Module::update_cb ( void *v )
  57. {
  58. ((Meter_Module*)v)->update_cb();
  59. }
  60. void
  61. Meter_Module::update_cb ( void )
  62. {
  63. Fl::repeat_timeout( METER_UPDATE_FREQ, update_cb, this );
  64. for ( int i = dpm_pack->children(); i--; )
  65. ((DPM*)dpm_pack->child( i ))->value( control_value[i] );
  66. }
  67. bool
  68. Meter_Module::configure_inputs ( int n )
  69. {
  70. THREAD_ASSERT( UI );
  71. int tx, ty, tw, th;
  72. bbox( tx,ty,tw,th );
  73. int on = audio_input.size();
  74. if ( n > on )
  75. {
  76. for ( int i = on; i < n; ++i )
  77. {
  78. DPM *dpm = new DPM( tx, ty, tw, th );
  79. dpm->type( FL_VERTICAL );
  80. align( (Fl_Align)(FL_ALIGN_CENTER | FL_ALIGN_INSIDE ) );
  81. dpm_pack->add( dpm );
  82. add_port( Port( this, Port::INPUT, Port::AUDIO ) );
  83. add_port( Port( this, Port::OUTPUT, Port::AUDIO ) );
  84. }
  85. }
  86. else
  87. {
  88. for ( int i = on; i > n; --i )
  89. {
  90. DPM *dpm = (DPM*)dpm_pack->child( dpm_pack->children() - 1 );
  91. dpm_pack->remove( dpm );
  92. delete dpm;
  93. audio_input.back().disconnect();
  94. audio_input.pop_back();
  95. audio_output.back().disconnect();
  96. audio_output.pop_back();
  97. }
  98. }
  99. control_output[0].hints.dimensions = n;
  100. delete[] (float*)control_output[0].buffer();
  101. {
  102. float *f = new float[n];
  103. for ( int i = n; i--; )
  104. f[i] = -70.0f;
  105. control_output[0].connect_to( f );
  106. }
  107. if ( control_value )
  108. delete [] control_value;
  109. control_value = new float[n];
  110. for ( int i = n; i--; )
  111. control_value[i] = -70.0f;
  112. if ( control_output[0].connected() )
  113. control_output[0].connected_port()->module()->handle_control_changed( control_output[0].connected_port() );
  114. return true;
  115. }
  116. int
  117. Meter_Module::handle ( int m )
  118. {
  119. switch ( m )
  120. {
  121. case FL_PUSH:
  122. {
  123. if ( test_press( FL_BUTTON1 ) )
  124. {
  125. /* don't let Module::handle eat our click */
  126. return Fl_Group::handle( m );
  127. }
  128. return Module::handle( m );
  129. }
  130. }
  131. return Module::handle( m );
  132. }
  133. static float
  134. get_peak_sample ( const sample_t* buf, nframes_t nframes )
  135. {
  136. float p = 0.0f;
  137. const sample_t *f = buf;
  138. for ( int j = nframes; j--; ++f )
  139. {
  140. float s = *f;
  141. /* rectify */
  142. if ( s < 0.0f )
  143. s = 0 - s;
  144. if ( s > p )
  145. p = s;
  146. }
  147. return p;
  148. }
  149. void
  150. Meter_Module::process ( void )
  151. {
  152. for ( unsigned int i = 0; i < audio_input.size(); ++i )
  153. {
  154. if ( audio_input[i].connected() )
  155. {
  156. float dB = 20 * log10( get_peak_sample( (float*)audio_input[i].buffer(), nframes() ) / 2.0f );
  157. ((float*)control_output[0].buffer())[i] = dB;
  158. control_value[i] = dB;
  159. }
  160. }
  161. }