Skip to content

Commit

Permalink
Updated GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
avnlearn committed Sep 13, 2024
1 parent c1bde9d commit 605a31d
Show file tree
Hide file tree
Showing 6 changed files with 1,101 additions and 38 deletions.
24 changes: 13 additions & 11 deletions manim_recorder/multimedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from pathlib import Path
import sox
import numpy as np
import noisereduce as nr

# import noisereduce as nr
import pyaudio
from pydub.utils import mediainfo
from mutagen.mp3 import MP3
Expand Down Expand Up @@ -110,6 +111,13 @@ def __setitem__(self, key, value):
def __iter__(self):
return iter(self.frames)

def __float__(self):
"""Return the total recording duration in seconds."""
return len(self) * self.chunk / self.rate if len(self) else 0.0

def __bool__(self):
return True if len(self) else False

def append(self, value):
self.frames.append(value)

Expand Down Expand Up @@ -210,7 +218,7 @@ def save_recording(self) -> None:

def play_playback(self) -> None:
"""Start playback of the recorded audio."""
if len(self):
if not len(self):
return False
self.is_playing = True
self.stop_playback_event.clear() # Clear the stop event
Expand Down Expand Up @@ -259,15 +267,9 @@ def close(self) -> None:
"""Terminate the PyAudio session."""
self.p.terminate()

def get_recording_duration(self) -> float:
"""Return the total recording duration in seconds."""
if len(self):
return 0.0
return len(self) * self.chunk / self.rate

def get_recording_format_duration(self) -> str:
"""Return the recording duration formatted as HH:MM:SS."""
struct_time = time.gmtime(self.get_recording_duration())
struct_time = time.gmtime(float(self))
return time.strftime("%H:%M:%S", struct_time)

