Updated FSF address in all files. Fixes ticket:51
[debian/gnuradio] / gnuradio-examples / python / gmsk2 / tx_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 time
34 import struct
35 import sys
36
37 # from current dir
38 from transmit_path import transmit_path
39
40 #import os
41 #print os.getpid()
42 #raw_input('Attach and press enter')
43
44
45 class my_graph(gr.flow_graph):
46
47     def __init__(self, tx_subdev_spec, bitrate, interp_rate, spb, bt):
48         gr.flow_graph.__init__(self)
49         self.txpath = transmit_path(self, tx_subdev_spec, bitrate, interp_rate, spb, bt)
50
51         audio_input = ""
52         sample_rate = 8000
53         src = audio.source(sample_rate, audio_input)
54         src_scale = gr.multiply_const_ff(32767)
55         f2s = gr.float_to_short()
56         voice_coder = gsm_full_rate.encode_sp()
57         self.packets_from_encoder = gr.msg_queue()
58         packet_sink = gr.message_sink(33, self.packets_from_encoder, False)
59         self.connect(src, src_scale, f2s, voice_coder, packet_sink)
60
61
62
63 # /////////////////////////////////////////////////////////////////////////////
64 #                                   main
65 # /////////////////////////////////////////////////////////////////////////////
66
67 def main():
68
69     def send_pkt(payload='', eof=False):
70         return fg.txpath.send_pkt(payload, eof)
71
72     def rx_callback(ok, payload):
73         print "ok = %r, payload = '%s'" % (ok, payload)
74
75     parser = OptionParser (option_class=eng_option)
76     parser.add_option("-T", "--tx-subdev-spec", type="subdev", default=None,
77                       help="select USRP Tx side A or B")
78     parser.add_option("-f", "--freq", type="eng_float", default=423.1e6,
79                        help="set Tx and Rx frequency to FREQ [default=%default]", metavar="FREQ")
80     parser.add_option("-r", "--bitrate", type="eng_float", default=100e3,
81                       help="specify bitrate.  spb and interp will be derived.")
82     parser.add_option("-S", "--spb", type="int", default=None, help="set samples/baud [default=%default]")
83     parser.add_option("-i", "--interp", type="intx", default=None,
84                       help="set fpga interpolation rate to INTERP [default=%default]")
85     parser.add_option("-s", "--size", type="eng_float", default=1500,
86                       help="set packet size [default=%default]")
87     parser.add_option("", "--bt", type="float", default=0.3, help="set bandwidth-time product [default=%default]")
88     parser.add_option("-M", "--megabytes", type="eng_float", default=1.0,
89                       help="set megabytes to transmit [default=%default]")
90     parser.add_option("","--discontinuous", action="store_true", default=False,
91                       help="enable discontinous transmission (bursts of 5 packets)")
92     (options, args) = parser.parse_args ()
93
94     if len(args) != 0:
95         parser.print_help()
96         sys.exit(1)
97
98     if options.freq < 1e6:
99         options.freq *= 1e6
100
101     pkt_size = int(options.size)
102
103     # build the graph
104     fg = my_graph(options.tx_subdev_spec, options.bitrate, options.interp,
105                   options.spb, options.bt)
106
107     print "bitrate: %sb/sec" % (eng_notation.num_to_str(fg.txpath.bitrate()),)
108     print "spb:     %3d" % (fg.txpath.spb(),)
109     print "interp:  %3d" % (fg.txpath.interp(),)
110
111     fg.txpath.set_freq(options.freq)
112
113     fg.start()                       # start flow graph
114
115     # generate and send packets
116     nbytes = int(1e6 * options.megabytes)
117     n = 0
118     pktno = 0
119
120     while n < nbytes:
121         packet = fg.packets_from_encoder.delete_head()
122         s = packet.to_string()
123         send_pkt(s)
124         n += len(s)
125         sys.stderr.write('.')
126         if options.discontinuous and pktno % 5 == 4:
127             time.sleep(1)
128         pktno += 1
129         
130     send_pkt(eof=True)
131     fg.wait()                       # wait for it to finish
132     fg.txpath.set_auto_tr(False)
133
134
135 if __name__ == '__main__':
136     try:
137         main()
138     except KeyboardInterrupt:
139         pass