Thursday, July 9, 2009

Forks: GNURadio-Simple Freq Setter

#! /usr/bin/env python

from gnuradio import gr
from gnuradio import audio
import time
class my_top_block(gr.top_block): #deriving a flow graph
def __init__(self):#Constructor
gr.top_block.__init__(self)#Base Class's Constructor

sample_rate = 44100
ampl = 0.1

#Making use of GNURadio's predefined sources to get sine waves
self.src0 = gr.sig_source_f (sample_rate, gr.GR_SIN_WAVE, 350, ampl)
self.src1 = gr.sig_source_f (sample_rate, gr.GR_SIN_WAVE, 440, ampl)

#GNURadio predefined signal multipliers
self.amp0 = gr.multiply_const_ff(1)
self.amp1 = gr.multiply_const_ff(1)

#Audio sink that outputs to Sound Card
dst = audio.sink (sample_rate, "")

#Connecting the two seperate channels of the flow graph
self.connect (self.src0, self.amp0, (dst, 0))
self.connect (self.src1, self.amp1, (dst, 1))

#Quick little routine to set both the channel's volume together
def set_volume(self, volume):
self.amp0.set_k(volume)
self.amp1.set_k(volume)


if __name__ == '__main__':
try:
go = my_top_block()
go.start()
while True: #Infinite loop for User Interface
#interacting with user
#in fact this rest is mostly python tricks (kinda) and not much
#GNURadio...
mode = raw_input ('Change Freq, Change Volume or Quit? ')
mode = mode.lower()
for x in mode[:]:
if x == 'f':
mode = 'f'
break
elif x == 'v':
mode = 'v'
break
elif x == 'q':
mode = 'q'
break
if mode == 'f':
side = raw_input ('Which side? (l or r) ')
new_freq = input ('Enter freq: ')
if side == 'l':
#Accessing members directly! Big No-No in C++
#apparently not bad in python-I could be wrong...
go.src0.set_frequency(new_freq)
elif side == 'r':
go.src1.set_frequency(new_freq)
else:
print ('Invalid Side')
elif mode == 'v':
volume = input ('Enter Volume: ')
go.set_volume(volume)
elif mode == 'q':
print ('Goodbye')
break
else:
print 'Invalid Selection'
go.src0.set_frequency(140)
go.src1.set_frequency(140)
go.set_volume(0)
time.sleep(0.5)
go.set_volume(2)
time.sleep(0.5)
go.set_volume(0)
time.sleep(0.5)
go.set_volume(2)
time.sleep(0.5)
go.set_volume(0)
time.sleep(0.5)
go.set_volume(2)
time.sleep(0.5)
go.set_volume(0)
go.stop()
except KeyboardInterrupt:
pass





There it is, my simple and probably very bad little frequency setting program using GNURadio. I welcome comments (if anyone actually reads this) but you must remember I'm an Engineer not a Computer Science guy so you can't expect too much from me when it comes to pretty code.

No comments:

Post a Comment