Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_squelch_base_ff.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2006 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 2, 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 <gr_squelch_base_ff.h>
28 #include <gr_io_signature.h>
29
30 gr_squelch_base_ff::gr_squelch_base_ff(const char *name, int ramp, bool gate) : 
31         gr_block(name,
32                  gr_make_io_signature(1, 1, sizeof(float)),
33                  gr_make_io_signature(1, 1, sizeof(float)))
34 {
35   set_ramp(ramp);
36   set_gate(gate);
37   d_state = ST_MUTED;
38   d_envelope = d_ramp ? 0.0 : 1.0;
39   d_ramped = 0;
40 }
41
42 int gr_squelch_base_ff::general_work(int noutput_items,
43                                      gr_vector_int &ninput_items,
44                                      gr_vector_const_void_star &input_items,
45                                      gr_vector_void_star &output_items)
46 {
47   const float *in = (const float *) input_items[0];
48   float *out = (float *) output_items[0];
49
50   int j = 0;
51
52   for (int i = 0; i < noutput_items; i++) {
53     update_state(in[i]);
54
55     // Adjust envelope based on current state
56     switch(d_state) {
57       case ST_MUTED:
58         if (!mute()) 
59           d_state = d_ramp ? ST_ATTACK : ST_UNMUTED; // If not ramping, go straight to unmuted
60         break;
61
62       case ST_UNMUTED:
63         if (mute())
64           d_state = d_ramp ? ST_DECAY : ST_MUTED;    // If not ramping, go straight to muted
65         break;
66
67       case ST_ATTACK:
68         d_envelope = 0.5-std::cos(M_PI*(++d_ramped)/d_ramp)/2.0; // FIXME: precalculate window for speed
69         if (d_ramped >= d_ramp) { // use >= in case d_ramp is set to lower value elsewhere
70           d_state = ST_UNMUTED;
71           d_envelope = 1.0;
72         }
73         break;
74
75       case ST_DECAY:
76         d_envelope = 0.5-std::cos(M_PI*(--d_ramped)/d_ramp)/2.0; // FIXME: precalculate window for speed
77         if (d_ramped == 0.0)
78           d_state = ST_MUTED;
79         break;
80     };
81        
82     // If unmuted, copy input times envelope to output
83     // Otherwise, if not gating, copy zero to output
84     if (d_state != ST_MUTED)
85       out[j++] = in[i]*d_envelope;
86     else
87       if (!d_gate)
88           out[j++] = 0.0;
89   }
90
91   consume_each(noutput_items);  // Use all the inputs
92   return j;                     // But only report outputs copied
93 }