root / gr-digital / examples / narrowband / rx_voice.py @ 73ccc57f
History | View | Annotate | Download (5.5 kB)
| 1 | #!/usr/bin/env python
|
|---|---|
| 2 | #
|
| 3 | # Copyright 2005,2006,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 struct |
| 33 | import sys |
| 34 | |
| 35 | # from current dir
|
| 36 | from receive_path import receive_path |
| 37 | from uhd_interface import uhd_receiver |
| 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.sample_rate = sample_rate = 8000 |
| 51 | self.packet_src = gr.message_source(33) |
| 52 | voice_decoder = vocoder.gsm_fr_decode_ps() |
| 53 | s2f = gr.short_to_float () |
| 54 | sink_scale = gr.multiply_const_ff(1.0/32767.) |
| 55 | audio_sink = audio.sink(sample_rate, audio_output_dev) |
| 56 | self.connect(self.packet_src, voice_decoder, s2f, sink_scale, audio_sink) |
| 57 | |
| 58 | def msgq(self): |
| 59 | return self.packet_src.msgq() |
| 60 | |
| 61 | |
| 62 | class my_top_block(gr.top_block): |
| 63 | def __init__(self, demod_class, rx_callback, options): |
| 64 | gr.top_block.__init__(self)
|
| 65 | self.rxpath = receive_path(demod_class, rx_callback, options)
|
| 66 | self.audio_tx = audio_tx(options.audio_output)
|
| 67 | |
| 68 | if(options.rx_freq is not None): |
| 69 | self.source = uhd_receiver(options.address, options.bitrate,
|
| 70 | options.samples_per_symbol, |
| 71 | options.rx_freq, options.rx_gain, |
| 72 | options.antenna, options.verbose) |
| 73 | options.samples_per_symbol = self.source._sps
|
| 74 | |
| 75 | audio_rate = self.audio_tx.sample_rate
|
| 76 | usrp_rate = self.source.get_sample_rate()
|
| 77 | rrate = audio_rate / usrp_rate |
| 78 | self.resampler = blks2.pfb_arb_resampler_ccf(rrate)
|
| 79 | |
| 80 | self.connect(self.source, self.resampler, self.rxpath) |
| 81 | |
| 82 | elif(options.from_file is not None): |
| 83 | self.thr = gr.throttle(gr.sizeof_gr_complex, options.bitrate)
|
| 84 | self.source = gr.file_source(gr.sizeof_gr_complex, options.from_file)
|
| 85 | self.connect(self.source, self.thr, self.rxpath) |
| 86 | |
| 87 | else:
|
| 88 | self.thr = gr.throttle(gr.sizeof_gr_complex, 1e6) |
| 89 | self.source = gr.null_source(gr.sizeof_gr_complex)
|
| 90 | self.connect(self.source, self.thr, self.rxpath) |
| 91 | |
| 92 | self.connect(self.audio_tx) |
| 93 | |
| 94 | # /////////////////////////////////////////////////////////////////////////////
|
| 95 | # main
|
| 96 | # /////////////////////////////////////////////////////////////////////////////
|
| 97 | |
| 98 | global n_rcvd, n_right
|
| 99 | |
| 100 | def main(): |
| 101 | global n_rcvd, n_right
|
| 102 | |
| 103 | n_rcvd = 0
|
| 104 | n_right = 0
|
| 105 | |
| 106 | def rx_callback(ok, payload): |
| 107 | global n_rcvd, n_right
|
| 108 | n_rcvd += 1
|
| 109 | if ok:
|
| 110 | n_right += 1
|
| 111 | |
| 112 | tb.audio_tx.msgq().insert_tail(gr.message_from_string(payload)) |
| 113 | |
| 114 | print "ok = %r n_rcvd = %4d n_right = %4d" % ( |
| 115 | ok, n_rcvd, n_right) |
| 116 | |
| 117 | demods = digital.modulation_utils.type_1_demods() |
| 118 | |
| 119 | # Create Options Parser:
|
| 120 | parser = OptionParser (option_class=eng_option, conflict_handler="resolve")
|
| 121 | expert_grp = parser.add_option_group("Expert")
|
| 122 | |
| 123 | parser.add_option("-m", "--modulation", type="choice", choices=demods.keys(), |
| 124 | default='gmsk',
|
| 125 | help="Select modulation from: %s [default=%%default]"
|
| 126 | % (', '.join(demods.keys()),))
|
| 127 | parser.add_option("-O", "--audio-output", type="string", default="", |
| 128 | help="pcm output device name. E.g., hw:0,0 or /dev/dsp")
|
| 129 | parser.add_option("","--from-file", default=None, |
| 130 | help="input file of samples to demod")
|
| 131 | receive_path.add_options(parser, expert_grp) |
| 132 | uhd_receiver.add_options(parser) |
| 133 | |
| 134 | for mod in demods.values(): |
| 135 | mod.add_options(expert_grp) |
| 136 | |
| 137 | parser.set_defaults(bitrate=50e3) # override default bitrate default |
| 138 | (options, args) = parser.parse_args () |
| 139 | |
| 140 | if len(args) != 0: |
| 141 | parser.print_help(sys.stderr) |
| 142 | sys.exit(1)
|
| 143 | |
| 144 | if options.from_file is None: |
| 145 | if options.rx_freq is None: |
| 146 | sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
|
| 147 | parser.print_help(sys.stderr) |
| 148 | sys.exit(1)
|
| 149 | |
| 150 | |
| 151 | # build the graph
|
| 152 | tb = my_top_block(demods[options.modulation], rx_callback, options) |
| 153 | |
| 154 | r = gr.enable_realtime_scheduling() |
| 155 | if r != gr.RT_OK:
|
| 156 | print "Warning: Failed to enable realtime scheduling." |
| 157 | |
| 158 | tb.run() |
| 159 | |
| 160 | if __name__ == '__main__': |
| 161 | try:
|
| 162 | main() |
| 163 | except KeyboardInterrupt: |
| 164 | pass
|