Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract noisereduce and visrecognize to optional dependencies #60

Merged
merged 7 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 94 additions & 20 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ on:

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11"]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: install ffmpeg
run: |
Expand All @@ -44,29 +43,71 @@ jobs:
cached: ${{ steps.cache-llvm.outputs.cache-hit }}

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'

- name: Install dependencies
run: |
sudo apt install libsndfile1-dev
python -m pip install wheel
python -m pip install --upgrade pip
python -m pip install flake8
pip install -e .[visrecognize,noisereduce,test]

- name: Cache pip
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

- name: Test with pytest
run: |
pytest


build-minimal:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11"]

steps:
- uses: actions/checkout@v4

- name: install ffmpeg
run: |
sudo apt-get update && sudo apt-get install ffmpeg -y

- name: Cache LLVM and Clang
id: cache-llvm
uses: actions/cache@v2
with:
# This path is specific to Ubuntu
path: ~/.cache/pip
# Look to see if there is a cache hit for the corresponding requirements file
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
${{ runner.os }}-
path: ${{ runner.temp }}/llvm
key: llvm-10.0

- name: Install LLVM and Clang
uses: KyleMayes/install-llvm-action@v1
with:
version: "10.0"
directory: ${{ runner.temp }}/llvm
cached: ${{ steps.cache-llvm.outputs.cache-hit }}

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'

- name: Install dependencies
run: |
sudo apt install libsndfile1-dev
python -m pip install wheel
python -m pip install --upgrade pip
python -m pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
if [ -f requirements_dev.txt ]; then pip install -r requirements_dev.txt; fi
python -m pip install flake8
pip install -e .[test]

- name: Lint with flake8
run: |
Expand All @@ -79,9 +120,42 @@ jobs:
run: |
pytest

# replaced with this https://github.com/KyleMayes/install-llvm-action
# sudo apt install llvm-9
# sudo -i
# cd /usr/bin
# rm llvm-config #if it exists already, which probably it does
# ln -s llvm-config-10 llvm-config
#
# build-windows:
# runs-on: windows-latest
# strategy:
# matrix:
# python-version: ["3.9", "3.10", "3.11"]
#
# steps:
# - uses: actions/checkout@v4
# - uses: FedericoCarboni/setup-ffmpeg@v3
# id: setup-ffmpeg
# with:
# ffmpeg-version: release
# linking-type: static
# github-token: ${{ github.server_url == 'https://github.com' && github.token || '' }}
#
# - name: Set up Python ${{ matrix.python-version }}
# uses: actions/setup-python@v5
# with:
# python-version: ${{ matrix.python-version }}
# cache: 'pip'
#
# - name: Install dependencies
# run: |
# python -m pip install wheel
# python -m pip install --upgrade pip
# python -m pip install flake8
# pip install -e .[visrecognize,noisereduce,test]
#
# - name: Lint with flake8
# run: |
# # stop the build if there are Python syntax errors or undefined names
# flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
# flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
#
# - name: Test with pytest
# run: |
# pytest
6 changes: 3 additions & 3 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ jobs:
- uses: FedericoCarboni/setup-ffmpeg@v1-beta
id: setup-ffmpeg
- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
pip install build wheel twine
- name: Build and publish
run: |
python setup.py sdist bdist_wheel
python -m build --wheel
- name: Publish package
uses: pypa/[email protected]
with:
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log

## [1.3.0] 2024 - 06 - 02

### Changed

- Migrated to pyproject.toml
- Extracted noisereduce and visrecognize to optional dependencies

## [1.2.4] 2024 - 01 - 07

### Added
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ pip install audalign

in the directory

### Optional dependencies

