#!/usr/bin/env python
##################################################
# Gnuradio Python Flow Graph
# Title: Records Player
# Author: Jakub Kakona
# Generated: Fri May 30 01:39:46 2014
##################################################

from gnuradio import blocks
from gnuradio import eng_notation
from gnuradio import filter
from gnuradio import gr
from gnuradio import wxgui
from gnuradio.eng_option import eng_option
from gnuradio.fft import window
from gnuradio.filter import firdes
from gnuradio.wxgui import scopesink2
from gnuradio.wxgui import waterfallsink2
from grc_gnuradio import wxgui as grc_wxgui
from optparse import OptionParser
import wx

class Records_player(grc_wxgui.top_block_gui):

    def __init__(self):
        grc_wxgui.top_block_gui.__init__(self, title="Records Player")
        _icon_path = "/usr/local/share/icons/hicolor/32x32/apps/gnuradio-grc.png"
        self.SetIcon(wx.Icon(_icon_path, wx.BITMAP_TYPE_ANY))

        ##################################################
        # Variables
        ##################################################
        self.prefix = prefix = "/media/kaklik/Radio_zaloha/2014-05-02/"
        self.out_dir = out_dir = "/home/kaklik/git/pysdr/"
        self.filename = filename = "2014.05.02.14.58.50.dat"
        self.srcfile = srcfile = prefix+filename
        self.samp_rate = samp_rate = 5e6
        self.outfile_ADC2 = outfile_ADC2 = out_dir+filename+"ADC2.wav"
        self.outfile_ADC1 = outfile_ADC1 = out_dir+filename+"ADC1.wav"
        self.out_sample_rate = out_sample_rate = int(2e3)
        self.normalize = normalize = 1/200.0
        self.cutoff = cutoff = 1000
        self.LO = LO = 286078822.488

        ##################################################
        # Blocks
        ##################################################
        self.notebook_0 = self.notebook_0 = wx.Notebook(self.GetWin(), style=wx.NB_TOP)
        self.notebook_0.AddPage(grc_wxgui.Panel(self.notebook_0), "RF signal")
        self.notebook_0.AddPage(grc_wxgui.Panel(self.notebook_0), "Scope")
        self.notebook_0.AddPage(grc_wxgui.Panel(self.notebook_0), "Demod")
        self.Add(self.notebook_0)
        self.wxgui_waterfallsink2_1_0 = waterfallsink2.waterfall_sink_c(
                self.notebook_0.GetPage(2).GetWin(),
                baseband_freq=0,
                dynamic_range=100,
                ref_level=0,
                ref_scale=2.0,
                sample_rate=out_sample_rate,
                fft_size=512,
                fft_rate=15,
                average=False,
                avg_alpha=None,
                title="IF Plot",
                win=window.blackmanharris,
                size=(1200, 400),
        )
        self.notebook_0.GetPage(2).Add(self.wxgui_waterfallsink2_1_0.win)
        self.wxgui_waterfallsink2_1 = waterfallsink2.waterfall_sink_c(
                self.notebook_0.GetPage(2).GetWin(),
                baseband_freq=0,
                dynamic_range=100,
                ref_level=0,
                ref_scale=2.0,
                sample_rate=out_sample_rate,
                fft_size=512,
                fft_rate=15,
                average=False,
                avg_alpha=None,
                title="IF Plot",
                win=window.blackmanharris,
                size=(1200,400),
        )
        self.notebook_0.GetPage(2).Add(self.wxgui_waterfallsink2_1.win)
        self.wxgui_waterfallsink2_0_0 = waterfallsink2.waterfall_sink_c(
                self.notebook_0.GetPage(0).GetWin(),
                baseband_freq=0,
                dynamic_range=100,
                ref_level=0,
                ref_scale=2.0,
                sample_rate=samp_rate,
                fft_size=1024,
                fft_rate=15,
                average=False,
                avg_alpha=None,
                title="ADC1 Waterfall Plot",
        )
        self.notebook_0.GetPage(0).Add(self.wxgui_waterfallsink2_0_0.win)
        self.wxgui_waterfallsink2_0 = waterfallsink2.waterfall_sink_c(
                self.notebook_0.GetPage(0).GetWin(),
                baseband_freq=0,
                dynamic_range=100,
                ref_level=0,
                ref_scale=2.0,
                sample_rate=samp_rate,
                fft_size=512,
                fft_rate=15,
                average=False,
                avg_alpha=None,
                title="ADC2 Waterfall Plot",
                win=window.hanning,
        )
        self.notebook_0.GetPage(0).Add(self.wxgui_waterfallsink2_0.win)
        self.wxgui_scopesink2_2 = scopesink2.scope_sink_c(
                self.GetWin(),
                title="Scope Plot",
                sample_rate=out_sample_rate,
                v_scale=0,
                v_offset=0,
                t_scale=0,
                ac_couple=False,
                xy_mode=True,
                num_inputs=1,
                trig_mode=wxgui.TRIG_MODE_AUTO,
                y_axis_label="Counts",
        )
        self.Add(self.wxgui_scopesink2_2.win)
        self.wxgui_scopesink2_1 = scopesink2.scope_sink_f(
                self.notebook_0.GetPage(1).GetWin(),
                title="Scope Plot",
                sample_rate=samp_rate/2,
                v_scale=0,
                v_offset=0,
                t_scale=1e-2,
                ac_couple=False,
                xy_mode=False,
                num_inputs=2,
                trig_mode=wxgui.TRIG_MODE_AUTO,
                y_axis_label="Counts",
        )
        self.notebook_0.GetPage(1).Add(self.wxgui_scopesink2_1.win)
        self.wxgui_scopesink2_0 = scopesink2.scope_sink_f(
                self.notebook_0.GetPage(1).GetWin(),
                title="Scope Plot",
                sample_rate=samp_rate,
                v_scale=0,
                v_offset=0,
                t_scale=0,
                ac_couple=False,
                xy_mode=False,
                num_inputs=4,
                trig_mode=wxgui.TRIG_MODE_AUTO,
                y_axis_label="Counts",
        )
        self.notebook_0.GetPage(1).Add(self.wxgui_scopesink2_0.win)
        self.low_pass_filter_0_0 = filter.fir_filter_ccf(int(samp_rate/out_sample_rate), firdes.low_pass(
                normalize, samp_rate, cutoff, 1e3, firdes.WIN_HAMMING, 6.76))
        self.low_pass_filter_0 = filter.fir_filter_ccf(int(samp_rate/out_sample_rate), firdes.low_pass(
                normalize, samp_rate, cutoff, 1e3, firdes.WIN_HAMMING, 6.76))
        self.freq_xlating_fir_filter_xxx_0_0 = filter.freq_xlating_fir_filter_ccc(1, ([1]), 143050000-LO/2, samp_rate)
        self.freq_xlating_fir_filter_xxx_0 = filter.freq_xlating_fir_filter_ccc(1, ([1]), 143050000-LO/2, samp_rate)
        self.blocks_wavfile_sink_1_0 = blocks.wavfile_sink(outfile_ADC2, 2, out_sample_rate, 16)
        self.blocks_wavfile_sink_1 = blocks.wavfile_sink(outfile_ADC1, 2, out_sample_rate, 16)
        self.blocks_vector_to_streams_0 = blocks.vector_to_streams(gr.sizeof_short*1, 10)
        self.blocks_short_to_float_2_0 = blocks.short_to_float(1, 1)
        self.blocks_short_to_float_2 = blocks.short_to_float(1, 1)
        self.blocks_short_to_float_1_0 = blocks.short_to_float(1, 1)
        self.blocks_short_to_float_1 = blocks.short_to_float(1, 1)
        self.blocks_short_to_float_0_0 = blocks.short_to_float(1, 1)
        self.blocks_short_to_float_0 = blocks.short_to_float(1, 1)
        self.blocks_multiply_conjugate_cc_0 = blocks.multiply_conjugate_cc(1)
        self.blocks_interleave_0_2 = blocks.interleave(gr.sizeof_short*1)
        self.blocks_interleave_0_1 = blocks.interleave(gr.sizeof_short*1)
        self.blocks_interleave_0_0 = blocks.interleave(gr.sizeof_short*1)
        self.blocks_interleave_0 = blocks.interleave(gr.sizeof_short*1)
        self.blocks_float_to_complex_0_0 = blocks.float_to_complex(1)
        self.blocks_float_to_complex_0 = blocks.float_to_complex(1)
        self.blocks_file_source_0 = blocks.file_source(gr.sizeof_short*10, srcfile, False)
        self.blocks_complex_to_float_0_0 = blocks.complex_to_float(1)
        self.blocks_complex_to_float_0 = blocks.complex_to_float(1)

        ##################################################
        # Connections
        ##################################################
        self.connect((self.blocks_vector_to_streams_0, 2), (self.blocks_interleave_0_2, 0))
        self.connect((self.blocks_vector_to_streams_0, 3), (self.blocks_interleave_0_2, 1))
        self.connect((self.blocks_vector_to_streams_0, 4), (self.blocks_interleave_0_1, 0))
        self.connect((self.blocks_vector_to_streams_0, 5), (self.blocks_interleave_0_1, 1))
        self.connect((self.blocks_vector_to_streams_0, 6), (self.blocks_interleave_0_0, 0))
        self.connect((self.blocks_vector_to_streams_0, 7), (self.blocks_interleave_0_0, 1))
        self.connect((self.blocks_vector_to_streams_0, 8), (self.blocks_interleave_0, 0))
        self.connect((self.blocks_vector_to_streams_0, 9), (self.blocks_interleave_0, 1))
        self.connect((self.blocks_file_source_0, 0), (self.blocks_vector_to_streams_0, 0))
        self.connect((self.blocks_interleave_0, 0), (self.blocks_short_to_float_1, 0))
        self.connect((self.blocks_interleave_0_0, 0), (self.blocks_short_to_float_0, 0))
        self.connect((self.blocks_short_to_float_1, 0), (self.wxgui_scopesink2_0, 3))
        self.connect((self.blocks_short_to_float_0, 0), (self.wxgui_scopesink2_0, 2))
        self.connect((self.blocks_interleave_0_1, 0), (self.blocks_short_to_float_1_0, 0))
        self.connect((self.blocks_interleave_0_2, 0), (self.blocks_short_to_float_0_0, 0))
        self.connect((self.low_pass_filter_0, 0), (self.blocks_complex_to_float_0, 0))
        self.connect((self.freq_xlating_fir_filter_xxx_0, 0), (self.low_pass_filter_0, 0))
        self.connect((self.low_pass_filter_0, 0), (self.wxgui_waterfallsink2_1, 0))
        self.connect((self.low_pass_filter_0_0, 0), (self.blocks_complex_to_float_0_0, 0))
        self.connect((self.freq_xlating_fir_filter_xxx_0_0, 0), (self.low_pass_filter_0_0, 0))
        self.connect((self.low_pass_filter_0_0, 0), (self.wxgui_waterfallsink2_1_0, 0))
        self.connect((self.blocks_short_to_float_2_0, 0), (self.wxgui_scopesink2_1, 1))
        self.connect((self.blocks_short_to_float_2, 0), (self.wxgui_scopesink2_1, 0))
        self.connect((self.blocks_vector_to_streams_0, 1), (self.blocks_short_to_float_2_0, 0))
        self.connect((self.blocks_vector_to_streams_0, 0), (self.blocks_short_to_float_2, 0))
        self.connect((self.blocks_float_to_complex_0, 0), (self.freq_xlating_fir_filter_xxx_0_0, 0))
        self.connect((self.blocks_float_to_complex_0_0, 0), (self.freq_xlating_fir_filter_xxx_0, 0))
        self.connect((self.blocks_short_to_float_0_0, 0), (self.blocks_float_to_complex_0_0, 0))
        self.connect((self.blocks_short_to_float_1_0, 0), (self.blocks_float_to_complex_0_0, 1))
        self.connect((self.blocks_short_to_float_1, 0), (self.blocks_float_to_complex_0, 1))
        self.connect((self.blocks_short_to_float_0, 0), (self.blocks_float_to_complex_0, 0))
        self.connect((self.blocks_short_to_float_0_0, 0), (self.wxgui_scopesink2_0, 0))
        self.connect((self.blocks_short_to_float_1_0, 0), (self.wxgui_scopesink2_0, 1))
        self.connect((self.blocks_float_to_complex_0, 0), (self.wxgui_waterfallsink2_0, 0))
        self.connect((self.blocks_float_to_complex_0_0, 0), (self.wxgui_waterfallsink2_0_0, 0))
        self.connect((self.blocks_complex_to_float_0_0, 0), (self.blocks_wavfile_sink_1_0, 0))
        self.connect((self.blocks_complex_to_float_0_0, 1), (self.blocks_wavfile_sink_1_0, 1))
        self.connect((self.blocks_complex_to_float_0, 0), (self.blocks_wavfile_sink_1, 0))
        self.connect((self.blocks_complex_to_float_0, 1), (self.blocks_wavfile_sink_1, 1))
        self.connect((self.low_pass_filter_0, 0), (self.blocks_multiply_conjugate_cc_0, 0))
        self.connect((self.low_pass_filter_0_0, 0), (self.blocks_multiply_conjugate_cc_0, 1))
        self.connect((self.blocks_multiply_conjugate_cc_0, 0), (self.wxgui_scopesink2_2, 0))


