##// END OF EJS Templates
Speedup _make_wav...
Jonas Haag -
Show More
@@ -130,7 +130,6 b' class Audio(DisplayObject):'
130 130 @staticmethod
131 131 def _make_wav(data, rate, normalize):
132 132 """ Transform a numpy array to a PCM bytestring """
133 import struct
134 133 from io import BytesIO
135 134 import wave
136 135
@@ -145,7 +144,7 b' class Audio(DisplayObject):'
145 144 waveobj.setframerate(rate)
146 145 waveobj.setsampwidth(2)
147 146 waveobj.setcomptype('NONE','NONE')
148 waveobj.writeframes(b''.join([struct.pack('<h',x) for x in scaled]))
147 waveobj.writeframes(scaled)
149 148 val = fp.getvalue()
150 149 waveobj.close()
151 150
@@ -170,12 +169,17 b' class Audio(DisplayObject):'
170 169
171 170 max_abs_value = np.max(np.abs(data))
172 171 normalization_factor = Audio._get_normalization_factor(max_abs_value, normalize)
173 scaled = np.int16(data / normalization_factor * 32767).tolist()
174 return scaled, nchan
172 scaled = data / normalization_factor * 32767
173 return scaled.astype('<h').tostring(), nchan
175 174
176 175
177 176 @staticmethod
178 177 def _validate_and_normalize_without_numpy(data, normalize):
178 import array
179 import sys
180
181 data = array.array('f', data)
182
179 183 try:
180 184 max_abs_value = float(max([abs(x) for x in data]))
181 185 except TypeError:
@@ -183,9 +187,11 b' class Audio(DisplayObject):'
183 187 'supported if numpy is not installed')
184 188
185 189 normalization_factor = Audio._get_normalization_factor(max_abs_value, normalize)
186 scaled = [int(x / normalization_factor * 32767) for x in data]
190 scaled = array.array('h', [int(x / normalization_factor * 32767) for x in data])
191 if sys.byteorder == 'big':
192 scaled.byteswap()
187 193 nchan = 1
188 return scaled, nchan
194 return scaled.tobytes(), nchan
189 195
190 196 @staticmethod
191 197 def _get_normalization_factor(max_abs_value, normalize):
General Comments 0
You need to be logged in to leave comments. Login now