From d3adc5658c66e3565f662002b08b6f15963df358 2013-10-02 23:13:09 From: David Österberg Date: 2013-10-02 23:13:09 Subject: [PATCH] Remove support for encoding audio without numpy for performance --- diff --git a/IPython/lib/display.py b/IPython/lib/display.py index cf08ca7..d140b43 100644 --- a/IPython/lib/display.py +++ b/IPython/lib/display.py @@ -96,22 +96,21 @@ class Audio(DisplayObject): self.mimetype = mimetypes.guess_type(self.url)[0] else: self.mimetype = "audio/wav" - - - def _make_wav(self,data,rate): + + def _make_wav(self, data, rate): """ Transform a numpy array to a PCM bytestring """ import struct from io import BytesIO import wave try: import numpy as np - if len(data.shape) > 1: - raise ValueError("encoding of stereo PCM signals are unsupported") data = np.array(data,dtype=float) - scaled = list(np.int16(data/np.max(np.abs(data))*32767)) + if len(data.shape) > 1: + raise ValueError("encoding of stereo PCM signals are unsupported") + scaled = np.int16(data/np.max(np.abs(data))*32767).tolist() except ImportError: - maxabsvalue = float(max(map(abs,data))) - scaled = map(lambda x: int(x/maxabsvalue*32767), data) + print("Numpy is required to encode PCM from an array") + raise fp = BytesIO() waveobj = wave.open(fp,mode='wb') waveobj.setnchannels(1) @@ -121,7 +120,7 @@ class Audio(DisplayObject): waveobj.writeframes(b''.join([struct.pack('