6fbb2f381ce26e251320602f42ab91f41bddeb83
[debian/gnuradio] / gr-qtgui / src / lib / qtgui_sink_f.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008,2009 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <qtgui_sink_f.h>
28 #include <gr_io_signature.h>
29 #include <string.h>
30
31 #include <QTimer>
32
33 qtgui_sink_f_sptr
34 qtgui_make_sink_f (int fftsize, int wintype,
35                    double fc, double bw,
36                    const std::string &name,
37                    bool plotfreq, bool plotwaterfall,
38                    bool plotwaterfall3d, bool plottime,
39                    bool plotconst,
40                    bool use_openGL,
41                    QWidget *parent)
42 {
43   return qtgui_sink_f_sptr (new qtgui_sink_f (fftsize, wintype,
44                                               fc, bw, name,
45                                               plotfreq, plotwaterfall,
46                                               plotwaterfall3d, plottime,
47                                               plotconst,
48                                               use_openGL,
49                                               parent));
50 }
51
52 qtgui_sink_f::qtgui_sink_f (int fftsize, int wintype,
53                             double fc, double bw,
54                             const std::string &name,
55                             bool plotfreq, bool plotwaterfall,
56                             bool plotwaterfall3d, bool plottime,
57                             bool plotconst,
58                             bool use_openGL,
59                             QWidget *parent)
60   : gr_block ("sink_f",
61               gr_make_io_signature (1, 1, sizeof(float)),
62               gr_make_io_signature (0, 0, 0)),
63     d_fftsize(fftsize),
64     d_wintype((gr_firdes::win_type)(wintype)),
65     d_center_freq(fc), d_bandwidth(bw), d_name(name),
66     d_plotfreq(plotfreq), d_plotwaterfall(plotwaterfall),
67     d_plotwaterfall3d(plotwaterfall3d), d_plottime(plottime),
68     d_plotconst(plotconst),
69     d_parent(parent)
70 {
71   d_main_gui = NULL;
72   pthread_mutex_init(&d_pmutex, NULL);
73   lock();
74
75   // Perform fftshift operation;
76   // this is usually desired when plotting
77   d_shift = true;
78
79   d_fft = new gri_fft_complex (d_fftsize, true);
80
81   d_index = 0;
82   d_residbuf = new float[d_fftsize];
83
84   buildwindow();
85
86   initialize(use_openGL);
87 }
88
89 qtgui_sink_f::~qtgui_sink_f()
90 {
91   delete d_object;
92   delete [] d_residbuf;
93   delete d_fft;
94 }
95
96 void
97 qtgui_sink_f::forecast(int noutput_items, gr_vector_int &ninput_items_required)
98 {
99   unsigned int ninputs = ninput_items_required.size();
100   for (unsigned int i = 0; i < ninputs; i++) {
101     ninput_items_required[i] = std::min(d_fftsize, 8191);
102   }
103 }
104
105 void qtgui_sink_f::lock()
106 {
107   pthread_mutex_lock(&d_pmutex);
108 }
109
110 void qtgui_sink_f::unlock()
111 {
112   pthread_mutex_unlock(&d_pmutex);
113 }
114
115 void
116 qtgui_sink_f::initialize(const bool opengl)
117 {
118   if(qApp != NULL) {
119     d_qApplication = qApp;
120   }
121   else {
122     int argc;
123     char **argv = NULL;
124     d_qApplication = new QApplication(argc, argv);
125   }
126
127
128   uint64_t maxBufferSize = 32768;
129   d_main_gui = new SpectrumGUIClass(maxBufferSize, d_fftsize,
130                                     d_center_freq, 
131                                     -d_bandwidth/2.0, 
132                                     d_bandwidth/2.0);
133   d_main_gui->SetDisplayTitle(d_name);
134   d_main_gui->SetFFTSize(d_fftsize);
135   d_main_gui->SetWindowType((int)d_wintype);
136
137   d_main_gui->OpenSpectrumWindow(d_parent,
138                                  d_plotfreq, d_plotwaterfall,
139                                  d_plotwaterfall3d, d_plottime,
140                                  d_plotconst,
141                                  opengl);
142
143   d_object = new qtgui_obj(d_qApplication);
144   qApp->postEvent(d_object, new qtgui_event(&d_pmutex));
145 }
146
147 void
148 qtgui_sink_f::exec_()
149 {
150   d_qApplication->exec();
151 }
152
153 QWidget*
154 qtgui_sink_f::qwidget()
155 {
156   return d_main_gui->qwidget();
157 }
158
159 PyObject*
160 qtgui_sink_f::pyqwidget()
161 {
162   PyObject *w = PyLong_FromVoidPtr((void*)d_main_gui->qwidget());
163   PyObject *retarg = Py_BuildValue("N", w);
164   return retarg;
165 }
166
167 void
168 qtgui_sink_f::set_frequency_range(const double centerfreq, 
169                                   const double bandwidth)
170 {
171   d_center_freq = centerfreq;
172   d_bandwidth = bandwidth;
173   d_main_gui->SetFrequencyRange(d_center_freq, 
174                                 -d_bandwidth/2.0,
175                                 d_bandwidth/2.0);
176 }
177
178 void
179 qtgui_sink_f::set_time_domain_axis(double min, double max)
180 {
181   d_main_gui->SetTimeDomainAxis(min, max);
182 }
183
184 void
185 qtgui_sink_f::set_constellation_axis(double xmin, double xmax,
186                                      double ymin, double ymax)
187 {
188   d_main_gui->SetConstellationAxis(xmin, xmax, ymin, ymax);
189 }
190
191 void 
192 qtgui_sink_f::set_constellation_pen_size(int size)
193 {
194   d_main_gui->SetConstellationPenSize(size);
195 }
196
197
198 void
199 qtgui_sink_f::set_frequency_axis(double min, double max)
200 {
201   d_main_gui->SetFrequencyAxis(min, max);
202 }
203
204 void
205 qtgui_sink_f::fft(const float *data_in, int size)
206 {
207   if (d_window.size()) {
208     gr_complex *dst = d_fft->get_inbuf();
209     for (int i = 0; i < size; i++)              // apply window
210       dst[i] = data_in[i] * d_window[i];
211   }
212   else {
213       gr_complex *dst = d_fft->get_inbuf();
214       for (int i = 0; i < size; i++)            // float to complex conversion
215         dst[i] = data_in[i];
216   }
217   
218   d_fft->execute ();     // compute the fft
219 }
220
221 void 
222 qtgui_sink_f::windowreset()
223 {
224   gr_firdes::win_type newwintype = (gr_firdes::win_type)d_main_gui->GetWindowType();  
225   if(d_wintype != newwintype) {
226     d_wintype = newwintype;
227     buildwindow();
228   }
229 }
230
231 void
232 qtgui_sink_f::buildwindow()
233 {
234   d_window.clear();
235   if(d_wintype != 0) {
236     d_window = gr_firdes::window(d_wintype, d_fftsize, 6.76);
237   }
238 }
239
240 void
241 qtgui_sink_f::fftresize()
242 {
243   int newfftsize = d_main_gui->GetFFTSize();
244
245   if(newfftsize != d_fftsize) {
246
247     // Resize residbuf and replace data
248     delete [] d_residbuf;
249     d_residbuf = new float[newfftsize];
250
251     // Set new fft size and reset buffer index 
252     // (throws away any currently held data, but who cares?) 
253     d_fftsize = newfftsize;
254     d_index = 0;
255     
256     // Reset window to reflect new size
257     buildwindow();
258
259     // Reset FFTW plan for new size
260     delete d_fft;
261     d_fft = new gri_fft_complex (d_fftsize, true);
262   }
263 }
264
265
266 int
267 qtgui_sink_f::general_work (int noutput_items,
268                             gr_vector_int &ninput_items,
269                             gr_vector_const_void_star &input_items,
270                             gr_vector_void_star &output_items)
271 {
272   int j=0;
273   const float *in = (const float*)input_items[0];
274
275   pthread_mutex_lock(&d_pmutex);
276
277   // Update the FFT size from the application
278   fftresize();
279   windowreset();
280
281   for(int i=0; i < noutput_items; i+=d_fftsize) {
282     unsigned int datasize = noutput_items - i;
283     unsigned int resid = d_fftsize-d_index;
284
285     // If we have enough input for one full FFT, do it
286     if(datasize >= resid) {
287       const timespec currentTime = get_highres_clock();
288       
289       // Fill up residbuf with d_fftsize number of items
290       memcpy(d_residbuf+d_index, &in[j], sizeof(float)*resid);
291       d_index = 0;
292
293       j += resid;
294       fft(d_residbuf, d_fftsize);
295       
296       d_main_gui->UpdateWindow(true, d_fft->get_outbuf(), d_fftsize,
297                                (float*)d_residbuf, d_fftsize, NULL, 0,
298                                1.0/4.0, currentTime, true);
299     }
300     // Otherwise, copy what we received into the residbuf for next time
301     else {
302       memcpy(d_residbuf+d_index, &in[j], sizeof(float)*datasize);
303       d_index += datasize;
304       j += datasize;
305     }   
306   }
307
308   pthread_mutex_unlock(&d_pmutex);
309
310   consume_each(j);
311   return j;
312 }