Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-examples / python / digital / rx_voice.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2005,2006,2009 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, gru, modulation_utils
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 import sys
35
36 # from current dir
37 import usrp_receive_path
38
39 #import os
40 #print os.getpid()
41 #raw_input('Attach and press enter')
42
43
44 class audio_tx(gr.hier_block2):
45     def __init__(self, audio_output_dev):
46         gr.hier_block2.__init__(self, "audio_tx",
47                                 gr.io_signature(0, 0, 0), # Input signature
48                                 gr.io_signature(0, 0, 0)) # Output signature
49                                 
50         self.packet_src = gr.message_source(33)
51         voice_decoder = gsm_full_rate.decode_ps()
52         s2f = gr.short_to_float ()
53         sink_scale = gr.multiply_const_ff(1.0/32767.)
54         audio_sink = audio.sink(8000, audio_output_dev)
55         self.connect(self.packet_src, voice_decoder, s2f, sink_scale, audio_sink)
56         
57     def msgq(self):
58         return self.packet_src.msgq()
59
60
61 class my_top_block(gr.top_block):
62     def __init__(self, demod_class, rx_callback, options):
63         gr.top_block.__init__(self)
64         self.rxpath = usrp_receive_path.usrp_receive_path(demod_class, rx_callback, options)
65         self.audio_tx = audio_tx(options.audio_output)
66         self.connect(self.rxpath)
67         self.connect(self.audio_tx)        
68
69 # /////////////////////////////////////////////////////////////////////////////
70 #                                   main
71 # /////////////////////////////////////////////////////////////////////////////
72
73 global n_rcvd, n_right
74
75 def main():
76     global n_rcvd, n_right
77
78     n_rcvd = 0
79     n_right = 0
80     
81     def rx_callback(ok, payload):
82         global n_rcvd, n_right
83         n_rcvd += 1
84         if ok:
85             n_right += 1
86
87         tb.audio_tx.msgq().insert_tail(gr.message_from_string(payload))
88         
89         print "ok = %r  n_rcvd = %4d  n_right = %4d" % (
90             ok, n_rcvd, n_right)
91
92     demods = modulation_utils.type_1_demods()
93
94     # Create Options Parser:
95     parser = OptionParser (option_class=eng_option, conflict_handler="resolve")
96     expert_grp = parser.add_option_group("Expert")
97
98     parser.add_option("-m", "--modulation", type="choice", choices=demods.keys(), 
99                       default='gmsk',
100                       help="Select modulation from: %s [default=%%default]"
101                             % (', '.join(demods.keys()),))
102     parser.add_option("-O", "--audio-output", type="string", default="",
103                       help="pcm output device name.  E.g., hw:0,0 or /dev/dsp")
104     usrp_receive_path.add_options(parser, expert_grp)
105
106     for mod in demods.values():
107         mod.add_options(expert_grp)
108
109     parser.set_defaults(bitrate=50e3)  # override default bitrate default
110     (options, args) = parser.parse_args ()
111
112     if len(args) != 0:
113         parser.print_help(sys.stderr)
114         sys.exit(1)
115
116     if options.rx_freq is None:
117         sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
118         parser.print_help(sys.stderr)
119         sys.exit(1)
120
121
122     # build the graph
123     tb = my_top_block(demods[options.modulation], rx_callback, options)
124
125     r = gr.enable_realtime_scheduling()
126     if r != gr.RT_OK:
127         print "Warning: Failed to enable realtime scheduling."
128
129     tb.run()
130
131 if __name__ == '__main__':
132     try:
133         main()
134     except KeyboardInterrupt:
135         pass