File: line_model_monitor.cpp

package info (click to toggle)
spandsp 0.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,484 kB
  • ctags: 9,250
  • sloc: ansic: 123,577; xml: 13,229; sh: 12,145; cpp: 1,521; makefile: 1,101
file content (458 lines) | stat: -rw-r--r-- 13,278 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
 * SpanDSP - a series of DSP components for telephony
 *
 * line_model_monitor.cpp - Model activity in a telephone line model, using the FLTK toolkit.
 *
 * Written by Steve Underwood <steveu@coppice.org>
 *
 * Copyright (C) 2006 Steve Underwood
 *
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * 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; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#if defined(HAVE_FL_FL_H)  &&  defined(HAVE_FL_FL_CARTESIAN_H)

#define __STDC_LIMIT_MACROS

#include <inttypes.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#if defined(HAVE_FFTW3_H)
#include <fftw3.h>
#else
#include <fftw.h>
#endif

#include <FL/Fl.H>
#include <FL/Fl_Overlay_Window.H>
#include <FL/Fl_Light_Button.H>
#include <FL/Fl_Cartesian.H>
#include <FL/Fl_Audio_Meter.H>
#include <FL/fl_draw.H>

#include "spandsp.h"
#include "line_model_monitor.h"

struct line_model_monitor_s
{
    Fl_Double_Window *w;

    Fl_Audio_Meter *audio_meter;

    Fl_Group *c_spec;
    Fl_Group *c_right;
    Fl_Group *c_can;
    Fl_Group *c_line_model;

    Ca_Canvas *canvas_spec;
    Ca_X_Axis *spec_freq;
    Ca_Y_Axis *spec_amp;
    Ca_Line *spec_re;
    double spec_re_plot[2*512];

    Ca_Canvas *canvas_can;
    Ca_X_Axis *can_x;
    Ca_Y_Axis *can_y;
    Ca_Line *can_re;
    double can_re_plot[512];

    Ca_Canvas *canvas_line_model;
    Ca_X_Axis *line_model_x;
    Ca_Y_Axis *line_model_y;
    Ca_Line *line_model_re;
    double line_model_re_plot[512];

    int in_ptr;
#if defined(HAVE_FFTW3_H)
    double in[1024][2];
    double out[1024][2];
#else
    fftw_complex in[1024];
    fftw_complex out[1024];
#endif
    fftw_plan p;
};

static int skip = 0;
static struct line_model_monitor_s model;
static struct line_model_monitor_s *s = &model;

int line_model_monitor_can_update(const float *coeffs, int len)
{
    int i;
    float min;
    float max;

    if (s->can_re)
        delete s->can_re;

    s->canvas_can->current(s->canvas_can);
    i = 0;
    min = coeffs[i];
    max = coeffs[i];
    for (i = 0;  i < len;  i++)
    {
        s->can_re_plot[2*i] = i;
        s->can_re_plot[2*i + 1] = coeffs[i];
        if (min > coeffs[i])
            min = coeffs[i];
        if (max < coeffs[i])
            max = coeffs[i];
    }
    s->can_y->maximum((max == min)  ?  max + 0.2  :  max);
    s->can_y->minimum(min);
    s->can_re = new Ca_Line(len, s->can_re_plot, 0, 0, FL_BLUE, CA_NO_POINT);
    if (++skip >= 100)
    {
        skip = 0;
        Fl::check();
    }
    return 0;
}
/*- End of function --------------------------------------------------------*/

int line_model_monitor_line_model_update(const float *coeffs, int len)
{
    int i;
    float min;
    float max;

    if (s->line_model_re)
        delete s->line_model_re;

    s->canvas_line_model->current(s->canvas_line_model);
    i = 0;
    min = coeffs[i];
    max = coeffs[i];
    for (i = 0;  i < len;  i++)
    {
        s->line_model_re_plot[2*i] = i;
        s->line_model_re_plot[2*i + 1] = coeffs[i];
        if (min > coeffs[i])
            min = coeffs[i];
        if (max < coeffs[i])
            max = coeffs[i];
    }
    s->line_model_y->maximum((max == min)  ?  max + 0.2  :  max);
    s->line_model_y->minimum(min);
    s->line_model_re = new Ca_Line(len, s->line_model_re_plot, 0, 0, FL_BLUE, CA_NO_POINT);
    if (++skip >= 100)
    {
        skip = 0;
        Fl::check();
    }
    return 0;
}
/*- End of function --------------------------------------------------------*/

