Skip to content

Commit

Permalink
Updated GUI_002: Interface
Browse files Browse the repository at this point in the history
  • Loading branch information
avnlearn committed Sep 6, 2024
1 parent d5a83e9 commit 15aa8ee
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 17 deletions.
8 changes: 7 additions & 1 deletion example/gui-example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ def construct(self):
circle = Circle()
square = Square().shift(2 * RIGHT)
math = MathTex(r"x = \dfrac{-b \pm \sqrt{b^2 - 4ac}}{2a}")
with self.voiceover(text="Say", mobject_path=math.get_file_path()) as tracker:
print(isinstance(math, Mobject))
with self.voiceover(mobject=math.get_file_path()) as tracker:
self.play(Write(math), run_time=tracker.duration)

self.play(Unwrite(math))

with self.voiceover(mobject_path=math.get_file_path()) as tracker:
self.play(Write(math), run_time=tracker.duration)

self.play(Unwrite(math))
Expand Down
1 change: 1 addition & 0 deletions manim_recorder/recorder/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def recording_cache_dir(self, cache_dir: Path, cache_dir_delete: bool = False):

def _wrap_generate_from_text(self, text: str, path: str = None, **kwargs) -> dict:
# Replace newlines with lines, reduce multiple consecutive spaces to single

text = " ".join(text.split())
dict_ = self.generate_from_text(
text, cache_dir=None, path=path, **kwargs)
Expand Down
10 changes: 5 additions & 5 deletions manim_recorder/recorder/gui/__gui__.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,15 @@ def resetUI(self):
# self.message_object.load(self.DEFAULT_SVG)
# self.setSvg_Size(self.DEFAULT_SVG)

def record(self, path: str = None, msg: str = None, voice_id: int = None, mobject_path: any = None, **kwargs):
def record(self, path: str = None, msg: str = None, voice_id: int = None, svg_path: any = None, **kwargs):
self.resetUI()
if path is not None:
self.recorder_service.set_filepath(path)
if mobject_path is not None:
mobject_path = str(mobject_path)
if svg_path is not None:
svg_path = str(svg_path)
self.message_object.setStyleSheet("background-color: white; padding: 20px;")
self.message_object.load(mobject_path)
self.setSvg_Size(mobject_path)
self.message_object.load(svg_path)
self.setSvg_Size(svg_path)

if msg is not None:
self.massage_label.setText(msg)
Expand Down
13 changes: 8 additions & 5 deletions manim_recorder/recorder/gui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,27 @@ def __init__(self, **kwargs,):
AudioService.__init__(self, **kwargs)

def generate_from_text(
self, text: str, cache_dir: str = None, path: str = None, voice_id: int = None, **kwargs
self, text: str, cache_dir: str = None, path: str = None, voice_id: int = None, svg_path: str | None = None, **kwargs
) -> dict:
""""""

if cache_dir is None:
cache_dir = self.cache_dir

input_data = {
'id': voice_id,
"input_text": text,
"MObject": str(svg_path)
}
cached_result = self.get_cached_result(input_data, cache_dir, voice_id=voice_id, **kwargs)

cached_result = self.get_cached_result(
input_data, cache_dir, voice_id=voice_id, **kwargs)

if cached_result is not None:
return cached_result

audio_path = self.get_audio_basename() + ".wav" if path is None else path
self.recorder.record(str(Path(cache_dir) / audio_path), text, voice_id, **kwargs)
self.recorder.record(
str(Path(cache_dir) / audio_path), text, voice_id, svg_path, **kwargs)
self.app.exec()
json_dict = {
"input_data": input_data,
Expand Down
25 changes: 20 additions & 5 deletions manim_recorder/voiceover_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from math import ceil
from contextlib import contextmanager
from typing import Optional, Generator
from manim import Scene, config
from manim import Scene, config, Mobject
from manim_recorder.recorder.base import AudioService
from manim_recorder.tracker import SoundTracker
from manim_recorder.multimedia import chunks
Expand Down Expand Up @@ -156,22 +156,37 @@ def safe_wait(self, duration: float) -> None:

@contextmanager
def voiceover(
self, text: str = None, **kwargs
self, text: str = None, mobject: Mobject | None = None, **kwargs
) -> Generator[SoundTracker, None, None]:
"""The main function to be used for adding sound to a scene.
Args:
text (str, optional): The text to be spoken. Defaults to None.
mobject (str, optional) : The Mobject to be spoken. Default to None
Yields:
Generator[SoundTracker, None, None]: The sound tracker object.
"""
if text is None:
if text is None and Mobject is None:
raise ValueError(
"Please specify either a sound text string.")
"Please specify either a sound text string and mobject path.")

match text:
case Mobject() if hasattr(mobject, "get_file_path") and mobject is None:
mobject = text.get_file_path()
case str() if os.path.exists(text) and mobject is None:
mobject = text
case None:
text = ""

match mobject:
case Mobject() if hasattr(mobject, "get_file_path"):
mobject = mobject.get_file_path()
case str() if os.path.exists(mobject):
pass

try:
# Increment voice_id after adding a new sound
self.voice_id += 1
yield self.add_voiceover_text(text, **kwargs)
yield self.add_voiceover_text(text, svg_path=mobject, **kwargs)
finally:
self.wait_for_voiceover()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "manim-recorder"
version = "0.2.1"
version = "0.2.2"
description = "Manim plugin for recorder"
authors = ["AvN Learn <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit 15aa8ee

Please sign in to comment.