3b1b-manim/manimlib/utils/sounds.py

41 lines
1.1 KiB
Python
Raw Normal View History

from __future__ import annotations
import subprocess
import threading
import platform
2021-01-23 11:02:02 -08:00
from manimlib.utils.directories import get_sound_dir
2022-04-12 19:19:59 +08:00
from manimlib.utils.file_ops import find_file
def get_full_sound_file_path(sound_file_name: str) -> str:
return find_file(
2019-01-24 22:24:01 -08:00
sound_file_name,
2021-01-23 11:02:02 -08:00
directories=[get_sound_dir()],
extensions=[".wav", ".mp3", ""]
2019-01-24 22:24:01 -08:00
)
def play_sound(sound_file):
"""Play a sound file using the system's audio player"""
full_path = get_full_sound_file_path(sound_file)
system = platform.system()
if system == "Windows":
# Windows
subprocess.Popen(
["powershell", "-c", f"(New-Object Media.SoundPlayer '{full_path}').PlaySync()"],
shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
elif system == "Darwin":
# macOS
subprocess.Popen(
["afplay", full_path],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
else:
subprocess.Popen(
["aplay", full_path],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)