SoX(3)
π NAME π
libsox β SoX, an audio file-format and effect library. This is the SoX(3) page from Sound eXchange, bestie, and it's giving "the library running the whole show backstage." β¨
π SYNOPSIS π
Okay so here's the include-and-call situation. Every signature below is exactly as it appears β keep the spellings, keep the types, keep the energy. π
#include <sox.h>
int sox_format_init(void);
void sox_format_quit(void);
sox_format_t sox_open_read(const char *path, const sox_signalinfo_t *info, const char *filetype);
sox_format_t sox_open_write(sox_bool (*overwrite_permitted)(const char *filename), const char *path, const sox_signalinfo_t *info, const char *filetype, const char *comment, sox_size_t length, const sox_instrinfo_t *instr, const sox_loopinfo_t *loops);
sox_size_t sox_read(sox_format_t ft, sox_ssample_t *buf, sox_size_t len);
sox_size_t sox_write(sox_format_t ft, sox_ssample_t *buf, sox_size_t len);
int sox_close(sox_format_t ft);
int sox_seek(sox_format_t ft, sox_size_t offset, int whence);
sox_effect_handler_t const *sox_find_effect(char const *name);
sox_effect_t *sox_create_effect(sox_effect_handler_t const *eh);
int sox_effect_options(sox_effect_t *effp, int argc, char * const argv[]);
sox_effects_chain_t *sox_create_effects_chain(sox_encodinginfo_t const *in_enc, sox_encodinginfo_t const *out_enc);
void sox_delete_effects_chain(sox_effects_chain_t *ecp);
int sox_add_effect(sox_effects_chaint_t *chain, sox_effect_t*effp, sox_signalinfo_t *in, sox_signalinfo_t const *out);
cc file.c -o file -lsox
π¦ DESCRIPTION π¦
libsox is a library of sound sample file format readers/writers and sound effects processors. It's mainly developed for use by SoX, but it's useful for any sound application. Slay. π
sox_format_initβ performs some required initialization related to all file format handlers. If compiled with dynamic library support, then this will detect and initialize all external libraries. This should be called before any other file operations are performed. Do it first, bestie. βsox_format_quitβ performs some required cleanup related to all file format handlers. Cleanup era. π§Ήsox_open_inputβ opens the file for reading whose name is the string pointed to bypathand associates ansox_format_twith it. Ifinfois non-NULLthen it will be used to specify the data format of the input file. This is normally only needed for headerless audio files since the information is not stored in the file. Iffiletypeis non-NULLthen it will be used to specify the file type. If this is not specified then the file type is attempted to be derived by looking at the file header and/or the filename extension. A special name of"-"can be used to read data from stdin. Yes, that dash is iconic. πsox_open_outputβ opens the file for writing whose name is the string pointed to bypathand associates ansox_format_twith it. Ifinfois non-NULLthen it will be used to specify the data format of the output file. Since most file formats can write data in different data formats, this generally has to be specified. Theinfostructure from the input format handler can be specified to copy data over in the same format. Ifcommentis non-NULL, it will be written in the file header for formats that support comments. Iffiletypeis non-NULLthen it will be used to specify the file type. If this is not specified then the file type is attempted to be derived by looking at the filename extension. A special name of"-"can be used to write data to stdout. Output go brrr. πsox_readβ readslensamples in tobufusing the format handler specified byft. All data read is converted to 32-bit signed samples before being placed in tobuf. The value oflenis specified in total samples.
π¨ If its value is not evenly divisable by the number of channels, undefined behavior will occur. Not a vibe. Do not do this. π
sox_writeβ writeslensamples frombufusing the format handler specified byft. Data inbufmust be 32-bit signed samples and will be converted during the write process. The value oflenis specified in total samples.
π¨ If its value is not evenly divisable by the number of channels, undefined behavior will occur. Same warning, same stakes, bestie. π
sox_closeβ dissociates the namedsox_format_tfrom its underlying file or set of functions. If the format handler was being used for output, any buffered data is written first. Flush it and close it. π«Άsox_find_effectβ finds effectname, returning a pointer to itssox_effect_handler_tif it exists, andNULLotherwise. Search result realness. πsox_create_effectβ instantiates an effect into asox_effect_tgiven asox_effect_handler_t *. Any missing methods are automatically set to the corresponding nothing method. Autofill but make it Unix. β¨sox_effect_optionsβ allows passing options into the effect to control its behavior. It will returnSOX_EOFif there were any invalid options passed in. On success,effp->in_signalwill optionally contain the rate and channel count it requires input data from, andeffp->out_signalwill optionally contain the rate and channel count it outputs in. When present, this information should be used to make sure appropriate effects are placed in the effects chain to handle any needed conversions. That's the plumbing, okay? π
β οΈ Passing in options is currently only supported when they are passed in before the effect is ever started. The behavior is undefined if its called once the effect is started. Do not test fate. π
sox_create_effects_chainβ will instantiate an effects chain that effects can be added to.in_encandout_encare the signal encoding of the input and output of the chain respectively. The pointers toin_encandout_encare stored internally and so their memory should not be freed. Also, it is OK if their values change over time to reflect new input or output encodings as they are referenced only as effects start up or are restarted. Do not free those pointers, bestie. π«Άsox_delete_effects_chainβ will release any resources reserved during the creation of the chain. This will also callsox_delete_effectsif any effects are still in the chain. Cleanup with consequences. π§sox_add_effectβ adds an effect to the chain.inspecifies the input signal info for this effect.outis a suggestion as to what the output signal should be but depending on the effects given options and oninthe effect can choose to do differently. Whatever output rate and channels the effect does produce are written back toin. It is meant thatinbe stored and passed to each new call tosox_add_effectso that changes will be propagated to each new effect. Pass thein, keep the chain moving. π₯
SoX includes skeleton C files to assist you in writing new formats (skelform.c) and effects (skeleff.c). Note that new formats can often just deal with the header and then use raw.c's routines for reading and writing. Skeleton files are your besties. π
example0.c and example1.c are a good starting point to see how to write applications using libsox. sox.c itself is also a good reference. Read the examples, queen. π
π RETURN VALUE π
Upon successful completion sox_open_input and sox_open_output return an sox_format_t (which is a pointer). Otherwise, NULL is returned.
TODO: Need a way to return reason for failures. Currently, relies on sox_warn to print information. π
sox_read and sox_write return the number of samples successfully read or written. If an error occurs, or the end-of-file is reached, the return value is a short item count or SOX_EOF.
TODO: sox_read does not distiguish between end-of-file and error. Need an feof() and ferror() concept to determine which occured. π
Upon successful completion sox_close returns 0. Otherwise, SOX_EOF is returned.
β οΈ In either case, any further access (including another call to sox_close()) to the handler results in undefined behavior.
TODO: Need a way to return reason for failures. Currently, relies on sox_warn to print information. π«
Upon successful completion sox_seek returns 0. Otherwise, SOX_EOF is returned.
TODO Need to set a global error and implement sox_tell. π
π ERRORS π
TODO
πΈ INTERNALS πΈ
SoX's formats and effects operate with an internal sample format of signed 32-bit integer. The data processing routines are called with buffers of these samples, and buffer sizes which refer to the number of samples processed, not the number of bytes. File readers translate the input samples to signed 32-bit integers and return the number of samples read. For example, data in linear signed byte format is left-shifted 24 bits. Bits are shifting, literally. β
Representing samples as integers can cause problems when processing the audio. For example, if an effect to mix down left and right channels into one monophonic channel were to use the line
*obuf++ = (*ibuf++ + *ibuf++)/2;
distortion might occur since the intermediate addition can overflow 32 bits. The line
*obuf++ = *ibuf++/2 + *ibuf++/2;
would get round the overflow problem (at the expense of the least significant bit). Math said "careful." π
Stereo data is stored with the left and right speaker data in successive samples. Quadraphonic data is stored in this order: left front, right front, left rear, right rear. Channel order is not a suggestion. π
π¦ FORMATS π¦
A format is responsible for translating between sound sample files and an internal buffer. The internal buffer is stored in signed longs with a fixed sampling rate. The format operates from two data structures: a format structure, and a private structure. Two structures, one mission. β¨
The format structure contains a list of control parameters for the sample: sampling rate, data size (8, 16, or 32 bits), encoding (unsigned, signed, floating point, etc.), number of sound channels. It also contains other state information: whether the sample file needs to be byte-swapped, whether sox_seek() will work, its suffix, its file stream pointer, its format pointer, and the private structure for the format. All the little details are in there. π
The private area is just a preallocated data array for the format to use however it wishes. It should have a defined data structure and cast the array to that structure. See voc.c for the use of a private data area. voc.c has to track the number of samples it writes and when finishing, seek back to the beginning of the file and write it out. The private area is not very large. The echo effect has to malloc() a much larger area for its delay line buffers. Budget private area, big echo energy. π
The 6 routines
A format has 6 routines: six little icons, one format. β
startreadβ Set up the format parameters, or read in a data header, or do what needs to be done. Get ready. πreadβ Given a buffer and a length: read up to that many samples, transform them into signed long integers, and copy them into the buffer. Return the number of samples actually read. Read, convert, return. β¨stopreadβ Do what needs to be done. Wrap it up. π«Άstartwriteβ Set up the format parameters, or write out a data header, or do what needs to be done. Get ready to write. πwriteβ Given a buffer and a length: copy that many samples out of the buffer, convert them from signed longs to the appropriate data, and write them to the file. If it can't write out all the samples, fail. All or fail, no half-sends. πstopwriteβ Fix up any file header, or do what needs to be done. Final header touch-up era. π
π EFFECTS π
Okay so each effect runs with one input and one output stream β one in, one out, no cap. An effectβs implementation is made of six functions, and the call flow is literally this:
LOOP (invocations with different parameters)
getopts
LOOP (invocations with the same parameters)
LOOP (channels)
start
LOOP (whilst there is input audio to process)
LOOP (channels)
flow
LOOP (whilst there is output audio to generate)
LOOP (channels)
drain
LOOP (channels)
stop
kill
Notes bestie:
- For some effects, some of the functions may not be needed and can be
NULL. - An effect that is marked
MCHANdoes not use theLOOP (channels)lines and must therefore perform multiple channel processing inside the affected functions. - Multiple effect instances may be processed (according to the above flow diagram) in parallel.
getoptsβ gets called with a character string argument list for the effect.startβ gets called with the signal parameters for the input and output streams.flowβ gets called with input and output data buffers, and (by reference) the input and output data buffer sizes. It processes the input buffer into the output buffer, and sets the size variables to the numbers of samples actually processed. It is under no obligation to read from the input buffer or write to the output buffer during the same call. If the call returnsSOX_EOFthen this should be used as an indication that this effect will no longer read any data and can be used to switch to drain mode sooner.drainβ gets called after there are no more input data samples. If the effect wishes to generate more data samples it copies the generated data into a given buffer and returns the number of samples generated. If it fills the buffer, it will be called again, etc. The echo effect uses this to fade away.stopβ gets called when there are no more input samples and no more output samples to process. It is typically used to release or close resources (e.g. allocated memory or temporary files) that were set-up instart. Seeecho.cfor an example.killβ gets called to allow resources allocated bygetoptsto be released. Seepad.cfor an example.
π LINKING π
The method of linking against libsox depends on how SoX was built on your system. For a static build, just link against the libraries as normal. For a dynamic build, you should use libtool to link with the correct linker flags. See the libtool manual for details; basically, you use it as:
libtool --mode=link gcc -o prog /path/to/libsox.la
π BUGS π
Real talk, bestie: this manual page is both incomplete and out of date.
π SEE ALSO π
sox(1)soxformat(7)example*.cin the SoX source distribution.
βοΈ LICENSE βοΈ
Copyright 1998-2011 by Chris Bagwell and SoX Contributors.
Copyright 1991 Lance Norskog and Sundry Contributors.
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1, or (at your option) any later version.
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 Lesser General Public License for more details.
π AUTHORS π
Chris Bagwell (cbagwell@users.sourceforge.net). Other authors and contributors are listed in the ChangeLog file that is distributed with the source code.
β¨ Thatβs the whole manual, bestie β source/version libsox, dated February 19, 2011, and itβs the SoX(3) page. bye! ππ
π¦ where this page comes from
- alpine 3.24: sox-doc 14.4.2-r14 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- epel 10: sox-devel 14.4.2.0-42.el10_1 Β· GPL-2.0-or-later AND LicenseRef-Callaway-LGPLv2+ AND LicenseRef-Callaway-MIT
- epel 9: sox-devel 14.4.2.0-32.el9 Β· GPLv2+ and LGPLv2+ and MIT
- fedora 43: sox-devel 14.4.2.0-43.fc43 Β· GPL-2.0-or-later AND LicenseRef-Callaway-LGPLv2+ AND LicenseRef-Callaway-MIT
- fedora 43: sox-devel 14.4.2.0-43.fc43 Β· GPL-2.0-or-later AND LicenseRef-Callaway-LGPLv2+ AND LicenseRef-Callaway-MIT
- fedora 44: sox-devel 14.4.2.0-44.fc44 Β· GPL-2.0-or-later AND LicenseRef-Callaway-LGPLv2+ AND LicenseRef-Callaway-MIT
- fedora 44: sox-devel 14.4.2.0-44.fc44 Β· GPL-2.0-or-later AND LicenseRef-Callaway-LGPLv2+ AND LicenseRef-Callaway-MIT
- opensuse-leap 16.0: sox-devel 14.4.2-bp160.1.13 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- opensuse-leap 16.0: sox-devel 14.4.2-bp160.1.13 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- opensuse-leap 16.0: sox-devel 14.4.2-bp160.1.13 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- opensuse-leap 16.0: sox-devel 14.4.2-bp160.1.13 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms