##// END OF EJS Templates
Move imports into methods and fix docstring typo
David Österberg -
Show More
@@ -4,11 +4,6 b' Authors : MinRK, gregcaporaso, dannystaple'
4 """
4 """
5 from os.path import exists, isfile, splitext, abspath, join, isdir
5 from os.path import exists, isfile, splitext, abspath, join, isdir
6 from os import walk, sep
6 from os import walk, sep
7 import base64
8 import struct
9 from io import BytesIO
10 import wave
11 import mimetypes
12
7
13 from IPython.core.display import DisplayObject
8 from IPython.core.display import DisplayObject
14
9
@@ -25,7 +20,7 b' class Audio(DisplayObject):'
25 data : numpy array, unicode, str or bytes
20 data : numpy array, unicode, str or bytes
26 Can be a
21 Can be a
27 * Numpy array containing the desired waveform,
22 * Numpy array containing the desired waveform,
28 * String containingvthe filename
23 * String containing the filename
29 * Bytestring containing raw PCM data or
24 * Bytestring containing raw PCM data or
30 * URL pointing to a file on the web.
25 * URL pointing to a file on the web.
31
26
@@ -91,6 +86,7 b' class Audio(DisplayObject):'
91
86
92 def reload(self):
87 def reload(self):
93 """Reload the raw data from file or URL."""
88 """Reload the raw data from file or URL."""
89 import mimetypes
94 if self.embed:
90 if self.embed:
95 super(Audio, self).reload()
91 super(Audio, self).reload()
96
92
@@ -104,6 +100,9 b' class Audio(DisplayObject):'
104
100
105 def _make_wav(self,data,rate):
101 def _make_wav(self,data,rate):
106 """ Transform a numpy array to a PCM bytestring """
102 """ Transform a numpy array to a PCM bytestring """
103 import struct
104 from io import BytesIO
105 import wave
107 maxabsvalue = max(map(abs,data))
106 maxabsvalue = max(map(abs,data))
108 scaled = map(lambda x: int(x/maxabsvalue*32767), data)
107 scaled = map(lambda x: int(x/maxabsvalue*32767), data)
109 fp = BytesIO()
108 fp = BytesIO()
@@ -137,6 +136,7 b' class Audio(DisplayObject):'
137 return src.format(src=self.src_attr(),type=self.mimetype, autoplay=self.autoplay_attr())
136 return src.format(src=self.src_attr(),type=self.mimetype, autoplay=self.autoplay_attr())
138
137
139 def src_attr(self):
138 def src_attr(self):
139 import base64
140 if self.embed and (self.data is not None):
140 if self.embed and (self.data is not None):
141 return """data:{type};base64,{base64}""".format(type=self.mimetype, base64=base64.encodestring(self.data))
141 return """data:{type};base64,{base64}""".format(type=self.mimetype, base64=base64.encodestring(self.data))
142 elif self.url is not None:
142 elif self.url is not None:
General Comments 0
You need to be logged in to leave comments. Login now