- visrecognize: additional recognizer based on spectrogram image comparison. `pip install audalign[visrecognize]`
- noisereduce: wrapper utils around [timsainb/noisereduce](https://github.com/timsainb/noisereduce). `pip install audalign[noisereduce]`

## Recognizers

There are currently four included recognizers, each with their own config objects.
Expand All @@ -64,7 +69,7 @@ import audalign as ad
fingerprint_rec = ad.FingerprintRecognizer()
correlation_rec = ad.CorrelationRecognizer()
cor_spec_rec = ad.CorrelationSpectrogramRecognizer()
visual_rec = ad.VisualRecognizer()
visual_rec = ad.VisualRecognizer() # requires installting optional visrecognize dependencies

fingerprint_rec.config.set_accuracy(3)
# recognizer.config.some_item
Expand Down Expand Up @@ -108,7 +113,7 @@ Correlation is more precise than fingerprints and will always give a best alignm
## Other Functions

```python
# wrapper for timsainb/noisereduce
# wrapper for timsainb/noisereduce, optional dependency
ad.remove_noise_file(
"target/file",
"5", # noise start in seconds
Expand Down
23 changes: 22 additions & 1 deletion audalign/filehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@
import os
import typing
from functools import partial
from functools import wraps

import noisereduce
import numpy as np
from numpy.core.defchararray import array
from pydub import AudioSegment, effects
from pydub.exceptions import CouldntDecodeError

from audalign.config import BaseConfig

try:
import noisereduce
except ImportError:
# Optional dependency
...


cant_write_ext = [".mov", ".mp4", ".m4a"]
cant_read_ext = [".txt", ".md", ".pkf", ".py", ".pyc"]
can_read_ext = [
Expand All @@ -30,6 +37,18 @@
".flac",
]

def _import_optional_dependencies(func):
@wraps(func)
def wrapper_decorator(*args, **kwargs):
try:
import noisereduce
except ImportError:
raise ImportError("skimage not found, please install 'visrecognize' module")
results = func(*args, **kwargs)
return results

return wrapper_decorator


def find_files(path, extensions=["*"]):
"""
Expand Down Expand Up @@ -192,6 +211,7 @@ def _int16ify_data(data: array):
return data.astype(np.int16)


@_import_optional_dependencies
def noise_remove(
filepath,
noise_start,
Expand Down Expand Up @@ -296,6 +316,7 @@ def noise_remove_directory(
_reduce_noise(i)


@_import_optional_dependencies
def _remove_noise(
file_path,
noise_section=[],
Expand Down
13 changes: 13 additions & 0 deletions audalign/recognizers/visrecognize/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,24 @@
import typing
import copy
from functools import partial
from functools import wraps

def _import_optional_dependencies(func):
@wraps(func)
def wrapper_decorator(*args, **kwargs):
try:
import skimage
except ImportError:
raise ImportError("skimage not found, please install 'visrecognize' module")
results = func(*args, **kwargs)
return results

return wrapper_decorator

class VisualRecognizer(BaseRecognizer):
config: VisualConfig

@_import_optional_dependencies
def __init__(self, config: VisualConfig = None):
self.config = VisualConfig() if config is None else config
self.last_recognition = None
Expand Down
8 changes: 6 additions & 2 deletions audalign/recognizers/visrecognize/visrecognize.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
from audalign.filehandler import find_files, get_shifted_file, read
from PIL import Image
from pydub.exceptions import CouldntDecodeError
from skimage.metrics import mean_squared_error
from skimage.metrics import structural_similarity as ssim
try:
from skimage.metrics import mean_squared_error
from skimage.metrics import structural_similarity as ssim
except ImportError:
# Optional dependency
...

upper_clip = 255

Expand Down
57 changes: 57 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "audalign"
version = "1.3.0"
description = "Audio Alignment and Recognition in Python"
requires-python = ">= 3.8"
readme = "README.md"
license = "MIT"
authors = [
{ name = "Ben Miller", email = "[email protected]" },
]
maintainers = [
{ name = "Ben Miller", email = "[email protected]" },
]
keywords = [
"align",
"alignment",
"audio",
"fingerprinting",
"music",
"python",
]
dependencies = [
"matplotlib==3.8.2",
"numpy==1.26.4",
"pydub==0.25.1",
"scipy==1.12.0",
"setuptools==59.6.0",
"tqdm==4.66.2",
]

[project.optional-dependencies]
noisereduce = [
"noisereduce==2.0.1",
"torch==2.2.0",
]
visrecognize = [
"Pillow==10.2.0",
"scikit-image==0.19.3",
]
test = [
"pytest==8.0.0",
"pytest-xdist==3.1.0",
]

[project.urls]
Homepage = "http://github.com/benfmiller/audalign"
Documentation = "https://github.com/benfmiller/audalign/wiki"
Changelog = "https://github.com/benfmiller/audalign/blob/main/CHANGELOG.md"

[tool.hatch.build.targets.sdist]
include = [
"/audalign",
]
Loading
Loading