Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Added sounds example to docs (#1188)

kolibril13cdd0dded6ad3
diff --git a/docs/source/_static/click.wav b/docs/source/_static/click.wav
new file mode 100644
index 000000000..6463e758a
Binary files /dev/null and b/docs/source/_static/click.wav differ
diff --git a/manim/scene/scene.py b/manim/scene/scene.py
index aa1615f7b..7cd40073a 100644
--- a/manim/scene/scene.py
+++ b/manim/scene/scene.py
@@ -983,15 +983,34 @@ class Scene(Container):
Parameters
----------
+
sound_file : str
The path to the sound file.
-
time_offset : int,float, optional
The offset in the sound file after which
the sound can be played.
+ gain : float
+ Amplification of the sound.
- gain :
+ Examples
+ --------
+ .. manim:: SoundExample
+ class SoundExample(Scene):
+ # Source of sound under Creative Commons 0 License. https://freesound.org/people/Druminfected/sounds/250551/
+ def construct(self):
+ dot = Dot().set_color(GREEN)
+ self.add_sound("click.wav")
+ self.add(dot)
+ self.wait()
+ self.add_sound("click.wav")
+ dot.set_color(BLUE)
+ self.wait()
+ self.add_sound("click.wav")
+ dot.set_color(RED)
+ self.wait()
+
+ Download the resource for the previous example `here <https://github.com/ManimCommunity/manim/blob/master/docs/source/_static/click.wav>`_ .
"""
if self.renderer.skip_animations:
return
diff --git a/manim/scene/scene_file_writer.py b/manim/scene/scene_file_writer.py
index 04363433a..d5ea930b7 100644
--- a/manim/scene/scene_file_writer.py
+++ b/manim/scene/scene_file_writer.py
@@ -489,16 +489,15 @@ class SceneFileWriter(object):
combine_process.wait()
if self.includes_sound:
- sound_file_path = movie_file_path.replace(
- config["movie_file_extension"], ".wav"
- )
+ extension = config["movie_file_extension"]
+ sound_file_path = movie_file_path.replace(extension, ".wav")
# Makes sure sound file length will match video file
self.add_audio_segment(AudioSegment.silent(0))
self.audio_segment.export(
sound_file_path,
bitrate="312k",
)
- temp_file_path = movie_file_path.replace(".", "_temp.")
+ temp_file_path = movie_file_path.replace(extension, f"_temp{extension}")
commands = [
FFMPEG_BIN,
"-i",
diff --git a/manim/utils/sounds.py b/manim/utils/sounds.py
index f63c32a26..e1c4a3554 100644
--- a/manim/utils/sounds.py
+++ b/manim/utils/sounds.py
@@ -5,7 +5,7 @@ __all__ = [
]
-from pathlib import Path
+from .. import config
from ..utils.file_ops import seek_full_path_from_defaults
@@ -13,6 +13,6 @@ from ..utils.file_ops import seek_full_path_from_defaults
def get_full_sound_file_path(sound_file_name):
return seek_full_path_from_defaults(
sound_file_name,
- default_dir=Path("assets") / "sounds",
+ default_dir=config.get_dir("assets_dir"),
extensions=[".wav", ".mp3"],
)