Created
May 5, 2020 01:10
-
-
Save dmyersturnbull/e613b25465293d3cbdbc7635e6450a24 to your computer and use it in GitHub Desktop.
Audio container in Jupyter notebook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Union | |
from pathlib import PurePath | |
def listen(path: Union[str, PurePath, bytes]): | |
""" | |
Returns an audio container that Jupyter notebook will display. | |
Must be run from a Jupyter notebook. | |
Will raise an ImportError if IPython cannot be imported. | |
:param path: The local path to the audio file | |
:return: A jupyter notebook ipd.Audio object | |
""" | |
# noinspection PyPackageRequirements | |
import IPython.display as ipd | |
if isinstance(path, (str, PurePath)): | |
path = str(Path(path)) | |
return ipd.Audio(filename=path) | |
else: | |
return ipd.Audio(data=path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment