Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-examples / python / digital / tx_voice.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2005,2006,2007,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 time
34 import struct
35 import sys
36
37 # from current dir
38 import usrp_transmit_path
39
40 #import os
41 #print os.getpid()
42 #raw_input('Attach and press enter')
43
44
45 class audio_rx(gr.hier_block2):
46     def __init__(self, audio_input_dev):
47         gr.hier_block2.__init__(self, "audio_rx",
48                                 gr.io_signature(0, 0, 0), # Input signature
49                                 gr.io_signature(0, 0, 0)) # Output signature
50         sample_rate = 8000
51         src = audio.source(sample_rate, audio_input_dev)
52         src_scale = gr.multiply_const_ff(32767)
53         f2s = gr.float_to_short()
54         voice_coder = gsm_full_rate.encode_sp()
55         self.packets_from_encoder = gr.msg_queue()
56         packet_sink = gr.message_sink(33, self.packets_from_encoder, False)
57         self.connect(src, src_scale, f2s, voice_coder, packet_sink)
58
59     def get_encoded_voice_packet(self):
60         return self.packets_from_encoder.delete_head()
61         
62
63 class my_top_block(gr.top_block):
64
65     def __init__(self, modulator_class, options):
66         gr.top_block.__init__(self)
67         self.txpath = usrp_transmit_path.usrp_transmit_path(modulator_class, options)
68         self.audio_rx = audio_rx(options.audio_input)
69         self.connect(self.txpath)
70         self.connect(self.audio_rx)
71
72
73 # /////////////////////////////////////////////////////////////////////////////
74 #                                   main
75 # /////////////////////////////////////////////////////////////////////////////
76
77 def main():
78
79     def send_pkt(payload='', eof=False):
80         return tb.txpath.send_pkt(payload, eof)
81
82     def rx_callback(ok, payload):
83         print "ok = %r, payload = '%s'" % (ok, payload)
84
85     mods = modulation_utils.type_1_mods()
86
87     parser = OptionParser(option_class=eng_option, conflict_handler="resolve")
88     expert_grp = parser.add_option_group("Expert")
89
90     parser.add_option("-m", "--modulation", type="choice", choices=mods.keys(),
91                       default='gmsk',
92                       help="Select modulation from: %s [default=%%default]"
93                             % (', '.join(mods.keys()),))
94     parser.add_option("-M", "--megabytes", type="eng_float", default=0,
95                       help="set megabytes to transmit [default=inf]")
96     parser.add_option("-I", "--audio-input", type="string", default="",
97                       help="pcm input device name.  E.g., hw:0,0 or /dev/dsp")
98     usrp_transmit_path.add_options(parser, expert_grp)
99
100     for mod in mods.values():
101         mod.add_options(expert_grp)
102
103     parser.set_defaults(bitrate=50e3)  # override default bitrate default
104     (options, args) = parser.parse_args ()
105
106     if len(args) != 0:
107         parser.print_help()
108         sys.exit(1)
109
110     if options.tx_freq is None:
111         sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
112         parser.print_help(sys.stderr)
113         sys.exit(1)
114
115
116     # build the graph
117     tb = my_top_block(mods[options.modulation], options)
118
119     r = gr.enable_realtime_scheduling()
120     if r != gr.RT_OK:
121         print "Warning: failed to enable realtime scheduling"
122
123
124     tb.start()                       # start flow graph
125
126     # generate and send packets
127     nbytes = int(1e6 * options.megabytes)
128     n = 0
129     pktno = 0
130
131     while nbytes == 0 or n < nbytes:
132         packet = tb.audio_rx.get_encoded_voice_packet()
133         s = packet.to_string()
134         send_pkt(s)
135         n += len(s)
136         sys.stderr.write('.')
137         pktno += 1
138         
139     send_pkt(eof=True)
140     tb.wait()                       # wait for it to finish
141
142
143 if __name__ == '__main__':
144     try:
145         main()
146     except KeyboardInterrupt:
147         pass