Skip to content

Commit

Permalink
Update tests & improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
pozitronik committed Feb 26, 2024
1 parent 082edc7 commit a517f1b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
1 change: 0 additions & 1 deletion sinner/models/audio/BaseAudioBackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from sinner.Status import Status
from sinner.utilities import normalize_path
from sinner.validators.AttributeLoader import Rules


class BaseAudioBackend(Status, ABC):
Expand Down
10 changes: 8 additions & 2 deletions sinner/models/audio/PygameAudioBackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ class PygameAudioBackend(BaseAudioBackend):

def __init__(self, parameters: Namespace, media_path: str | None = None) -> None:
self._temp_dir = os.path.abspath(os.path.join(os.path.normpath(vars(parameters).get('temp_dir', tempfile.gettempdir())), 'extracted_audio'))
os.makedirs(self._temp_dir, exist_ok=True)
super().__init__(parameters, media_path)
pygame.mixer.init()


@property
def media_path(self) -> str | None:
return self._media_path
Expand All @@ -30,9 +32,13 @@ def media_path(self, media_path: str) -> None:
self._media_path = str(normalize_path(media_path))
self.update_status(f"Using audio backend for {self._media_path}")
self._clip = AudioFileClip(self.media_path)
self._audio_path = os.path.join(self._temp_dir, get_file_name(self.media_path), '.wav')
self._audio_path = os.path.join(self._temp_dir, get_file_name(self.media_path) + '.wav')
if not os.path.exists(self._audio_path):
self._clip.write_audiofile(self._audio_path, codec='pcm_s16le')
try:
self._clip.write_audiofile(self._audio_path, codec='pcm_s32le')
except Exception as exception:
self.update_status(message=f"Unable to save the temp audio. Possible reasons: no audio in the media/no access rights/no space on device. \n {str(exception)}", mood=Mood.BAD)
return
try:
pygame.mixer.music.load(self._audio_path)
self._media_loaded = True
Expand Down
30 changes: 26 additions & 4 deletions tests/audio/test_pygame_audio_backend.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import os.path
import shutil
import tempfile
from argparse import Namespace
from tests.constants import tmp_dir, target_mp4

import pytest

from tests.constants import tmp_dir, target_mp4, silent_target_mp4
from sinner.Parameters import Parameters
from sinner.models.audio.PygameAudioBackend import PygameAudioBackend


def setup():
# clean previous results, if exists
if os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)


def setup_function():
setup()


def test_init_default() -> None:
params: Namespace = Parameters().parameters
backend = PygameAudioBackend(params)
Expand All @@ -14,8 +28,16 @@ def test_init_default() -> None:


def test_init_parameters() -> None:
params: Namespace = Parameters(f'--temp_dir={tmp_dir}').parameters
params: Namespace = Parameters(f'--temp_dir="{tmp_dir}"').parameters
backend = PygameAudioBackend(params, target_mp4)
assert backend._temp_dir == tmp_dir
assert backend.media_path is os.path.join(tmp_dir, 'extracted_audio', 'target.wav')
assert backend._temp_dir == os.path.join(tmp_dir, 'extracted_audio')
assert backend._audio_path == os.path.join(tmp_dir, 'extracted_audio', 'target.wav')
assert os.path.exists(backend.media_path)


def test_on_silent(capsys) -> None:

params: Namespace = Parameters(f'--temp_dir="{tmp_dir}"').parameters
PygameAudioBackend(params, silent_target_mp4)
captured: str = capsys.readouterr()
assert "Unable to save the temp audio" in captured.out

0 comments on commit a517f1b

Please sign in to comment.