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.

203 lines
5.0KB

  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 ( int W, int, const char *L )
  27. : Module ( W, 100, L )
  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. }
  46. Meter_Module::~Meter_Module ( )
  47. {
  48. if ( control_value )
  49. delete[] control_value;
  50. }
  51. void
  52. Meter_Module::update_cb ( void *v )
  53. {
  54. ((Meter_Module*)v)->update_cb();
  55. }
  56. void
  57. Meter_Module::update_cb ( void )
  58. {
  59. Fl::repeat_timeout( METER_UPDATE_FREQ, update_cb, this );
  60. for ( int i = dpm_pack->children(); i--; )
  61. ((DPM*)dpm_pack->child( i ))->value( control_value[i] );
  62. }
  63. bool
  64. Meter_Module::configure_inputs ( int n )
  65. {
  66. int tx, ty, tw, th;
  67. bbox( tx,ty,tw,th );
  68. int on = audio_input.size();
  69. if ( n > on )
  70. {
  71. for ( int i = on; i < n; ++i )
  72. {
  73. DPM *dpm = new DPM( tx, ty, tw, th );
  74. dpm->type( FL_VERTICAL );
  75. align( (Fl_Align)(FL_ALIGN_CENTER | FL_ALIGN_INSIDE ) );
  76. dpm_pack->add( dpm );
  77. add_port( Port( this, Port::INPUT, Port::AUDIO ) );
  78. add_port( Port( this, Port::OUTPUT, Port::AUDIO ) );
  79. }
  80. }
  81. else
  82. {
  83. for ( int i = on; i > n; --i )
  84. {
  85. DPM *dpm = (DPM*)dpm_pack->child( dpm_pack->children() - 1 );
  86. dpm_pack->remove( dpm );
  87. delete dpm;
  88. audio_input.back().disconnect();
  89. audio_input.pop_back();
  90. audio_output.back().disconnect();
  91. audio_output.pop_back();
  92. control_output.back().disconnect();
  93. control_output.pop_back();
  94. }
  95. }
  96. control_output[0].hints.dimensions = n;
  97. delete[] (float*)control_output[0].buffer();
  98. {
  99. float *f = new float[n];
  100. for ( int i = n; i--; )
  101. f[i] = -70.0f;
  102. control_output[0].connect_to( f);
  103. }
  104. if ( control_value )
  105. delete [] control_value;
  106. control_value = new float[n];
  107. return true;
  108. }
  109. int
  110. Meter_Module::handle ( int m )
  111. {
  112. switch ( m )
  113. {
  114. case FL_PUSH:
  115. {
  116. /* Fl_Single_Window *win = new Fl_Single_Window( 0, 0, 400, 400 ); */
  117. /* win->add( dpm ); */
  118. /* win->resizable( dpm ); */
  119. /* win->end(); */
  120. /* win->show(); */
  121. /* break; */
  122. }
  123. }
  124. return Module::handle( m );
  125. }
  126. static float
  127. get_peak_sample ( const sample_t* buf, nframes_t nframes )
  128. {
  129. float p = 0.0f;
  130. const sample_t *f = buf;
  131. for ( int j = nframes; j--; ++f )
  132. {
  133. float s = *f;
  134. /* rectify */
  135. if ( s < 0.0f )
  136. s = 0 - s;
  137. if ( s > p )
  138. p = s;
  139. }
  140. return p;
  141. }
  142. void
  143. Meter_Module::process ( void )
  144. {
  145. for ( unsigned int i = 0; i < audio_input.size(); ++i )
  146. {
  147. if ( audio_input[i].connected() )
  148. {
  149. float dB = 20 * log10( get_peak_sample( (float*)audio_input[i].buffer(), nframes() ) / 2.0f );
  150. ((float*)control_output[0].buffer())[i] = dB;
  151. control_value[i] = dB;
  152. }
  153. }
  154. }