API Reference¶
This section documents the cysox public API.
Module Functions¶
Initialization¶
init()¶
Initialize the SoX library. Must be called before using any other low-level cysox functions. The high-level API calls this automatically.
quit()¶
Clean up the SoX library. Should be called when done using the low-level API.
format_init()¶
Initialize format handlers. Called automatically by init().
format_quit()¶
Clean up format handlers. Called automatically by quit().
Version Information¶
version() -> str¶
Returns the libsox version string (e.g., "14.4.2").
version_info() -> dict¶
Returns detailed version information as a dictionary with keys:
version: Version stringversion_code: Numeric version codeflags: Feature flagstime: Build timecompiler: Compiler infoarch: Architecture info
Lookup Functions¶
find_effect(name: str) -> Optional[EffectHandler]¶
Find an effect handler by name.
find_format(name: str, ignore_devices: bool = False) -> Optional[FormatHandler]¶
Find a format handler by name.
Utility Functions¶
strerror(sox_errno: int) -> str¶
Convert a SoX error code to an error message string.
is_playlist(filename: str) -> bool¶
Check if a file is a known playlist format.
basename(filename: str) -> str¶
Extract the basename from a file path.
precision(encoding: int, bits_per_sample: int) -> int¶
Calculate the precision (useful bits) for an encoding.
format_supports_encoding(path: str, encoding: EncodingInfo) -> bool¶
Check if a format supports a specific encoding.
get_encodings() -> List[EncodingsInfo]¶
Get a list of all available encodings.
get_effects_globals() -> dict¶
Get global effects settings.
get_format_fns() -> List[dict]¶
Get a list of available format handlers.
get_last_callback_exception() -> Optional[Tuple]¶
Retrieve the last exception that occurred in a flow_effects callback.
Since exceptions cannot propagate through C code, they are stored for
later retrieval. Returns (type, value, traceback) or None.
chain.flow_effects(callback=my_callback)
exc_info = sox.get_last_callback_exception()
if exc_info:
raise exc_info[1].with_traceback(exc_info[2])
Core Classes¶
Format¶
Audio format handle for reading and writing audio files.
Parameters:
filename- Path to the audio filesignal- SignalInfo for output files (required for write mode)encoding- EncodingInfo (optional)mode-'r'for reading,'w'for writing
Properties:
filename(str): Path to the audio filesignal(SignalInfo): Signal informationencoding(EncodingInfo): Encoding informationfiletype(str): Format identifier (e.g.,"wav")seekable(bool): Whether seeking is supportedmode(str):'r'or'w'clips(int): Clipping countersox_errno(int): Last error codesox_errstr(str): Last error message
Methods:
read(length: int) -> List[int]- Read samples into a Python listread_buffer(length: int) -> memoryview- Read samples into a memoryview (buffer protocol)read_into(buffer) -> int- Read samples into a pre-allocated buffer. Returns samples readwrite(samples) -> int- Write samples from a list or buffer. Returns samples writtenseek(offset: int, whence: int = SOX_SEEK_SET) -> int- Seek to a sample positionclose() -> int- Close the file handle
Context Manager:
SignalInfo¶
Audio signal parameters.
Properties:
rate(float): Sample rate in Hz (0 if unknown)channels(int): Channel count (0 if unknown)precision(int): Bits per sample (0 if unknown)length(int): Total samples * channelsmult(float): Headroom multiplier
EncodingInfo¶
Audio encoding parameters.
Properties:
encoding(int): Encoding typebits_per_sample(int): Bits per samplecompression(float): Compression factorreverse_bytes(int): Byte reversal settingreverse_nibbles(int): Nibble reversal settingreverse_bits(int): Bit reversal settingopposite_endian(bool): Endianness reversal
Effect Classes¶
EffectHandler¶
Describes an effect type. Obtained via find_effect().
Properties:
name(str): Effect nameusage(str): Parameter usage descriptionflags(int): Effect flagspriv_size(int): Private data size
Effect¶
An effect instance.
Methods:
set_options(options: List) -> int- Set effect options. Options can be strings or Format objects.stop() -> int- Stop the effect and return clip count.
Properties:
handler(EffectHandler): The effect's handlerin_signal(SignalInfo): Input signal infoout_signal(SignalInfo): Output signal infoclips(int): Clipping count
EffectsChain¶
A chain of effects to process audio.
Methods:
add_effect(effect, in_signal, out_signal) -> int- Add an effect to the chainflow_effects(callback=None, client_data=None) -> int- Process audio through the chainget_clips() -> int- Get total clipping count
The callback parameter accepts a function (all_done: bool, user_data) -> bool.
Return True to continue, False to abort.
Properties:
effects(List[Effect]): Effects in the chainlength(int): Number of effects
Constants¶
Error Codes¶
SUCCESS- Operation succeededEOF- End of fileEHDR- Invalid headerEFMT- Invalid formatENOMEM- Out of memoryEPERM- Permission deniedENOTSUP- Not supportedEINVAL- Invalid argument
Encodings¶
ENCODINGS is a list of (name, description) tuples for all supported encodings: