Imported Upstream version 3.2.2
[debian/gnuradio] / gr-cvsd-vocoder / src / python / cvsd.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2007 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
24 from gnuradio.vocoder import cvsd_vocoder
25
26 class cvsd_encode(gr.hier_block2):
27     '''
28     This is a wrapper for the CVSD encoder that performs interpolation and filtering
29     necessary to work with the vocoding. It converts an incoming float (+-1) to a short, scales
30     it (to 32000; slightly below the maximum value), interpolates it, and then vocodes it.
31
32     The incoming sampling rate can be anything, though, of course, the higher the sampling rate and the
33     higher the interpolation rate are, the better the sound quality.
34     '''
35     
36     def __init__(self, resample=8, bw=0.5):
37         '''
38         When using the CVSD vocoder, appropriate sampling rates are from 8k to 64k with resampling rates
39         from 1 to 8. A rate of 8k with a resampling rate of 8 provides a good quality signal.
40         '''
41
42         gr.hier_block2.__init__(self, "cvsd_encode",
43                                 gr.io_signature(1, 1, gr.sizeof_float), # Input signature
44                                 gr.io_signature(1, 1, gr.sizeof_char))  # Output signature
45
46         scale_factor = 32000.0
47         self.interp = resample
48
49         src_scale = gr.multiply_const_ff(scale_factor)
50         taps = gr.firdes.low_pass(self.interp, self.interp, bw, 2*bw)
51         interp = gr.interp_fir_filter_fff(self.interp, taps)
52         f2s = gr.float_to_short()
53         enc = cvsd_vocoder.encode_sb()
54
55         self.connect(self, src_scale, interp, f2s, enc, self)
56
57
58 class cvsd_decode(gr.hier_block2):
59     '''
60     This is a wrapper for the CVSD decoder that performs decimation and filtering
61     necessary to work with the vocoding. It converts an incoming CVSD-encoded short to a float, decodes it
62     to a float, decimates it, and scales it (by 32000; slightly below the maximum value to avoid clipping).
63
64     The sampling rate can be anything, though, of course, the higher the sampling rate and the
65     higher the interpolation rate are, the better the sound quality.
66     '''
67
68     def __init__(self, resample=8, bw=0.5):
69         '''
70         When using the CVSD vocoder, appropriate sampling rates are from 8k to 64k with resampling rates
71         from 1 to 8. A rate of 8k with a resampling rate of 8 provides a good quality signal.
72         '''
73         gr.hier_block2.__init__(self, "cvsd_decode",
74                                 gr.io_signature(1, 1, gr.sizeof_char),  # Input signature
75                                 gr.io_signature(1, 1, gr.sizeof_float)) # Output signature
76
77         scale_factor = 32000.0
78         self.decim = resample
79
80         dec = cvsd_vocoder.decode_bs()
81         s2f = gr.short_to_float()
82         taps = gr.firdes.low_pass(1, 1, bw, 2*bw)
83         decim = gr.fir_filter_fff(self.decim, taps)
84         sink_scale = gr.multiply_const_ff(1.0/scale_factor)
85
86         self.connect(self, dec, s2f, decim, sink_scale, self)
87