2009-12-25 01:59:39 +01:00
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* Copyright (C) 2009 Jonathan Moore Liles */
|
|
|
|
|
/* */
|
|
|
|
|
/* This program is free software; you can redistribute it and/or modify it */
|
|
|
|
|
/* under the terms of the GNU General Public License as published by the */
|
|
|
|
|
/* Free Software Foundation; either version 2 of the License, or (at your */
|
|
|
|
|
/* option) any later version. */
|
|
|
|
|
/* */
|
|
|
|
|
/* This program is distributed in the hope that it will be useful, but WITHOUT */
|
|
|
|
|
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
|
|
|
|
|
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
|
|
|
|
|
/* more details. */
|
|
|
|
|
/* */
|
|
|
|
|
/* You should have received a copy of the GNU General Public License along */
|
|
|
|
|
/* with This program; see the file COPYING. If not,write to the Free Software */
|
|
|
|
|
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
|
2010-01-25 03:12:10 +01:00
|
|
|
|
#include "const.h"
|
|
|
|
|
|
|
|
|
|
#include <math.h>
|
2009-12-25 01:59:39 +01:00
|
|
|
|
#include <FL/Fl.H>
|
|
|
|
|
#include <FL/Fl_Single_Window.H>
|
2010-01-25 03:12:10 +01:00
|
|
|
|
|
|
|
|
|
#include "FL/Fl_Scalepack.H"
|
2010-01-25 02:47:10 +01:00
|
|
|
|
#include "FL/test_press.H"
|
2009-12-25 01:59:39 +01:00
|
|
|
|
|
2010-01-25 03:12:10 +01:00
|
|
|
|
#include "Meter_Module.H"
|
|
|
|
|
#include "DPM.H"
|
|
|
|
|
#include "JACK/Port.H"
|
|
|
|
|
|
2009-12-25 01:59:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const float METER_UPDATE_FREQ = 0.1f;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-28 06:25:28 +01:00
|
|
|
|
Meter_Module::Meter_Module ( )
|
|
|
|
|
: Module ( 50, 100, name() )
|
2009-12-25 01:59:39 +01:00
|
|
|
|
{
|
|
|
|
|
box( FL_THIN_UP_FRAME );
|
|
|
|
|
dpm_pack = new Fl_Scalepack( x(), y(), w(), h() );
|
|
|
|
|
dpm_pack->type( FL_HORIZONTAL );
|
|
|
|
|
|
2009-12-26 07:03:34 +01:00
|
|
|
|
control_value = 0;
|
|
|
|
|
|
2009-12-25 01:59:39 +01:00
|
|
|
|
color( FL_BLACK );
|
|
|
|
|
|
|
|
|
|
end();
|
|
|
|
|
|
|
|
|
|
Port p( this, Port::OUTPUT, Port::CONTROL, "dB level" );
|
|
|
|
|
p.hints.type = Port::Hints::LOGARITHMIC;
|
|
|
|
|
p.hints.ranged = true;
|
|
|
|
|
p.hints.maximum = 6.0f;
|
|
|
|
|
p.hints.minimum = -70.0f;
|
|
|
|
|
p.hints.dimensions = 1;
|
|
|
|
|
p.connect_to( new float[1] );
|
|
|
|
|
p.control_value_no_callback( -70.0f );
|
|
|
|
|
|
|
|
|
|
add_port( p );
|
|
|
|
|
|
|
|
|
|
Fl::add_timeout( METER_UPDATE_FREQ, update_cb, this );
|
|
|
|
|
|
2009-12-28 06:25:28 +01:00
|
|
|
|
log_create();
|
2009-12-25 01:59:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Meter_Module::~Meter_Module ( )
|
|
|
|
|
{
|
2009-12-26 07:03:34 +01:00
|
|
|
|
if ( control_value )
|
|
|
|
|
delete[] control_value;
|
2009-12-26 09:04:17 +01:00
|
|
|
|
|
|
|
|
|
Fl::remove_timeout( update_cb, this );
|
2009-12-28 06:25:28 +01:00
|
|
|
|
|
|
|
|
|
log_destroy();
|
2009-12-25 01:59:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-25 02:47:10 +01:00
|
|
|
|
|
|
|
|
|
|
2009-12-25 01:59:39 +01:00
|
|
|
|
void
|
|
|
|
|
Meter_Module::update_cb ( void *v )
|
|
|
|
|
{
|
|
|
|
|
((Meter_Module*)v)->update_cb();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Meter_Module::update_cb ( void )
|
|
|
|
|
{
|
|
|
|
|
Fl::repeat_timeout( METER_UPDATE_FREQ, update_cb, this );
|
|
|
|
|
|
|
|
|
|
for ( int i = dpm_pack->children(); i--; )
|
2009-12-26 07:03:34 +01:00
|
|
|
|
((DPM*)dpm_pack->child( i ))->value( control_value[i] );
|
2009-12-25 01:59:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
Meter_Module::configure_inputs ( int n )
|
|
|
|
|
{
|
2010-01-15 08:53:29 +01:00
|
|
|
|
THREAD_ASSERT( UI );
|
|
|
|
|
|
2009-12-25 01:59:39 +01:00
|
|
|
|
int tx, ty, tw, th;
|
|
|
|
|
bbox( tx,ty,tw,th );
|
|
|
|
|
|
|
|
|
|
int on = audio_input.size();
|
|
|
|
|
|
|
|
|
|
if ( n > on )
|
|
|
|
|
{
|
|
|
|
|
for ( int i = on; i < n; ++i )
|
|
|
|
|
{
|
|
|
|
|
DPM *dpm = new DPM( tx, ty, tw, th );
|
|
|
|
|
dpm->type( FL_VERTICAL );
|
|
|
|
|
align( (Fl_Align)(FL_ALIGN_CENTER | FL_ALIGN_INSIDE ) );
|
|
|
|
|
|
|
|
|
|
dpm_pack->add( dpm );
|
|
|
|
|
|
|
|
|
|
add_port( Port( this, Port::INPUT, Port::AUDIO ) );
|
|
|
|
|
add_port( Port( this, Port::OUTPUT, Port::AUDIO ) );
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for ( int i = on; i > n; --i )
|
|
|
|
|
{
|
|
|
|
|
DPM *dpm = (DPM*)dpm_pack->child( dpm_pack->children() - 1 );
|
|
|
|
|
dpm_pack->remove( dpm );
|
|
|
|
|
delete dpm;
|
|
|
|
|
|
|
|
|
|
audio_input.back().disconnect();
|
|
|
|
|
audio_input.pop_back();
|
|
|
|
|
audio_output.back().disconnect();
|
|
|
|
|
audio_output.pop_back();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
control_output[0].hints.dimensions = n;
|
|
|
|
|
delete[] (float*)control_output[0].buffer();
|
|
|
|
|
{
|
|
|
|
|
float *f = new float[n];
|
|
|
|
|
|
|
|
|
|
for ( int i = n; i--; )
|
|
|
|
|
f[i] = -70.0f;
|
|
|
|
|
|
2009-12-28 06:25:28 +01:00
|
|
|
|
control_output[0].connect_to( f );
|
2009-12-25 01:59:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
2009-12-26 07:03:34 +01:00
|
|
|
|
if ( control_value )
|
|
|
|
|
delete [] control_value;
|
|
|
|
|
|
|
|
|
|
control_value = new float[n];
|
2009-12-28 06:25:28 +01:00
|
|
|
|
for ( int i = n; i--; )
|
|
|
|
|
control_value[i] = -70.0f;
|
2009-12-26 07:03:34 +01:00
|
|
|
|
|
2010-01-15 08:53:29 +01:00
|
|
|
|
if ( control_output[0].connected() )
|
|
|
|
|
control_output[0].connected_port()->module()->handle_control_changed( control_output[0].connected_port() );
|
|
|
|
|
|
2009-12-25 01:59:39 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-25 03:12:10 +01:00
|
|
|
|
|
|
|
|
|
|
2009-12-25 01:59:39 +01:00
|
|
|
|
int
|
|
|
|
|
Meter_Module::handle ( int m )
|
|
|
|
|
{
|
2010-01-25 02:47:10 +01:00
|
|
|
|
switch ( m )
|
|
|
|
|
{
|
|
|
|
|
case FL_PUSH:
|
|
|
|
|
{
|
|
|
|
|
if ( test_press( FL_BUTTON1 ) )
|
|
|
|
|
{
|
|
|
|
|
/* don't let Module::handle eat our click */
|
|
|
|
|
return Fl_Group::handle( m );
|
|
|
|
|
}
|
|
|
|
|
return Module::handle( m );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Module::handle( m );
|
2009-12-26 07:23:51 +01:00
|
|
|
|
}
|
2010-01-25 02:47:10 +01:00
|
|
|
|
|
2009-12-25 01:59:39 +01:00
|
|
|
|
|
|
|
|
|
|
2010-01-25 03:12:10 +01:00
|
|
|
|
/**********/
|
|
|
|
|
/* Engine */
|
|
|
|
|
/**********/
|
|
|
|
|
|
2009-12-25 01:59:39 +01:00
|
|
|
|
static float
|
|
|
|
|
get_peak_sample ( const sample_t* buf, nframes_t nframes )
|
|
|
|
|
{
|
|
|
|
|
float p = 0.0f;
|
|
|
|
|
|
|
|
|
|
const sample_t *f = buf;
|
|
|
|
|
|
|
|
|
|
for ( int j = nframes; j--; ++f )
|
|
|
|
|
{
|
|
|
|
|
float s = *f;
|
|
|
|
|
|
|
|
|
|
/* rectify */
|
|
|
|
|
if ( s < 0.0f )
|
|
|
|
|
s = 0 - s;
|
|
|
|
|
|
|
|
|
|
if ( s > p )
|
|
|
|
|
p = s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Meter_Module::process ( void )
|
|
|
|
|
{
|
2009-12-26 07:03:34 +01:00
|
|
|
|
for ( unsigned int i = 0; i < audio_input.size(); ++i )
|
2009-12-25 01:59:39 +01:00
|
|
|
|
{
|
|
|
|
|
if ( audio_input[i].connected() )
|
|
|
|
|
{
|
|
|
|
|
float dB = 20 * log10( get_peak_sample( (float*)audio_input[i].buffer(), nframes() ) / 2.0f );
|
|
|
|
|
|
|
|
|
|
((float*)control_output[0].buffer())[i] = dB;
|
2009-12-26 07:03:34 +01:00
|
|
|
|
control_value[i] = dB;
|
2009-12-25 01:59:39 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|