Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / audio.py
1 #
2 # Copyright 2004,2006 Free Software Foundation, Inc.
3
4 # This file is part of GNU Radio
5
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
9 # any later version.
10
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING.  If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
20
21
22 """
23 This is the 'generic' audio or soundcard interface.
24
25 The behavior of this module is controlled by the [audio] audio_module
26 configuration parameter.  If it is 'auto' we attempt to import modules
27 from the known_modules list, using the first one imported successfully.
28
29 If [audio] audio_module is not 'auto', we assume it's the name of
30 an audio module and attempt to import it.
31 """
32
33 __all__ = ['source', 'sink']
34
35 from gnuradio import gr
36 import sys
37
38 source = None
39 sink = None
40
41
42 known_modules = (
43     'audio_alsa', 'audio_oss', 'audio_osx', 'audio_jack', 'audio_portaudio', 'audio_windows')
44
45
46 def try_import(name):
47     """
48     Build a blob of code and try to execute it.
49     If it succeeds we will have set the globals source and sink
50     as side effects.
51
52     returns True or False
53     """
54     global source, sink
55     full_name = "gnuradio." + name
56     code = """
57 import %s
58 source = %s.source
59 sink = %s.sink
60 """ % (full_name, full_name, full_name)
61     try:
62         exec code in globals()
63         return True
64     except ImportError:
65         return False
66     
67
68 def __init__ ():
69     p = gr.prefs()              # get preferences (config file) object
70     verbose = p.get_bool('audio', 'verbose', False)
71     module = p.get_string('audio', 'audio_module', 'auto')
72
73     if module == 'auto':        # search our list for the first one that we can import
74         for m in known_modules:
75             if try_import(m):
76                 if verbose: sys.stderr.write('audio: using %s\n' % (m,))
77                 return
78         raise ImportError, 'Unable to locate an audio module.'
79
80     else:                       # use the one the user specified
81         if try_import(module):
82             if verbose: sys.stderr.write('audio: using %s\n' % (module,))
83         else:
84             msg = 'Failed to import user-specified audio module %s' % (module,)
85             if verbose: sys.stderr.write('audio: %s\n' % (msg,))
86             raise ImportError, msg
87
88 __init__()