root / gr-digital / examples / tx_voice.py @ fcc37807
History | View | Annotate | Download (5.6 kB)
| 1 | #!/usr/bin/env python
|
|---|---|
| 2 | #
|
| 3 | # Copyright 2005-2007,2009,2011 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, blks2, audio, uhd |
| 24 | from gnuradio import eng_notation |
| 25 | from gnuradio.eng_option import eng_option |
| 26 | from optparse import OptionParser |
| 27 | |
| 28 | from gnuradio import digital |
| 29 | from gnuradio import vocoder |
| 30 | |
| 31 | import random |
| 32 | import time |
| 33 | import struct |
| 34 | import sys |
| 35 | |
| 36 | # from current dir
|
| 37 | from transmit_path import transmit_path |
| 38 | from uhd_interface import uhd_transmitter |
| 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 | self.sample_rate = 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 = vocoder.gsm_fr_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 = transmit_path(modulator_class, options)
|
| 68 | self.audio_rx = audio_rx(options.audio_input)
|
| 69 | |
| 70 | if(options.tx_freq is not None): |
| 71 | self.sink = uhd_transmitter(options.address, options.bitrate,
|
| 72 | options.samples_per_symbol, |
| 73 | options.tx_freq, options.tx_gain, |
| 74 | options.antenna, options.verbose) |
| 75 | options.samples_per_symbol = self.sink._sps
|
| 76 | audio_rate = self.audio_rx.sample_rate
|
| 77 | usrp_rate = self.sink.get_sample_rate()
|
| 78 | rrate = usrp_rate / audio_rate |
| 79 | |
| 80 | elif(options.to_file is not None): |
| 81 | self.sink = gr.file_sink(gr.sizeof_gr_complex, options.to_file)
|
| 82 | rrate = 1
|
| 83 | else:
|
| 84 | self.sink = gr.null_sink(gr.sizeof_gr_complex)
|
| 85 | rrate = 1
|
| 86 | |
| 87 | self.resampler = blks2.pfb_arb_resampler_ccf(rrate)
|
| 88 | |
| 89 | self.connect(self.audio_rx) |
| 90 | self.connect(self.txpath, self.resampler, self.sink) |
| 91 | |
| 92 | |
| 93 | # /////////////////////////////////////////////////////////////////////////////
|
| 94 | # main
|
| 95 | # /////////////////////////////////////////////////////////////////////////////
|
| 96 | |
| 97 | def main(): |
| 98 | |
| 99 | def send_pkt(payload='', eof=False): |
| 100 | return tb.txpath.send_pkt(payload, eof)
|
| 101 | |
| 102 | def rx_callback(ok, payload): |
| 103 | print "ok = %r, payload = '%s'" % (ok, payload) |
| 104 | |
| 105 | mods = digital.modulation_utils2.type_1_mods() |
| 106 | |
| 107 | parser = OptionParser(option_class=eng_option, conflict_handler="resolve")
|
| 108 | expert_grp = parser.add_option_group("Expert")
|
| 109 | |
| 110 | parser.add_option("-m", "--modulation", type="choice", choices=mods.keys(), |
| 111 | default='gmsk',
|
| 112 | help="Select modulation from: %s [default=%%default]"
|
| 113 | % (', '.join(mods.keys()),))
|
| 114 | parser.add_option("-M", "--megabytes", type="eng_float", default=0, |
| 115 | help="set megabytes to transmit [default=inf]")
|
| 116 | parser.add_option("-I", "--audio-input", type="string", default="", |
| 117 | help="pcm input device name. E.g., hw:0,0 or /dev/dsp")
|
| 118 | parser.add_option("","--to-file", default=None, |
| 119 | help="Output file for modulated samples")
|
| 120 | |
| 121 | transmit_path.add_options(parser, expert_grp) |
| 122 | uhd_transmitter.add_options(parser) |
| 123 | |
| 124 | for mod in mods.values(): |
| 125 | mod.add_options(expert_grp) |
| 126 | |
| 127 | parser.set_defaults(bitrate=50e3) # override default bitrate default |
| 128 | (options, args) = parser.parse_args () |
| 129 | |
| 130 | if len(args) != 0: |
| 131 | parser.print_help() |
| 132 | sys.exit(1)
|
| 133 | |
| 134 | if options.to_file is None: |
| 135 | if options.tx_freq is None: |
| 136 | sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
|
| 137 | parser.print_help(sys.stderr) |
| 138 | sys.exit(1)
|
| 139 | |
| 140 | # build the graph
|
| 141 | tb = my_top_block(mods[options.modulation], options) |
| 142 | |
| 143 | r = gr.enable_realtime_scheduling() |
| 144 | if r != gr.RT_OK:
|
| 145 | print "Warning: failed to enable realtime scheduling" |
| 146 | |
| 147 | |
| 148 | tb.start() # start flow graph
|
| 149 | |
| 150 | # generate and send packets
|
| 151 | nbytes = int(1e6 * options.megabytes) |
| 152 | n = 0
|
| 153 | pktno = 0
|
| 154 | |
| 155 | while nbytes == 0 or n < nbytes: |
| 156 | packet = tb.audio_rx.get_encoded_voice_packet() |
| 157 | s = packet.to_string() |
| 158 | send_pkt(s) |
| 159 | n += len(s)
|
| 160 | sys.stderr.write('.')
|
| 161 | pktno += 1
|
| 162 | |
| 163 | send_pkt(eof=True)
|
| 164 | tb.wait() # wait for it to finish
|
| 165 | |
| 166 | |
| 167 | if __name__ == '__main__': |
| 168 | try:
|
| 169 | main() |
| 170 | except KeyboardInterrupt: |
| 171 | pass
|