shithub: sox

ref: 018886f3f3bb452a480bb6c4e833dce14562f9fe
dir: /libst.3/

View raw version
.de Sh
.br
.ne 5
.PP
\fB\\$1\fR
.PP
..
.de Sp
.if t .sp .5v
.if n .sp
..
.TH ST 3 "September 26 2005"
.SH NAME
libst \- Sound Tools : sound sample file and effects libraries.
.SH SYNOPSIS
.B #include <st.h>
.P
.B ft_t st_open_input(const char *\fIpath\fB, const st_signalinfo_t *\fIinfo\fB, const char *\fIfiletype\fB);
.P
.B ft_t st_open_output(const char *\fIpath\fB, const st_signalinfo_t *\fIinfo\fB, const char *\fIfiletype\fB, const char *\fIcomment\fB);
.P
.B st_ssize_t st_read(ft_t \fIft\fB, st_sample_t *\fIbuf\fB, st_ssize_t \fIlen\fB);
.P
.B st_ssize_t st_write(ft_t \fIft\fB, st_sample_t *\fIbuf\fB, st_ssize_t \fIlen\fB);
.P
.B int st_close(ft_t \fIft\fB);
.P
.B int st_seek(ft_t \fIft\fB, st_size_t \fIoffset\fB, int \fIwhence\fB);
.P
.B cc \fIfile.c\fB -o \fIfile \fBlibst.a
.SH DESCRIPTION
.I Sound\ Tools
is a library of sound sample file format readers/writers
and sound effects processors.  It is mainly developed for use by SoX but is useful for any sound application.
.P
\fBst_open_input\fR function opens the file for reading whose name is the string pointed to by \fIpath\fR and associates an ft_t with it. If \fIinfo\fR is non-NULL then 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.  If \fIfiletype\fR is non-NULL then 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.
.P
\fBst_open_output\fR function opens the file for writing whose name is the string pointed to by \fIpath\fR and associates an ft_t with it.  If \fIinfo\fR is non-NULL then 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.  The info structure from the input format handler can be specified to copy data over in the same format.  If \fIcomment\fR is non-NULL, it will be written in the file header for formats that support comments. If \fIfiletype\fR is non-NULL then 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.
.P
The function \fBst_read\fR reads \fIlen\fR samples in to \fIbuf\fR using the format handler specified by \fIft\fR.  All data read is converted to 32-bit signed samples before being placed in to \fIbuf\fR.  The value of \fIlen\fR is specified in total samples.  If its value is not evenly divisable by the number of channels, undefined behavior will occur.
.P
The function \fBst_write\fR writes \fIlen\fR samples from \fIbuf\fR using the format handler specified by \fIft\fR.  Data in \fIbuf\fR must be 32-bit signed samples and will be converted during the write process.  The value of \fIlen\fR is specified in total samples.  If its value is not evenly divisable by the number of channels, undefined behavior will occur.
.P
The \fBst_close\fR function dissociates the named \fIft_t\fR from its underlying file or set of functions.  If the format handler was being used for output, any buffered data is written first.
.P
Sound Tools includes skeleton C
files to assist you in writing new formats and effects.  
The full skeleton driver, skel.c, helps you write drivers 
for a new format which has data structures.  
The simple skeleton drivers
help you write a new driver for raw (headerless) formats, or
for formats which just have a simple header followed by raw data.
.SH RETURN VALUE
Upon successful completion \fBst_open_input\fR and \fBst_open_output\fR return a ft_t (which is a pointer).  Otherwise, NULL is returned.  TODO: Need a what to return reason for failures.  Currently, relies on st_warn to print information.
.P
\fBst_read\fR and \fBst_write\fR 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 ST_EOF. TODO: \fBst_read\fR does not distiguish between end-of-ifle and error.  Need an feof() and ferror() concept to determine which occured.
.P
Upon successful completion \fBst_close\fR returns 0.  Otherwise, ST_EOF is returned.  In either case, any further access (including another call to \fBst_close\fR()) to the handler results in undefined behavior. TODO: Need a way to return reason for failures.  Currently, relies on st_warn to print information.
.P
Upon successful completion \fBst_seek\fR returns 0.  Otherwise, ST_EOF is returned.  TODO Need to set a global error and implement st_tell.
.SH ERRORS
TODO
.SH INTERNALS
The Sound Tools formats and effects operate on an internal buffer format
of signed 32-bit longs.
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.
.P
This does cause problems in processing the data.  
For example:
.br
	*obuf++ = (*ibuf++ + *ibuf++)/2;
.br
would
.I not
mix down left and right channels into one monophonic channel,
because the resulting samples would overflow 32 bits.
Instead, the ``avg'' effects must use:
.br
	*obuf++ = *ibuf++/2 + *ibuf++/2;
.br
.P
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.
.SH FORMATS
A 
.I format 
is responsible for translating between sound sample files
and an internal buffer.  The internal buffer is store in signed longs
with a fixed sampling rate.  The 
.I format
operates from two data structures:
a format structure, and a private structure.
.P
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 st_seek() will work, its suffix,
its file stream pointer, its 
.I format
pointer, and the 
.I private
structure for the 
.I format .
.P
The 
.I private 
area is just a preallocated data array for the 
.I 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.
.P
A 
.I format
has 6 routines:
.TP 20
startread
Set up the format parameters, or read in
a data header, or do what needs to be done.
.TP 20
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.
.TP 20
stopread
Do what needs to be done.
.TP 20
startwrite
Set up the format parameters, or write out 
a data header, or do what needs to be done.
.TP 20
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.
.TP 20
stopwrite
Fix up any file header, or do what needs to be done.
.SH EFFECTS
An effects loop has one input and one output stream.
It has 5 routines.
.TP 20
getopts
is called with a character string argument list for the effect.
.TP 20
start
is called with the signal parameters for the input and output
streams.
.TP 20 
flow
is 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 returns ST_EOF then 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.
.TP 20 
drain
is 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.
.TP 20
stop
is called when there are no more input samples to process.
.I stop
may generate output samples on its own.
See echo.c for how to do this, 
and see that what it does is absolutely bogus.
.SH BUGS
The HCOM format is not re-entrant; it can only be used once in a program.
.P
On errors, the effects currently invoke st_fail and rely on that
calling exit().  They do not currently gracefully fail.
.P
The program/library interface is pretty weak.