Skip to main content

AudioStretchy is a Python library and CLI tool that which performs fast, high-quality time-stretching of WAV/MP3 files without changing their pitch. Works well for speech, can time-stretch silence separately. AudioStretchy is a wrapper around the audio-stretch C library by David Bryant.

Project description

AudioStretchy

AudioStretchy is a Python library and CLI tool that which performs fast, high-quality time-stretching of WAV/MP3 files without changing their pitch. Works well for speech, can time-stretch silence separately. The library is a wrapper around David Bryant’s audio-stretch C library.

Version: 1.2.2

Features

  • Fast, high-quality time stretching of audio files without changing their pitch
  • Adjustable stretching ratio from 0.25 to 4.0
  • Cross-platform: Windows, macOS, and Linux
  • Supports WAV files and file-like objects. With [all] installation, also supports MP3 files and file-like objects
  • With [all] installation, also supports resampling

Time-domain harmonic scaling (TDHS) is a method for time-scale modification of speech (or other audio signals), allowing the apparent rate of speech articulation to be changed without affecting the pitch-contour and the time-evolution of the formant structure. TDHS differs from other time-scale modification algorithms in that time-scaling operations are performed in the time domain (not the frequency domain).

The core functionality of this package is provided by David Bryant’s excellent audio-stretch C library that performs fast, high-quality TDHS on WAV in the ratio range of 0.25 (4× slower) to 4.0 (4× faster).

The library gives very good results with speech recordings, especially with modest stretching at the ratio between 0.9 (10% slower) and 1.1 (10% faster). AudioStretchy is a Python wrapper around that library. The Python package also offers some additional, optional functionality: supports MP3 (in addition to WAV), and allows you to preform resampling.

Installation

Full installation

To be able to stretch and resample both WAV and MP3 files, install AudioStretchy using pip like so:

pip install audiostretchy[all]

This installs the package and the pre-compiled audio-stretch libraries for macOS, Windows and Linux.

This also installs optional dependencies:

  • for MP3 support: pydub on macOS, pymp3 on Linux and Windows
  • for resampling: soxr

On macOS, you also need to install HomeBrew and then in Terminal run:

brew install ffmpeg

Minimal installation

To only be able to stretch WAV files (no resampling, no MP3 support), install AudioStretchy with minimal dependencies like so:

pip install audiostretchy

This only installs the package and the pre-compiled audio-stretch libraries for macOS, Windows and Linux.

Full development installation

To install the development version, use:

python3 -m pip install git+https://github.com/twardoch/audiostretchy#egg=audiostretchy[all]

Usage

CLI

audiostretchy INPUT_WAV OUTPUT_WAV <flags>

POSITIONAL ARGUMENTS
    INPUT_PATH
        The path to the input WAV or MP3 audio file.
    OUTPUT_PATH
        The path to save the stretched WAV or MP3 audio file.

FLAGS
    -r, --ratio=RATIO
        The stretch ratio, where values greater than 1.0 will extend the audio and values less than 1.0 will shorten the audio. Default is 1.0 = no stretching.
    -g, --gap_ratio=GAP_RATIO
        The stretch ratio for gaps (silence) in the audio. Default is 0.0 = use ratio.
    -u, --upper_freq=UPPER_FREQ
        The upper frequency limit for period detection in Hz. Default is 333 Hz.
    -l, --lower_freq=LOWER_FREQ
        The lower frequency limit. Default is 55 Hz.
    -b, --buffer_ms=BUFFER_MS
        The buffer size in milliseconds for processing the audio in chunks. Default is 25 ms.
    -t, --threshold_gap_db=THRESHOLD_GAP_DB
        The threshold level in dB to determine if a section of audio is considered a gap (silence). Default is -40 dB.
    -d, --dual_force=DUAL_FORCE
        If set, forces the algorithm to operate in dual-force mode, which may improve the quality of the stretched audio but may also increase processing time.
    -f, --fast_detection=FAST_DETECTION
        If set, enables fast period detection, which may speed up processing but reduce the quality of the stretched audio.
    -n, --normal_detection=NORMAL_DETECTION
        If set, forces the algorithm to use normal period detection instead of fast period detection.
    -s, --sample_rate=SAMPLE_RATE
        The target sample rate for resampling the stretched audio in Hz. Default is 0 = use sample rate of the input audio.

Python

from audiostretchy.stretch import stretch_audio

stretch_audio("input.wav", "output.wav", ratio=1.1)

In this example, the input.wav file will be time-stretched by a factor of 1.1, meaning it will be 10% longer, and the result will be saved in the output.wav file.

For advanced usage, you can use the AudioStretch class that lets you open and save files provided as paths or as file-like BytesIO objects:

from audiostretchy.stretch import AudioStretch

audio_stretch = AudioStretch()
audio_stretch.open(file=MP3DataAsBytesIO, format="mp3") # Needs [all] installation for MP3 support
audio_stretch.stretch(
    ratio=1.1,
    gap_ratio=1.2,
    upper_freq=333,
    lower_freq=55,
    buffer_ms=25,
    threshold_gap_db=-40,
    dual_force=False,
    fast_detection=False,
    normal_detection=False,
)
audio_stretch.resample(sample_rate=44100) # Needs [all] installation for soxr support
audio_stretch.save(file=WAVDataAsBytesIO, format="wav")

License

Project details


Download files

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

Source Distribution

audiostretchy-1.2.2.tar.gz (1.8 MB view details)

Uploaded Source

Built Distribution

audiostretchy-1.2.2-py3-none-any.whl (83.8 kB view details)

Uploaded Python 3

File details

Details for the file audiostretchy-1.2.2.tar.gz.

File metadata

  • Download URL: audiostretchy-1.2.2.tar.gz
  • Upload date:
  • Size: 1.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for audiostretchy-1.2.2.tar.gz
Algorithm Hash digest
SHA256 24cd41a60bd7d729bca8fff4d1058ef555fbf8a6101e71b9ab6be2916dcf7f3c
MD5 e4765172595a95f6ecf804e9e30f6c82
BLAKE2b-256 388495367ec33ecc6260e438b256cadf849b949d794f4782c27cc343700206e7

See more details on using hashes here.

File details

Details for the file audiostretchy-1.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for audiostretchy-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 49f77126f143e8c4a228d5807604e8b7d59612af965a54ed7200d67da37116f1
MD5 92ef38b5f6df29bd3a9fa5c1cd5e2549
BLAKE2b-256 6aae8ffed9bb7e0bd7b27c80729993ccacf7f105db04e5f9138c6269b84277f2

See more details on using hashes here.

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