/*******************************************************************************/ /* Copyright (C) 2013 Mark McCurry */ /* Copyright (C) 2013 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. */ /*******************************************************************************/ #include "SpectrumView.H" #include #include #include #include #include #include #include #include static std::map _cached_plan; float SpectrumView::_fmin = 0; float SpectrumView::_fmax = 0; unsigned int SpectrumView::_sample_rate = 0; void SpectrumView::clear_bands ( void ) { if ( _bands ) delete[] _bands; _bands = NULL; } void SpectrumView::data ( float *data, unsigned int nframes ) { if ( _data ) delete[] _data; _data = data; _nframes = nframes; clear_bands(); redraw(); } void SpectrumView::clear_plans ( void ) { /* invalidate all plans */ for ( std::map::iterator i = _cached_plan.begin(); i != _cached_plan.end(); i++ ) { delete[] i->second; } _cached_plan.clear(); } void SpectrumView::sample_rate ( unsigned int sample_rate ) { if ( _sample_rate != sample_rate ) { _sample_rate = sample_rate; _fmin = 10; /* _fmax = 28000; */ /* /\* if ( _fmax > _sample_rate * 0.5f ) *\/ */ _fmax = _sample_rate * 0.5f; clear_plans(); } } #define min(a,b) (a b=log(Fmin)/log(10) // log10(Fmax)=a+b -> a=log(Fmax)/log(10)-b const float b = logf(Fmin)/logf(10); const float a = logf(Fmax)/logf(10)-b; //Evaluate at set frequencies const float one_over_samples = 1.0f / samples; const float one_over_samplerate = 1.0f / Fs; for(unsigned i=0; i -0.001) { fl_line(xloc*W+x(), y(), xloc*W+x(), y()+H); if ( j == 1 || j == 2 || j == 5 ) { sprintf(label, "%0.f%s", freq < 1000.0 ? freq : freq / 1000.0, freq < 1000.0 ? "" : "k" ); int sx = x() + xloc*W + 1; if ( sx < x() * W - 20 ) fl_draw(label, sx, y()+h()); } } } } /* draw 0dB line */ { fl_line_style(FL_DASH,0); float i = ((_dbmax-_dbmin)+_dbmin) / (_dbmax-_dbmin); int level = y()+H*i; fl_color(fl_color_add_alpha(fl_rgb_color(240,240,240), 60 )); fl_line(x(), level, x()+W, level); fl_line_style(FL_SOLID,0); } } void SpectrumView::draw_curve ( void ) { if ( !_bands ) return; int W = w() - padding_right; //Build lines float inc = 1.0f / (float)W; float fx = 0; for( int i = 0; i < W; i++, fx += inc ) fl_vertex(fx, 1.0f - _bands[i]); } void SpectrumView::draw ( void ) { //Clear Widget Fl_Box::draw(); int W = w() - padding_right; int H = h() - padding_bottom; if ( !_bands ) { analyze_data( W ); } //Draw grid fl_color(fl_color_add_alpha(fl_rgb_color( 100,100,100), 50 )); draw_semilog(); fl_push_clip( x(),y(),W,H); fl_color(fl_color_add_alpha( selection_color(), 20 )); fl_push_matrix(); fl_translate( x(), y() + 2 ); fl_scale( W,H- 2 ); fl_begin_polygon(); fl_vertex(0.0,1.0); draw_curve(); fl_vertex(1.0,1.0); fl_end_polygon(); fl_color(fl_color_add_alpha( selection_color(), 100 )); fl_begin_line(); fl_line_style(FL_SOLID,2); /* fl_vertex(0.0,1.0); */ draw_curve(); /* fl_vertex(1.0,1.0); */ fl_end_line(); fl_pop_matrix(); fl_line_style(FL_SOLID,0); fl_pop_clip(); } void SpectrumView::resize ( int X, int Y, int W, int H ) { if ( W != w() ) clear_bands(); Fl_Box::resize(X,Y,W,H); }