##// END OF EJS Templates
Fix Audio from data without numpy on Python 3.
Matan Gover -
Show More
@@ -159,16 +159,12 b' class Audio(DisplayObject):'
159 159
160 160 @staticmethod
161 161 def _validate_and_normalize_without_numpy(data):
162 # check that it is a "1D" list
163 idata = iter(data) # fails if not an iterable
164 162 try:
165 iter(idata.next())
163 maxabsvalue = float(max([abs(x) for x in data]))
164 except TypeError:
166 165 raise TypeError('Only lists of mono audio are '
167 166 'supported if numpy is not installed')
168 except TypeError:
169 # this means it's not a nested list, which is what we want
170 pass
171 maxabsvalue = float(max([abs(x) for x in data]))
167
172 168 scaled = [int(x/maxabsvalue*32767) for x in data]
173 169 nchan = 1
174 170 return scaled, nchan
@@ -19,6 +19,7 b' try:'
19 19 import pathlib
20 20 except ImportError:
21 21 pass
22 from unittest import mock
22 23
23 24 # Third-party imports
24 25 import nose.tools as nt
@@ -186,6 +187,17 b' def test_audio_from_file():'
186 187 def test_audio_from_numpy_array():
187 188 display.Audio(get_test_tone(), rate=44100)
188 189
190 def test_audio_from_list_without_numpy():
191 # Simulate numpy not installed.
192 with mock.patch('numpy.array', side_effect=ImportError):
193 display.Audio(list(get_test_tone()), rate=44100)
194
195 def test_audio_from_list_without_numpy_raises_for_nested_list():
196 # Simulate numpy not installed.
197 with mock.patch('numpy.array', side_effect=ImportError):
198 stereo_signal = [list(get_test_tone())] * 2
199 nt.assert_raises(TypeError, lambda: display.Audio(stereo_signal, rate=44100))
200
189 201 def test_audio_from_numpy_array_without_rate_raises():
190 202 nt.assert_raises(ValueError, display.Audio, get_test_tone())
191 203
General Comments 0
You need to be logged in to leave comments. Login now