Skip to main content

A fast python library for aligning similar audio snippets passed in as NumPy arrays.

Project description

fast-align-audio: high-speed NumPy audio alignment

fast-align-audio is designed to swiftly align two similar 1-dimensional NumPy arrays — a common need in various fields including audio signal processing. If you have two arrays where one "lags behind" the other due to factors such as different capture sensors (microphones), propagation delays, or post-processing like reverberation or MP3 compression, fast-align-audio is here to help.

The package employs a "brute force" alignment approach, leveraging a C-based algorithm for maximum speed while providing a user-friendly Python API for easy integration.

While this library was initially developed with audio ndarrays in mind, it could also be used to align other kinds of time-series data that are represented as 1D NumPy arrays.

Installation

$ pip install fast-align-audio

Usage

Here is a basic usage example:

import fast_align_audio
import numpy as np

# Create a random NumPy array
reference = np.random.uniform(size=10_000).astype("float32")
delayed = np.pad(reference, (121, 0))[0:10_000]

# Find the best offset for aligning two arrays
offset, mse = fast_align_audio.find_best_alignment_offset(
    reference_signal=reference,
    delayed_signal=delayed,
    max_offset_samples=1000,
    lookahead_samples=5000,
)
print(offset)  # 121

negative_offset, mse2 = fast_align_audio.find_best_alignment_offset(
    reference_signal=reference,
    delayed_signal=reference[121:],
    max_offset_samples=1000,
    lookahead_samples=5000,
)
print(negative_offset)  # -121

# Align two arrays and confirm they're equal post alignment
aligned_audio, gaps = fast_align_audio.align_delayed_signal_with_reference(
    reference, delayed, offset=offset
)
print(np.array_equal(reference[500:600], aligned_audio[500:600]))  # True

In this example, we first create a random numpy array. We then call the find_best_alignment_offset method to find the best offset to align two arrays, and we use the align method to align the arrays. The np.array_equal method checks if two arrays are equal, demonstrating the successful alignment of the two original arrays.

Tips

  • For more reliable alignments, filter out unwanted/unrelated sounds before passing the audio snippets to fast-align-audio. E.g. if you are aligning two speech recordings, you could band-pass filter and/or denoise them first.
  • This library assumes that the delay is fixed throughout the audio snippet. If you need something that aligns audio tracks in a dynamic way (e.g. due to distance between microphones changing over time), look elsewhere.
  • The "mse" method is sensitive to loudness differences. If you use this method, make sure the two input audio snippets have roughly the same loudness
  • This lib only works well for small offsets, like up to 500 ms, and suitable audio file durations, like for example between 3 and 45 seconds. If you have large audio files with large offsets between them, a different algorithm may be required to solve the problem well.

Development

  • Install dev/build/test dependencies as denoted in setup.py
  • python setup.py develop
  • pytest

Acknowledgements

Original C implementation by jonashaag. Now maintained/backed by Nomono.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

fast_align_audio-0.3.0-pp310-pypy310_pp73-win_amd64.whl (12.9 kB view hashes)

Uploaded PyPy Windows x86-64

fast_align_audio-0.3.0-pp310-pypy310_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.7 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

fast_align_audio-0.3.0-pp39-pypy39_pp73-win_amd64.whl (12.9 kB view hashes)

Uploaded PyPy Windows x86-64

fast_align_audio-0.3.0-pp39-pypy39_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.7 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

fast_align_audio-0.3.0-pp38-pypy38_pp73-win_amd64.whl (12.9 kB view hashes)

Uploaded PyPy Windows x86-64

fast_align_audio-0.3.0-pp38-pypy38_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.7 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

fast_align_audio-0.3.0-pp37-pypy37_pp73-win_amd64.whl (12.9 kB view hashes)

Uploaded PyPy Windows x86-64

fast_align_audio-0.3.0-pp37-pypy37_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.8 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

fast_align_audio-0.3.0-cp311-cp311-win_amd64.whl (13.8 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

fast_align_audio-0.3.0-cp311-cp311-musllinux_1_1_x86_64.whl (22.6 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

fast_align_audio-0.3.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.5 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

fast_align_audio-0.3.0-cp310-cp310-win_amd64.whl (13.8 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

fast_align_audio-0.3.0-cp310-cp310-musllinux_1_1_x86_64.whl (22.6 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

fast_align_audio-0.3.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.5 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

fast_align_audio-0.3.0-cp39-cp39-win_amd64.whl (13.8 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

fast_align_audio-0.3.0-cp39-cp39-musllinux_1_1_x86_64.whl (22.6 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

fast_align_audio-0.3.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.5 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

fast_align_audio-0.3.0-cp38-cp38-win_amd64.whl (13.8 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

fast_align_audio-0.3.0-cp38-cp38-musllinux_1_1_x86_64.whl (23.1 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

fast_align_audio-0.3.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.6 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page