-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml5_audio.py
66 lines (49 loc) · 1.58 KB
/
html5_audio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Adapted from
# http://nbviewer.ipython.org/github/Carreau/posts/blob/master/07-the-sound-of-hydrogen.ipynb
import struct
from io import BytesIO
import sys
import base64
import numpy as np
DEFAULT_RATE = 44100
def get_html5_wave_player(data, rate=DEFAULT_RATE):
bio = BytesIO()
bio.write(b'RIFF')
bio.write(b'\x00\x00\x00\x00')
bio.write(b'WAVE')
# clip
data = np.minimum(1, data)
data = np.maximum(-1, data)
data = data * np.iinfo(np.int16).max
data = data.astype(np.int16)
bio.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)
bio.write(struct.pack('<ihHIIHH', 16, 1, noc, rate, sbytes, ba, bits))
# data chunk
bio.write(b'data')
bio.write(struct.pack('<i', data.nbytes))
if (data.dtype.byteorder == '>'
or (data.dtype.byteorder == '=' and sys.byteorder == 'big')):
data = data.byteswap()
bio.write(data.tostring())
# Determine file size and place it at the correct position at start of the
# file.
size = bio.tell()
bio.seek(4)
bio.write(struct.pack('<i', size-8))
val = bio.getvalue()
src = """
<audio controls="controls" style="width:600px" >
<source
controls src="data:audio/wav;base64,{base64}" type="audio/wav" />
Your browser does not support the audio element.
</audio>
""".format(base64=base64.encodebytes(val).decode())
from IPython.core.display import HTML
return HTML(src)