<s> import os <EOL> import struct <EOL> from io import BytesIO <EOL> import base64 <EOL> import sys <EOL> from IPython . utils . py3compat import string_types <EOL> from IPython . core . display import DisplayObject <EOL> class Audio ( DisplayObject ) : <EOL> def __init__ ( self , data = None , url = None , filename = None , rate = None , autoplay = False ) : <EOL> """<STR_LIT>""" <EOL> super ( Audio , self ) . __init__ ( data , url , filename ) <EOL> if data is not None and not isinstance ( data , string_types ) : <EOL> buffer = BytesIO ( ) <EOL> buffer . write ( b'<STR_LIT>' ) <EOL> buffer . write ( b'<STR_LIT>' ) <EOL> buffer . write ( b'<STR_LIT>' ) <EOL> buffer . write ( b'<STR_LIT>' ) <EOL> if data . ndim == <NUM_LIT:1> : <EOL> noc = <NUM_LIT:1> <EOL> else : <EOL> noc = data . shape [ <NUM_LIT:1> ] <EOL> bits = data . dtype . itemsize * <NUM_LIT:8> <EOL> sbytes = rate * ( bits // <NUM_LIT:8> ) * noc <EOL> ba = noc * ( bits // <NUM_LIT:8> ) <EOL> buffer . write ( struct . pack ( '<STR_LIT>' , <NUM_LIT:16> , <NUM_LIT:1> , noc , rate , sbytes , ba , bits ) ) <EOL> buffer . write ( b'<STR_LIT:data>' ) <EOL> buffer . write ( struct . pack ( '<STR_LIT>' , data . nbytes ) ) <EOL> if data . dtype . byteorder == '<STR_LIT:>>' or ( data . dtype . byteorder == '<STR_LIT:=>' and sys . byteorder == '<STR_LIT>' ) : <EOL> data = data . byteswap ( ) <EOL> buffer . write ( data . tostring ( ) ) <EOL> size = buffer . tell ( ) <EOL> buffer . seek ( <NUM_LIT:4> ) <EOL> buffer . write ( struct . pack ( '<STR_LIT>' , size - <NUM_LIT:8> ) ) <EOL> self . data = buffer . getvalue ( ) <EOL> self . autoplay = autoplay <EOL> def _repr_html_ ( self ) : <EOL> src = """<STR_LIT>""" . format ( src = self . src_attr ( ) , autoplay = self . autoplay_attr ( ) ) <EOL> return src <EOL> def src_attr ( self ) : <EOL> if ( self . data is not None ) : <EOL> return """<STR_LIT>""" . format ( base64 = base64 . encodestring ( self . data ) ) <EOL> else : <EOL> return "<STR_LIT>" <EOL> def autoplay_attr ( self ) : <EOL> if ( self . autoplay ) : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' </s>
