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.

190 lines
4.7KB

  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. const float METER_UPDATE_FREQ = 0.1f;
  26. Meter_Module::Meter_Module ( )
  27. : Module ( 50, 100, name() )
  28. {
  29. box( FL_THIN_UP_FRAME );
  30. dpm_pack = new Fl_Scalepack( x(), y(), w(), h() );
  31. dpm_pack->type( FL_HORIZONTAL );
  32. control_value = 0;
  33. color( FL_BLACK );
  34. end();
  35. Port p( this, Port::OUTPUT, Port::CONTROL, "dB level" );
  36. p.hints.type = Port::Hints::LOGARITHMIC;
  37. p.hints.ranged = true;
  38. p.hints.maximum = 6.0f;
  39. p.hints.minimum = -70.0f;
  40. p.hints.dimensions = 1;
  41. p.connect_to( new float[1] );
  42. p.control_value_no_callback( -70.0f );
  43. add_port( p );
  44. Fl::add_timeout( METER_UPDATE_FREQ, update_cb, this );
  45. log_create();
  46. }
  47. Meter_Module::~Meter_Module ( )
  48. {
  49. if ( control_value )
  50. delete[] control_value;
  51. Fl::remove_timeout( update_cb, this );
  52. log_destroy();
  53. }
  54. void
  55. Meter_Module::update_cb ( void *v )
  56. {
  57. ((Meter_Module*)v)->update_cb();
  58. }
  59. void
  60. Meter_Module::update_cb ( void )
  61. {
  62. Fl::repeat_timeout( METER_UPDATE_FREQ, update_cb, this );
  63. for ( int i = dpm_pack->children(); i--; )
  64. ((DPM*)dpm_pack->child( i ))->value( control_value[i] );
  65. }
  66. bool
  67. Meter_Module::configure_inputs ( int n )
  68. {
  69. int tx, ty, tw, th;
  70. bbox( tx,ty,tw,th );
  71. int on = audio_input.size();
  72. if ( n > on )
  73. {
  74. for ( int i = on; i < n; ++i )
  75. {
  76. DPM *dpm = new DPM( tx, ty, tw, th );
  77. dpm->type( FL_VERTICAL );
  78. align( (Fl_Align)(FL_ALIGN_CENTER | FL_ALIGN_INSIDE ) );
  79. dpm_pack->add( dpm );
  80. add_port( Port( this, Port::INPUT, Port::AUDIO ) );
  81. add_port( Port( this, Port::OUTPUT, Port::AUDIO ) );
  82. }
  83. }
  84. else
  85. {
  86. for ( int i = on; i > n; --i )
  87. {
  88. DPM *dpm = (DPM*)dpm_pack->child( dpm_pack->children() - 1 );
  89. dpm_pack->remove( dpm );
  90. delete dpm;
  91. audio_input.back().disconnect();
  92. audio_input.pop_back();
  93. audio_output.back().disconnect();
  94. audio_output.pop_back();
  95. }
  96. }
  97. control_output[0].hints.dimensions = n;
  98. delete[] (float*)control_output[0].buffer();
  99. {
  100. float *f = new float[n];
  101. for ( int i = n; i--; )
  102. f[i] = -70.0f;
  103. control_output[0].connect_to( f );
  104. }
  105. if ( control_value )
  106. delete [] control_value;
  107. control_value = new float[n];
  108. for ( int i = n; i--; )
  109. control_value[i] = -70.0f;
  110. return true;
  111. }
  112. int
  113. Meter_Module::handle ( int m )
  114. {
  115. return Fl_Group::handle( m );
  116. }
  117. static float
  118. get_peak_sample ( const sample_t* buf, nframes_t nframes )
  119. {
  120. float p = 0.0f;
  121. const sample_t *f = buf;
  122. for ( int j = nframes; j--; ++f )
  123. {
  124. float s = *f;
  125. /* rectify */
  126. if ( s < 0.0f )
  127. s = 0 - s;
  128. if ( s > p )
  129. p = s;
  130. }
  131. return p;
  132. }
  133. void
  134. Meter_Module::process ( void )
  135. {
  136. for ( unsigned int i = 0; i < audio_input.size(); ++i )
  137. {
  138. if ( audio_input[i].connected() )
  139. {
  140. float dB = 20 * log10( get_peak_sample( (float*)audio_input[i].buffer(), nframes() ) / 2.0f );
  141. ((float*)control_output[0].buffer())[i] = dB;
  142. control_value[i] = dB;
  143. }
  144. }
  145. }