audioop-lts
audioop-lts is an LTS (Long Term Support) port of Python's built-in `audioop` module, which was deprecated in Python 3.11 and completely removed in Python 3.13. This project aims to maintain the functionality of the `audioop` module for future Python versions, specifically targeting Python 3.13 and greater. It provides operations for manipulating raw audio data, such as adding, averaging, and calculating RMS of sound fragments. The current version is 0.2.2. The library sees releases as needed for maintenance and compatibility with newer Python versions, indicating active maintenance rather than a strict release cadence.
Warnings
- breaking This library is designed for Python 3.13 and newer. Attempting to install or use `audioop-lts` on Python versions older than 3.13 will result in dependency resolution failures or runtime errors, as the original `audioop` module was only removed in Python 3.13.
- gotcha When working with `audioop` functions, ensure that the `width` parameter (sample width in bytes) correctly matches the actual sample size of your audio data. Incorrect `width` values can lead to `audioop.error` exceptions or corrupted audio manipulation results. Typical widths are 1, 2, 3, or 4 bytes for 8, 16, 24, or 32-bit samples, respectively.
Install
-
pip install audioop-lts
Imports
- audioop
import audioop
Quickstart
import audioop
# Simulate a 16-bit mono audio fragment (2 bytes per sample)
# For a real scenario, this would come from an audio file or stream
# Example: two samples, value 1000 and -500
fragment = b'\xe8\x03\x0c\xfe' # Little-endian for 1000 and -500 (approx)
width = 2 # 2 bytes per sample (16-bit)
# Calculate the RMS (Root Mean Square) of the audio fragment
rms_value = audioop.rms(fragment, width)
print(f"RMS value: {rms_value}")
# Add two fragments (requires them to be of the same length and width)
fragment1 = b'\x01\x00\x02\x00'
fragment2 = b'\x05\x00\x06\x00'
sum_fragment = audioop.add(fragment1, fragment2, width)
print(f"Sum fragment: {sum_fragment.hex()}")
# Convert to u-LAW encoding (8-bit output, input width still matters)
u_law_fragment = audioop.lin2ulaw(fragment, width)
print(f"u-LAW fragment: {u_law_fragment.hex()}")