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