#!/usr/bin/env python
##################################################
# Gnuradio Python Flow Graph
# Title: Sonar
# Author: Kaklik
# Description: Sonar waform generator
# Generated: Sat Feb 21 11:50:42 2009
##################################################

from gnuradio import audio
from gnuradio import gr
from gnuradio.wxgui import scopesink2
from gnuradio.wxgui import waterfallsink2
from grc_gnuradio import wxgui as grc_wxgui
import wx

class Sonar(grc_wxgui.top_block_gui):

	def __init__(self):
		grc_wxgui.top_block_gui.__init__(
			self,
			title="GRC - Executing: Sonar",
			icon="/usr/local/share/icons/hicolor/32x32/apps/gnuradio-grc.png",
		)

		##################################################
		# Variables
		##################################################
		self.samp_rate = samp_rate = 96000
		self.frequency = frequency = 20000
		self.ampl = ampl = .4

		##################################################
		# Controls
		##################################################
		_frequency_control = grc_wxgui.slider_horizontal_control(
			window=self.GetWin(),
			callback=self.set_frequency,
			label='frequency',
			value=frequency,
			min=5000,
			max=30000,
			num_steps=1000,
			slider_length=500,
		)
		self.Add(_frequency_control)
		_ampl_control = grc_wxgui.slider_horizontal_control(
			window=self.GetWin(),
			callback=self.set_ampl,
			label="Volume",
			value=ampl,
			min=0,
			max=.5,
			num_steps=100,
			slider_length=200,
		)
		self.GridAdd(_ampl_control, 0, 0, 1, 2)

		##################################################
		# Blocks
		##################################################
		self.audio_sink = audio.sink(48000, "hw:1,0", True)
		self.audio_source_0 = audio.source(48000, "hw:1,0", True)
		self.gr_sig_source_x = gr.sig_source_f(samp_rate, gr.GR_SIN_WAVE, frequency, ampl, 0)
		self.wxgui_scopesink2_0 = scopesink2.scope_sink_f(
			self.GetWin(),
			title="Scope Plot",
			sample_rate=samp_rate*2,
			frame_decim=15,
			v_scale=None,
			t_scale=.0001,
			num_inputs=2,
		)
		self.wxgui_scopesink2_0.win.set_format_line()
		self.Add(self.wxgui_scopesink2_0.win)
		self.wxgui_waterfallsink2_0 = waterfallsink2.waterfall_sink_f(
			self.GetWin(),
			baseband_freq=0,
			y_per_div=10,
			ref_level=50,
			sample_rate=samp_rate,
			fft_size=512,
			fft_rate=15,
			average=True,
			avg_alpha=None,
			title="Waterfall Plot",
		)
		self.Add(self.wxgui_waterfallsink2_0.win)

		##################################################
		# Connections
		##################################################
		self.connect((self.gr_sig_source_x, 0), (self.audio_sink, 0))
		self.connect((self.gr_sig_source_x, 0), (self.wxgui_scopesink2_0, 0))
		self.connect((self.audio_source_0, 0), (self.wxgui_scopesink2_0, 1))
		self.connect((self.audio_source_0, 0), (self.wxgui_waterfallsink2_0, 0))

	def set_samp_rate(self, samp_rate):
		self.samp_rate = samp_rate
		self.gr_sig_source_x.set_sampling_freq(self.samp_rate)
		self.wxgui_scopesink2_0.set_sample_rate(self.samp_rate*2)
		self.wxgui_waterfallsink2_0.set_sample_rate(self.samp_rate)

	def set_frequency(self, frequency):
		self.frequency = frequency
		self.gr_sig_source_x.set_frequency(self.frequency)

	def set_ampl(self, ampl):
		self.ampl = ampl
		self.gr_sig_source_x.set_amplitude(self.ampl)

if __name__ == '__main__':
	if gr.enable_realtime_scheduling() != gr.RT_OK:
		print "Error: failed to enable realtime scheduling."
	tb = Sonar()
	tb.Run()

