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