1 """File I/O classes for the harmonical
2
3 """
4 """
5 ============================== License ========================================
6 Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding
7
8 This file is part of The Jazz Parser.
9
10 The Jazz Parser is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 The Jazz Parser is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with The Jazz Parser. If not, see <http://www.gnu.org/licenses/>.
22
23 ============================ End license ======================================
24
25 """
26 __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>"
27
28 import wave, os.path
29
30 DEFAULT_SAMPLE_RATE = 44100
31
33 """
34 A wave file to store uncompressed audio signal data.
35 """
37 self.filename = filename
38 self._signal = []
39 self.sr = sample_rate
40
42 file = wave.open(self.filename, 'wb')
43
44 str_signal = self.get_data()
45
46 file.setparams((1, 2, self.sr, self.sr*4, 'NONE', 'noncompressed'))
47 file.writeframes(str_signal)
48 file.close()
49
52
54 self._signal.extend(signal)
55
58
60 """
61 Return the raw data that will be written to the file.
62
63 """
64 return ''.join([wave.struct.pack('h', samp) for samp in self._signal])
65
67 """
68 Returns a file-like object containing the data that would be
69 written to the file. This object won't get updated if the
70 SoundFile is changed.
71
72 """
73 from StringIO import StringIO
74 return StringIO(self.get_data())
75
77 """
78 Shortcut to store a wave file given some sample data.
79 Assumes the standard sample rate.
80
81 """
82 filename = os.path.abspath(filename)
83 f = SoundFile(filename)
84 f.set_signal(signal)
85 f.save()
86