Updated FSF address in all files. Fixes ticket:51
[debian/gnuradio] / gnuradio-examples / python / gmsk2 / rx_voice.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2005 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 from gnuradio import gr, gru, blks
24 from gnuradio import usrp
25 from gnuradio import audio
26 from gnuradio import eng_notation
27 from gnuradio.eng_option import eng_option
28 from optparse import OptionParser
29
30 from gnuradio.vocoder import gsm_full_rate
31
32 import random
33 import struct
34
35 # from current dir
36 from receive_path import receive_path
37
38 #import os
39 #print os.getpid()
40 #raw_input('Attach and press enter')
41
42
43 class audio_tx(gr.hier_block):
44     def __init__(self, fg):
45         self.packet_src = gr.message_source(33)
46         voice_decoder = gsm_full_rate.decode_ps()
47         s2f = gr.short_to_float ()
48         sink_scale = gr.multiply_const_ff(1.0/32767.)
49         audio_sink = audio.sink(8000)
50         fg.connect(self.packet_src, voice_decoder, s2f, sink_scale, audio_sink)
51         gr.hier_block.__init__(self, fg, self.packet_src, audio_sink)
52         
53     def msgq(self):
54         return self.packet_src.msgq()
55
56
57 class my_graph(gr.flow_graph):
58
59     def __init__(self, rx_subdev_spec, bitrate, decim_rate, spb, rx_callback, log):
60         gr.flow_graph.__init__(self)
61         self.rxpath = receive_path(self, rx_subdev_spec, bitrate, decim_rate,
62                                    spb, rx_callback, log)
63
64         self.audio_tx = audio_tx(self)
65         
66
67 # /////////////////////////////////////////////////////////////////////////////
68 #                                   main
69 # /////////////////////////////////////////////////////////////////////////////
70
71 global n_rcvd, n_right
72
73 def main():
74     global n_rcvd, n_right
75
76     n_rcvd = 0
77     n_right = 0
78     
79     def rx_callback(ok, payload):
80         global n_rcvd, n_right
81         n_rcvd += 1
82         if ok:
83             n_right += 1
84
85         fg.audio_tx.msgq().insert_tail(gr.message_from_string(payload))
86         
87         print "ok = %r  n_rcvd = %4d  n_right = %4d" % (
88             ok, n_rcvd, n_right)
89
90     parser = OptionParser (option_class=eng_option)
91     parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=None,
92                       help="select USRP Rx side A or B")
93     parser.add_option("-f", "--freq", type="eng_float", default=423.1e6,
94                        help="set Rx frequency to FREQ [default=%default]", metavar="FREQ")
95     parser.add_option("-r", "--bitrate", type="eng_float", default=100e3,
96                       help="specify bitrate.  spb and interp will be derived.")
97     parser.add_option("-S", "--spb", type="int", default=None, help="set samples/baud [default=%default]")
98     parser.add_option("-d", "--decim", type="intx", default=None,
99                       help="set fpga decim rate to DECIM [default=%default]")
100     parser.add_option("-g", "--gain", type="eng_float", default=27,
101                       help="set rx gain")
102     parser.add_option("","--log", action="store_true", default=False,
103                       help="enable diagnostic logging")
104     (options, args) = parser.parse_args ()
105
106     if len(args) != 0:
107         parser.print_help()
108         sys.exit(1)
109
110     if options.freq < 1e6:
111         options.freq *= 1e6
112
113     # build the graph
114     fg = my_graph(options.rx_subdev_spec, options.bitrate,
115                   options.decim, options.spb, rx_callback, options.log)
116
117     print "bitrate: %sb/sec" % (eng_notation.num_to_str(fg.rxpath.bitrate()),)
118     print "spb:     %3d" % (fg.rxpath.spb(),)
119     print "decim:   %3d" % (fg.rxpath.decim(),)
120
121     fg.rxpath.set_freq(options.freq)
122     fg.rxpath.set_gain(options.gain)
123     print "Rx gain_range: ", fg.rxpath.subdev.gain_range(), " using", fg.rxpath.gain
124
125     fg.start()        # start flow graph
126     fg.wait()         # wait for it to finish
127
128 if __name__ == '__main__':
129     try:
130         main()
131     except KeyboardInterrupt:
132         pass