int line_model_monitor_line_spectrum_update(const int16_t amp[], int len)
{
    int i;
    int x;

    for (i = 0;  i < len;  i++)
        s->audio_meter->sample(amp[i]/32768.0);

    if (s->in_ptr + len < 512)
    {
        /* Just add this fragment to the buffer. */
        for (i = 0;  i < len;  i++)
#if defined(HAVE_FFTW3_H)
            s->in[s->in_ptr + i][0] = amp[i];
#else
            s->in[s->in_ptr + i].re = amp[i];
#endif
        s->in_ptr += len;
        return 0;
    }
    if (len >= 512)
    {
        /* We have enough for a whole block. Use the last 512 samples
           we have. */
        x = len - 512;
        for (i = 0;  i < 512;  i++)
#if defined(HAVE_FFTW3_H)
            s->in[i][0] = amp[x + i];
#else
            s->in[i].re = amp[x + i];
#endif
    }
    else
    {
        /* We want the last 512 samples. */
        x = 512 - len;
        for (i = 0;  i < x;  i++)
#if defined(HAVE_FFTW3_H)
            s->in[i][0] = s->in[s->in_ptr - x + i][0];
#else
            s->in[i].re = s->in[s->in_ptr - x + i].re;
#endif
        for (i = x;  i < 512;  i++)
#if defined(HAVE_FFTW3_H)
            s->in[i][0] = amp[i - x];
#else
            s->in[i].re = amp[i - x];
#endif
    }
    s->in_ptr = 0;
#if defined(HAVE_FFTW3_H)
    fftw_execute(s->p);
#else
    fftw_one(s->p, s->in, s->out);
#endif
    if (s->spec_re)
        delete s->spec_re;
    s->canvas_spec->current(s->canvas_spec);
    for (i = 0;  i < 512;  i++)
    {
        s->spec_re_plot[2*i] = i*4000.0/512.0;
#if defined(HAVE_FFTW3_H)
        s->spec_re_plot[2*i + 1] = 10.0*log10((s->out[i][0]*s->out[i][0] + s->out[i][1]*s->out[i][1])/(256.0*32768*256.0*32768) + 1.0e-10) + 3.14;
#else
        s->spec_re_plot[2*i + 1] = 10.0*log10((s->out[i].re*s->out[i].re + s->out[i].im*s->out[i].im)/(256.0*32768*256.0*32768) + 1.0e-10) + 3.14;
#endif
    }
    s->spec_re = new Ca_Line(512, s->spec_re_plot, 0, 0, FL_BLUE, CA_NO_POINT);
    Fl::check();
    return 0;
}
/*- End of function --------------------------------------------------------*/

