1 /***************************************************************************
2  *   Copyright (C) 2009 by Pere Ràfols Soler                               *
3  *   sapista2@gmail.com                                                    *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 
21 #ifndef PLOT_BODE_CURVE_H
22   #define PLOT_BODE_CURVE_H
23 
24 #include <iostream>
25 #include <gtkmm/drawingarea.h>
26 #include "filter.h"
27 #include "../eq_defines.h"
28 
29 #define MIN_FREQ 18.0
30 #define MAX_FREQ 22000.0
31 #define MIN_SPAN_DEC 0.5
32 #define CURVE_NUM_OF_POINTS 1000
33 #define GRID_VERTICAL_LINES 28
34 #define CURVE_MARGIN 8
35 #define CURVE_BORDER 1.5
36 #define CURVE_TEXT_OFFSET_X 18
37 #define CURVE_TEXT_OFFSET_Y 38
38 #define ZOOM_WIDGET_BORDER_Y 22
39 #define PLOT_HIGHT 300
40 #define PLOT_WIDTH 500
41 #define SCROLL_EVENT_INCREMENT 0.3
42 #define AUTO_REFRESH_TIMEOUT_MS 20
43 #define SPECTROGRAM_LINE_THICKNESS 3.0
44 
45 typedef struct
46 {
47   float Gain;
48   float Freq;
49   float Q;
50   bool bIsOn;
51   FilterType fType;
52 }FilterBandParams;
53 
54 class PlotEQCurve : public Gtk::DrawingArea
55 {
56   public:
57     PlotEQCurve(int iNumOfBands, int channels);
58     virtual ~PlotEQCurve();
59     void resetCurve();
60     virtual void setBandGain(int bd_ix, float newGain);
61     virtual void setBandFreq(int bd_ix, float newFreq);
62     virtual void setBandQ(int bd_ix, float newQ);
63     virtual void setBandType(int bd_ix, int newType);
64     virtual void setBandEnable(int bd_ix, bool bIsEnabled);
65     virtual void setBypass(bool bypass);
66     virtual void setSampleRate(double samplerate);
67     virtual void setFftData(double *fft_data);
68     virtual void setFftActive(bool active, bool isSpectrogram);
69     virtual void setFftGain(double g);
70     virtual void setFftRange(double r);
71     virtual void setFftHold(bool hold);
72     virtual void setPlotdBRange(double range);
73     virtual void glowBand(int band);
74     virtual void unglowBands();
75 
76     enum MSState { ML, DUAL, SR, MONO};
77     void setStereoState(int band, MSState state);
78 
79     //signal accessor:
80     //Slot prototype: void on_band_changed(int band_ix, float Gain, float Freq, float Q);
81     typedef sigc::signal<void, int, float, float, float> signal_BandChanged;
82     signal_BandChanged signal_changed();
83 
84     //Slot prototype: void on_band_enabled(int band_ix, bool enabled);
85     typedef sigc::signal<void, int, bool> signal_BandEnabled;
86     signal_BandEnabled signal_enabled();
87 
88     //Slot prototype: void on_band_selected(int band_ix, bool enabled);
89     typedef sigc::signal<void, int> signal_BandSelected;
90     signal_BandSelected signal_selected();
91 
92     //Slot prototype: void on_band_selected(int band_ix, bool enabled);
93     typedef sigc::signal<void> signal_BandUnselected;
94     signal_BandUnselected signal_unselected();
95 
96   protected:
97       //Mouse grab signal handlers
98       virtual bool on_button_press_event(GdkEventButton* event);
99       virtual bool on_button_release_event(GdkEventButton* event);
100       virtual bool on_scrollwheel_event(GdkEventScroll* event);
101       virtual bool on_mouse_motion_event(GdkEventMotion* event);
102       virtual bool on_timeout_redraw();
103       virtual bool on_mouse_leave_widget(GdkEventCrossing* event);
104       virtual void cueBandRedraws(int band);
105       virtual void redraw_background_widget();
106       virtual void redraw_zoom_widget();
107       virtual void redraw_curve_widgets(int band);
108       virtual void redraw_main_curve();
109       virtual void redraw_grid_widget();
110       virtual void redraw_xAxis_widget();
111       virtual void redraw_yAxis_widget();
112       virtual void redraw_fft_widget();
113       virtual void redraw_cursor(double x, double y);
114 
115       //Override default signal handler:
116       virtual bool on_expose_event(GdkEventExpose* event);
117 
118   private:
119     int width, height;
120     int m_TotalBandsCount;
121     int m_NumChannels;
122     bool m_Bypass;
123     int m_iBandSel;
124     bool bMotionIsConnected;
125     bool bBandFocus;
126     bool *m_Bands2Redraw;
127     bool m_BandRedraw, m_fullRedraw, m_justRedraw;
128     double SampleRate;
129     bool m_FftActive;
130     double m_minFreq, m_maxFreq;
131     double m_dB_plot_range;
132 
133     //To hadle mouse mouve events
134     sigc::connection m_motion_connection;
135 
136     //Store filters data
137     FilterBandParams **m_filters;  //This pointer is initialized by construcor to an array of total num of bands
138 
139     //X axes LUT tables
140     int xPixels_Grid[GRID_VERTICAL_LINES]; //Pixels used to draw the grind in logspace
141     double *f; //This pointer is initialized by construcor to an array of total num of points acording min/max freq define
142     int *xPixels; //This pointer is initialized by construcor to an array of total num of points, each item is the pixel space transaltion of corresponding freq
143 
144     //Curve vector for Y axes in dB units
145     double **main_y; //This pointer is initialized by construcor to an array of total num of points acording the format main_y[channel][num_points]
146     double **band_y;  //This pointer is initialized by construcor to an array acording the format band_y[bd_ix][num_points]
147     MSState *band_state; //A vector containing the Stereo states for each band
148 
149     //FFT vectors
150     double *xPixels_fft, *xPixels_fft_bins;
151     double *fft_pink_noise;
152     double *fft_plot;
153     double *fft_ant_data;
154     double fft_gain;
155     double fft_range;
156     float *fft_log_lut;
157     bool m_bIsSpectrogram, m_bFftHold;
158     double *fft_raw_data;
159 
160     //Zoom widget data
161     struct zoom_widget
162     {
163       bool center_focus;
164       bool f1_focus;
165       bool f2_focus;
166       double x1;
167       double x2;
168       double x_ant;
169       bool center_press;
170       bool f1_press;
171       bool f2_press;
172     } m_zoom_widget;
173 
174 
175     //Cairo surfaces
176     Cairo::RefPtr<Cairo::ImageSurface> m_background_surface_ptr, m_fft_surface_ptr, m_zoom_surface_ptr, *m_curve_surface_ptr, m_maincurve_surface_ptr, m_grid_surface_ptr, m_xAxis_surface_ptr, m_yAxis_surface_ptr, m_cursor_surface_ptr;
177 
178     //Bode change signal
179     signal_BandChanged m_BandChangedSignal;
180     signal_BandEnabled m_BandEnabledSignal;
181     signal_BandSelected m_BandSelectedSignal;
182     signal_BandUnselected m_BandUnselectedSignal;
183 
184     //Function for dB to pixels convertion
185     double dB2Pixels(double db);
186 
187     //Function for Hz to pixels convertion
188     double freq2Pixels(double f);
189 
190     //Function for pixels to dB convertion
191     double Pixels2dB(double pixels);
192 
193     //Function for pixels to Hz convertion
194     double Pixels2freq(double pixels);
195 
196     //Compute a filter points
197     void ComputeFilter(int bd_ix);
198 
199     //Curve math functions
200     void CalcBand_DigitalFilter(int bd_ix);
201 
202     //Compute zoom bar
203     virtual void setCenterSpan(double center, double span);
204     virtual void resetCenterSpan();
205     virtual void setCenter(double center);
206     virtual void setSpan(double span);
207     void recomputeMinFreq_fromX1Pixel(double x1);
208     void recomputeMaxFreq_fromX2Pixel(double x2);
209     void recomputeCenterFreq(double xDiff);
210 };
211 #endif