##// END OF EJS Templates
Remove support for encoding audio without numpy for performance
David Österberg -
Show More
@@ -96,22 +96,21 b' class Audio(DisplayObject):'
96 self.mimetype = mimetypes.guess_type(self.url)[0]
96 self.mimetype = mimetypes.guess_type(self.url)[0]
97 else:
97 else:
98 self.mimetype = "audio/wav"
98 self.mimetype = "audio/wav"
99
99
100
100 def _make_wav(self, data, rate):
101 def _make_wav(self,data,rate):
102 """ Transform a numpy array to a PCM bytestring """
101 """ Transform a numpy array to a PCM bytestring """
103 import struct
102 import struct
104 from io import BytesIO
103 from io import BytesIO
105 import wave
104 import wave
106 try:
105 try:
107 import numpy as np
106 import numpy as np
108 if len(data.shape) > 1:
109 raise ValueError("encoding of stereo PCM signals are unsupported")
110 data = np.array(data,dtype=float)
107 data = np.array(data,dtype=float)
111 scaled = list(np.int16(data/np.max(np.abs(data))*32767))
108 if len(data.shape) > 1:
109 raise ValueError("encoding of stereo PCM signals are unsupported")
110 scaled = np.int16(data/np.max(np.abs(data))*32767).tolist()
112 except ImportError:
111 except ImportError:
113 maxabsvalue = float(max(map(abs,data)))
112 print("Numpy is required to encode PCM from an array")
114 scaled = map(lambda x: int(x/maxabsvalue*32767), data)
113 raise
115 fp = BytesIO()
114 fp = BytesIO()
116 waveobj = wave.open(fp,mode='wb')
115 waveobj = wave.open(fp,mode='wb')
117 waveobj.setnchannels(1)
116 waveobj.setnchannels(1)
@@ -121,7 +120,7 b' class Audio(DisplayObject):'
121 waveobj.writeframes(b''.join([struct.pack('<h',x) for x in scaled]))
120 waveobj.writeframes(b''.join([struct.pack('<h',x) for x in scaled]))
122 val = fp.getvalue()
121 val = fp.getvalue()
123 waveobj.close()
122 waveobj.close()
124 return val
123 return val
125
124
126 def _data_and_metadata(self):
125 def _data_and_metadata(self):
127 """shortcut for returning metadata with url information, if defined"""
126 """shortcut for returning metadata with url information, if defined"""
General Comments 0
You need to be logged in to leave comments. Login now