[snippet]在Jupyter (IPython Notebook)中插入播放器以播放远程音频

做音频处理需要听一听合成出来的语音,在Juypter(以前叫IPython Notebook)中可以通过内嵌一个音频播放器的方式将音频文件直接在浏览器中播放出来,不再需要每次用scp下载到本地了。


import scipy
import scipy.constants as const
from scipy.io import wavfile
from IPython.core.display import HTML
from __future__ import division
def wavPlayer(filepath):
    """ will display html 5 player for compatible browser

    Parameters :
    filepath : relative filepath with respect to the notebook directory ( where the .ipynb are not cwd)
               of the file to play

    The browser need to know how to play wav through html5.

    there is no autoplay to prevent file playing when the browser opens
    src = """
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Simple Test</title>
    <audio controls="controls" style="width:600px" >
      <source src="files/%s" type="audio/wav" />
      Your browser does not support the audio element.

# 用法如下:
wavPlayer("test.wav") # test.wav在同目录

来源:The sound Of Hydrogen