1 """
2 Pattern generators for audio signals.
3
4
5 $Id: audio.py 6931 2007-10-21 06:01:35Z ceball $
6 """
7 __version__='$Revision: 6931 $'
8
9
10
11 try:
12 import pyaudiolab
13 except ImportError:
14 print "Warning: pyaudiolab must be built to use audio.py"
15
16 import numpy
17
18 from topo.base.parameterclasses import Integer,Parameter
19
20 from topo.misc.filepaths import Filename
21
22 from topo.patterns.basic import OneDPowerSpectrum
23
24
25
26 -class Audio(OneDPowerSpectrum):
27 """
28 ** Untested: currently being written. **
29 """
30
31
32
33 filename = Filename(
34 default='lib/python2.4/site-packages/pyaudiolab/test_data/test.flac',
35 precedence=0.9,doc=
36 """
37 File path (can be relative to Topographica's base path) to an audio file.
38 The audio can be in any format accepted by pyaudiolab, e.g. WAV, AIFF, or FLAC.
39 """)
40
41
42
43
45 """
46 Read the audio file into an array.
47 """
48 self._source = pyaudiolab.sndfile(params.get('filename',self.filename),'read')
49 n_frames = self._source.get_nframes()
50 sig = self._source.read_frames(n_frames,dtype=numpy.float32)
51 spacing = 1.0/self._source.get_samplerate()
52
53 super(Audio,self).__init__(signal=sig,sample_spacing=spacing,**params)
54