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.

222 lines
5.3KB

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