def set_filepath(self, path: str) -> None:
Expand Down Expand Up @@ -371,8 +373,8 @@ def trim_silence(
return trimmed_sound


def suppress_noise(audio_data: np.ndarray, rate) -> np.ndarray:
return nr.reduce_noise(y=audio_data, sr=rate)
# def suppress_noise(audio_data: np.ndarray, rate) -> np.ndarray:
# return nr.reduce_noise(y=audio_data, sr=rate)


def normalize(audio_data: np.ndarray) -> np.ndarray:
Expand Down
38 changes: 16 additions & 22 deletions manim_recorder/recorder/gui/__gui__.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def initUI(self):
self.save_button = Create_Button(
icon=self.style().standardIcon(QStyle.SP_DialogSaveButton),
stylesheet="modern",
func=self.save_audio,
func=self._save,
disable=True,
toolTip="Save (<b>Ctrl + S</b>)",
)
Expand Down Expand Up @@ -208,15 +208,6 @@ def initUI(self):
# Additional Features Layout
feature_layout = QHBoxLayout()

self.audacity_button = Create_Button(
"Trim Silence",
lambda: Run_Audacity(self.recorder_service.__str__()),
stylesheet="modern",
disable=True,
toolTip="Audacity Run",
)
feature_layout.addWidget(self.audacity_button)

# Run Audacity Button
self.audacity_button = Create_Button(
"Run Audacity",
Expand Down Expand Up @@ -255,7 +246,7 @@ def initUI(self):
"s": self.__stop,
"p": self.__play,
"t": self.__pause,
"Ctrl+S": self.save_audio,
"Ctrl+S": self._save,
"a": self._next,
"Ctrl+Q": self.close,
},
Expand Down Expand Up @@ -291,12 +282,12 @@ def reset_timer(self):

def update_plot(self):
"""Updates the waveform plot with the latest audio data."""
if self.recorder_service.frames:
if self.recorder_service:
# Convert the last chunk of frames to numpy array
data = None

if self.recorder_service.is_recording:
data = np.frombuffer(self.recorder_service.frames[-1], dtype=np.int16)
data = np.frombuffer(self.recorder_service[-1], dtype=np.int16)
# time = np.linspace(
# self.recorder_service.get_recording_duration() - 1,
# self.recorder_service.get_recording_duration(),
Expand All @@ -311,7 +302,7 @@ def update_plot(self):
< len(self.recorder_service)
):
data = np.frombuffer(
self.recorder_service.frames[
self.recorder_service[
self.recorder_service.current_playback_frame_index
],
dtype=np.int16,
Expand All @@ -328,7 +319,7 @@ def update_progress_bar(self):
# current_duration = (
# self.recorder_service.get_recording_duration()
# ) # Total duration in seconds
total_frames = len(self.recorder_service.frames) # Total frames recorded
total_frames = len(self.recorder_service) # Total frames recorded
if total_frames > 0:
current_frame = (
self.recorder_service.current_playback_frame_index
Expand Down Expand Up @@ -387,18 +378,21 @@ def __stop(self):
self.play_button.setDisabled(False)
self.rec_button.setDisabled(False)
self.save_button.setDisabled(False)
self.audacity_button.setDisabled(True)
self.accept_button.setDisabled(False)

def __rec(self):
"""Starts audio recording."""
if not self.recorder_service.is_recording:
self.status_bar.showMessage("Status : Recording ...")
self.rec_button.setDisabled(True)
self.play_button.setDisabled(True)
self.save_button.setDisabled(True)

self.pause_button.setDisabled(False)
self.play_button.setDisabled(True)
self.stop_button.setDisabled(False)

self.rec_button.setDisabled(True)
self.save_button.setDisabled(True)
self.audacity_button.setDisabled(True)
self.accept_button.setDisabled(True)
self.recorder_service.set_device_index(self.device_index)
self.recorder_service.set_channels(self.device_index)
self.recorder_service.start_recording()
Expand All @@ -411,7 +405,7 @@ def _next(self):
show_message("Confirmation", "Do you want to save the audio file?")
== QMessageBox.Yes
):
self.save_audio()
self._save()
self.pause_button.setDisabled(True)
self.play_button.setDisabled(True)
self.stop_button.setDisabled(True)
Expand All @@ -424,13 +418,13 @@ def _next(self):
if self.communicator:
self.communicator.accept.emit(self.recorder_service.__str__())

def save_audio(self):
def _save(self):
"""Saves the recorded audio to a file."""
if self.recorder_service.is_recording:
self.recorder_service.stop_recording()
if self.recorder_service.is_playing:
self.recorder_service.stop_playback()
if self.recorder_service.frames:
if self.recorder_service:
self.recorder_service.save_recording()
self.File_Saved = True
self.audacity_button.setDisabled(False)
Expand Down
8 changes: 5 additions & 3 deletions manim_recorder/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ def __init__(self, scene: Scene, data: dict, cache_dir: str, voice_id: int):
self.data = data
self.cache_dir = cache_dir
self.voice_id = voice_id
audio_file_path = Path(cache_dir) / self.data["final_audio"]

self.duration = 1
if audio_file_path.exists():
self.duration = get_duration(Path(cache_dir) / self.data["final_audio"])
if self.data.get("final_audio"):
audio_file_path = Path(cache_dir) / self.data.get("final_audio")
if audio_file_path.exists():
self.duration = get_duration(audio_file_path)
last_t = scene.renderer.time
if last_t is None:
last_t = 0
Expand Down
17 changes: 17 additions & 0 deletions manim_recorder/voiceover_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ class RecorderScene(Scene):
create_subcaption: bool
create_script: bool
voice_id: int = -1
skip: bool

def set_audio_service(
self,
audio_service: AudioService,
create_subcaption: bool | None = True,
cache_dir_delete: bool = False,
skip: bool = False,
) -> None:
"""Sets the Audio service to be used for the sound. This method
should be called before adding any sound to the scene.
Expand All @@ -47,6 +49,7 @@ def set_audio_service(
self.audio_service = audio_service
self.current_tracker = None
self.create_subcaption = create_subcaption
self.skip = skip

def add_voiceover_text(
self,
Expand All @@ -71,6 +74,20 @@ def add_voiceover_text(
if not hasattr(self, "audio_service"):
raise Exception("You need to call init_sound() before adding a sound.")

if self.skip:
return SoundTracker(
self,
{
"input_data": {
"id": self.voice_id,
"input_text": text,
"MObject": kwargs.get("mobject"),
}
},
self.audio_service.cache_dir,
self.voice_id,
)

dict_ = self.audio_service._wrap_generate_from_text(
text=text, voice_id=self.voice_id, **kwargs
)
Expand Down
Loading

0 comments on commit 605a31d

Please sign in to comment.