Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-examples / python / audio / audio_fft.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2004,2005 Free Software Foundation, Inc.
4
5 # This file is part of GNU Radio
6
7 # GNU Radio is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2, or (at your option)
10 # any later version.
11
12 # GNU Radio is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Radio; see the file COPYING.  If not, write to
19 # the Free Software Foundation, Inc., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
21
22
23 from gnuradio import gr, gru, audio
24 from gnuradio import eng_notation
25 from gnuradio.eng_option import eng_option
26 from gnuradio.wxgui import stdgui, fftsink, waterfallsink, scopesink, form, slider
27 from optparse import OptionParser
28 import wx
29 import sys
30
31 class app_flow_graph(stdgui.gui_flow_graph):
32     def __init__(self, frame, panel, vbox, argv):
33         stdgui.gui_flow_graph.__init__(self)
34
35         self.frame = frame
36         self.panel = panel
37         
38         parser = OptionParser(option_class=eng_option)
39         parser.add_option("-W", "--waterfall", action="store_true", default=False,
40                           help="Enable waterfall display")
41         parser.add_option("-S", "--oscilloscope", action="store_true", default=False,
42                           help="Enable oscilloscope display")
43         parser.add_option("-I", "--audio-input", type="string", default="",
44                           help="pcm input device name.  E.g., hw:0,0 or /dev/dsp")
45         parser.add_option("-r", "--sample-rate", type="eng_float", default=48000,
46                           help="set sample rate to RATE (48000)")
47
48         (options, args) = parser.parse_args()   
49         sample_rate = int(options.sample_rate)
50         
51         if len(args) != 0:
52             parser.print_help()
53             sys.exit(1)
54
55         self.show_debug_info = True
56         
57         # build the graph
58         if options.waterfall:
59             self.scope = \
60               waterfallsink.waterfall_sink_f (self, panel, fft_size=1024, sample_rate=sample_rate)
61         elif options.oscilloscope:
62             self.scope = scopesink.scope_sink_f(self, panel, sample_rate=sample_rate)
63         else:
64             self.scope = fftsink.fft_sink_f (self, panel, fft_size=1024, sample_rate=sample_rate, fft_rate=30)
65
66         self.src = audio.source (sample_rate, options.audio_input)
67
68         self.connect(self.src, self.scope)
69
70         self._build_gui(vbox)
71
72         # set initial values
73
74     def _set_status_msg(self, msg):
75         self.frame.GetStatusBar().SetStatusText(msg, 0)
76
77     def _build_gui(self, vbox):
78
79         def _form_set_freq(kv):
80             return self.set_freq(kv['freq'])
81             
82         vbox.Add(self.scope.win, 10, wx.EXPAND)
83         
84         #self._build_subpanel(vbox)
85
86     def _build_subpanel(self, vbox_arg):
87         # build a secondary information panel (sometimes hidden)
88
89         # FIXME figure out how to have this be a subpanel that is always
90         # created, but has its visibility controlled by foo.Show(True/False)
91         
92         def _form_set_decim(kv):
93             return self.set_decim(kv['decim'])
94
95         if not(self.show_debug_info):
96             return
97
98         panel = self.panel
99         vbox = vbox_arg
100         myform = self.myform
101
102         #panel = wx.Panel(self.panel, -1)
103         #vbox = wx.BoxSizer(wx.VERTICAL)
104
105         hbox = wx.BoxSizer(wx.HORIZONTAL)
106         hbox.Add((5,0), 0)
107
108         myform['decim'] = form.int_field(
109             parent=panel, sizer=hbox, label="Decim",
110             callback=myform.check_input_and_call(_form_set_decim, self._set_status_msg))
111
112         hbox.Add((5,0), 1)
113         myform['fs@usb'] = form.static_float_field(
114             parent=panel, sizer=hbox, label="Fs@USB")
115
116         hbox.Add((5,0), 1)
117         myform['dbname'] = form.static_text_field(
118             parent=panel, sizer=hbox)
119
120         hbox.Add((5,0), 1)
121         myform['baseband'] = form.static_float_field(
122             parent=panel, sizer=hbox, label="Analog BB")
123
124         hbox.Add((5,0), 1)
125         myform['ddc'] = form.static_float_field(
126             parent=panel, sizer=hbox, label="DDC")
127
128         hbox.Add((5,0), 0)
129         vbox.Add(hbox, 0, wx.EXPAND)
130
131         
132 def main ():
133     app = stdgui.stdapp(app_flow_graph, "Audio FFT", nstatus=1)
134     app.MainLoop()
135
136 if __name__ == '__main__':
137     main ()