-
Notifications
You must be signed in to change notification settings - Fork 2
/
vscode_audio.py
39 lines (34 loc) · 1.34 KB
/
vscode_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
import IPython.display
import numpy as np
import json
def Audio(audio: np.ndarray, sr: int):
"""
Use instead of IPython.display.Audio as a workaround for VS Code.
`audio` is an array with shape (channels, samples) or just (samples,) for mono.
"""
if np.ndim(audio) == 1:
channels = [audio.tolist()]
else:
channels = audio.tolist()
return IPython.display.HTML("""
<script>
function stopAudio() {
if (window.audioContext)
window.audioContext.close();
}
window.playAudio = function(audioChannels, sr) {
stopAudio()
window.audioContext = new AudioContext();
const buffer = audioContext.createBuffer(audioChannels.length, audioChannels[0].length, sr);
for (let [channel, data] of audioChannels.entries()) {
buffer.copyToChannel(Float32Array.from(data), channel);
}
const source = audioContext.createBufferSource();
source.buffer = buffer;
source.connect(audioContext.destination);
source.start();
}
</script>
<button onclick="playAudio(%s, %s)">Play</button>
<button onclick="stopAudio()">Stop</button>
""" % (json.dumps(channels), sr))