2 /*******************************************************************************/
3 /* Copyright (C) 2009 Jonathan Moore Liles */
5 /* This program is free software; you can redistribute it and/or modify it */
6 /* under the terms of the GNU General Public License as published by the */
7 /* Free Software Foundation; either version 2 of the License, or (at your */
8 /* option) any later version. */
10 /* This program is distributed in the hope that it will be useful, but WITHOUT */
11 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
12 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
15 /* You should have received a copy of the GNU General Public License along */
16 /* with This program; see the file COPYING. If not,write to the Free Software */
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /*******************************************************************************/
24 #include <FL/Fl_Single_Window.H>
26 #include "FL/Fl_Scalepack.H"
27 #include "FL/test_press.H"
29 #include "Meter_Module.H"
31 #include "JACK/Port.H"
35 const float METER_UPDATE_FREQ = 0.2f;
39 Meter_Module::Meter_Module ( )
40 : Module ( 50, 100, name() )
42 box( FL_THIN_UP_FRAME );
43 dpm_pack = new Fl_Scalepack( x(), y(), w(), h() );
44 dpm_pack->type( FL_HORIZONTAL );
52 Port p( this, Port::OUTPUT, Port::CONTROL, "dB level" );
53 p.hints.type = Port::Hints::LOGARITHMIC;
54 p.hints.ranged = true;
55 p.hints.maximum = 6.0f;
56 p.hints.minimum = -70.0f;
57 p.hints.dimensions = 1;
58 p.connect_to( new float[1] );
59 p.control_value_no_callback( -70.0f );
63 Fl::add_timeout( METER_UPDATE_FREQ, update_cb, this );
68 Meter_Module::~Meter_Module ( )
71 delete[] control_value;
73 Fl::remove_timeout( update_cb, this );
81 Meter_Module::update_cb ( void *v )
83 ((Meter_Module*)v)->update_cb();
87 Meter_Module::update_cb ( void )
89 Fl::repeat_timeout( METER_UPDATE_FREQ, update_cb, this );
91 for ( int i = dpm_pack->children(); i--; )
92 ((DPM*)dpm_pack->child( i ))->value( control_value[i] );
96 Meter_Module::configure_inputs ( int n )
103 int on = audio_input.size();
107 for ( int i = on; i < n; ++i )
109 DPM *dpm = new DPM( tx, ty, tw, th );
110 dpm->type( FL_VERTICAL );
111 align( (Fl_Align)(FL_ALIGN_CENTER | FL_ALIGN_INSIDE ) );
113 dpm_pack->add( dpm );
115 add_port( Port( this, Port::INPUT, Port::AUDIO ) );
116 add_port( Port( this, Port::OUTPUT, Port::AUDIO ) );
122 for ( int i = on; i > n; --i )
124 DPM *dpm = (DPM*)dpm_pack->child( dpm_pack->children() - 1 );
125 dpm_pack->remove( dpm );
128 audio_input.back().disconnect();
129 audio_input.pop_back();
130 audio_output.back().disconnect();
131 audio_output.pop_back();
135 control_output[0].hints.dimensions = n;
136 delete[] (float*)control_output[0].buffer();
138 float *f = new float[n];
140 for ( int i = n; i--; )
143 control_output[0].connect_to( f );
147 delete [] control_value;
149 control_value = new float[n];
150 for ( int i = n; i--; )
151 control_value[i] = -70.0f;
153 if ( control_output[0].connected() )
154 control_output[0].connected_port()->module()->handle_control_changed( control_output[0].connected_port() );
162 Meter_Module::handle ( int m )
168 if ( test_press( FL_BUTTON1 ) )
170 /* don't let Module::handle eat our click */
171 return Fl_Group::handle( m );
173 return Module::handle( m );
177 return Module::handle( m );
187 get_peak_sample ( const sample_t* buf, nframes_t nframes )
191 const sample_t *f = buf;
193 for ( int j = nframes; j--; ++f )
195 const float s = fabs( *f );
205 Meter_Module::process ( nframes_t nframes )
207 for ( unsigned int i = 0; i < audio_input.size(); ++i )
209 if ( audio_input[i].connected() )
211 // float dB = 20 * log10( get_peak_sample( (float*)audio_input[i].buffer(), nframes ) / 2.0f );
212 float dB = 20 * log10( get_peak_sample( (float*)audio_input[i].buffer(), nframes ) );
214 ((float*)control_output[0].buffer())[i] = dB;
215 control_value[i] = dB;