Imported Upstream version 3.0.4
[debian/gnuradio] / gnuradio-examples / python / audio / dial_squelch.py
1 #!/usr/bin/env python
2
3 # Copyright 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 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 from gnuradio import gr, audio, eng_option
24 from gnuradio.eng_option import eng_option
25 from math import pi, cos
26 from optparse import OptionParser
27
28 """
29 This script generates a standard dial tone and then applies a sinusoidal
30 envelope to vary it's loudness.  The audio is then passed through the
31 power squelch block before it gets sent to the sound card. By varying
32 the command line parameters, one can see the effect of differing
33 amounts of power averaging, threshold, and attack/decay ramping.
34 """
35
36 class app_flow_graph(gr.flow_graph):
37     def __init__(self, options, args):
38         gr.flow_graph.__init__(self)
39         
40         # Create dial tone by adding two sine waves
41         SRC1 = gr.sig_source_f(options.rate, gr.GR_SIN_WAVE, 350, 0.5, 0.0)
42         SRC2 = gr.sig_source_f(options.rate, gr.GR_SIN_WAVE, 440, 0.5, 0.0)
43         ADD = gr.add_ff()
44
45         # Convert to vector stream (and back) to apply raised cosine envelope
46         # You could also do this with a vector_source_f block that repeats.
47         S2V = gr.stream_to_vector(gr.sizeof_float, options.rate)
48         ENV = [0.5-cos(2*pi*x/options.rate)/2 for x in range(options.rate)]
49         MLT = gr.multiply_const_vff(ENV)
50         V2S = gr.vector_to_stream(gr.sizeof_float, options.rate)
51
52         # Run through power squelch with user supplied or default options
53         # Zero output when squelch is invoked
54         SQL = gr.pwr_squelch_ff(options.threshold, options.alpha, options.ramp, False)
55         DST = audio.sink(options.rate)
56
57         # Solder it all together
58         self.connect(SRC1, (ADD, 0))
59         self.connect(SRC2, (ADD, 1))
60         self.connect(ADD, S2V, MLT, V2S, SQL, DST)
61         
62 def main():
63     parser = OptionParser(option_class=eng_option)
64     parser.add_option("-r", "--rate", type="int", default=8000, help="set audio output sample rate to RATE", metavar="RATE")
65     parser.add_option("-t", "--threshold", type="eng_float", default=-10.0, help="set power squelch to DB", metavar="DB")
66     parser.add_option("-a", "--alpha", type="eng_float", default=None, help="set alpha to ALPHA", metavar="ALPHA")
67     parser.add_option("-m", "--ramp", type="int", default=None, help="set attack/decay ramp to SAMPLES", metavar="SAMPLES")
68     (options, args) = parser.parse_args()
69
70     if options.alpha == None:
71         options.alpha = 50.0/options.rate
72         
73     if options.ramp == None:
74         options.ramp = options.rate/50  # ~ 20 ms
75
76     print "Using audio rate of", options.rate
77     print "Using threshold of", options.threshold, "db"
78     print "Using alpha of", options.alpha
79     print "Using ramp of", options.ramp, "samples"
80
81     fg = app_flow_graph(options, args)
82
83     try:
84       fg.run()
85     except KeyboardInterrupt:
86       pass
87
88 if __name__ == "__main__":
89     main()