8magsvn – Rev 40

Subversion Repositories:
Rev:
#!/usr/bin/env python
##################################################
# Gnuradio Python Flow Graph
# Title: UHD WBFM Receive
# Author: Example
# Description: WBFM Receive
# Generated: Thu May 24 20:00:08 2012
##################################################

from PyQt4 import Qt
from gnuradio import audio
from gnuradio import blks2
from gnuradio import eng_notation
from gnuradio import gr
from gnuradio.eng_option import eng_option
from gnuradio.gr import firdes
from gnuradio.qtgui import qtgui
from optparse import OptionParser
import PyQt4.Qwt5 as Qwt
import baz
import sip
import sys

class uhd_wbfm_receive(gr.top_block, Qt.QWidget):

        def __init__(self, audio_output="", freq=107.6e6, gain=10, address="addr=192.168.10.2", samp_rate=1056e3):
                gr.top_block.__init__(self, "UHD WBFM Receive")
                Qt.QWidget.__init__(self)
                self.setWindowTitle("UHD WBFM Receive")
                self.setWindowIcon(Qt.QIcon.fromTheme('gnuradio-grc'))
                self.top_scroll_layout = Qt.QVBoxLayout()
                self.setLayout(self.top_scroll_layout)
                self.top_scroll = Qt.QScrollArea()
                self.top_scroll.setFrameStyle(Qt.QFrame.NoFrame)
                self.top_scroll_layout.addWidget(self.top_scroll)
                self.top_scroll.setWidgetResizable(True)
                self.top_widget = Qt.QWidget()
                self.top_scroll.setWidget(self.top_widget)
                self.top_layout = Qt.QVBoxLayout(self.top_widget)
                self.top_grid_layout = Qt.QGridLayout()
                self.top_layout.addLayout(self.top_grid_layout)


                ##################################################
                # Parameters
                ##################################################
                self.audio_output = audio_output
                self.freq = freq
                self.gain = gain
                self.address = address
                self.samp_rate = samp_rate

                ##################################################
                # Variables
                ##################################################
                self.volume = volume = 2
                self.tun_gain = tun_gain = gain
                self.tun_freq = tun_freq = 9.30825e+07
                self.fine = fine = 0
                self.audio_decim = audio_decim = 22

                ##################################################
                # Blocks
                ##################################################
                self._volume_layout = Qt.QVBoxLayout()
                self._volume_knob = Qwt.QwtKnob()
                self._volume_knob.setRange(0, 10, 1e-3)
                self._volume_knob.setValue(self.volume)
                self._volume_knob.valueChanged.connect(self.set_volume)
                self._volume_layout.addWidget(self._volume_knob)
                self._volume_label = Qt.QLabel("Volume")
                self._volume_label.setAlignment(Qt.Qt.AlignTop | Qt.Qt.AlignHCenter)
                self._volume_layout.addWidget(self._volume_label)
                self.top_layout.addLayout(self._volume_layout)
                self._tun_gain_layout = Qt.QVBoxLayout()
                self._tun_gain_knob = Qwt.QwtKnob()
                self._tun_gain_knob.setRange(0, 20, 1e-2)
                self._tun_gain_knob.setValue(self.tun_gain)
                self._tun_gain_knob.valueChanged.connect(self.set_tun_gain)
                self._tun_gain_layout.addWidget(self._tun_gain_knob)
                self._tun_gain_label = Qt.QLabel("tun_gain")
                self._tun_gain_label.setAlignment(Qt.Qt.AlignTop | Qt.Qt.AlignHCenter)
                self._tun_gain_layout.addWidget(self._tun_gain_label)
                self.top_layout.addLayout(self._tun_gain_layout)
                self._tun_freq_layout = Qt.QVBoxLayout()
                self._tun_freq_tool_bar = Qt.QToolBar(self)
                self._tun_freq_layout.addWidget(self._tun_freq_tool_bar)
                self._tun_freq_tool_bar.addWidget(Qt.QLabel("Freq (Hz)"+": "))
                self._tun_freq_counter = Qwt.QwtCounter()
                self._tun_freq_counter.setRange(80e6, 110e6, 1e-2)
                self._tun_freq_counter.setNumButtons(2)
                self._tun_freq_counter.setValue(self.tun_freq)
                self._tun_freq_tool_bar.addWidget(self._tun_freq_counter)
                self._tun_freq_counter.valueChanged.connect(self.set_tun_freq)
                self._tun_freq_slider = Qwt.QwtSlider(None, Qt.Qt.Horizontal, Qwt.QwtSlider.BottomScale, Qwt.QwtSlider.BgSlot)
                self._tun_freq_slider.setRange(80e6, 110e6, 1e-2)
                self._tun_freq_slider.setValue(self.tun_freq)
                self._tun_freq_slider.setMinimumWidth(200)
                self._tun_freq_slider.valueChanged.connect(self.set_tun_freq)
                self._tun_freq_layout.addWidget(self._tun_freq_slider)
                self.top_layout.addLayout(self._tun_freq_layout)
                self._fine_layout = Qt.QVBoxLayout()
                self._fine_tool_bar = Qt.QToolBar(self)
                self._fine_layout.addWidget(self._fine_tool_bar)
                self._fine_tool_bar.addWidget(Qt.QLabel("fine"+": "))
                self._fine_counter = Qwt.QwtCounter()
                self._fine_counter.setRange(-1e-1, 1e-1, 1e-3)
                self._fine_counter.setNumButtons(2)
                self._fine_counter.setValue(self.fine)
                self._fine_tool_bar.addWidget(self._fine_counter)
                self._fine_counter.valueChanged.connect(self.set_fine)
                self._fine_slider = Qwt.QwtSlider(None, Qt.Qt.Horizontal, Qwt.QwtSlider.BottomScale, Qwt.QwtSlider.BgSlot)
                self._fine_slider.setRange(-1e-1, 1e-1, 1e-3)
                self._fine_slider.setValue(self.fine)
                self._fine_slider.setMinimumWidth(200)
                self._fine_slider.valueChanged.connect(self.set_fine)
                self._fine_layout.addWidget(self._fine_slider)
                self.top_layout.addLayout(self._fine_layout)
                self.rtl2832_source_0 = baz.rtl_source_c(defer_creation=True)
                self.rtl2832_source_0.set_verbose(True)
                self.rtl2832_source_0.set_vid(0x0)
                self.rtl2832_source_0.set_pid(0x0)
                self.rtl2832_source_0.set_tuner_name("e4000")
                self.rtl2832_source_0.set_default_timeout(0)
                self.rtl2832_source_0.set_use_buffer(True)
                self.rtl2832_source_0.set_fir_coefficients(([]))
                
                
                
                
                
                if self.rtl2832_source_0.create() == False: raise Exception("Failed to create RTL2832 Source: rtl2832_source_0")
                
                
                self.rtl2832_source_0.set_sample_rate(samp_rate)
                
                self.rtl2832_source_0.set_frequency(tun_freq+fine)
                
                
                self.rtl2832_source_0.set_auto_gain_mode(False)
                self.rtl2832_source_0.set_relative_gain(True)
                self.rtl2832_source_0.set_gain(tun_gain)
                  
                self.qtgui_sink_x_0 = qtgui.sink_c(
                        1024, #fftsize
                        firdes.WIN_BLACKMAN_hARRIS, #wintype
                        0, #fc
                        samp_rate, #bw
                        "QT GUI Plot", #name
                        True, #plotfreq
                        True, #plotwaterfall
                        True, #plottime
                        True, #plotconst
                )
                self._qtgui_sink_x_0_win = sip.wrapinstance(self.qtgui_sink_x_0.pyqwidget(), Qt.QWidget)
                self.top_layout.addWidget(self._qtgui_sink_x_0_win)
                self.low_pass_filter_0 = gr.fir_filter_ccf(1, firdes.low_pass(
                        1, samp_rate, 115e3, 30e3, firdes.WIN_HANN, 6.76))
                self.gr_multiply_const_vxx = gr.multiply_const_vff((volume, ))
                self.blks2_wfm_rcv = blks2.wfm_rcv(
                        quad_rate=samp_rate,
                        audio_decimation=audio_decim,
                )
                self.audio_sink = audio.sink(int(samp_rate/audio_decim), audio_output, True)

                ##################################################
                # Connections
                ##################################################
                self.connect((self.gr_multiply_const_vxx, 0), (self.audio_sink, 0))
                self.connect((self.blks2_wfm_rcv, 0), (self.gr_multiply_const_vxx, 0))
                self.connect((self.low_pass_filter_0, 0), (self.blks2_wfm_rcv, 0))
                self.connect((self.rtl2832_source_0, 0), (self.low_pass_filter_0, 0))
                self.connect((self.low_pass_filter_0, 0), (self.qtgui_sink_x_0, 0))

        def get_audio_output(self):
                return self.audio_output

        def set_audio_output(self, audio_output):
                self.audio_output = audio_output

        def get_freq(self):
                return self.freq

        def set_freq(self, freq):
                self.freq = freq

        def get_gain(self):
                return self.gain

        def set_gain(self, gain):
                self.gain = gain
                self.set_tun_gain(self.gain)

        def get_address(self):
                return self.address

        def set_address(self, address):
                self.address = address

        def get_samp_rate(self):
                return self.samp_rate

        def set_samp_rate(self, samp_rate):
                self.samp_rate = samp_rate
                self.rtl2832_source_0.set_sample_rate(self.samp_rate)
                self.low_pass_filter_0.set_taps(firdes.low_pass(1, self.samp_rate, 115e3, 30e3, firdes.WIN_HANN, 6.76))
                self.qtgui_sink_x_0.set_frequency_range(0, self.samp_rate)

        def get_volume(self):
                return self.volume

        def set_volume(self, volume):
                self.volume = volume
                self.gr_multiply_const_vxx.set_k((self.volume, ))
                self._volume_knob.setValue(self.volume)

        def get_tun_gain(self):
                return self.tun_gain

        def set_tun_gain(self, tun_gain):
                self.tun_gain = tun_gain
                self.rtl2832_source_0.set_gain(self.tun_gain)
                self._tun_gain_knob.setValue(self.tun_gain)

        def get_tun_freq(self):
                return self.tun_freq

        def set_tun_freq(self, tun_freq):
                self.tun_freq = tun_freq
                self.rtl2832_source_0.set_frequency(self.tun_freq+self.fine)
                self._tun_freq_counter.setValue(self.tun_freq)
                self._tun_freq_slider.setValue(self.tun_freq)

        def get_fine(self):
                return self.fine

        def set_fine(self, fine):
                self.fine = fine
                self.rtl2832_source_0.set_frequency(self.tun_freq+self.fine)
                self._fine_counter.setValue(self.fine)
                self._fine_slider.setValue(self.fine)

        def get_audio_decim(self):
                return self.audio_decim

        def set_audio_decim(self, audio_decim):
                self.audio_decim = audio_decim

if __name__ == '__main__':
        parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
        parser.add_option("-O", "--audio-output", dest="audio_output", type="string", default="",
                help="Set Audio Output Device [default=%default]")
        parser.add_option("-f", "--freq", dest="freq", type="eng_float", default=eng_notation.num_to_str(107.6e6),
                help="Set Default Frequency [default=%default]")
        parser.add_option("-g", "--gain", dest="gain", type="eng_float", default=eng_notation.num_to_str(10),
                help="Set Default Gain [default=%default]")
        parser.add_option("-a", "--address", dest="address", type="string", default="addr=192.168.10.2",
                help="Set IP Address [default=%default]")
        parser.add_option("-s", "--samp-rate", dest="samp_rate", type="eng_float", default=eng_notation.num_to_str(1056e3),
                help="Set Sample Rate [default=%default]")
        (options, args) = parser.parse_args()
        qapp = Qt.QApplication(sys.argv)
        tb = uhd_wbfm_receive(audio_output=options.audio_output, freq=options.freq, gain=options.gain, address=options.address, samp_rate=options.samp_rate)
        tb.start()
        tb.show()
        qapp.exec_()
        tb.stop()