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.

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