Skip to content

Instantly share code, notes, and snippets.

@nils-werner
Created July 12, 2013 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nils-werner/5988269 to your computer and use it in GitHub Desktop.
Save nils-werner/5988269 to your computer and use it in GitHub Desktop.
IPython Notebook Audio Viewer
from IPython.core.display import DisplayObject
import StringIO
import base64
import struct
import sys
class Audio(DisplayObject):
def __init__(self, data=None, rate=None, url=None, filename=None, autoplay=False):
if(data is not None):
buffer = StringIO.StringIO()
buffer.write(b'RIFF')
buffer.write(b'\x00\x00\x00\x00')
buffer.write(b'WAVE')
buffer.write(b'fmt ')
if data.ndim == 1:
noc = 1
else:
noc = data.shape[1]
bits = data.dtype.itemsize * 8
sbytes = rate*(bits // 8)*noc
ba = noc * (bits // 8)
buffer.write(struct.pack('<ihHIIHH', 16, 1, noc, rate, sbytes, ba, bits))
# data chunk
buffer.write(b'data')
buffer.write(struct.pack('<i', data.nbytes))
if data.dtype.byteorder == '>' or (data.dtype.byteorder == '=' and sys.byteorder == 'big'):
data = data.byteswap()
buffer.write(data.tostring())
size = buffer.tell()
buffer.seek(4)
buffer.write(struct.pack('<i', size-8))
self.data = buffer.getvalue()
else:
self.data = None
self.autoplay = autoplay
self.url = None if url is None else unicode(url)
self.filename = None if filename is None else unicode(filename)
def _repr_html_(self):
src = """
<audio controls="controls" style="width:600px" {autoplay}>
<source controls src="{src}" type="audio/wav" />
Your browser does not support the audio element.
</audio>
""".format(src=self.src_attr(),autoplay=self.autoplay_attr())
return src
def src_attr(self):
if(self.data is not None):
return """data:audio/wav;base64,{base64}""".format(base64=base64.encodestring(self.data))
elif(self.url is not None):
return self.url
elif(self.filename is not None):
return 'files/' + self.filename
def autoplay_attr(self):
if(self.autoplay):
return 'autoplay="autoplay"'
else:
return ''
@nils-werner
Copy link
Author

Try this using

import Audio
import numpy

data=2**13*numpy.sin(2*pi*440/44100*numpy.arange(44100))

Audio.Audio(data=data.astype(numpy.int16), rate=44100, autoplay=True)

or, if you already have a WAV file

import Audio

Audio.Audio(filename='something.wav', autoplay=True)

@nils-werner
Copy link
Author

I have also posted a pullrequest to scipy that will integrate most of the (taken from scipy, but slightly modified) code in this class into the upstream codebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment