Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio displayobject #3656

Closed
wants to merge 12 commits into from
105 changes: 105 additions & 0 deletions IPython/lib/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
"""
from os.path import exists, isfile, splitext, abspath, join, isdir
from os import walk, sep
import struct
import base64
import sys
from io import BytesIO
from IPython.core.display import DisplayObject
from IPython.utils.py3compat import string_types
import mimetypes


class IFrame(object):
Expand Down Expand Up @@ -341,3 +348,101 @@ def __repr__(self):
for dirname, subdirs, fnames in walked_dir:
result_lines += self.terminal_display_formatter(dirname, fnames, self.included_suffixes)
return '\n'.join(result_lines)

class Audio(DisplayObject):
def __init__(self, data=None, url=None, filename=None, rate=None, autoplay=False):
"""Create an audio player given raw PCM data.

When this object is returned by an expression or passed to the
display function, it will result in an audio player widget
displayed in the frontend.

Parameters
----------
data : unicode, str or bytes
The raw audio data.
url : unicode
A URL to download the data from.
filename : unicode
Path to a local file to load the data from.
rate : integer
The sampling rate of the raw data.
Only required when data parameter is being used
autoplay : bool
Set to True if the audio should immediately start playing.

Default is `False`.

Examples
--------
# embedded raw audio data
Audio(data=(2**13*numpy.sin(2*numpy.pi*440/44100*numpy.arange(44100))).astype(numpy.int16),
rate=44100)

# Specifying Audio(url=...) or Audio(filename=...) will load the data
# from an existing WAV file and embed it into the notebook.
Audio(filename='something.wav', autoplay=True)

"""
self.rate = rate
self.autoplay = autoplay
super(Audio, self).__init__(data=data, url=url, filename=filename)

def reload(self):
super(Audio, self).reload()

if self.filename is not None:
self.mimetype = mimetypes.guess_type(self.filename)[0]
elif self.url is not None:
self.mimetype = mimetypes.guess_type(self.url)[0]
else:
self.mimetype = "audio/wav"

buffer = BytesIO()
buffer.write(b'RIFF')
buffer.write(b'\x00\x00\x00\x00')
buffer.write(b'WAVE')

buffer.write(b'fmt ')
if self.data.ndim == 1:
noc = 1
else:
noc = self.data.shape[1]
bits = self.data.dtype.itemsize * 8
sbytes = self.rate*(bits // 8)*noc
ba = noc * (bits // 8)
buffer.write(struct.pack('<ihHIIHH', 16, 1, noc, self.rate, sbytes, ba, bits))

# data chunk
buffer.write(b'data')
buffer.write(struct.pack('<i', self.data.nbytes))

if self.data.dtype.byteorder == '>' or (self.data.dtype.byteorder == '=' and sys.byteorder == 'big'):
self.data = self.data.byteswap()

buffer.write(self.data.tostring())
size = buffer.tell()
buffer.seek(4)
buffer.write(struct.pack('<i', size-8))
self.data = buffer.getvalue()

def _repr_html_(self):
src = """
<audio controls="controls" {autoplay}>
<source controls src="{src}" type="{type}" />
Your browser does not support the audio element.
</audio>
""".format(src=self.src_attr(),type=self.mimetype, autoplay=self.autoplay_attr())
return src

def src_attr(self):
if(self.data is not None):
return """data:{type};base64,{base64}""".format(type=self.mimetype, base64=base64.encodestring(self.data))
else:
return ""

def autoplay_attr(self):
if(self.autoplay):
return 'autoplay="autoplay"'
else:
return ''