# QT sink close method reimplementation

    def get_prefix(self):
        return self.prefix

    def set_prefix(self, prefix):
        self.prefix = prefix
        self.set_srcfile(self.prefix+self.filename)

    def get_out_dir(self):
        return self.out_dir

    def set_out_dir(self, out_dir):
        self.out_dir = out_dir
        self.set_outfile_ADC2(self.out_dir+self.filename+"ADC2.wav")
        self.set_outfile_ADC1(self.out_dir+self.filename+"ADC1.wav")

    def get_filename(self):
        return self.filename

    def set_filename(self, filename):
        self.filename = filename
        self.set_outfile_ADC2(self.out_dir+self.filename+"ADC2.wav")
        self.set_outfile_ADC1(self.out_dir+self.filename+"ADC1.wav")
        self.set_srcfile(self.prefix+self.filename)

    def get_srcfile(self):
        return self.srcfile

    def set_srcfile(self, srcfile):
        self.srcfile = srcfile
        self.blocks_file_source_0.open(self.srcfile, False)

    def get_samp_rate(self):
        return self.samp_rate

    def set_samp_rate(self, samp_rate):
        self.samp_rate = samp_rate
        self.wxgui_scopesink2_0.set_sample_rate(self.samp_rate)
        self.wxgui_scopesink2_1.set_sample_rate(self.samp_rate/2)
        self.wxgui_waterfallsink2_0_0.set_sample_rate(self.samp_rate)
        self.wxgui_waterfallsink2_0.set_sample_rate(self.samp_rate)
        self.low_pass_filter_0.set_taps(firdes.low_pass(self.normalize, self.samp_rate, self.cutoff, 1e3, firdes.WIN_HAMMING, 6.76))
        self.low_pass_filter_0_0.set_taps(firdes.low_pass(self.normalize, self.samp_rate, self.cutoff, 1e3, firdes.WIN_HAMMING, 6.76))

    def get_outfile_ADC2(self):
        return self.outfile_ADC2

    def set_outfile_ADC2(self, outfile_ADC2):
        self.outfile_ADC2 = outfile_ADC2
        self.blocks_wavfile_sink_1_0.open(self.outfile_ADC2)

    def get_outfile_ADC1(self):
        return self.outfile_ADC1

    def set_outfile_ADC1(self, outfile_ADC1):
        self.outfile_ADC1 = outfile_ADC1
        self.blocks_wavfile_sink_1.open(self.outfile_ADC1)

    def get_out_sample_rate(self):
        return self.out_sample_rate

    def set_out_sample_rate(self, out_sample_rate):
        self.out_sample_rate = out_sample_rate
        self.wxgui_waterfallsink2_1_0.set_sample_rate(self.out_sample_rate)
        self.wxgui_waterfallsink2_1.set_sample_rate(self.out_sample_rate)
        self.wxgui_scopesink2_2.set_sample_rate(self.out_sample_rate)

    def get_normalize(self):
        return self.normalize

    def set_normalize(self, normalize):
        self.normalize = normalize
        self.low_pass_filter_0.set_taps(firdes.low_pass(self.normalize, self.samp_rate, self.cutoff, 1e3, firdes.WIN_HAMMING, 6.76))
        self.low_pass_filter_0_0.set_taps(firdes.low_pass(self.normalize, self.samp_rate, self.cutoff, 1e3, firdes.WIN_HAMMING, 6.76))

    def get_cutoff(self):
        return self.cutoff

    def set_cutoff(self, cutoff):
        self.cutoff = cutoff
        self.low_pass_filter_0.set_taps(firdes.low_pass(self.normalize, self.samp_rate, self.cutoff, 1e3, firdes.WIN_HAMMING, 6.76))
        self.low_pass_filter_0_0.set_taps(firdes.low_pass(self.normalize, self.samp_rate, self.cutoff, 1e3, firdes.WIN_HAMMING, 6.76))

    def get_LO(self):
        return self.LO

    def set_LO(self, LO):
        self.LO = LO
        self.freq_xlating_fir_filter_xxx_0.set_center_freq(143050000-self.LO/2)
        self.freq_xlating_fir_filter_xxx_0_0.set_center_freq(143050000-self.LO/2)

if __name__ == '__main__':
    import ctypes
    import os
    if os.name == 'posix':
        try:
            x11 = ctypes.cdll.LoadLibrary('libX11.so')
            x11.XInitThreads()
        except:
            print "Warning: failed to XInitThreads()"
    parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
    (options, args) = parser.parse_args()
    tb = Records_player()
    tb.Start(True)
    tb.Wait()