write_pcm_wav (diffusers/audio_utils.py)

LocalAI · backend/python/diffusers/audio_utils.py · python · MIT
voice, tag:knn-classified

Writes normalized floating-point audio samples to a mono 16‑bit PCM WAV file for use in audio processing pipelines.

# Source: https://github.com/mudler/LocalAI | License: MIT | Copyright 2023-2025 Ettore Di Giacinto (mudler@localai.io)
# Provided via justclaw.it.com; keep this notice with the code.
import array
import sys
import wave


def write_pcm_wav(destination, samples, sampling_rate):
    """Write normalized floating-point audio samples as mono 16-bit PCM."""
    pcm = array.array(
        "h",
        (
            max(-32768, min(32767, round(float(sample) * 32768)))
            for sample in samples
        ),
    )
    if pcm.itemsize != 2:
        raise RuntimeError("16-bit PCM requires two-byte signed integers")
    if sys.byteorder != "little":
        pcm.byteswap()

    with wave.open(destination, "wb") as output:
        output.setnchannels(1)
        output.setsampwidth(2)
        outpu

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key