blob: 0a91a7163452f768f57d3402d975abdb0e268154 [file] [log] [blame]
#!/usr/bin/python
# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Bandpass Recorder Tool.
This script uses the URSP and records signals from the air. The user can
specify the center-frequency, gain value, width of the recording and the
duration of the recording.
"""
from distutils.version import StrictVersion
import os
import sys
import time
from gnuradio import gr
from PyQt4 import Qt
from bandpass_recorder_file import bandpass_recorder_file
from bandpass_recorder_gui import bandpass_recorder_gui
import options
optspec = """
bandpass_recorder.py [options...] filename
--
v,visual Displays a GUI for viewing the region to be recorded.
f,frequency= Specify the center frequency to record from in MHz [2412]
g,gain= Specify the decibel gain of the recording. [50.0]
w,width= Specify the width of the interval of the recording in KHz. [20000]
d,duration= Specify the length of the recording in seconds. [5]
"""
def record(gain, freq, width, duration, filename):
"""Record a specified signal and store it into a file.
Args:
gain: Amplification of the signal. (dB)
freq: Center Frequency of the recording (MHz).
width: Width of the bandpass filter. (KHz)
duration: Length of the recording. (s)
filename: Path to the file that the recording is stored into.
"""
print 'test'
file_block = bandpass_recorder_file()
print 'recording... ({0} seconds)\n'.format(duration)
file_block.set_gain(gain)
file_block.set_freq(freq * 1e6)
file_block.set_width(width * 1e3)
file_block.start()
time.sleep(duration)
file_block.stop()
file_block.wait()
# Rename output file
try:
os.rename('recorder.out', filename)
except OSError as e:
print '\nOSError [{0}]: {1}'.format(e.errno, e.strerror)
print 'Recording stored in \'recorder.out\'\n'
def main():
o = options.Options(optspec)
opt, flags, extra = o.parse(sys.argv[1:])
if len(extra) != 1:
o.fatal('Filename missing or extra arguments present')
sys.exit(1)
recording_filename = extra[0]
if StrictVersion(Qt.qVersion()) >= StrictVersion('4.5.0'):
prefs = gr.prefs().get_string('qtgui', 'style', 'raster')
Qt.QApplication.setGraphicsSystem(prefs)
qapp = Qt.QApplication(sys.argv)
if opt.visual:
gui_block = bandpass_recorder_gui()
gui_block.start()
gui_block.show()
# Define callback behaviour for the gui_block: starts the file_block and
# begins recording once the gui_block is closed.
def gui_callback():
gain = gui_block.get_gain()
freq = gui_block.get_freq() / 1000000.0
width = gui_block.get_width()
record_time = gui_block.get_record_time() / 1000.0
gui_block.stop()
gui_block.wait()
record(gain, freq, width, record_time, recording_filename)
qapp.connect(qapp, Qt.SIGNAL('aboutToQuit()'), gui_callback)
qapp.exec_()
else:
record(float(opt.gain), float(opt.frequency), float(opt.width),
float(opt.duration), recording_filename)
if __name__ == '__main__':
main()