LoginSignup
3
3

More than 5 years have passed since last update.

Jupyter で wav ファイルを埋め込む

Posted at

問題

以下のブログなどにあるように、 Jupyter で動画を埋め込むことができる。
http://jakevdp.github.io/blog/2013/05/12/embedding-matplotlib-animations/

同じように音のファイルも埋め込んで、 Jupyter から再生したい。

解決方法

HTML5 Audio 要素を使って、上記ブログと同じように埋め込む。

こんな風に関数を定義して、

from IPython.display import HTML
def embedAudio(filename):
    tmpl = """
    <audio controls>
    <source src="data:audio/x-m4v;base64,{0}" type="audio/wav">
    """

    return HTML(tmpl.format(open(filename, "rb").read().encode("base64")))

こんな感じで実行すると埋め込める。

embedAudio("myaudio.wav")

ただし、ひとつの Cell に一つだけ。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3