LIBSOX_NG(3)
π LIBSOX_NG(3) π
okay so welcome to the Sound eXchange_ng lib manual, bestie π
this is the whole vibe for libsox_ng, and we're gonna get you comfy with it. no cap.
π NAME π
libsox_ng β another audio file format and effect library. periodt.
β¨ SYNOPSIS β¨
#include <sox_ng.h>
cc -o file file.c -lsox_ng
πΈ DESCRIPTION πΈ
okay so libsox_ng is a library of sound sample file format readers/writers and sound effects processors. It is mainly developed to be used by SoX but any audio application might find it useful. fr fr.
If you want to dive straight in reading and writing audio files, look at the start of GENERIC FUNCTIONS and the section on READING AND WRITING AUDIO FILES. π
#include <sox_ng.h> β Declarations for all of the below (and a lot more!). However, you should only use items whose names begin with sox_ or SOX_; the lsx_ and LSX_ items are intended for internal use by SoX's format and effect handlers. They are exported by libsox so that dynamically-loaded format handlers and custom format and effect handlers can use them but should not be used by libSoX-based applications. iykyk β stay in your lane with the sox_ and SOX_ names, bestie.
Programs should normally include <sox.h> to be portable to systems that have not upgraded to sox_ng yet; those that have upgraded should provide <sox.h> as an alias for <sox_ng.h> and the interface is unlikely to change.
cc -o file file.c -lsox_ng β Ensure that if a program includes <sox.h>, it is linked with -lsox, and <sox_ng.h> with -lsox_ng as some software distributions (or you) may choose to install them both side by side and the contents of their internal data structures differ. this is important, okay? π«Ά
π¦ BASIC TYPES π¦
sox_int8_tβ Signed twos-complement 8-bit typesox_uint8_tβ Unsigned 8-bit typesox_int16_tβ Signed twos-complement 16-bit typesox_uint16_tβ Unsigned 16-bit typesox_int32_tβ Signed twos-complement 32-bit typesox_uint32_tβ Unsigned 32-bit typesox_int64_tβ Signed twos-complement 64-bit typesox_uint64_tβ Unsigned 64-bit typesox_int24_tβ Alias forsox_int32_t(beware of the extra byte)sox_uint24_tβ Alias forsox_uint32_t(beware of the extra byte)sox_sample_tβ Native SoX audio sample type (alias forsox_int32_t)sox_rate_tβ Samples per second are stored as a doublesox_boolβ Boolean type:sox_false(= 0) orsox_true(= 1)
π ERROR HANDLING π
vibe check: most of the functions in libSoX return an integer error code which is SOX_SUCCESS if the functions succeeded or a sox_error_t which can take the following values:
SOX_SUCCESSβ Function succeeded = 0SOX_EOFβ End Of File or other errorSOX_EHDRβ Invalid Audio HeaderSOX_EFMTβ Unsupported data formatSOX_ENOMEMβ Can't alloc memorySOX_EPERMβ Operation not permittedSOX_ENOTSUPβ Operation not supportedSOX_EINVALβ Invalid argumentSOX_ENOKEYMAPβ No such keymapSOX_ENOEFFECTβ No such effect
or other values mapped from errno(3).
char *sox_strerror(int sox_errno) β Converts a SoX error code into an error string and returns the error string corresponding to the specified error code, or a generic message if the error code is not recognized. slay.
π₯ GENERIC FUNCTIONS π₯
int sox_init(void)β Initialize the effects library. ReturnsSOX_SUCCESSif successful.int sox_format_init(void)β Find and load format handler plugins. ReturnsSOX_SUCCESSif successful.void sox_format_quit(void)β Unload format handler plugins.int sox_quit(void)β Close the effects library and unload format handler plugins. ReturnsSOX_SUCCESSif successful.char *sox_version(void)β Returns the version number string of libSoX, for example,"14.4.0".sox_version_info_t *sox_version_info(void)β Returns information about this build of libSoX, containing:size_t sizeβ Structure size =sizeof(sox_version_info_t)sox_version_flags_t flagsβ Feature flags bits indicating whether optional features are present in this build of libSoX, the logical OR of:sox_version_noneβ No special features (= 0).sox_version_have_popenβ Pipes are available to fetch URLs withwgetorcurl, to use external codec programs likeffmpegand for piped inputs like"|sox_ng -n -p synth 1".sox_version_have_magicβlibmagicis in use to autodetect the formats of files.sox_version_have_threadsβ OpenMP support is included, so effects and channels will run in parallel ifsox_get_globals()->use_threadshas not been set tosox_false.sox_version_have_memopenβsox_open_mem_read(),sox_open_mem_write()andsox_open_memstream_write()do what it says on the box. Iffmemopen(3)is not available, they fall back tosox_open_read()andsox_open_write().
sox_uint32_t version_codeβ Version number, for example0x0E0402.char *versionβ Version string, for example,"14.4.2".char *version_extraβ Version extra info orNULL, set byPACKAGE_EXTRA, for example,"beta".char *distroβ Distro orNULL, set byDISTRO, for example,"Debian".char *compilerβ Compiler info orNULL, for example,"msvc 160040219".char *archβ Architecture-dependent feature code. For example,"1248 48 44 L OMP"where the first four digits give the sizes in bytes of char, short, long and off_t, the next two the sizes of float and double, the next two the sizes of a data pointer and a function pointer, followed byLif the machine is little-endian orBif big-endian, andOMPif multi-threading is available using OpenMP.
sox_globals_t *sox_get_globals(void)β Returns a pointer to the structure with global settings for formats and effects, containing:char *mynameβ The name the program was invoked as, for error reporting.sox_ngsets this to the bare program name without a directory path (or a.exeextension on Windows) as"sox_ng","soxi_ng","play_ng"or"rec_ng", or"sox","soxi","play"or"rec"when it replaces sox.
Other programs using libsox_ng can set it to argv[0] on startup if they wish, but it is only used by libSoX when creating error messages for lsx_fail() when there are syntax errors in effects' options and leaving it NULL is harmless.
unsigned verbosityβ Messages are output by the default output message handler ifsox_get_globals()->verbosity >= level.sox_output_message_handler_t output_message_handlerβ The address of a client-specified message output handling function of typevoid message_handler(unsigned level, char *filename, char *fmt, va_list ap)levelβ1=FAIL,2=WARN,3=INFO,4=DEBUG,5=DEBUG_MORE,6=DEBUG_MOST.filenameβ Source code__FILENAME__from which a message originates.fmtβ Message format string.apβ Message format parameters.
For examples of how to use it, see src/example3.c and demo/soxcopy.c.
sox_bool repeatableβ Use pre-determined timestamps and random number generator seed.size_t bufsizβ Default size (in bytes) used by libSoX for blocks of sample data. Plugins should use similarly-sized buffers to get best performance.size_t input_bufsizβ Default size (in bytes) used by libSoX for blocks of input sample data. Plugins should use similarly-sized buffers to get best performance.sox_int32_t ranqd1β Can be used to re-seed libSoX's random number generator.size_t log2_dft_min_sizeβ Log to base 2 of the minimum size used by libSoX for DFT filtering. Plugins should use similarly-sized DFTs to get best performance.float A4β The frequency in Hz of A above middle C, usually 440, that is used when converting note names to frequencies. iconic π
π FILE FORMAT HANDLERS π
Okay bestie, format handlers are the decode/encode girlies for audio files, and you reach them through these types and functions:
sox_format_handler_t π
This is the handler struct every format defines, and it contains:
unsigned sox_lib_version_codeβ gets checked when loading format handler plugins.char *descriptionβ a short description of the format.char **namesβ a null-terminated array of filename extensions this format handles.unsigned int flagsβ file flags, the logical OR of:SOX_FILE_NOSTDIOβ does not use stdio routines.SOX_FILE_DEVICEβ file is an audio device.SOX_FILE_PHONYβ phony file/device (for example/dev/null).SOX_FILE_REWINDβ file should be rewound to write header.SOX_FILE_BIT_REVβ is the file bit-reversed?SOX_FILE_NIB_REVβ is the file nibble-reversed?SOX_FILE_ENDIANβ is the file format endian?SOX_FILE_ENDBIGβ for an endian file format, is it big endian?SOX_FILE_MONOβ do channel restrictions allow mono?SOX_FILE_STEREOβ do channel restrictions allow stereo?SOX_FILE_QUADβ do channel restrictions allow quad?SOX_FILE_LIT_ENDβ a mask to OR in if the file is little-endian.SOX_FILE_BIG_ENDβ a mask to OR in if the file is big-endian.SOX_FILE_CHANSβ a mask to interrogate channels restrictions. Ifflags & SOX_FILE_CHANSis 0, there are no restrictions.
int (*startread)(sox_format_t *ft)β function to initialize the decoder. If it isNULL, the format cannot be read.size_t (*read)(sox_format_t *ft, sox_sample_t *buf, size_t len)β decode a block of samples.int (*stopread)(sox_format_t *ft)β close reader (decoder);NULLif no closing necessary.int (*startwrite)(sox_format_t *ft)β function to initialize the encoder. If it isNULL, the format cannot be written.size_t (*write)(sox_format_t *ft, sox_sample_t *buf, size_t len);β encode a block of sample.int (*stopwrite)(sox_format_t *ft)β close writer (decoder);NULLif no closing necessary.int (*seek)(sox_format_t *ft, sox_uint64_t offset)β reposition reader;NULLif not supported.unsigned *write_formatsβ an array of values indicating the encodings and precisions supported for writing. The data format isencoding, precision, precision, ..., 0, repeat, and end with 0. The default precision is given first. Example for a format supporting signed values at 16 and 24 bits, defaulting to 16 bits, and unsigned at 8 bits:
unsigned *formats = {
SOX_ENCODING_SIGN2, 16, 24, 0,
SOX_ENCODING_UNSIGNED, 8, 0,
0
}
sox_rate_t *write_ratesβ a null-terminated array of sample rates supported for writing;NULLif all (or almost all) rates are supported.size_t priv_sizeβ SoX automatically allocates a buffer in which the handler can store data, of this size in bytes.
sox_format_t π¦
Data passed to and from format handlers, containing:
char *filenameβ the file's name.sox_signalinfo_t signalβ signal specifications for the reader (decoder) or the writer (encoder): sample rate, number of channels, precision, length, headroom multiplier. Info will beSOX_UNSPECif the user provided no info.sox_encodinginfo_t encodingβ encoding specifications for the reader or writer.char *filetypeβ the type of the file, as determined by inspection of the file's header or bylibmagic.sox_oob_t oobβ Out-Of-Band data: comments (ID3 tags), instrument info, loop info.sox_bool seekableβ can seek on this file.sox_bool last_byte_was_zeroβ the last byte written was a zero.char modeβ read or write mode ('r'or'w').sox_uint64_t olengthβ samples*chanswritten to file.sox_uint64_t clipsβ incremented if clipping occurs.int sox_errnoβ failure error code.char sox_errstr[256]β failure error text.void *fpβ file stream pointer.sox_uint8_t *pending_bufferβ buffer of unreturned read bytes.sox_uint8_t *pending_bytesβ bytes read but not returned yet.size_t pending_countβ how many bytes read but not returned.lsx_io_type io_typeβ whether this stream is a file, a pipe, or a URL, one of:lsx_io_fileβ stream is a real file.lsx_io_pipeβ stream is a pipe.lsx_io_urlβ stream is a URL.
sox_uint64_t tell_offβ current offset within file.sox_uint64_t data_startβ offset at which sound data begins.sox_format_handler_t handlerβ the format handler for this file.void *privβ the format handler's private data area.
sox_encodinginfo_t πΈ
How samples are encoded, containing:
sox_encoding_t encodingβ the encoding used for samples.unsigned bits_per_sampleβ 0 if unknown or variable; uncompressed value if lossless; compressed value if lossy.double compressionβ compression factor (where applicable).sox_option_t reverse_bytesβ should bytes be reversed?sox_option_t reverse_nibblesβ should nibbles be reversed?sox_option_t reverse_bitsβ should bits be reversed?sox_bool opposite_endianβ if set to true, the format should reverse its default endianness.
If the reverse_ fields are sox_option_default during sox_open_read() or sox_open_write(), libSoX will set them to either sox_option_no or sox_option_yes according to the default of the machine or format.
sox_option_t β¨
The type of the sox_encodinginfo_t.reverse_* fields, one of:
sox_option_noβ option specified as no (= 0).sox_option_yesβ option specified as yes.sox_option_defaultβ option unspecified, usually implies some kind of auto-detect logic.
sox_format_tab_t *sox_get_format_fns(void) π₯
Returns a null-terminated table of information about the loaded format handlers, of which each entry contains:
char *nameβ the name of the format handler.sox_format_handler_t *(*fn)(void)β the function to call to get a pointer to the format handler's data.
sox_format_handler_t *sox_find_format(char *name, sox_bool ignore_devices) π
Finds a format handler by name and returns it, or NULL if it is not found.
sox_format_supports_encoding π
sox_bool sox_format_supports_encoding(char *path, char *filetype, sox_encodinginfo_t *encoding)
pathβ path to file to be examined (required iffiletypeisNULL).filetypeβ a previously-determined file type, orNULLto use the extension ofpath.encodingβ encoding for which the format handler should be queried.
Returns true if the format handler for the specified file type supports the specified encoding.
sox_format_handler_t sox_write_handler π«Ά
sox_format_handler_t sox_write_handler(char *path, char *filetype, char **filetype1)
Gets the format handler for a specified file type. Returns the found format handler, or NULL if not found.
pathβ path to file (required iffiletypeisNULL).filetypeβ filetype for which handler is needed, orNULLto use the extension frompath.filetype1β receives the filetype that was detected. PassNULLif not needed.
π ENCODINGS π
sox_encoding_t π
The format of encoded sample data, one of:
SOX_ENCODING_UNKNOWNβ encoding has not yet been determined.SOX_ENCODING_SIGN2β signed linear 2's compliment.SOX_ENCODING_UNSIGNEDβ unsigned linear: Sound Blaster.SOX_ENCODING_FLOATβ floating point (binary format).SOX_ENCODING_FLOAT_TEXTβ floating point (text format).SOX_ENCODING_FLACβ FLAC compression.SOX_ENCODING_HCOMβ Mac FSSD files with Huffman compression.SOX_ENCODING_WAVPACKβ WavPack with integer samples.SOX_ENCODING_WAVPACKFβ WavPack with float samples.SOX_ENCODING_ULAWβ Mu-law signed logs: US telephony.SOX_ENCODING_ALAWβ A-law signed logs: non-US telephony.SOX_ENCODING_G721β G.721 4-bit ADPCM.SOX_ENCODING_G723β G.723 3 or 5 bit ADPCM.SOX_ENCODING_CL_ADPCMβ Creative Labs 8->2,3,4 bit compressed PCM.SOX_ENCODING_CL_ADPCM16β Creative Labs 16->4 bit compressed PCM.SOX_ENCODING_MS_ADPCMβ Microsoft Compressed PCM.SOX_ENCODING_IMA_ADPCMβ IMA Compressed PCM.SOX_ENCODING_OKI_ADPCMβ Dialogic/OKI Compressed PCM.SOX_ENCODING_DPCMβ Differential PCM: Fasttracker 2 (xi format).SOX_ENCODING_DWVWβ Delta Width Variable Word.SOX_ENCODING_DWVWNβ Delta Width Variable Word N-bit.SOX_ENCODING_GSMβ GSM 6.10 33byte frame lossy compression.SOX_ENCODING_MP1β MPEG 1 Layer 1 compression.SOX_ENCODING_MP2β MPEG 1 Layer 2 compression.SOX_ENCODING_MP3β MPEG 1 Layer 3 compression.SOX_ENCODING_VORBISβ Vorbis compression.SOX_ENCODING_AMR_WBβ AMR-WB compression.SOX_ENCODING_AMR_NBβ AMR-NB compression.SOX_ENCODING_CVSDβ Continuously Variable Slope Delta.SOX_ENCODING_LPC10β Linear Predictive Coding.SOX_ENCODING_OPUSβ Opus compression.SOX_ENCODING_DSDβ Direct Stream Digital.SOX_ENCODINGSβ end of list marker.
sox_encodings_info_t *sox_get_encodings_info(void) β
Returns a pointer to a null-terminated list of available encodings, containing:
sox_encodings_flags_t flagsβ how lossy the format is, one of:sox_encodings_noneβ no flags specified (implies lossless encoding).sox_encodings_lossy1β encode, decode: lossy once.sox_encodings_lossy2β encode, decode, encode, decode: lossy twice.
char *nameβ short name for the encoding.char *descβ description of the encoding.
void sox_init_encodinginfo(sox_encodinginfo_t *e) π§
Fills in a sox_encodinginfo_t with default values.
unsigned sox_precision(sox_encoding_t encoding, unsigned bits) π
Given an encoding and the encoded bits_per_sample, returns the number of useful bits per sample in the decoded data, or 0 to indicate that the value returned by the format handler should be used instead of a pre-determined precision.
encodingβ encoding for which to lookup precision information.bitsβ the number of encoded bits per sample.
π READING AND WRITING AUDIO FILES π
okay so this is the part where the library actually touches audio files and memory buffers β the I/O era, bestie. every opener here hands you a sox_format_t *, and every one of those has to be closed with sox_close() when youβre done. no cap. π
π sox_open_read β opens a decoding session for a file
sox_format_t *sox_open_read(char *path, sox_signalinfo_t *signal,
sox_encodinginfo_t *encoding, char *filetype)
Opens a decoding session for a file. literally the βopen this file for readingβ moment β¨
pathβ Path to file to be opened (required).signalβ Information already known about audio stream, orNULLif none.encodingβ Information already known about sample encoding, orNULLif none.filetypeβ Previously-determined file type, orNULLto auto-detect.
Returns the handle for the new session, which must be closed with sox_close(), or NULL on failure. donβt lose that handle, bestie π«Ά
π¦ sox_open_mem_read β opens a decoding session for a memory buffer
sox_format_t *sox_open_mem_read(void *buffer, size_t size, sox_signalinfo_t
*signal, sox_encodinginfo_t *encoding, char *filetype)
Opens a decoding session for a memory buffer. buffer girlies rise π
bufferβ Pointer to audio data buffer (required).sizeβ Number of bytes to read from audio data buffer.signalβ Information already known about audio stream, orNULLif none.encodingβ Information already known about sample encoding, orNULLif none.filetypeβ Previously-determined file type, orNULLto auto-detect.
Returns a handle for the new session, which must be closed with sox_close(), or NULL on failure.
πΈ sox_read β reads samples from a decoding session into a sample buffer
size_t sox_read(sox_format_t *ft, sox_sample_t *buf, size_t len)
Reads samples from a decoding session into a sample buffer. the βgimme the audio dataβ function β¨
bufβ Buffer from which to read samples.lenβ Number of samples available inbuf.
Returns the number of samples decoded, or 0 on EOF or a read error. If it was the end of the file, errno will be zero, non-zero if there was a read error. iykyk π
π§ sox_seek β sets the location at which the next samples will be decoded
int sox_seek(sox_format_t *ft, sox_uint64_t offset, int whence)
Sets the location at which the next samples will be decoded. teleporting through the audio stream, lowkey β¨
offsetβ Sample offset at which to position reader.whenceβ OnlySOX_SEEK_SETis currently supported.
Returns SOX_SUCCESS if successful.
π sox_open_write β opens an encoding session for a file
sox_format_t *sox_open_write(char *path, sox_signalinfo_t *signal,
sox_encodinginfo_t *encoding, char *filetype, sox_oob_t *oob, sox_bool
(*overwrite)(char *filename))
Opens an encoding session for a file. main character energy: weβre writing now π
pathβ Path to file to be written (required).signalβ Information about desired audio stream (required).encodingβ Information about desired sample encoding, orNULLto use defaults.filetypeβ Previously-determined file type, orNULLto auto-detect.oobβ Out-of-band data to add to file, orNULLif none.overwriteβ Called if file exists to determine whether overwrite is ok.
Returns the new session handle, which must be closed with sox_close(), or NULL on failure.
π sox_open_mem_write β opens an encoding session for a memory buffer
sox_format_t *sox_open_mem_write(void *buffer, size_t size,
sox_signalinfo_t *signal, sox_encodinginfo_t *encoding, char *filetype,
sox_oob_t *oob)
Opens an encoding session for a memory buffer. writing into memory, not a file β cute and chaotic π
bufferβ A pointer to the audio data buffer that receives data (required).sizeβ The maximum number of bytes to write to audio data buffer.signalβ Information about the desired audio stream (required).encodingβ Information about the desired sample encoding, orNULLto use defaults.filetypeβ The previously-determined file type, orNULLto auto-detect.oobβ Out-of-band data to add to file, orNULLif none.
Returns the new session handle, which must be closed with sox_close(), or NULL on failure.
πͺ sox_open_memstream_write β opens an encoding session for a memstream buffer
sox_format_t *sox_open_memstream_write(char **buffer_p, size_t *size_p,
sox_signalinfo_t *signal, sox_encodinginfo_t *encoding, char *filetype,
sox_oob_t *oob)
Opens an encoding session for a memstream buffer. memstream moment, very niche, very slay π
buffer_pβ Receives pointer to audio data buffer that receives data (required).size_pβ Receives size of data written to audio data buffer (required).signalβ Information about desired audio stream (required).encodingβ Information about desired sample encoding, orNULLto use defaults.filetypeβ Previously-determined file type, orNULLto auto-detect.oobβ Out-of-band data to add to file, orNULLif none.
Returns the new session handle, which must be closed with sox_close(), or NULL on failure.
β sox_write β writes samples to an encoding session from a sample buffer
size_t sox_write(sox_format_t *ft, sox_sample_t *buf, size_t len)
Writes samples to an encoding session from a sample buffer. the actual βput the audio out thereβ function π₯
bufβ Buffer from which to read samples.lenβ Number of samples available inbuf.
Returns the number of samples encoded, or zero if a write error occurred (for the reason, check errno(3).)
π sox_close β closes an encoding or decoding session
int sox_close(sox_format_t *ft)
Closes an encoding or decoding session. Returns SOX_SUCCESS if successful. cleanup era, bestie β close your sessions π«Ά
β¨ EFFECTS AND EFFECTS CHAINS β¨
The following types and functions are how you get at libSoX's powerful effects, bestie. This is the good stuff. π
sox_effect_t
Effect information, containing:
sox_effects_globals_t *global_infoβ Global effects parameters, containing:sox_plot_t plotβ The type of plot requested, one of:sox_plot_offβ No plot.sox_plot_octaveβ Octave plot.sox_plot_gnuplotβ Gnuplot plot.sox_plot_dataβ Output the plot data as text.
sox_globals_t *global_infoβ A pointer to the associatedsox_globals_t.
sox_signalinfo_t in_signalβ Info about the incoming data stream.sox_signalinfo_t out_signalβ Info about the outgoing data stream.sox_encodinginfo_t *in_encodingβ Info about the incoming data encoding.sox_encodinginfo_t *out_encodingβ Info about the outgoing data encoding.sox_effect_handler_t handlerβ The handler for this effect.sox_uint64_t clipsβ Incremented when clipping occurs. πsize_t flowsβ 1 ifhandler.flags & SOX_EFF_MCHAN, or the number of channels otherwise.size_t flowβ Flow number.void *privβ The effect's private data area (each flow has a separate copy).
sox_signalinfo_t
Signal parameters, containing:
sox_rate_t rateβ Samples per second, 0 if unknown.unsigned channelsβ Number of sound channels, 0 if unknown.unsigned precisionβ Bits per sample, 0 if unknown.sox_uint64_t lengthβ Samples in the file (sample frames Γ channels),SOX_UNSPEC(= 0) if the actual value is not yet known,SOX_UNKNOWN_LENis used within the effects chain if the actual length is not known. Format handlers useSOX_UNSPECinstead.SOX_IGNORE_LENGTHto indicate that a format handler should ignore length information in file headers.double *multβ Effects headroom multiplier; may beNULL.
Members are SOX_UNSPEC (= 0) if the actual value is not yet known. π«Ά
sox_effect_handler_t
Effect handler information, containing:
char *nameβ The effect's name.char *usageβ One-line effect usage and parameters.char **extra_usageβ Additional lines of usage, null-terminated.unsigned int flagsβ The logical OR of:SOX_EFF_CHANβ The effect might alter the number of channels.SOX_EFF_RATEβ The effect might alter sample rate.SOX_EFF_PRECβ The effect does its own calculation of output sample precision (otherwise a default value is taken, depending on the presence ofSOX_EFF_MODIFY).SOX_EFF_LENGTHβ The effect might alter audio length (as measured in time units, not necessarily in samples).SOX_EFF_MCHANβ The effect handles multiple channels internally.SOX_EFF_NULLβ The effect does nothing and can be optimized out of the chain. πSOX_EFF_GAINβ The effect does not support gain-r.SOX_EFF_MODIFYβ The effect does not modify sample values (but it might remove or duplicate samples or insert zeros).SOX_EFF_INTERNALβ The effect is present in libSoX but is not valid for use by SoX command-line tools.
int (*getopts)(sox_effect_t *effp, int argc, char **argv)β The effect's function to parse command-line arguments, called once per effect.int (*start)(sox_effect_t *effp)β The function to initialize effect, called once per flow.int (*flow)(sox_effect_t *effp, sox_sample_t *ibuf, sox_sample_t *obuf, size_t *isamp, size_t *osamp)
int (*flow)(sox_effect_t *effp, sox_sample_t *ibuf, sox_sample_t *obuf,
size_t *isamp, size_t *osamp)
The function to process samples. β¨
ibufβ Buffer from which to read samplesobufβ Buffer to which samples are written.isampβ On entry, contains the capacity ofibufin samples; on exit, contains the number of samples consumed.osampβ On entry, contains the capacity ofobufin samples; on exit, contains the number of samples written.
int (*drain)(sox_effect_t *effp, sox_sample_t *obuf, size_t *osamp)β The function to output after input is complete. Parameters are as forflow.int (*stop)(sox_effect_t *effp)β The function to shut the effect down (called once per flow).int (*kill)(sox_effect_t *effp)β The function to shut the effect down (called once per effect).char *(*get)(sox_effect_t *effp, char *field)β Pointer to a function to read the current value of an effect's parameter, returningNULLif there is no readable parameter of that name, or a pointer to mallocked memory containing a string value sprintfed with"%g"which it is the caller's responsibility to free. i.e. you free it, not me πchar *(*set)(sox_effect_t *effp, char *field, char *value)β Pointer to a function to set the value of an effect's parameter, returningNULLif that parameter cannot be set or if the value is garbage, or a pointer to mallocked memory containing the new value sprintfed with"%g". Values outside the parameter's range set the minimum or maximum value and return that.size_t priv_sizeβ The size of the effect's private data.
sox_effects_chain_t
A chain of effects to be applied to a stream, containing:
sox_effect_t **effectsβ Table of effects to be applied to a stream.size_t lengthβ Number of effects to be applied.sox_effects_globals_t global_infoβ Copy of global effects settings.sox_encodinginfo_t *in_encβ Input encoding.sox_encodinginfo_t *out_encβ Output encoding.
The functions π
sox_effects_globals_t *sox_get_effects_globals(void)β Returns global parameters for effects.sox_effect_handler_t *sox_find_effect(char *name)β Finds the effect handler with the given name. Returns an effect pointer, orNULLif not found.sox_effect_t *sox_create_effect(sox_effect_handler_t *eh)β Creates an effect using the given handler. Returns the new effect, orNULLif not found. π¦int sox_effect_options(sox_effect_t *effp, int argc, char **argv)β Applies the command-line options to the effect. Returns the number of arguments consumed.sox_effect_handler_t *sox_get_effect_fns(void)β Returns an null-terminated array of the known effect handlers.sox_effects_chain_t *sox_create_effects_chain(sox_encodinginfo_t *in_enc, sox_encodinginfo_t *out_enc)
sox_effects_chain_t *sox_create_effects_chain(sox_encodinginfo_t *in_enc,
sox_encodinginfo_t *out_enc)
Initializes an effects chain.
in_encβ Input encoding.out_encβ Output encoding.
Returns a handle, which must be closed with sox_delete_effects_chain(). or NULL on failure. Don't forget to close it, bestie, that's a leak π«Ά
void sox_delete_effects_chain(sox_effects_chain_t *chain)β Closes an effects chain and deletes all the effects in it.int sox_add_effect(sox_effects_chain_t *chain, sox_effect_t *effp, sox_signalinfo_t *in, sox_signalinfo_t *out)
int sox_add_effect(sox_effects_chain_t *chain, sox_effect_t *effp,
sox_signalinfo_t *in, sox_signalinfo_t *out)
Adds an effect to the end of the effects chain. Returns SOX_SUCCESS if successful.
inβ Characteristics of the input signal to the chain.outβ Characteristics of the output signal from the chain.
int sox_flow_effects(sox_effects_chain_t *chain, int (*callback)(sox_bool all_done, void *data), void *data)
int sox_flow_effects(sox_effects_chain_t *chain,
int (*callback)(sox_bool all_done, void *data),
void *data)
Runs the effects chain. Returns SOX_SUCCESS if successful. π₯
chainβ Effects chain to run.callbackβ If notNULL, a pointer to a function for monitoring flow progress, called between processing each block of data. It is handed a copy of theclient_datapointer that was passed tosox_flow_effectsand if the callback returns anything other thanSOX_SUCCESS, the flow is stopped.dataβ A pointer that is pass to the callback.
sox_uint64_t sox_effects_clips(sox_effects_chain_t *chain)β Returns the number of clips that occurred while running an effects chain.sox_uint64_t sox_stop_effect(sox_effect_t *effp)β Shuts down an effect (callsstopon each of its flows) and returns the number of clips from all flows.void sox_push_effect_last(sox_effects_chain_t *chain, sox_effect_t *effp)β Adds an already-initialized effect to the end of the chain.sox_effect_t *sox_pop_effect_last(sox_effects_chain_t *chain)β Removes and returns an effect from the end of the chain. Returns the removed effect, orNULLif the chain has no effects.void sox_delete_effect(sox_effect_t *effp)β Shut down and delete an effect.void sox_delete_effect_last(sox_effects_chain_t *chain)β Shut down and delete the last effect in the chain.void sox_delete_effects(sox_effects_chain_t *chain)β Shut down and delete all effects in the chain.
πΈ EFFECT-SPECIFIC FUNCTIONS πΈ
For both of these, *effp should be a trim effect. yes, specifically trim, don't come at me π
sox_uint64_t sox_trim_get_start(sox_effect_t *effp)β Gets the sample offset of the start of the trim, useful for efficiently skipping the part that will be trimmed anyway (get trim start, seek, then clear trim start). Returns the sample offset of the start of the trim. πvoid sox_trim_clear_start(sox_effect_t *effp)β Clears the start of the trim to 0.
π¦ OUT-OF-BAND DATA π¦
sox_oob_t
Out-Of-Band data: comments, instrument info and loop info, containing:
sox_comments_t commentsβ File's metadata as comment strings inid=valueformat, accessed via thesox_*_commentsfunctions below. βsox_instrinfo_t instrβ Instrument specification, containing:signed char MIDInoteβ For unity pitch playback.signed char MIDIlowβ MIDI pitch-bend low range.signed char MIDIhiβ MIDI pitch-bend high range.unsigned char loopmodeβ Loop modes: the lower 4 bits describe the loop behaviour, one of:sox_loop_noneβ Single-shot.sox_loop_forwardβ Forward loop.sox_loop_forward_back.β Forward/back loop.
and the upper 4 bits mask the loop blass, the logical OR of
sox_loop_8β 8 loops (??). the manpage literally says (??), I'm not making this up πsox_loop_sustain_decayβ AIFF style, one sustain & one decay loop.
unsigned nloopsβ Number of active loops (maxSOX_MAX_NLOOPS= 8).
sox_loopinfo_t loops[SOX_MAX_NLOOPS];β Looping specifications, containing:sox_uint64_t startβ First sample.sox_uint64_t lengthβ Length of loop.unsigned countβ Number of repeats, 0=forever.unsigned char typeβ Bits with the same meaning asloopmodeabove.
π¬ COMMENTS π¬
Okay bestie, these are the metadata-block girlies β all about the "id=value" comment items. Let's get into it.
size_t sox_num_comments(sox_comments_t comments)β Returns the number of items in the metadata block.void sox_append_comment(sox_comments_t *comments, char *item)β Adds an item to the metadata block, in"id=value"format.void sox_append_comments(sox_comments_t *comments, char *items)β Adds a newline-separated list of"id=value"items to the metadata block, for example"id1=value1\nid2=value2".sox_comments_t sox_copy_comments(sox_comments_t comments)β Duplicates the metadata block and returns the copy.void sox_delete_comments(sox_comments_t *comments)β Frees the metadata block.char *sox_find_comment(sox_comments_t comments, char *id)β If"id=value"is found in the comments, returns a pointer to its value, orNULLif the id was not found.
π΅ PLAYLIST FUNCTIONS π΅
Playlist behavior, but make it cute. These functions check playlist types and walk through playlist entries.
sox_bool sox_is_playlist(char *filename)β Returns true if the specified file is a known playlist file type.int sox_parse_playlist(int (*callback)(void *data, char *filename), void *data, char *listname)β Parses the specified playlist file. ReturnsSOX_SUCCESSif successful.callbackβ The function to call for each item in the playlist.dataβ A pointer to client data, passed to the callback function.listnameβ The name of the playlist file.
πΉ KEYMAPS πΉ
Keymaps are the mechanism by which SoX allows you to modify effects' internal parameters on the fly by mapping keys with --keymap and then pressing them while in --interactive mode (e.g. when using play or with -d as the output filename). This is genuinely such a fun little live-tweaking moment, ngl.
void sox_keymap_add(char *key, char *effect, char *field, char op, double step)β Add a keymap.keyβ The string name of the key to map, for example"D".effectβ The name of the effect to affect."echo"will affect all echoes in the effects chain;"echo2"will only affect the second instance of it.fieldβ The name of the parameter in the effect'spriv_tto change. For effects with multiple stages,"decay"will affect the decay of all stages and"decay2"will only affect the decay of the second stage.op 10β How to affect the parameter:'+','β','*','/'or'='.step 10β How much to add or subtract from the parameter, to multiply or divide it by, or to set its value to.
extern sox_bool sox_is_keymapped(char *key)β See if a key or aneffect.fieldis used in a keymap.int sox_keymap_apply(sox_effects_chain_t *effp, char *key)β Apply a keymap. ReturnsSOX_SUCCESSon successful application,SOX_ENOEFFECTif the effect was not found in the chain or it was found but doesn't have a keymappable parameter of that name,SOX_ENOKEYMAPif the key was not mapped to anything.void sox_keymap_free(void)β Forget all keymaps.
π§° MISCELLANEOUS UTILITY FUNCTIONS π§°
Just a helpful little utility function doing basename things. Tiny but iconic.
size_t sox_basename(char *buffer, size_t len, char *filename)β Gets the basename of the specified file; for example, the basename of"/a/b/c.d"would be"c". Returns the number of characters written to the buffer, excluding the final nul character, or 0 on failure.bufferβ Buffer into which basename should be written.lenβ Size of buffer, in bytes.filenameβ Filename from which to extract basename.
𧬠GENERIC MACROS π§¬
Macro time, bestie. These are the little preprocessor besties that compute versions, integer limits, sample limits and default values. Keep the exact numbers β they are the whole point.
SOX_LIB_VERSION(a, b, c)β Compute a 32-bit integer API version from three 8-bit parts.ais the major version number,bthe minor version andcthe micro or bugfix version. Returns a 32-bit integer API version like0x000a0b0c.SOX_LIB_VERSION_CODEβ The current API version as a 32-bit integer which follows the version number of SoX.SOX_INT_MIN(bits)β Returns the smallest (most negative) value storable in a twos-complement signed integer with the specified number of bits, cast to an unsigned integer. For example,SOX_INT_MIN(8) = 0x80,SOX_INT_MIN(16) = 0x8000, etc.bitsβ The size of value for which to calculate minimum and maximum values.
SOX_INT_MAX(bits)β Returns the largest (positive) value storable in a twos-complement signed integer with the specified number of bits, cast to an unsigned integer for example,SOX_INT_MAX(8) = 0x7F,SOX_INT_MAX(16) = 0x7FFF, etc.SOX_UINT_MAX(bits)β Returns the largest value storable in an unsigned integer with the specified number of bits; for example,SOX_UINT_MAX(8) = 0xFF,SOX_UINT_MAX(16) = 0xFFFF, etc.SOX_INT8_MAXβ Returns0x7F.SOX_INT16_MAXβ Returns0x7FFF.SOX_INT24_MAXβ Returns0x7FFFFF.SOX_INT32_MAXβ Returns0x7FFFFFFF.SOX_SAMPLE_PRECISIONβ Bits in asox_sample_t(= 32).SOX_SAMPLE_MAXβ The maximum value of asox_sample_t(=0x7FFFFFFF).SOX_SAMPLE_MINβ The minimum (most negative) value of asox_sample_t(=0x80000000).SOX_SAMPLE_NEGβ The sign bit forsox_sample_t(=0x80000000).SOX_SIZE_MAXβ The maximum value of asize_t.
π SAMPLE CONVERSION π
Okay, this is the sample-conversion section and it is lowkey iconic because it's where PCM values and sox_sample_t meet. The core relationship is Linear PCM <--> sox_sample_t.
Here are all the rows from the conversion table, preserved in bullet form so nobody loses a single number:
- Float β Input Minimum
βinf;sox_sample_tMinimumβ1; Clips Iy; Clips On; Input Maximuminf;sox_sample_tMaximum1 β 5eβ10; Clips Iy; Clips On. - Int8 β Input Minimum
β128;sox_sample_tMinimumβ128; Clips In; Clips On; Input Maximum127;sox_sample_tMaximum127.9999999; Clips In; Clips Oy. - Int16 β Input Minimum
β32768;sox_sample_tMinimumβ32768; Clips In; Clips On; Input Maximum32767;sox_sample_tMaximum32767.99998; Clips In; Clips Oy. - Int24 β Input Minimum
β8388608;sox_sample_tMinimumβ8388608; Clips In; Clips On; Input Maximum8388607;sox_sample_tMaximum8388607.996; Clips In; Clips Oy. - Int32 β Input Minimum
β2147483648;sox_sample_tMinimumβ2147483648; Clips In; Clips On; Input Maximum2147483647;sox_sample_tMaximum2147483647; Clips In; Clips On.
Conversions are as accurate as possible (with rounding).
Halves are rounded toward +infinity, all others to nearest integer.
Clips shows whether or not there is the possibility of a conversion clipping to the minimum or maximum value when inputting from or outputting to a given type.
Unsigned integers are converted to and from signed integers by flipping the uppermost bit then treating them as signed integers.
Before you use the following SoX sample conversion macros in a function, SOX_SAMPLE_LOCALS declares the temporary local variables they require. For example:
sox_int16_t sox_sample_to_CD(sox_sample_t sample)
{
SOX_SAMPLE_LOCALS
unsigned clips = 0;
return SOX_SAMPLE_TO_SIGNED(16, sample, clips);
}
The following macros return a SoX native sample value and parameters are:
bitsβ The width of the resulting sample (1 through 32).dβ The value to be converted.clipsβ A variable that is incremented if the result is too big.
These macros are largely agnostic to the numeric types of their arguments.
SOX_SIGNED_TO_SAMPLE(bits, d)β Converts a signed integer of widthbitstosox_sample_t.SOX_UNSIGNED_TO_SAMPLE(bits, d)β Converts an unsigned integer of widthbitstosox_sample_t.SOX_UNSIGNED_8BIT_TO_SAMPLE(sox_uint8_t d, clips)β Converts an unsigned 8-bit integer tosox_sample_t. Theclipsparameter is not used.SOX_SIGNED_8BIT_TO_SAMPLE(sox_int8_t d, clips)β Converts a signed 8-bit integer tosox_sample_t. Theclipsparameter is not used.SOX_UNSIGNED_16BIT_TO_SAMPLE(sox_uint16_t d, clips)β Converts an unsigned 16-bit integer tosox_sample_t. Theclipsparameter is not used.SOX_SIGNED_16BIT_TO_SAMPLE(sox_int16_t d, clips)β Converts a signed 16-bit integer tosox_sample_t. Theclipsparameter is not used.SOX_UNSIGNED_24BIT_TO_SAMPLE(sox_int24_t d, clips)β Converts an unsigned 24-bit integer tosox_sample_t. Theclipsparameter is not used.SOX_SIGNED_24BIT_TO_SAMPLE(sox_int24_t d, clips)β Converts a signed 24-bit integer tosox_sample_t. Theclipsparameter is not used.SOX_UNSIGNED_32BIT_TO_SAMPLE(sox_uint32_t d, clips)β Converts an unsigned 32-bit integer tosox_sample_t. Theclipsparameter is not used.SOX_SIGNED_32BIT_TO_SAMPLE(sox_int32_t d, clips)β Converts a signed 32-bit integer tosox_sample_t. Theclipsparameter is not used.SOX_FLOAT_32BIT_TO_SAMPLE(float d, clips)β Converts a 32-bit float tosox_sample_t.SOX_FLOAT_64BIT_TO_SAMPLE(double d, clips)β Converts a 64-bit float tosox_sample_t.SOX_SAMPLE_TO_UNSIGNED(bits, sox_sample_t samp, clips)β Converts a SoX native sample to an unsigned integer of widthbits.SOX_SAMPLE_TO_SIGNED(bits, sox_sample_t samp, clips)β Converts a SoX native sample to a signed integer of widthbits.SOX_SAMPLE_TO_UNSIGNED_8BIT(sox_sample_t samp, clips)β Converts a SoX native sample to an unsigned 8-bit integer.SOX_SAMPLE_TO_SIGNED_8BIT(sox_sample_t samp, clips)β Converts a SoX native sample to a signed 8-bit integer.SOX_SAMPLE_TO_UNSIGNED_16BIT(sox_sample_t samp, clips)β Converts a SoX native sample to an unsigned 16-bit integer.SOX_SAMPLE_TO_SIGNED_16BIT(sox_sample_t samp, clips)β Converts a SoX native sample to a signed 16-bit integer.SOX_SAMPLE_TO_UNSIGNED_24BIT(sox_sample_t samp, clips)β Converts a SoX native sample to an unsigned 24-bit integer.SOX_SAMPLE_TO_SIGNED_24BIT(sox_sample_t samp, clips)β Converts a SoX native sample to a signed 24-bit integer.SOX_SAMPLE_TO_UNSIGNED_32BIT(sox_sample_t samp, clips)β Converts a SoX native sample to an unsigned 32-bit integer. Theclipsparameter is not used.SOX_SAMPLE_TO_SIGNED_32BIT(sox_sample_t samp, clips)β Converts a SoX native sample to a signed 32-bit integer. Theclipsparameter is not used.SOX_SAMPLE_TO_FLOAT_32BIT(sox_sample_t samp, clips)β Converts a SoX native sample to a 32-bit float.SOX_SAMPLE_TO_FLOAT_64BIT(sox_sample_t samp, clips)β Converts a SoX native sample to a 64-bit float. Theclipsparameter is not used.SOX_SAMPLE_CLIP_COUNT(d, clips)β Clips a value of a type that is larger thansox_sample_t(for example,sox_int64_t) tosox_sample_t's limits and increment a counter if clipping occurs.dis the value (an lvalue) to be clipped, updated as necessary.SOX_ROUND_CLIP_COUNT(d, clips)β Clips a value of a type that is larger thansox_sample_t(for example,sox_int64_t) tosox_sample_t's limits and increment a counter if clipping occurs. Returns the clipped value.SOX_INTEGER_CLIP_COUNT(bits, d, clips)β Clips a value to the limits of a signed integer of the specified width and increment a counter if clipping occurs. Returns the clipped value.SOX_16BIT_CLIP_COUNT(d, clips)β Clips a value to the limits of a 16-bit signed integer and increment a counter if clipping occurs. Returns the clipped value.SOX_24BIT_CLIP_COUNT(d, clips)β Clips a value to the limits of a 24-bit signed integer and increments a counter if clipping occurs. Returns the clipped value.SOX_DEFAULT_CHANNELSβ The default channel count is 2 (stereo).SOX_DEFAULT_RATEβ The default sample rate is 48000Hz.SOX_DEFAULT_PRECISIONβ The default precision is 16 bits per sample.SOX_DEFAULT_ENCODINGβ The default encoding isSOX_ENCODING_SIGN2(linear 2's complement PCM).
π LINKING
Okay bestie, so how you link against libsox_ng is genuinely going to depend on how SoX got built on your system β it's giving "it depends" energy and honestly that's just how it is π
- Static build β easy, iconic, no drama: just link against the library.
- Dynamic build β here's where you pull out
libtoolso you get the correct linker flags. Lowkey the libtool manual has all the details, but basically you use it like this:
libtool --mode=link gcc -o prog /path/to/libsox_ng.la
π¦ That's the whole vibe check. Link flags slayed, program compiled, periodt.
Β©οΈ COPYRIGHT
Copyright 1991-2015 Lance Norskog, Chris Bagwell and sundry contributors.
This library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
π ngl bestie, that all-caps section is not me being dramatic, it's giving legally load-bearing. No warranty fr fr β the license literally says so. Read the GNU General Public License for the full tea π
π AUTHORS
The many authors and contributors are listed in the README file that is distributed with the source code.
Like, many. A whole squad π We don't list them all here, the README has the receipts.
π SEE ALSO
sox_ng(1)libsoxeffect_ng(3)soxformat_ng(7)src/example*.canddemo/*.cin the SoX source distribution
β¨ Go poke at those example files, they're lowkey the best way to see this library in its main character era.
and that's a wrap on the whole page, bestie!! π You just read the SoX manual page LIBSOX_NG(3), from the August 11, 2024 edition of the SoX source. Go build something iconic with it ππ«Ά
π¦ where this page comes from
- arch rolling: sox 14.8.0.1-1 Β· GPL-2.0-only
- nixpkgs unstable: sox 14.8.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms