Update NTK.
[nondaw.git] / mixer / src / Meter_Module.C
blobbb37c3ec1365b620af7b57976da35e7272b2470c
2 /*******************************************************************************/
3 /* Copyright (C) 2009 Jonathan Moore Liles                                     */
4 /*                                                                             */
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.                                                  */
9 /*                                                                             */
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   */
13 /* more details.                                                               */
14 /*                                                                             */
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 /*******************************************************************************/
20 #include "const.h"
22 #include <math.h>
23 #include <FL/Fl.H>
24 #include <FL/Fl_Single_Window.H>
26 #include "FL/Fl_Scalepack.H"
27 #include "FL/test_press.H"
29 #include "Meter_Module.H"
30 #include "DPM.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 );
46     control_value = 0;
48     color( FL_BLACK );
50     end();
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 );
61     add_port( p );
63     Fl::add_timeout( METER_UPDATE_FREQ, update_cb, this );
65     log_create();
68 Meter_Module::~Meter_Module ( )
70     if ( control_value )
71         delete[] control_value;
73     Fl::remove_timeout( update_cb, this );
75     log_destroy();
80 void
81 Meter_Module::update_cb ( void *v )
83     ((Meter_Module*)v)->update_cb();
86 void
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] );
95 bool
96 Meter_Module::configure_inputs ( int n )
98     THREAD_ASSERT( UI );
100     int tx, ty, tw, th;
101     bbox( tx,ty,tw,th );
103     int on = audio_input.size();
105     if ( n > on )
106     {
107         for ( int i = on; i < n; ++i )
108         {
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 ) );
118         }
119     }
120     else
121     {
122         for ( int i = on; i > n; --i )
123         {
124             DPM *dpm = (DPM*)dpm_pack->child( dpm_pack->children() - 1 );
125             dpm_pack->remove( dpm );
126             delete dpm;
128             audio_input.back().disconnect();
129             audio_input.pop_back();
130             audio_output.back().disconnect();
131             audio_output.pop_back();
132         }
133     }
135     control_output[0].hints.dimensions = n;
136     delete[] (float*)control_output[0].buffer();
137     {
138         float *f = new float[n];
140         for ( int i = n; i--; )
141             f[i] = -70.0f;
143         control_output[0].connect_to( f );
144     }
146     if ( control_value )
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() );
156     return true;
162 Meter_Module::handle ( int m )
164     switch ( m )
165     {
166         case FL_PUSH:
167         {
168             if ( test_press( FL_BUTTON1 ) )
169             {
170                 /* don't let Module::handle eat our click */
171                 return Fl_Group::handle( m );
172             }
173             return Module::handle( m );
174         }
175     }
177     return Module::handle( m );
182 /**********/
183 /* Engine */
184 /**********/
186 static float
187 get_peak_sample ( const sample_t* buf, nframes_t nframes )
189     float p = 0.0f;
191     const sample_t *f = buf;
193     for ( int j = nframes; j--; ++f )
194     {
195         const float s = fabs( *f );
197         if ( s > p )
198             p = s;
199     }
201     return p;
204 void
205 Meter_Module::process ( nframes_t nframes )
207     for ( unsigned int i = 0; i < audio_input.size(); ++i )
208     {
209         if ( audio_input[i].connected() )
210         {
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;
216         }
217     }