From 205c280a2c51f3b42416137076e48f420cc4cd50 2013-10-02 20:18:43 From: David Österberg Date: 2013-10-02 20:18:43 Subject: [PATCH] Speed up normalization if numpy is avaliable --- diff --git a/IPython/lib/display.py b/IPython/lib/display.py index 1b36aa4..a337743 100644 --- a/IPython/lib/display.py +++ b/IPython/lib/display.py @@ -103,8 +103,13 @@ class Audio(DisplayObject): import struct from io import BytesIO import wave - maxabsvalue = float(max(map(abs,data))) - scaled = map(lambda x: int(x/maxabsvalue*32767), data) + try: + import numpy as np + data = np.array(data,dtype=float) + scaled = list(np.int16(data/np.max(np.abs(data))*32767)) + except ImportError: + maxabsvalue = float(max(map(abs,data))) + scaled = map(lambda x: int(x/maxabsvalue*32767), data) fp = BytesIO() waveobj = wave.open(fp,mode='wb') waveobj.setnchannels(1)