Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Mews committed Jun 17, 2024
2 parents cccd016 + 7e1d9c0 commit 7f6f9cc
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 26 deletions.
24 changes: 14 additions & 10 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#Tell sphinx and rtd where to find source code
import sys, pathlib
sys.path.append((pathlib.Path(__file__).parent.parent.parent / "src").resolve().as_posix())
# Tell sphinx and rtd where to find source code
import pathlib
import sys

sys.path.append(
(pathlib.Path(__file__).parent.parent.parent / "src").resolve().as_posix()
)

# Configuration file for the Sphinx documentation builder.
#
Expand All @@ -10,24 +14,24 @@
# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = 'simpleaudiostretch'
copyright = '2024, Mews'
author = 'Mews'
release = '0.1.0'
project = "simpleaudiostretch"
copyright = "2024, Mews"
author = "Mews"
release = "0.1.0"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
import sphinx_rtd_theme

extensions = ["sphinx.ext.autodoc", "sphinx_rtd_theme", "sphinx.ext.autosummary"]

templates_path = ['_templates']
templates_path = ["_templates"]
exclude_patterns = []

autodoc_mock_imports = ["numpy", "soundfile"]

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = 'sphinx_rtd_theme'
html_static_path = ['_static']
html_theme = "sphinx_rtd_theme"
html_static_path = ["_static"]
11 changes: 6 additions & 5 deletions src/simplestretch/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pathlib
from typing import Optional, Tuple, Union

import soundfile
from numpy import ndarray
import pathlib


def stretch_audio(
Expand Down Expand Up @@ -42,13 +42,14 @@ def stretch_audio(

if factor <= 0:
raise ValueError("'factor' must be greater than 0")

if isinstance(audio, ndarray) and not isinstance(samplerate, int):
try:
samplerate = int(samplerate)
except:
raise TypeError(f"You must provide a valid sample rate when working with raw audio data (Not {type(samplerate)})")

raise TypeError(
f"You must provide a valid sample rate when working with raw audio data (Not {type(samplerate)})"
)

# If a file path is provided, load it as a ndarray using soundfile
if isinstance(audio, str):
Expand Down Expand Up @@ -108,4 +109,4 @@ def speedup_audio(
# Stretch audio to match the specified speedup factor
return stretch_audio(
audio=audio, factor=1 / factor, output=output, samplerate=samplerate
)
)
31 changes: 24 additions & 7 deletions src/simplestretch/cli.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
from . import stretch_audio, speedup_audio
import argparse

from . import speedup_audio, stretch_audio


def main():
parser = argparse.ArgumentParser("Stretch an audio file or speed it up or down")

parser.add_argument('-a', '--audio', required=True, type=str, help="Path to the audio file")
parser.add_argument('-f', '--factor', required=True, type=float, help="Factor for the change in audio length/speed")
parser.add_argument('-o', '--output', required=True, type=str, help="Path for the output file")
parser.add_argument('-s', '--speed', action='store_true', help="If this is passed, the factor will be applied to the speed of the audio rather than the length")
parser.add_argument(
"-a", "--audio", required=True, type=str, help="Path to the audio file"
)
parser.add_argument(
"-f",
"--factor",
required=True,
type=float,
help="Factor for the change in audio length/speed",
)
parser.add_argument(
"-o", "--output", required=True, type=str, help="Path for the output file"
)
parser.add_argument(
"-s",
"--speed",
action="store_true",
help="If this is passed, the factor will be applied to the speed of the audio rather than the length",
)

args = parser.parse_args()

if args.speed:
speedup_audio(audio=args.audio, factor=args.factor, output=args.output)

else:
stretch_audio(audio=args.audio, factor=args.factor, output=args.output)
stretch_audio(audio=args.audio, factor=args.factor, output=args.output)
7 changes: 4 additions & 3 deletions tests/test_speedup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import numpy as np
import pytest
import soundfile

from simplestretch import speedup_audio


Expand All @@ -34,7 +35,7 @@ def test_speedup_audio_raw(self):
assert self.sine_audio.all() == spedup_audio.all()

assert round(self.sine_samplerate / (1 / factor)) == spedup_samplerate

def test_speedup_audio_file_wav(self):
factor = 2
file = "tests/sample_files/440hz.wav"
Expand All @@ -46,7 +47,7 @@ def test_speedup_audio_file_wav(self):
assert audio.all() == spedup_audio.all()

assert round(samplerate / (1 / factor)) == spedup_samplerate

def test_speedup_audio_file_mp3(self):
factor = 2
file = "tests/sample_files/440hz.wav"
Expand Down Expand Up @@ -91,4 +92,4 @@ def test_speedup_invalid_factor_negative(self):
with pytest.raises(ValueError):
speedup_audio(
audio=self.sine_audio, factor=-1, samplerate=self.sine_samplerate
)
)
3 changes: 2 additions & 1 deletion tests/test_stretch.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import numpy as np
import pytest
import soundfile

from simplestretch import stretch_audio


Expand Down Expand Up @@ -137,4 +138,4 @@ def test_stretch_save_invalid_mp3_samplerate(self):
)

# Assert file was deleted
assert not pathlib.Path(out_file).exists()
assert not pathlib.Path(out_file).exists()

0 comments on commit 7f6f9cc

Please sign in to comment.