跳转到主要内容

AudioStretchy是一个Python库和CLI工具,它可以快速、高质量地对WAV/MP3文件进行时间伸缩,而不会改变它们的音高。非常适合语音处理,可以单独对静音进行时间伸缩。AudioStretchy是David Bryant的audio-stretch C库的包装。

项目描述

AudioStretchy

AudioStretchy是一个Python库和CLI工具,它可以快速、高质量地对WAV/MP3文件进行时间伸缩,而不会改变它们的音高。非常适合语音处理,可以单独对静音进行时间伸缩。该库是David Bryant的audio-stretch C库的包装。

版本:1.3.5

功能

  • 音频文件快速、高质量的时间伸缩,不改变音高
  • 伸缩比例可调,从0.25到4.0
  • 跨平台:Windows、macOS和Linux
  • 支持WAV文件和类似文件对象。使用[all]安装时,也支持MP3文件和类似文件对象
  • 使用[all]安装时,也支持重采样

时域谐波缩放(TDHS)是一种用于语音(或其他音频信号)的时间伸缩方法,允许改变语音发音的速率,而不会影响音高轮廓和音素结构的时域演变。TDHS与其他时间伸缩算法的不同之处在于,时间伸缩操作是在时域(而不是频域)进行的。

本软件包的核心功能由David Bryant编写的优秀的audio-stretch C库提供,该库能够在0.25(4倍慢)到4.0(4倍快)的比率范围内对WAV文件进行快速、高质量的TDHS处理。

该库在语音录音方面表现优异,特别是在0.9(慢10%)到1.1(快10%)的比率范围内适度拉伸时。AudioStretchy是该库的Python包装器。Python软件包还提供了一些额外的可选功能:支持MP3(除了WAV),并允许您进行预采样。

演示

以下是一些链接,链接到一个小音频文件(WAV和MP3格式),该文件在1.2(慢20%)的比率下拉伸。

输入 拉伸
audio.wav audio-1.2.wav
audio.mp3 audio-1.2.mp3

安装

完整安装

要能够拉伸和重采样WAV和MP3文件,请使用如下方式安装AudioStretchy:

python3 -m pip install audiostretchy[all]

这将为macOS、Windows和Linux安装该软件包和预编译的audio-stretch库。

这还将安装可选的依赖项:

  • 支持MP3:macOS上的pydub,Linux和Windows上的pymp3
  • 重采样:soxr

在macOS上,您还需要安装HomeBrew,然后在终端运行

brew install ffmpeg

最小安装

要仅能拉伸WAV文件(无重采样,无MP3支持),请以最小依赖项安装AudioStretchy,如下所示

python3 -m pip install audiostretchy

这仅安装了软件包和预编译的audio-stretch库,适用于macOS、Windows和Linux。

完整开发安装

要安装开发版本,请使用

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

用法

命令行界面

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. From 0.5 to 2.0, or with `-d` 
        from 0.25 to 4.0. Default is 1.0 = no stretching.
    -g, --gap_ratio=GAP_RATIO
        The stretch ratio for gaps (silence) in the audio. 
        Default is 0.0 = uses 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 
        (useful with `-g`). 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 (for `-g`). Default is -40 dB.
    -d, --double_range=DOUBLE_RANGE
        If set, doubles the min/max range of stretching.
    -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 (if installed 
        with `[all]`). 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)

在此示例中,input.wav文件将被拉伸1.1倍,这意味着它将变长10%,并将结果保存到output.wav文件中。

对于高级用法,您可以使用AudioStretch类,该类允许您以路径或文件样式的BytesIO对象打开和保存文件。

from audiostretchy.stretch import AudioStretch

audio_stretch = AudioStretch()
# This needs [all] installation for MP3 support
audio_stretch.open(file=MP3DataAsBytesIO, format="mp3") 
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,
)
# This needs [all] installation for soxr support
audio_stretch.resample(sample_rate=44100) 
audio_stretch.save(file=WAVDataAsBytesIO, format="wav")

变更日志

  • v1.3.5: 修复MP3写入问题
  • v1.3.2: 修复MP3打开问题
  • v1.3.0: 在Windows上也能正常工作
  • v1.2.x: 在macOS和Linux上工作

许可证

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

audiostretchy-1.3.5.tar.gz (1.8 MB 查看散列值

上传时间 源代码

构建分发

audiostretchy-1.3.5-py3-none-any.whl (105.5 kB 查看散列值

上传于 Python 3

由...支持