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