int start_line_model_monitor(int len)
{
    char buf[132 + 1];
    float x;
    float y;
    int i;

    s->w = new Fl_Double_Window(850, 400, "Telephone line model monitor");

    s->c_spec = new Fl_Group(0, 0, 380, 400);
    s->c_spec->box(FL_DOWN_BOX);
    s->c_spec->align(FL_ALIGN_TOP | FL_ALIGN_INSIDE);

    s->canvas_spec = new Ca_Canvas(60, 30, 300, 300, "Spectrum");
    s->canvas_spec->box(FL_PLASTIC_DOWN_BOX);
    s->canvas_spec->color(7);
    s->canvas_spec->align(FL_ALIGN_TOP);
    s->canvas_spec->border(15);

    s->spec_freq = new Ca_X_Axis(65, 330, 290, 30, "Freq (Hz)");
    s->spec_freq->align(FL_ALIGN_BOTTOM);
    s->spec_freq->minimum(0);
    s->spec_freq->maximum(4000);
    s->spec_freq->label_format("%g");
    s->spec_freq->minor_grid_color(fl_gray_ramp(20));
    s->spec_freq->major_grid_color(fl_gray_ramp(15));
    s->spec_freq->label_grid_color(fl_gray_ramp(10));
    s->spec_freq->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
    s->spec_freq->minor_grid_style(FL_DOT);
    s->spec_freq->major_step(5);
    s->spec_freq->label_step(1);
    s->spec_freq->axis_color(FL_BLACK);
    s->spec_freq->axis_align(CA_BOTTOM | CA_LINE);

    s->spec_amp = new Ca_Y_Axis(20, 35, 40, 290, "Amp (dBmO)");
    s->spec_amp->align(FL_ALIGN_LEFT);
    s->spec_amp->minimum(-80.0);
    s->spec_amp->maximum(10.0);
    s->spec_amp->minor_grid_color(fl_gray_ramp(20));
    s->spec_amp->major_grid_color(fl_gray_ramp(15));
    s->spec_amp->label_grid_color(fl_gray_ramp(10));
    //s->spec_amp->grid_visible(CA_MINOR_TICK | CA_MAJOR_TICK | CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
    s->spec_amp->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
    s->spec_amp->minor_grid_style(FL_DOT);
    s->spec_amp->major_step(5);
    s->spec_amp->label_step(1);
    s->spec_amp->axis_color(FL_BLACK);

    s->spec_amp->current();
    s->spec_re = NULL;

    s->c_spec->end();

    s->c_right = new Fl_Group(440, 0, 465, 405);

    s->c_can = new Fl_Group(380, 0, 415, 200);
    s->c_can->box(FL_DOWN_BOX);
    s->c_can->align(FL_ALIGN_TOP | FL_ALIGN_INSIDE);
    s->c_can->current();

    s->canvas_can = new Ca_Canvas(460, 35, 300, 100, "??? coefficients");
    s->canvas_can->box(FL_PLASTIC_DOWN_BOX);
    s->canvas_can->color(7);
    s->canvas_can->align(FL_ALIGN_TOP);
    Fl_Group::current()->resizable(s->canvas_can);
    s->canvas_can->border(15);

    s->can_x = new Ca_X_Axis(465, 135, 290, 30, "Tap");
    s->can_x->align(FL_ALIGN_BOTTOM);
    s->can_x->minimum(0.0);
    s->can_x->maximum((float) len);
    s->can_x->label_format("%g");
    s->can_x->minor_grid_color(fl_gray_ramp(20));
    s->can_x->major_grid_color(fl_gray_ramp(15));
    s->can_x->label_grid_color(fl_gray_ramp(10));
    s->can_x->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
    s->can_x->minor_grid_style(FL_DOT);
    s->can_x->major_step(5);
    s->can_x->label_step(1);
    s->can_x->axis_align(CA_BOTTOM | CA_LINE);
    s->can_x->axis_color(FL_BLACK);
    s->can_x->current();

    s->can_y = new Ca_Y_Axis(420, 40, 40, 90, "Amp");
    s->can_y->align(FL_ALIGN_LEFT);
    s->can_y->minimum(-0.1);
    s->can_y->maximum(0.1);
    s->can_y->minor_grid_color(fl_gray_ramp(20));
    s->can_y->major_grid_color(fl_gray_ramp(15));
    s->can_y->label_grid_color(fl_gray_ramp(10));
    s->can_y->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
    s->can_y->minor_grid_style(FL_DOT);
    s->can_y->major_step(5);
    s->can_y->label_step(1);
    s->can_y->axis_color(FL_BLACK);
    s->can_y->current();

    s->c_can->end();

    s->can_re = NULL;

    s->c_line_model = new Fl_Group(380, 200, 415, 200);
    s->c_line_model->box(FL_DOWN_BOX);
    s->c_line_model->align(FL_ALIGN_TOP | FL_ALIGN_INSIDE);
    s->c_line_model->current();

    s->canvas_line_model = new Ca_Canvas(460, 235, 300, 100, "Line impulse response model");
    s->canvas_line_model->box(FL_PLASTIC_DOWN_BOX);
    s->canvas_line_model->color(7);
    s->canvas_line_model->align(FL_ALIGN_TOP);
    Fl_Group::current()->resizable(s->canvas_line_model);
    s->canvas_line_model->border(15);

    s->line_model_x = new Ca_X_Axis(465, 335, 290, 30, "Tap");
    s->line_model_x->align(FL_ALIGN_BOTTOM);
    s->line_model_x->minimum(0.0);
    s->line_model_x->maximum((float) len);
    s->line_model_x->label_format("%g");
    s->line_model_x->minor_grid_color(fl_gray_ramp(20));
    s->line_model_x->major_grid_color(fl_gray_ramp(15));
    s->line_model_x->label_grid_color(fl_gray_ramp(10));
    s->line_model_x->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
    s->line_model_x->minor_grid_style(FL_DOT);
    s->line_model_x->major_step(5);
    s->line_model_x->label_step(1);
    s->line_model_x->axis_align(CA_BOTTOM | CA_LINE);
    s->line_model_x->axis_color(FL_BLACK);
    s->line_model_x->current();

    s->line_model_y = new Ca_Y_Axis(420, 240, 40, 90, "Amp");
    s->line_model_y->align(FL_ALIGN_LEFT);
    s->line_model_y->minimum(-0.1);
    s->line_model_y->maximum(0.1);
    s->line_model_y->minor_grid_color(fl_gray_ramp(20));
    s->line_model_y->major_grid_color(fl_gray_ramp(15));
    s->line_model_y->label_grid_color(fl_gray_ramp(10));
    s->line_model_y->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
    s->line_model_y->minor_grid_style(FL_DOT);
    s->line_model_y->major_step(5);
    s->line_model_y->label_step(1);
    s->line_model_y->axis_color(FL_BLACK);
    s->line_model_y->current();
    s->line_model_re = NULL;

    s->c_line_model->end();

    s->audio_meter = new Fl_Audio_Meter(810, 40, 10, 250, "");
    s->audio_meter->box(FL_PLASTIC_UP_BOX);
    s->audio_meter->type(FL_VERT_AUDIO_METER);

    s->c_right->end();

    Fl_Group::current()->resizable(s->c_right);
    s->w->end();
    s->w->show();

#if defined(HAVE_FFTW3_H)
    s->p = fftw_plan_dft_1d(1024, s->in, s->out, FFTW_BACKWARD, FFTW_ESTIMATE);
    for (i = 0;  i < 1024;  i++)
    {
        s->in[i][0] = 0.0;
        s->in[i][1] = 0.0;
    }
#else
    s->p = fftw_create_plan(1024, FFTW_BACKWARD, FFTW_ESTIMATE);
    for (i = 0;  i < 1024;  i++)
    {
        s->in[i].re = 0.0;
        s->in[i].im = 0.0;
    }
#endif
    s->in_ptr = 0;

    Fl::check();
    return 0;
}
/*- End of function --------------------------------------------------------*/

void line_model_monitor_wait_to_end(void) 
{
    fd_set rfds;
    int res;
    struct timeval tv;

    fprintf(stderr, "Processing complete.  Press the <enter> key to end\n");
    do
    {
        usleep(100000);
        Fl::check();
        FD_ZERO(&rfds);
        FD_SET(0, &rfds);
        tv.tv_usec = 100000;
        tv.tv_sec = 0;
        res = select(1, &rfds, NULL, NULL, &tv);
    }
    while (res <= 0);
}
/*- End of function --------------------------------------------------------*/

void line_model_monitor_update_display(void) 
{
    Fl::check();
    Fl::check();
    Fl::check();
    Fl::check();
    Fl::check();
    Fl::check();
    Fl::check();
    Fl::check();
    Fl::check();
    Fl::check();
    Fl::check();
    Fl::check();
}
/*- End of function --------------------------------------------------------*/
#endif
/*- End of file ------------------------------------------------------------*/