GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: A collection of audio handling programs which work from the command line.
Homepage: http://audiotools.sourceforge.net
Clone URL: git://github.com/tuffy/python-audio-tools.git
Converted test.py to a unittest-based app.
More tests should probably be added.
Brian Langenberger (author)
Sat Sep 08 17:01:57 -0700 2007
commit  591e0d3d9bf5aac2422bf8316c353e8ebf60a591
tree    f90341e1e7012ff43eb700ac4f6523d3c3eced4d
parent  c17368aa19e74a2a0f0dab7bf0316ed7d07588d7
...
7
8
9
 
 
 
10
11
12
...
7
8
9
10
11
12
13
14
15
0
@@ -7,6 +7,9 @@ install:
0
   $(PYTHON) setup.py install
0
   cd docs && $(MAKE) install
0
 
0
+check:
0
+ cd test && $(PYTHON) test.py
0
+
0
 clean:
0
   rm -rfv build
0
   rm -fv audiotools/*.pyc
...
19
20
21
22
 
 
 
 
23
24
25
...
30
31
32
33
34
35
 
 
36
37
38
39
40
41
42
43
44
45
46
47
48
 
49
50
51
 
52
53
54
...
67
68
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
 
 
...
19
20
21
 
22
23
24
25
26
27
28
...
33
34
35
 
 
 
36
37
38
39
 
 
 
 
 
 
 
 
 
 
 
40
41
42
 
43
44
45
46
...
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
199
0
@@ -19,7 +19,10 @@
0
 
0
 
0
 import audiotools
0
-import tempfile,sys
0
+import tempfile
0
+import sys
0
+import cStringIO
0
+import unittest
0
 
0
 class BLANK_PCM_Reader:
0
     #length is the total length of this PCM stream, in seconds
0
@@ -30,25 +33,14 @@ class BLANK_PCM_Reader:
0
         self.channels = channels
0
         self.bits_per_sample = bits_per_sample
0
 
0
- self.bytes_remaining = length * sample_rate * channels * \
0
- bits_per_sample / 8
0
- self.buffer = ""
0
+ byte_total = length * sample_rate * channels * bits_per_sample / 8
0
+ self.buffer = cStringIO.StringIO('\x01\x00' * (byte_total / 2))
0
 
0
     def read(self, bytes):
0
- if (bytes < self.bytes_remaining):
0
- if (bytes != len(self.buffer)):
0
- self.buffer = '\x01\x00' * (bytes / 2)
0
- self.bytes_remaining -= bytes
0
- return self.buffer
0
- elif (self.bytes_remaining > 0):
0
- self.bytes_remaining = 0
0
- return '\x01\x00' * (bytes / 2)
0
- else:
0
- return ""
0
-
0
+ return self.buffer.read(bytes)
0
 
0
     def close(self):
0
- pass
0
+ self.buffer.close()
0
 
0
 class PCM_Count:
0
     def __init__(self):
0
@@ -67,65 +59,141 @@ class DummyMetaData(audiotools.MetaData):
0
                                      track_number=5,
0
                                      album_name=u"Album Name",
0
                                      artist_name=u"Artist Name")
0
+
0
+class DummyMetaData2(audiotools.MetaData):
0
+ def __init__(self):
0
+ audiotools.MetaData.__init__(self,
0
+ track_name=u"New Track Name",
0
+ track_number=6,
0
+ album_name=u"New Album Name",
0
+ artist_name=u"New Artist Name")
0
+
0
+
0
+TEST_LENGTH = 30
0
+SHORT_LENGTH = 5
0
+
0
+class TestWaveAudio(unittest.TestCase):
0
+ def setUp(self):
0
+ self.audio_class = audiotools.WaveAudio
0
+
0
+ def testblankencode(self):
0
+ temp = tempfile.NamedTemporaryFile(suffix="." + self.audio_class.SUFFIX)
0
+ try:
0
+ new_file = self.audio_class.from_pcm(temp.name,
0
+ BLANK_PCM_Reader(TEST_LENGTH))
0
+ if (new_file.lossless()):
0
+ self.assertEqual(audiotools.pcm_cmp(
0
+ new_file.to_pcm(),
0
+ BLANK_PCM_Reader(TEST_LENGTH)),True)
0
+ else:
0
+ counter = PCM_Count()
0
+ pcm = new_file.to_pcm()
0
+ audiotools.transfer_data(pcm.read,counter.write)
0
+ self.assert_(len(counter) > 0)
0
+ finally:
0
+ temp.close()
0
+
0
+ def testmassencode(self):
0
+ temp = tempfile.NamedTemporaryFile(suffix="." + self.audio_class.SUFFIX)
0
+
0
+ tempfiles = [(tempfile.NamedTemporaryFile(
0
+ suffix="." + audio_class.SUFFIX),
0
+ audio_class) for audio_class in audiotools.AVAILABLE_TYPES]
0
+
0
+ other_files = [
0
+ audio_class.from_pcm(temp_file.name,
0
+ BLANK_PCM_Reader(SHORT_LENGTH))
0
+ for (temp_file,audio_class) in tempfiles]
0
+ try:
0
+ for f in other_files:
0
+ new_file = self.audio_class.from_pcm(
0
+ temp.name,
0
+ f.to_pcm())
0
+
0
+ if (new_file.lossless() and f.lossless()):
0
+ self.assertEqual(audiotools.pcm_cmp(
0
+ new_file.to_pcm(),
0
+ f.to_pcm()),True)
0
+ else:
0
+ counter = PCM_Count()
0
+ pcm = new_file.to_pcm()
0
+ audiotools.transfer_data(pcm.read,counter.write)
0
+ self.assert_(len(counter) > 0)
0
+ finally:
0
+ temp.close()
0
+ for (temp_file,audio_class) in tempfiles:
0
+ temp_file.close()
0
+
0
+
0
+ def testmetadata(self):
0
+ temp = tempfile.NamedTemporaryFile(suffix="." + self.audio_class.SUFFIX)
0
+ try:
0
+ new_file = self.audio_class.from_pcm(temp.name,
0
+ BLANK_PCM_Reader(TEST_LENGTH))
0
+
0
+ if (new_file.get_metadata() is not None):
0
+ metadata = DummyMetaData()
0
+ new_file.set_metadata(metadata)
0
+ new_file = audiotools.open(temp.name)
0
+ self.assertEqual(metadata,new_file.get_metadata())
0
+
0
+ metadata2 = DummyMetaData2()
0
+ new_file.set_metadata(metadata2)
0
+ new_file = audiotools.open(temp.name)
0
+ self.assertEqual(metadata2,new_file.get_metadata())
0
+
0
+ metadata2.track_name = u'Track Name 3'
0
+ new_file.set_metadata(metadata2)
0
+ new_file = audiotools.open(temp.name)
0
+ self.assertEqual(metadata2,new_file.get_metadata())
0
+ finally:
0
+ temp.close()
0
+
0
+class TestAiffAudio(TestWaveAudio):
0
+ def setUp(self):
0
+ self.audio_class = audiotools.AiffAudio
0
+
0
+class TestAuAudio(TestWaveAudio):
0
+ def setUp(self):
0
+ self.audio_class = audiotools.AuAudio
0
+
0
+class TestFlacAudio(TestWaveAudio):
0
+ def setUp(self):
0
+ self.audio_class = audiotools.FlacAudio
0
+
0
+class TestWavPackAudio(TestWaveAudio):
0
+ def setUp(self):
0
+ self.audio_class = audiotools.WavPackAudio
0
+
0
+class TestOggFlacAudio(TestWaveAudio):
0
+ def setUp(self):
0
+ self.audio_class = audiotools.OggFlacAudio
0
+
0
+class TestMP3Audio(TestWaveAudio):
0
+ def setUp(self):
0
+ self.audio_class = audiotools.MP3Audio
0
+
0
+class TestMP2Audio(TestWaveAudio):
0
+ def setUp(self):
0
+ self.audio_class = audiotools.MP2Audio
0
+
0
+class TestVorbisAudio(TestWaveAudio):
0
+ def setUp(self):
0
+ self.audio_class = audiotools.VorbisAudio
0
+
0
+class TestM4AAudio(TestWaveAudio):
0
+ def setUp(self):
0
+ self.audio_class = audiotools.M4AAudio
0
+
0
+class TestMusepackAudio(TestWaveAudio):
0
+ def setUp(self):
0
+ self.audio_class = audiotools.MusepackAudio
0
+
0
+class TestSpeexAudio(TestWaveAudio):
0
+ def setUp(self):
0
+ self.audio_class = audiotools.SpeexAudio
0
 
0
-TEST_LENGTH = 120
0
-#TEST_LENGTH = 3600
0
-#TEST_LENGTH = 60
0
-
0
-def test_format(AudioFormat):
0
- temp = tempfile.NamedTemporaryFile(suffix="." + AudioFormat.SUFFIX)
0
- try:
0
- file_matches = True
0
- dummy_metadata = DummyMetaData()
0
-
0
- audiofile = AudioFormat.from_pcm(temp.name,
0
- BLANK_PCM_Reader(TEST_LENGTH))
0
- audiofile.set_metadata(dummy_metadata)
0
- if (audiofile.lossless()):
0
- file_matches = audiotools.pcm_cmp(
0
- audiofile.to_pcm(),BLANK_PCM_Reader(TEST_LENGTH))
0
- else:
0
- p = audiofile.to_pcm()
0
- counter = PCM_Count()
0
- audiotools.transfer_data(p.read,counter.write)
0
- p.close()
0
- file_matches = (len(counter) > 0)
0
-
0
- audiofile = audiotools.open(audiofile.filename)
0
- file_metadata = audiofile.get_metadata()
0
- if (file_metadata != None):
0
- if (not file_matches):
0
- print >>sys.stderr,"* File data mismatch"
0
- if (file_metadata != dummy_metadata):
0
- #print >>sys.stderr,""
0
- #print >>sys.stderr,repr(file_metadata)
0
- #print >>sys.stderr,repr(dummy_metadata)
0
- print >>sys.stderr,"* File metadata mismatch"
0
- return file_matches and (file_metadata == dummy_metadata)
0
- else:
0
- if (not file_matches):
0
- print >>sys.stderr,"* File data mismatch"
0
- return file_matches
0
- finally:
0
- temp.close()
0
 
0
 if (__name__ == '__main__'):
0
- print "Testing Audio Tools"
0
-
0
- if (len(sys.argv) == 1):
0
- type_dict = audiotools.TYPE_MAP
0
- else:
0
- type_dict = dict([(key,value) for (key,value) in
0
- audiotools.TYPE_MAP.items() if
0
- key in sys.argv[1:]])
0
-
0
- max_suffix_length = max([len(s) for s in type_dict.keys()])
0
- for audiotype in type_dict.values():
0
- sys.stdout.write(
0
- "* Testing %%%d.%ds :" % (max_suffix_length,
0
- max_suffix_length) % (audiotype.SUFFIX))
0
- sys.stdout.flush()
0
- if (test_format(audiotype)):
0
- print " OK"
0
- else:
0
- print " FAILS"
0
+ unittest.main()
0
+

Comments

    No one has commented yet.