Skip to content

Commit

Permalink
Fix pre-commit warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
AliOsm committed Jun 26, 2024
1 parent 04e4162 commit 0120136
Show file tree
Hide file tree
Showing 19 changed files with 1,054 additions and 1,042 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/formatter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: 3.9
- name: black formatter
uses: rickstaa/action-black@v1
with:
black_args: ". --check --diff --skip-string-normalization --line-length 120"
python-version: 3.11
- name: isort formatter
uses: isort/isort-action@v1
with:
configuration: "--profile black --src tafrigh --line-length 120 --lines-between-types 1 --lines-after-imports 2 --case-sensitive --trailing-comma --check-only --diff"
configuration: "--src src --line-length 120 --lines-between-types 1 --lines-after-imports 2 --check-only --diff"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ homepage = "https://tafrigh.ieasybooks.com"
repository = "https://github.com/ieasybooks/tafrigh"

[tool.poetry.dependencies]
python = ">=3.10,<3.12"
python = "3.11"
tqdm = ">=4.66.4"
yt-dlp = ">=2024.4.9"
auditok = {version = ">=0.2.0", extras = ["wit"]}
Expand Down
26 changes: 13 additions & 13 deletions src/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
__all__ = [
"farrigh",
"Config",
"Downloader",
"TranscriptType",
"Writer",
"WhisperRecognizer",
"AudioSplitter",
"WitRecognizer",
'farrigh',
'Config',
'Downloader',
'TranscriptType',
'Writer',
'WhisperRecognizer',
'AudioSplitter',
'WitRecognizer',
]


Expand All @@ -18,12 +18,12 @@


try:
from .recognizers.whisper_recognizer import WhisperRecognizer
from .recognizers.whisper_recognizer import WhisperRecognizer
except ModuleNotFoundError:
pass
pass

try:
from .audio_splitter import AudioSplitter
from .recognizers.wit_recognizer import WitRecognizer
from .audio_splitter import AudioSplitter
from .recognizers.wit_recognizer import WitRecognizer
except ModuleNotFoundError:
pass
pass
118 changes: 59 additions & 59 deletions src/audio_splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,71 @@


class AudioSplitter:
def split(
self,
file_path: str,
output_dir: str,
min_dur: float = 0.5,
max_dur: float = 15,
max_silence: float = 0.5,
energy_threshold: float = 50,
expand_segments_with_noise: bool = False,
noise_seconds: int = 1,
noise_amplitude: int = 0,
) -> list[tuple[str, float, float]]:
segments = split(
file_path,
min_dur=min_dur,
max_dur=max_dur,
max_silence=max_silence,
energy_threshold=energy_threshold,
)
def split(
self,
file_path: str,
output_dir: str,
min_dur: float = 0.5,
max_dur: float = 15,
max_silence: float = 0.5,
energy_threshold: float = 50,
expand_segments_with_noise: bool = False,
noise_seconds: int = 1,
noise_amplitude: int = 0,
) -> list[tuple[str, float, float]]:
segments = split(
file_path,
min_dur=min_dur,
max_dur=max_dur,
max_silence=max_silence,
energy_threshold=energy_threshold,
)

if expand_segments_with_noise:
segments = [
(
self._expand_segment_with_noise(segment, noise_seconds, noise_amplitude),
segment.meta.start,
segment.meta.end,
) for segment in segments
]
if expand_segments_with_noise:
segments = [
(
self._expand_segment_with_noise(segment, noise_seconds, noise_amplitude),
segment.meta.start,
segment.meta.end,
) for segment in segments
]

return self._save_segments(output_dir, segments)
return self._save_segments(output_dir, segments)

def _expand_segment_with_noise(
self,
segment: AudioRegion,
noise_seconds: int,
noise_amplitude: int,
) -> AudioSegment:
def _expand_segment_with_noise(
self,
segment: AudioRegion,
noise_seconds: int,
noise_amplitude: int,
) -> AudioSegment:

audio_segment = AudioSegment(
segment._data,
frame_rate=segment.sampling_rate,
sample_width=segment.sample_width,
channels=segment.channels,
)
audio_segment = AudioSegment(
segment._data,
frame_rate=segment.sampling_rate,
sample_width=segment.sample_width,
channels=segment.channels,
)

pre_noise = WhiteNoise().to_audio_segment(duration=noise_seconds * 1000, volume=noise_amplitude)
post_noise = WhiteNoise().to_audio_segment(duration=noise_seconds * 1000, volume=noise_amplitude)
pre_noise = WhiteNoise().to_audio_segment(duration=noise_seconds * 1000, volume=noise_amplitude)
post_noise = WhiteNoise().to_audio_segment(duration=noise_seconds * 1000, volume=noise_amplitude)

return pre_noise + audio_segment + post_noise
return pre_noise + audio_segment + post_noise

def _save_segments(
self,
output_dir: str,
segments: list[AudioSegment | tuple[AudioSegment, float, float]],
) -> list[tuple[str, float, float]]:
segment_paths = []
def _save_segments(
self,
output_dir: str,
segments: list[AudioSegment | tuple[AudioSegment, float, float]],
) -> list[tuple[str, float, float]]:
segment_paths = []

for i, segment in enumerate(segments):
output_file = os.path.join(output_dir, f'segment_{i + 1}.mp3')
for i, segment in enumerate(segments):
output_file = os.path.join(output_dir, f'segment_{i + 1}.mp3')

if isinstance(segment, tuple):
segment[0].export(output_file, format='mp3')
segment_paths.append((output_file, segment[1], segment[2]))
else:
segment.save(output_file)
segment_paths.append((output_file, segment.meta.start, segment.meta.end))
return segment_paths
if isinstance(segment, tuple):
segment[0].export(output_file, format='mp3')
segment_paths.append((output_file, segment[1], segment[2]))
else:
segment.save(output_file)
segment_paths.append((output_file, segment.meta.start, segment.meta.end))

return segment_paths
Loading

0 comments on commit 0120136

Please sign in to comment.