简体   繁体   English

从numpy数组中以HTML格式呈现音频

[英]Render audio in HTML from numpy array

To render image in HTML from numpy array, I can do 要从numpy数组中以HTML格式呈现图像,我可以这样做

def numpy_to_b64(array):
    im_pil = Image.fromarray(array)
    buff = BytesIO()
    im_pil.save(buff, format="png")
    im_b64 = base64.b64encode(buff.getvalue()).decode("utf-8")

    return im_b64

and use img tag 并使用img标签

array_b64 = numpy_to_b64(array)
html.Img(src='data:image/png;base64,{}'.format(array_b64))

I wonder if there is a similar approach for audio, or how can I convert a 1-D numpy array which represents audio waveform to a format that can be put in audio tag? 我想知道是否有类似的音频方法,或者我如何将表示音频波形的1-D numpy数组转换为可放入audio标签的格式?

Not raw waveform data, since the browser has no way of interpreting that (it wouldn't know the sampling rate, etc.). 不是原始波形数据,因为浏览器无法解释它(它不知道采样率等)。 You need to convert the audio data to eg OGG format, which is supported by HTML5. 您需要将音频数据转换为HTML格式支持的OGG格式。 A library like pydub might be the easiest way to accomplish that. pydub这样的库可能是实现这一目标的最简单方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM