History log of /openbsd-current/usr.bin/aucat/aucat.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.182 22-Mar-2024 ratchov

aucat: Allow any device sample encoding.

If the device doesn't support the aucat internal encoding,
then setup a conversion layer instead of failing. This allows
aucat to be used for audio equipment testing/debugging
without involving the full sndiod processing chain.


# 1.181 22-Mar-2024 ratchov

aucat: Add a bytes-per-sample argument to allcobuf()

No behavior change.


# 1.180 20-Mar-2024 ratchov

aucat: Add generic channel mapping in place of -j and -c options.

The argument to the -m option specifies the source and destination
channel ranges to be mapped. Compatibility is maintained: if -m
is not used, the -c and -j options still work.

Help and suggestions from Jan Stary <hans@stare.cz>, thanks.


Revision tags: OPENBSD_7_5_BASE
# 1.179 01-Feb-2024 ratchov

aucat: Fix MIDI control of the levels of individual files

Change the order of slot_list to match the order of the -io options on
the command-line and fix the way slot_list searched using the MIDI
channel number of the CC-07 message.

Reported and analysed by Dirk-Wilhelm Peters <peters at schwertfisch.de>

Thanks!


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE OPENBSD_7_3_BASE OPENBSD_7_4_BASE
# 1.178 07-Mar-2022 ratchov

Switch internal sample representation to 24-bit fixed-point

Note that this also changes the default file encoding to 24-bit.


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.177 12-Jan-2021 naddy

const-ify lookup tables; ok ratchov@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.176 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.175 19-May-2019 ratchov

Fix integer overflow in block size calculation.

Fixes truncation of contents of files with rate above 64kHz
generate in off-line mode (i.e. -n option).


Revision tags: OPENBSD_6_5_BASE
# 1.174 21-Feb-2019 ratchov

Use reallocarray in place of malloc(a * b) to handle possible
overflow in multiplication of malloc argument.


Revision tags: OPENBSD_6_4_BASE
# 1.173 13-May-2018 ratchov

Add missing pledge(). From Jesper Wallin <jesper at ifconfig.se>.
Thanks!


Revision tags: OPENBSD_6_3_BASE
# 1.172 07-Nov-2017 ratchov

prefill with silence the buffer where cmap_copy() stores samples
rather than the file buffer. Fixes initialized data being treated as
recorded samples when resampling or format conversions are involved
but mono->stereo conversion is disabled.


# 1.171 03-Nov-2017 ratchov

Prefill rec buffer with silence. This is necessary because certain
channels don't get samples, for instance when mono->stereo conversion
is disabled.


Revision tags: OPENBSD_6_2_BASE
# 1.170 20-Jul-2017 ratchov

Fix integer overflow that would causes >12 hours files
to not properly relocate. Found with coverity.


Revision tags: OPENBSD_6_1_BASE
# 1.169 03-Jan-2017 ratchov

Assert we're not freeing buffers we didn't allocate (DEBUG mode).


# 1.168 03-Jan-2017 ratchov

Log play volume together with other play parameters.


# 1.167 30-Sep-2016 ratchov

Set the device rate to the calculated rate. Otherwise, the signal
may endup resampled twice.


# 1.166 30-Sep-2016 ratchov

Remove unnecessary includes. From Michael W. Bombardieri <mb at ii.net>.


# 1.165 27-Sep-2016 ratchov

Don't rely on the resampling code to calculate the number of samples
to process, as it may produce one extra sample (to handle accumulation
of fractional samples), which would cause access to one sample past
the end of the buffer and crash aucat. Fix this by limiting the number
of samples processed to a single block.

Found by and help from Michael W. Bombardieri <mb at ii.net>. Thanks.


Revision tags: OPENBSD_6_0_BASE
# 1.164 07-Jun-2016 ratchov

Add -g and -p options to control device and file start position.


# 1.163 07-Jun-2016 ratchov

Add resamp_getcnt() routine to calculate the exact number of samples
that would be consumed and produced by the sampler rate converter. Use
it to avoid partial samples that are not properly handled. Fixes
last samples of certain files causing aucat to abort.


# 1.162 02-Jun-2016 ratchov

remove forgotten debug printfs


# 1.161 02-Jun-2016 ratchov

set exit status to 1 if -n is used without -i and -o


# 1.160 31-May-2016 ratchov

Pass hr:min:sec:frame.cent to dev_mmcloc() directly and remove the
useless intermediate conversion from and to 2400-th of seconds.


# 1.159 31-May-2016 ratchov

To store MMC position, use samples (at device rate) as time units
instead of 2400-ths of second. No behaviour change.


# 1.158 28-May-2016 ratchov

Fix file block size rounding and ensure it's large enough to store a
full audio block.


# 1.157 27-May-2016 ratchov

When resampling, use the exact resampling factor instead of the ratio
between input and output block sizes. This was inherited from sndiod,
but is not required for files because they are continuous streams of
samples and do not need to be split in blocks of equal duration.

This change makes playback/recording rate match exactly the requested
sample rate.


# 1.156 27-May-2016 ratchov

Flush rec buffer if there's less than one block space left and refill
play buffer if there's less than one block of data left. This is the
correct condition in the general case. No behaviour change, as all
input/output is multiple of the block size.


# 1.155 27-May-2016 ratchov

Simplify slot_fill() and slot_flush(). No behaviour change


# 1.154 27-May-2016 ratchov

Make resamp_do() get the exact number input and output samples and
provide routines to calculate them. This way we don't rely on it
to calculate the bytes procuded/consumed anymore. No behaviour change.


# 1.153 26-May-2016 ratchov

Make format conversion routines return the number of frames
consumed from both input and output buffers. No behaviour change.


Revision tags: OPENBSD_5_9_BASE
# 1.152 29-Jan-2016 ratchov

Check that byte order is native only if we're using more than one
byte per sample, check that samples are lsb-aligned only if there's
padding. Fixes unsupported parameters not being detected if compiled
in 24-bit mode and sndiod is not running.


# 1.151 10-Jan-2016 ratchov

trailing spaces, fit in 80 columns


# 1.150 09-Nov-2015 mmcc

Remove xfree(). From Michael W. Bombardieri.

ok ratchov@


# 1.149 27-Aug-2015 ratchov

fix typos in comments and error messages


Revision tags: OPENBSD_5_8_BASE
# 1.148 16-May-2015 ratchov

Call slot_stop() after the play buffer is drained to properly cleanup
the slot. Fixes periodic glitches occurring after eof is reached, and
then playback restarted with a MMC-relocate and MMC-start.


Revision tags: OPENBSD_5_7_BASE
# 1.147 21-Jan-2015 jmc

fix macro breakage and update usage();


# 1.146 21-Jan-2015 ratchov

Simplify internals and rewrite file header parsing code:
- remove -M, -t, -w, -C, -x flags that don't make sense anymore
- make "-j off" the default (sndiod already does the job)
- don't limit the number of played/recorded files.
- add support for floating-point encoded files.
- add support for apple .aiff and sun/next .au files


# 1.145 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.144 18-Nov-2013 ratchov

remove unused variables


# 1.143 18-Nov-2013 ratchov

add missing prototypes


# 1.142 12-Nov-2013 ratchov

remove bogus legacy server-specific bits that cause confusion


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.141 03-Dec-2012 ratchov

Remove server capability description from the documentation and from
the usage string, and just say that for historic reasons aucat behaves
like sndiod.
with help from jmc@


# 1.140 23-Nov-2012 ratchov

Since there's no sndiod -> aucat link anymore, allow aucat to be run
in server mode even when not invoked as sndiod. This allows aucat and
the new sndiod daemon to live together in /usr/bin, which makes easy
to quickly compare sndiod and aucat (and spot possible regressions)
without having to modify /usr/bin.


# 1.139 27-Oct-2012 ratchov

don't stat & test sndio home directory since we never use it.


# 1.138 27-Oct-2012 ratchov

don't (re-)define SNDIO_USER and SNDIO_PRIO if they are already defined,
this way they can be defined at compilation time


Revision tags: OPENBSD_5_2_BASE
# 1.137 27-Jun-2012 ratchov

Uniformize error messages printing, use err() instead of fprintf()
when appropriate. From Michael W. Bombardieri <mb at ii.net>


# 1.136 19-Jun-2012 ratchov

Remove hidden options that are not needed any more,
from Remco <remco at d-compu.dyndns.org>. Thanks


# 1.135 23-May-2012 ratchov

Make the "default" string a valid device name that has the same effect
as NULL has. This will (hopefully) simplify ports where the user
passes the device string.


# 1.134 11-Apr-2012 ratchov

Use "unsigned int" rather than "unsigned". No object change.
suggested by deraadt@


Revision tags: OPENBSD_5_1_BASE
# 1.133 09-Feb-2012 ratchov

Crank the default buffer size to 160ms (ie 7680 frames at 48kHz).
Workarounds stuttering caused by something hogging the cpu in kernel
mode and preventing sndiod from running at time.
ok deraadt


# 1.132 26-Jan-2012 ratchov

Make the default rate 48kHz and the default block size 10ms. These
settings ensure video players and programs using MTC are smooth by
default. Thanks to all who tested.


# 1.131 10-Jan-2012 ratchov

When a sub-device is created (-s), check if a hardware
device was already created (-f) before trying to use the
default one, otherwise all sub-devices end up attached
to the default device. Noticed by peters at schwertfisch.de,
thanks!


# 1.130 09-Dec-2011 ratchov

fix usage string: remove -M from aucat and -l from aucat and sndiod
from jmc


# 1.129 09-Dec-2011 ratchov

add a new sndiod audio/midi daemon and hide aucat server-specific
options.


# 1.128 02-Dec-2011 ratchov

don't use a "server" flag, just check if we expose sub-devices


# 1.127 02-Dec-2011 ratchov

reuse midi-control code to implement midi thru boxes and remove
the old midithru implementation; less code, less bugs. As a side
effect, midi output doesn't implement running status "compression"
any more.


# 1.126 20-Nov-2011 ratchov

Move mmc/mtc and volume control bits from struct aproc to struct
dev. Allows volume settings to be saved while the device is kept
closed. Besides that, no behabiour changes.


# 1.125 15-Nov-2011 ratchov

Add a "device number" component in sndio(7) device names, allowing a
single aucat instance to handle all audio and MIDI services. Since
this partially breaks compatibility, this is a opportunitiy to fix few
other design mistakes (eg ':' being used by inet6, type name vs api
name confusion, etc..). This leads to the following names:

type[@hostname][,unit]/devnum[.option]

The device number is the minor device number for direct hardware
access (ie the 'N' in /dev/audioN). For aucat, this is the occurence
number of the -f (or -M) option.

There's a compatibility hook to keep old names working if only one
aucat server is running.


# 1.124 18-Oct-2011 ratchov

keep the device closed by default (when not used)


# 1.123 18-Oct-2011 ratchov

remove duplicate options in getopt() string


# 1.122 17-Oct-2011 ratchov

Remove midicat since aucat can now be used instead of midicat
with almost the same syntax (roughly an extra -M option).
Thru boxes are created with aucat, and corresponding MIDI port
names have the "aucat" prefix instead of "midithru". The old
device name will still work some time for backward compatibility.

ok deraadt


# 1.121 12-Oct-2011 jmc

- avoid line wrapping an example
- sort usage() for midicat
- tweak a bit of unclear wording

ok ratchov


# 1.120 12-Oct-2011 ratchov

Simplify and improve the way options are parsed and remove ~300 lines
of code that becomes unused. Few command line arguments changes are
required though:

- stream definitions (-ios) now must follow devices definitions they
are attached to (-fMn)

- the -n option is now a special "loopback" device and is thus used
like -f, eg it must precede streams

- in midicat, midi thru boxes are not created automatically anymore,
the new "-M" option must be used for that

- channel numbers (-Cc options) correspond always to channel numbers
of the hardware.

- the -u option isn't needed anymore

- increase the log verbosity so user errors are logged
without using -d

tested by many, help from jmc


Revision tags: OPENBSD_5_0_BASE
# 1.119 29-Jun-2011 ratchov

make -u flag per-device similarly to -a and -w


# 1.118 20-Jun-2011 ratchov

Make -aoff option apply to MIDI ports (-q) as well, ensuring the device
stays closed also if -q is used. As we're at it, add -a to midicat so
it behaves like aucat.


# 1.117 03-Jun-2011 ratchov

Allow device encoding to be set if -u is also used. Useful to degrade
quality to save bandwith when a remote audio device is used.


# 1.116 03-Jun-2011 ratchov

Reorganize the way command line options are parsed and stored.
No behaviour change.


# 1.115 26-May-2011 ratchov

add a new -w flag to control whether master volume is automatically
adjusted when new streams are connected and disconnected. Disabling
automatic volume adjustment makes sense if all streams are recorded
with properly lowered volumes.


# 1.114 10-May-2011 ratchov

fix option handling and enable TCP in midicat
from peters at schwertfisch.de, thanks!


# 1.113 28-Apr-2011 ratchov

Implement a new authentication method allowing aucat and midicat to
work over TCP, for instance, to expose the sound card of one machine
with other machines of the network.

The first client generates a 128-bit random number (aka the
session cookie), saves it in $HOME/.aucat_cookie and sends it to the
server. Successive clients load the cookie from $HOME/.aucat_cookie
and send it to the server but the server accepts only clients whose
cookie matches the session cookie. When all clients are gone, the
session is over, and another cookie could start a new session, and so
on.

TCP is enabled on the server with the new -L option, and on the client
side hostnames are specified with a new optional component in the
device name.

hints from damien, dlg and deraadt, tweaks from jmc


# 1.112 27-Apr-2011 ratchov

move amsg.h containing protocol defs from aucat side to libsndio side.

requested by deraadt


# 1.111 27-Apr-2011 deraadt

use more volatile sig_atomic_t in signal handlers
ok ratchov


# 1.110 19-Apr-2011 ratchov

don't hold a pointer to "listen" structure. Instead, when it must
be free()ed, iterate over the file_list, and find the structure
to free. This is safer and simpler. No behaviour change


# 1.109 16-Apr-2011 ratchov

for unix domain socket addresses use AUCAT_PATH and MIDICAT_PATH
macros instead of hardcoded strings. No object change


# 1.108 17-Mar-2011 ratchov

use a DEFAULT_RATE macro instead of hardcoding 44100, no object change


Revision tags: OPENBSD_4_9_BASE
# 1.107 05-Nov-2010 ratchov

unbreak compilation without DEBUG defined


# 1.106 21-Oct-2010 ratchov

use MODE_xxx and XRUN_xxx macros everywhere, and zap corresponding
AMSG_xxx macros, which in turns simplifies the code


# 1.105 08-Sep-2010 deraadt

aucat: getpwnam: No such file or directory
is not a proper error message.


# 1.104 20-Aug-2010 ratchov

polish the code: remove few unused #includes, add missing ones,
fix NULL vs 0, etc. No behaviour change.


# 1.103 19-Aug-2010 ratchov

fix NULL pointer deref in midicat option parsing when ``-'' is
used as filename


Revision tags: OPENBSD_4_8_BASE
# 1.102 31-Jul-2010 ratchov

remove 2 years old compatibility mode


# 1.101 10-Jul-2010 ratchov

In midi server mode, don't terminate the midi thru box if there are
no connections anymore.


# 1.100 10-Jul-2010 ratchov

fix parameter handling: don't try to open a ``default'' midi port
if no files are given on the command line


# 1.99 06-Jul-2010 jmc

various tweaks to previous; ok ratchov


# 1.98 06-Jul-2010 ratchov

Handle all streams the same way because there's no actual
difference between audio files and client connections. Clean up
the way command line options are handled and clarify this in the
manual page: stream parameters (-Ccehjmrtvx) must precede stream
definitions (-ios) and per-device parameters (-abz) and stream
definitions (-ios) must precede device definitions (-f). Since
there's no ``server'' and ``non-server'' modes anymore, make the
-l option just detach the process.

ok and help from jakemsr and jmc


# 1.97 29-Jun-2010 jakemsr

If the device isn't opened yet (as can be the case with '-a off'),
don't check if the midi control interface is idle
from ratchov


# 1.96 25-Jun-2010 ratchov

don't attempt to drain devices after they are destroyed, which
results in a use after free(). Catched by jakemsr@ with MALLOC_OPTIONS=J


# 1.95 20-Jun-2010 ratchov

fix midicat(1) device mode when only -i or only -o is used (ie
open the device input-only if -o is used, and output-only if -i is
used).


# 1.94 05-Jun-2010 ratchov

don't prime server buffers, because it's ugly and conceptually
complicated. Instead, request clients to provide enough samples
and start with buffers full.


# 1.93 05-Jun-2010 ratchov

Don't send the initial position as a clock tick with a negative delta.
Add a new ``initial position'' message which is simpler. No bahaviour
change, except effects of the protocol version crank.


# 1.92 04-Jun-2010 ratchov

Allow the audio device to be opened only while it's actually used.
This is necessary for uaudio devices, for instance to start aucat
before the device is plugged. Or to unplug a device whithout
having to restart aucat when another device is plugged. This is
controlled with the new -a option.

Allow multiple audio devices to be used concurently, i.e.
multiple ``-f devname'' options to be used; -f options must follow
per-device options, which is what we do for other options.


# 1.91 08-May-2010 ratchov

prime play buffers as soon as the device is opened, otherwise a
midi client could start it with empty buffers, in turn hurting
audio clients.


# 1.90 02-May-2010 ratchov

Clean up device handling code to clarify different initialization phases
and different device states. Split initialization in two phases:
first global variables are initialized then the audio hardware is opened.
Allow devices that don't support full-duplex to work in play-only or
record-only mode, even if ``-m play'' or ``-m rec'' are not specified.


# 1.89 02-May-2010 ratchov

Don't systematically fill with silence the mixer output. This
might result in extra samples being written to the device
when the mixer is closed.


# 1.88 24-Apr-2010 ratchov

if there are no files to play in legacy mode, print the usage message


# 1.87 24-Apr-2010 ratchov

rename ibuflist/obuflist -> ins/outs to cut long lines
from Thomas Pfaff, thanks


# 1.86 22-Apr-2010 ratchov

Allow multiple users to share the same aucat server. If aucat is
run by root, it binds a shared address to the socket, cranks the
process priority and drops privileges. sio_open(3) will try to
connect to the private socket first (if any), then to the shared
socket. Only one user may have connections to aucat at a given
time.

based on discussions with henning, pyr and others
ok jacek, deraadt


# 1.85 21-Apr-2010 ratchov

when creating the ``default'' subdevice, use last parameters of the
command line, instead of the device parameters.


# 1.84 21-Apr-2010 ratchov

adds the necessary bits to join channels (ex stereo->mono) or
expand channels (ex mono->stereo). It's switched on/off with
the ``-j'' option, (default is "on").


# 1.83 06-Apr-2010 ratchov

aucat (server):

- make the ``-m mode'' option per subdevice, allowing
subdevices to be play-only or rec-only even if the
server is full-duplex

- add ``monitoring'' mode (with ``-m mon''). This is a
record-only stream from which played streams can be
recorded (kind of ``record what you hear'').

- allow MIDI devices to be subscribed to the controlling
MIDI port of the server, ie what midicat does (with -f
option), but using the -q option.

- add flow control to the protocol, and serialize clock
ticks (sio_onmove() calls) and data chunks. This should
fix certain full-duplex programs, broken with ``magic''
block/buffer size combinations.

- enable 3 block latency which is the minimum theoretical.
Make -z and -b options correspond to device parameters.

- make sio_getcap(3) the same for aucat and sun backends,
ie return whatever is supported (``everything'' in the
aucat case, since everything is actulally supported).

aucat (player):

- enable ``-m mode'' option to select between monitoring
and recording when ``-o file'' is used.

- plug MIDI code to non-server codepath. The MIDI control
device is specified with the ``-q device'' option, as in
server mode.

- impliment lseek()'ing within files (controlled
through MIDI). Necessary to use aucat with a MIDI
sequencer.

midicat (thrubox):

- rename ``-f'' flag to ``-q'', so it has the
same name as in aucat (-f is still working)

ok jakemsr@, tweaks from jmc@


# 1.82 03-Apr-2010 ratchov

backout last change, committed by mistake, sorry...


# 1.81 03-Apr-2010 ratchov

doc fixes from jmc


Revision tags: OPENBSD_4_7_BASE
# 1.80 14-Jan-2010 ratchov

Use -C option to specify recorded channel range instead of -c which
is for played channels.

spotted by Jan Stary <hans at stare.cz>, thanks


# 1.79 11-Jan-2010 ratchov

When resuming from suspend, prime play buffers with silence,
otherwise the client would underrun by more than its own buffer
size, which currently is not allowed (causes the client to
deadlock).

Set the minimum client buffer size to two blocks. Single block
buffers don't work properly yet.

with help from jakemsr


# 1.78 10-Jan-2010 ratchov

Add traces to help tracking subtle timing bugs often causing
underuns and overruns; such bugs are hard to debug with ktrace
or a debugger. They are also handy to debug or monitor code
using aucat.

To enable traces, compile aucat with ``make DEBUG=-DDEBUG'' and
use the -d option multiple times.

ok jakemsr


# 1.77 05-Jan-2010 ratchov

Make the way .wav files are handled closer to the way sockets are
handled, which is conceptually simpler. No behaviour changes.


# 1.76 21-Nov-2009 ratchov

use strtonum(3) and strtol(3) instead of sscanf(3)
suggested by deraadt@


# 1.75 21-Nov-2009 deraadt

spelling


# 1.74 03-Nov-2009 ratchov

Allow any program using aucat to act as MMC slave and MTC master
transparently. Multiple audio applications can be started
synchronously from external software/hardware supporting the
standard Start/Stop/Relocate messages. The server clock is exposed
through MTC, allowing non-audio software/hardware to be
synchronized to audio applications.


# 1.73 27-Oct-2009 ratchov

add missing word in error message


# 1.72 10-Oct-2009 ratchov

don't use a references to the device file to check the current mode


# 1.71 10-Oct-2009 ratchov

make dev_done() common to audio and MIDI, and drop dev_thrudone()


# 1.70 10-Oct-2009 ratchov

use dev_done() for loopback devices too and remove dev_loopdone()


# 1.69 05-Oct-2009 ratchov

add -d flag. When started in server mode, if the -d flag is
used aucat and midicat don't daemonize and log on stderr.


# 1.68 27-Sep-2009 ratchov

remove all debug traces


# 1.67 26-Aug-2009 jmc

oops, forgot to update usage();


# 1.66 19-Aug-2009 ratchov

don't try to process -o arguments using the input file list, fixes
midicat crashing sometimes when -o is used


# 1.65 19-Aug-2009 ratchov

organize midi code like audio code, no functional change


# 1.64 17-Aug-2009 ratchov

allow midicat to take multiple -i and -o options


# 1.63 17-Aug-2009 ratchov

if one or more MIDI devices are specified in server mode, subscribe
them to the MIDI thru box


# 1.62 25-Jul-2009 ratchov

fix comments, remove no longer needed declaration, reorder includes
from Thomas Pfaff


# 1.61 25-Jul-2009 ratchov

Currently midi capable programs can control midi hardware, but
cannot cooperate with other programs. The aim of this change is
to allow any program to send midi data to other programs as they
were midi hardware. For instance, this change should solve the
longstanding problem of using a midi sequencer with software
synthesizers. More precisely:

- new midicat(1) utility (actually hardlink to aucat(1)).
it creates software midi thru boxes, allowing programs
to send midi messages to other programs as they were
midi(4) hardware.

- new midi api in libsndio (see mio_open(3)), to access
midi(4) devices and midicat(1) sockets in a uniform way.

- new device naming scheme <service>:<unit>[.<option>],
common to audio and midi.

- new sndio(7) manual describing concepts and naming

The current audio device naming still works, but people having
scripts or configuration files containing device names could read
the sndio(7) man page and slowly start updating device names.

discussed with jakemsr@ and deraadt@, help form jmc@


Revision tags: OPENBSD_4_6_BASE
# 1.60 27-Apr-2009 ratchov

make -v option apply also to implicit default socket


# 1.59 17-Mar-2009 jmc

- rename the arg to -b "nframes" and sync usage()
- grammar fix for the server section

from Thomas Pfaff
ok ratchov


Revision tags: OPENBSD_4_5_BASE
# 1.58 06-Feb-2009 ratchov

move error messages reported to user into main()


# 1.57 04-Feb-2009 ratchov

if there are too many connections, stop acceping new ones rather
than exit()ing with ``too many open files'' fatal error


# 1.56 04-Feb-2009 ratchov

daemonize when in server mode, suggested by many
ok jakemsr


# 1.55 03-Feb-2009 ratchov

in server mode, create /tmp/aucat-userid/ directory with
permissions 0700, and create sockets in it. This prevents
one local user to eavesdrop or disturb audio programs of
other users.

if you're using the ``-s socket'' option with an absolute
path as argument, please update it to use a socket name.

requested by many, bits from jakemsr and otto
ok jakemsr


# 1.54 25-Jan-2009 ratchov

don't set malloc_options to not interfer with /etc/malloc.conf
requested and explained by fgsch@, henning@, otto@


# 1.53 23-Jan-2009 ratchov

catch SIGHUP and SIGTERM too, to ensure that recorded files are
properly closed and sockets are cleaned up.


# 1.52 23-Jan-2009 ratchov

spacing + typo fix from Matt Jibson <matt.jibson(at)gmail.com>


# 1.51 10-Jan-2009 ratchov

add "loopback" mode in which input is connected to the output.
This is useful to mix, demultiplex, resample or reencode audio
files off-line.
tweak + ok jakemsr


# 1.50 29-Dec-2008 ratchov

make the code "more correct": add reference counters to aproc
structures so we can keep a pointer to the device. Beside some
simplifications, this allows to easily handle the situation
where the audio device desappears. No change in the behaviour.


# 1.49 26-Dec-2008 ratchov

*** empty log message ***


# 1.48 07-Dec-2008 ratchov

When resampling, don't require the sample frequency to be an integer.
This removes the arithmetic constraint between the sample frequency
and the block size and all the associated code. Now aucat can work
in server mode with any block size.
ok jakemsr


# 1.47 23-Nov-2008 ratchov

Use file input parameter as default device output and file output parameters
as default device input. That's what the man page says


# 1.46 20-Nov-2008 ratchov

take into account socket parameters into calculation of device
parameters; without this change aucat uses the device default
parameters and they are not necessarily usable for multi-stream
mode.


# 1.45 17-Nov-2008 jmc

- rename -s arg name to "socket"
- condense EXAMPLES into a single screenful

ok ratchov


# 1.44 17-Nov-2008 jmc

tweak previous man page changes, and sync usage();


# 1.43 17-Nov-2008 ratchov

allow aucat to run as server in play-only and record-only mode, so
it can be used on play-only, record-only and half-duplex devices.
ok jakemsr


# 1.42 16-Nov-2008 ratchov

allow aucat to listen on multiple sockets. Each socket carries its
channel and volume settings allowing multiple configuration to
coexist. Mostly useful for envy(4)-like devices, but can be used
to force different apps to use different settings.


# 1.41 16-Nov-2008 ratchov

add ``template'' parameters to sockets rather than using device
parameters. No functional change.


# 1.40 16-Nov-2008 ratchov

Make clients inherit the volume parameter when the -v option is used in
server mode. It gives the maximum volume a client may have. This wastes
dynamic range, but allows volume to stay constant when other clients
connect or disconnect.


# 1.39 12-Nov-2008 ratchov

when destroying the device, mark the mixer and the demultiplexer
for ``AUTOQUIT'' first, and then call file_eof() and friends.
fixes crashes sthen@ noticed on zaurus


# 1.38 11-Nov-2008 ratchov

if the device disappear the mixer and demultiplexer chains are
destroyed. If this happen then warn and exit, rather than crashing.


# 1.37 11-Nov-2008 ratchov

termninate non-server aucat when it's idle rather that using
the hackish {MIX,SUB}_AUTOQUIT features. This way we don't
have to care about possible references on destroyed objects.


# 1.36 11-Nov-2008 ratchov

when not in server mode, aucat terminates automatically using
the {MIX,SUB}_AUTAQUIT features. In this case we're not
allowed to directly touch dev_mix and dev_sub because they
can disappar. So disable the ``suspend'' and ``quit'' bits
when {MIX,SUB}_AUTAQUIT are used.


# 1.35 10-Nov-2008 ratchov

add a per-stream ``soft volume'' knob and the corresponding -v option.
The code will be useful later for the volume knob in the sndio API.


# 1.34 09-Nov-2008 ratchov

make aucat(1) stop automatically the audio(1) device if it's idle. This
way, when in server mode, it consumes no CPU if there are no clients.
Later, this will allow to start aucat(1) at session or system startup.


# 1.33 08-Nov-2008 ratchov

set the default device to /dev/audio for legacy mode.
From Thomas Pfaff <tpfaff _at_ agderlink.no> and tweaks from me


# 1.32 07-Nov-2008 ratchov

expose the block size in the sndio API by making par->round writable
and thus remove the ugly rate <-> block-size table from
sio_setpar(3). Handle the block size negociation in aucat(1), since
it has few constrains the code is overally simpler.
ok jakemsr@, major crank suggested by deraadt@


# 1.31 03-Nov-2008 ratchov

A small optimization: handle most N-channel <-> M-channel conversions
inside the mixer and the demultiplexer. This way, aucat will not
trigger the heavy conversion code when only channel conversions are
required. Cuts ~50% of the CPU usage on envy(4) devices, can improve
surround 4.0, 5.1 and 7.1 capable devices. No functionnal change.


# 1.30 26-Oct-2008 ratchov

zap `q' from the getopts string too,
requested by jmc@


# 1.29 26-Oct-2008 jmc

sync SYNOPSIS and usage();


# 1.28 26-Oct-2008 ratchov

add minimal server capability to aucat(1). When started in server
mode, it listens on an unix socket and mixes/demultiplexes any number
of full-duplex streams, doing necessary format conversions and
resampling on the fly.

programs can use the new libsa(3) library to play and record audio.
The library provides a very simple API to connect to the audio server;
if aucat(1) isn't running, it uses the audio(4) driver transparently
instead.


# 1.27 25-Aug-2008 sobrado

minor tweak for removal of "[-d devel]" (change done in r1.19 and r1.21).

ok ratchov@


# 1.26 14-Aug-2008 ratchov

move all device related stuff from aucat.c to a new dev.c file.
The new dev_xxx() routines expose a "high level" self-contained
interface to the device. At initialization, the device is opened
and two chains of aproc structures are created:

* a playback chain that exposes a (initially) empty mix
aproc to which the rest of the code can attach new
streams to be played

* record chain that exposes a (initially) empty sub aproc
to which the rest of the code can attach new stream to
to record

The rest of the code, has just to use dev_attach() routine to
attach streams. While we're at it, add a ``devops'' structure
containing pointers to the device-specific routines. This will
allow later to add support for other type of device than the
Sun API.

Also, write the .wav headers in file_del(), so put all header
related data in the file strucuture. This allows to close() the
file, as soon as wpipe_xxx() aproc terminates. This will be
useful for the server, because it will need to close() descripts
of closed connections immediately.

add mix_pushzero() routine to fill the mixer with silence. It
will be used to avoid the mixer to underrun when there are no
input streams. Since we always have at least one input stream
there's no behaviour change.

ok jakemsr


Revision tags: OPENBSD_4_4_BASE
# 1.25 02-Jun-2008 ratchov

document latest changes: -d flag is replaced by AUCAT_DEBUG
environment variable, new -xX options

bits from eric, ok jakemsr


# 1.24 02-Jun-2008 ratchov

currently, we start the device as soon as write() blocks; it's better
to continue filling the play buffer until it's full. This way, the
play buffer has fewer chances to underrun when the device is started.

ok jakemsr


# 1.23 02-Jun-2008 ratchov

if aucat is suspended, then the kernel continues running the device
(produces silence) and later, once aucat is resumed the kernel starts
dropping samples, it will try to drop as many samples as silence was
produced. So suspending breaks the aucat process permanently.

workaround this by blocking the signals ie disabling suspending of aucat
from the tty. The long term solution would be to catch SIGCONT and to stop
the device, resync/refill buffers and restart the device. That's really a
lot of work...

ok jakemsr


# 1.22 02-Jun-2008 ratchov

allow the user to choose the aucat behaviour when underruns/overruns
occur on a per-stream basis, using -Xx flags. There are 3 possible
policies:

- ignore : ignores underruns/overruns, for instance, this mode
could be used for creating simple pipes with utilities; like in
your last cdio diff.

- sync : insert/discard samples in order to keep all streams in
sync, useful for multi-tracker-like apps and/or to sync
midi/video/whatever on audio streams (this was the previous
behaviour)

- error : if overruns/underruns occur, consider it as fatal error
and kill the corresponding stream (without disturbing others).
Useful, for reliable recordings (and/or debugging aucat
itself:).

ok jakemsr


# 1.21 02-Jun-2008 ratchov

(remove -d, second part) use strtonum() instead of sscanf() and inlined
checks

suggested and ok jakemsr


# 1.20 02-Jun-2008 ratchov

since underruns/overruns are handled outside devices, remove code that
check for xruns in devices.

ok jakemsr


# 1.19 02-Jun-2008 ratchov

(remove -d, first part) replace -d flag by AUCAT_DEBUG environment
variable. This eases turning debugging on/off when aucat is started by
another program.

"i like the idea" jakemsr


# 1.18 26-May-2008 jmc

put -q in the right place;


# 1.17 26-May-2008 jakemsr

add -q option which enables "quiet" operation.

ok ratchov@


# 1.16 23-May-2008 jmc

various fixes for aucat.1, and sync usage(); ok ratchov


# 1.15 23-May-2008 ratchov

add support for:
- recording, full-duplex operation
- format conversions and resampling on the fly
- mixing on the fly of multiple inputs of different formats
- up to 16 channels, simplistic "routing" of channel ranges
- more linear encodings (in raw and wav files)
the old behaviour is fully preserved if none of the new -i and -o
options are used.

code and fixes from jakemsr@ and eric@, suggestions by others.
ok "go ahead" deraadt@


# 1.14 13-Apr-2008 jakemsr

- remove code that was rendered pointless by the last commit
- minor knf

from Pierre Riteau


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE
# 1.13 20-Mar-2007 uwe

Support for playing simple RIFF files (.wav) from Simon Effenberg, savar
at schuldeigen de, with some tweaks from me. ok millert, tests ckuethe,
liked by some disliked by nobody, but he's not even a real user :)


Revision tags: OPENBSD_4_1_BASE
# 1.12 20-Dec-2006 steven

remove unneeded #include

ok mbalmer@ deraadt@


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.11 20-Jan-2005 jaredy

- knf
- check read/write/etc. calls for errors
- use constants instead of hardcoded pathnames
- better variable types
- add usage output

suggestions from various, ok ian


# 1.10 13-Jan-2005 ian

Close what you open: obvious fix, ok jared@, more to follow


Revision tags: OPENBSD_3_5_BASE OPENBSD_3_6_BASE
# 1.9 20-Oct-2003 jmc

typos from Jared Yanovich, slightly ammended;


Revision tags: OPENBSD_3_4_BASE
# 1.8 10-Jul-2003 david

add missing includes
ok deraadt@ tedu@


# 1.7 02-Jul-2003 deraadt

protos


# 1.6 10-Jun-2003 deraadt

mostly ansi cleanup; pval ok


Revision tags: OPENBSD_3_3_BASE
# 1.5 09-Dec-2002 millert

From Andrushock, s/sucess/success/g


Revision tags: OPENBSD_2_8_BASE OPENBSD_2_9_BASE OPENBSD_3_0_BASE OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.4 01-Jul-2000 millert

Add -f option to specify audio device, honor AUDIODEVICE environment vairable,
and some KNF.


Revision tags: OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE
# 1.3 05-Jan-1997 kstailey

minor tweaks; htonl() -> ntohl(); added SEE ALSO


# 1.2 03-Jan-1997 kstailey

removed unnecessary test


# 1.1 02-Jan-1997 kstailey

new utility for playing sound files


# 1.180 20-Mar-2024 ratchov

aucat: Add generic channel mapping in place of -j and -c options.

The argument to the -m option specifies the source and destination
channel ranges to be mapped. Compatibility is maintained: if -m
is not used, the -c and -j options still work.

Help and suggestions from Jan Stary <hans@stare.cz>, thanks.


Revision tags: OPENBSD_7_5_BASE
# 1.179 01-Feb-2024 ratchov

aucat: Fix MIDI control of the levels of individual files

Change the order of slot_list to match the order of the -io options on
the command-line and fix the way slot_list searched using the MIDI
channel number of the CC-07 message.

Reported and analysed by Dirk-Wilhelm Peters <peters at schwertfisch.de>

Thanks!


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE OPENBSD_7_3_BASE OPENBSD_7_4_BASE
# 1.178 07-Mar-2022 ratchov

Switch internal sample representation to 24-bit fixed-point

Note that this also changes the default file encoding to 24-bit.


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.177 12-Jan-2021 naddy

const-ify lookup tables; ok ratchov@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.176 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.175 19-May-2019 ratchov

Fix integer overflow in block size calculation.

Fixes truncation of contents of files with rate above 64kHz
generate in off-line mode (i.e. -n option).


Revision tags: OPENBSD_6_5_BASE
# 1.174 21-Feb-2019 ratchov

Use reallocarray in place of malloc(a * b) to handle possible
overflow in multiplication of malloc argument.


Revision tags: OPENBSD_6_4_BASE
# 1.173 13-May-2018 ratchov

Add missing pledge(). From Jesper Wallin <jesper at ifconfig.se>.
Thanks!


Revision tags: OPENBSD_6_3_BASE
# 1.172 07-Nov-2017 ratchov

prefill with silence the buffer where cmap_copy() stores samples
rather than the file buffer. Fixes initialized data being treated as
recorded samples when resampling or format conversions are involved
but mono->stereo conversion is disabled.


# 1.171 03-Nov-2017 ratchov

Prefill rec buffer with silence. This is necessary because certain
channels don't get samples, for instance when mono->stereo conversion
is disabled.


Revision tags: OPENBSD_6_2_BASE
# 1.170 20-Jul-2017 ratchov

Fix integer overflow that would causes >12 hours files
to not properly relocate. Found with coverity.


Revision tags: OPENBSD_6_1_BASE
# 1.169 03-Jan-2017 ratchov

Assert we're not freeing buffers we didn't allocate (DEBUG mode).


# 1.168 03-Jan-2017 ratchov

Log play volume together with other play parameters.


# 1.167 30-Sep-2016 ratchov

Set the device rate to the calculated rate. Otherwise, the signal
may endup resampled twice.


# 1.166 30-Sep-2016 ratchov

Remove unnecessary includes. From Michael W. Bombardieri <mb at ii.net>.


# 1.165 27-Sep-2016 ratchov

Don't rely on the resampling code to calculate the number of samples
to process, as it may produce one extra sample (to handle accumulation
of fractional samples), which would cause access to one sample past
the end of the buffer and crash aucat. Fix this by limiting the number
of samples processed to a single block.

Found by and help from Michael W. Bombardieri <mb at ii.net>. Thanks.


Revision tags: OPENBSD_6_0_BASE
# 1.164 07-Jun-2016 ratchov

Add -g and -p options to control device and file start position.


# 1.163 07-Jun-2016 ratchov

Add resamp_getcnt() routine to calculate the exact number of samples
that would be consumed and produced by the sampler rate converter. Use
it to avoid partial samples that are not properly handled. Fixes
last samples of certain files causing aucat to abort.


# 1.162 02-Jun-2016 ratchov

remove forgotten debug printfs


# 1.161 02-Jun-2016 ratchov

set exit status to 1 if -n is used without -i and -o


# 1.160 31-May-2016 ratchov

Pass hr:min:sec:frame.cent to dev_mmcloc() directly and remove the
useless intermediate conversion from and to 2400-th of seconds.


# 1.159 31-May-2016 ratchov

To store MMC position, use samples (at device rate) as time units
instead of 2400-ths of second. No behaviour change.


# 1.158 28-May-2016 ratchov

Fix file block size rounding and ensure it's large enough to store a
full audio block.


# 1.157 27-May-2016 ratchov

When resampling, use the exact resampling factor instead of the ratio
between input and output block sizes. This was inherited from sndiod,
but is not required for files because they are continuous streams of
samples and do not need to be split in blocks of equal duration.

This change makes playback/recording rate match exactly the requested
sample rate.


# 1.156 27-May-2016 ratchov

Flush rec buffer if there's less than one block space left and refill
play buffer if there's less than one block of data left. This is the
correct condition in the general case. No behaviour change, as all
input/output is multiple of the block size.


# 1.155 27-May-2016 ratchov

Simplify slot_fill() and slot_flush(). No behaviour change


# 1.154 27-May-2016 ratchov

Make resamp_do() get the exact number input and output samples and
provide routines to calculate them. This way we don't rely on it
to calculate the bytes procuded/consumed anymore. No behaviour change.


# 1.153 26-May-2016 ratchov

Make format conversion routines return the number of frames
consumed from both input and output buffers. No behaviour change.


Revision tags: OPENBSD_5_9_BASE
# 1.152 29-Jan-2016 ratchov

Check that byte order is native only if we're using more than one
byte per sample, check that samples are lsb-aligned only if there's
padding. Fixes unsupported parameters not being detected if compiled
in 24-bit mode and sndiod is not running.


# 1.151 10-Jan-2016 ratchov

trailing spaces, fit in 80 columns


# 1.150 09-Nov-2015 mmcc

Remove xfree(). From Michael W. Bombardieri.

ok ratchov@


# 1.149 27-Aug-2015 ratchov

fix typos in comments and error messages


Revision tags: OPENBSD_5_8_BASE
# 1.148 16-May-2015 ratchov

Call slot_stop() after the play buffer is drained to properly cleanup
the slot. Fixes periodic glitches occurring after eof is reached, and
then playback restarted with a MMC-relocate and MMC-start.


Revision tags: OPENBSD_5_7_BASE
# 1.147 21-Jan-2015 jmc

fix macro breakage and update usage();


# 1.146 21-Jan-2015 ratchov

Simplify internals and rewrite file header parsing code:
- remove -M, -t, -w, -C, -x flags that don't make sense anymore
- make "-j off" the default (sndiod already does the job)
- don't limit the number of played/recorded files.
- add support for floating-point encoded files.
- add support for apple .aiff and sun/next .au files


# 1.145 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.144 18-Nov-2013 ratchov

remove unused variables


# 1.143 18-Nov-2013 ratchov

add missing prototypes


# 1.142 12-Nov-2013 ratchov

remove bogus legacy server-specific bits that cause confusion


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.141 03-Dec-2012 ratchov

Remove server capability description from the documentation and from
the usage string, and just say that for historic reasons aucat behaves
like sndiod.
with help from jmc@


# 1.140 23-Nov-2012 ratchov

Since there's no sndiod -> aucat link anymore, allow aucat to be run
in server mode even when not invoked as sndiod. This allows aucat and
the new sndiod daemon to live together in /usr/bin, which makes easy
to quickly compare sndiod and aucat (and spot possible regressions)
without having to modify /usr/bin.


# 1.139 27-Oct-2012 ratchov

don't stat & test sndio home directory since we never use it.


# 1.138 27-Oct-2012 ratchov

don't (re-)define SNDIO_USER and SNDIO_PRIO if they are already defined,
this way they can be defined at compilation time


Revision tags: OPENBSD_5_2_BASE
# 1.137 27-Jun-2012 ratchov

Uniformize error messages printing, use err() instead of fprintf()
when appropriate. From Michael W. Bombardieri <mb at ii.net>


# 1.136 19-Jun-2012 ratchov

Remove hidden options that are not needed any more,
from Remco <remco at d-compu.dyndns.org>. Thanks


# 1.135 23-May-2012 ratchov

Make the "default" string a valid device name that has the same effect
as NULL has. This will (hopefully) simplify ports where the user
passes the device string.


# 1.134 11-Apr-2012 ratchov

Use "unsigned int" rather than "unsigned". No object change.
suggested by deraadt@


Revision tags: OPENBSD_5_1_BASE
# 1.133 09-Feb-2012 ratchov

Crank the default buffer size to 160ms (ie 7680 frames at 48kHz).
Workarounds stuttering caused by something hogging the cpu in kernel
mode and preventing sndiod from running at time.
ok deraadt


# 1.132 26-Jan-2012 ratchov

Make the default rate 48kHz and the default block size 10ms. These
settings ensure video players and programs using MTC are smooth by
default. Thanks to all who tested.


# 1.131 10-Jan-2012 ratchov

When a sub-device is created (-s), check if a hardware
device was already created (-f) before trying to use the
default one, otherwise all sub-devices end up attached
to the default device. Noticed by peters at schwertfisch.de,
thanks!


# 1.130 09-Dec-2011 ratchov

fix usage string: remove -M from aucat and -l from aucat and sndiod
from jmc


# 1.129 09-Dec-2011 ratchov

add a new sndiod audio/midi daemon and hide aucat server-specific
options.


# 1.128 02-Dec-2011 ratchov

don't use a "server" flag, just check if we expose sub-devices


# 1.127 02-Dec-2011 ratchov

reuse midi-control code to implement midi thru boxes and remove
the old midithru implementation; less code, less bugs. As a side
effect, midi output doesn't implement running status "compression"
any more.


# 1.126 20-Nov-2011 ratchov

Move mmc/mtc and volume control bits from struct aproc to struct
dev. Allows volume settings to be saved while the device is kept
closed. Besides that, no behabiour changes.


# 1.125 15-Nov-2011 ratchov

Add a "device number" component in sndio(7) device names, allowing a
single aucat instance to handle all audio and MIDI services. Since
this partially breaks compatibility, this is a opportunitiy to fix few
other design mistakes (eg ':' being used by inet6, type name vs api
name confusion, etc..). This leads to the following names:

type[@hostname][,unit]/devnum[.option]

The device number is the minor device number for direct hardware
access (ie the 'N' in /dev/audioN). For aucat, this is the occurence
number of the -f (or -M) option.

There's a compatibility hook to keep old names working if only one
aucat server is running.


# 1.124 18-Oct-2011 ratchov

keep the device closed by default (when not used)


# 1.123 18-Oct-2011 ratchov

remove duplicate options in getopt() string


# 1.122 17-Oct-2011 ratchov

Remove midicat since aucat can now be used instead of midicat
with almost the same syntax (roughly an extra -M option).
Thru boxes are created with aucat, and corresponding MIDI port
names have the "aucat" prefix instead of "midithru". The old
device name will still work some time for backward compatibility.

ok deraadt


# 1.121 12-Oct-2011 jmc

- avoid line wrapping an example
- sort usage() for midicat
- tweak a bit of unclear wording

ok ratchov


# 1.120 12-Oct-2011 ratchov

Simplify and improve the way options are parsed and remove ~300 lines
of code that becomes unused. Few command line arguments changes are
required though:

- stream definitions (-ios) now must follow devices definitions they
are attached to (-fMn)

- the -n option is now a special "loopback" device and is thus used
like -f, eg it must precede streams

- in midicat, midi thru boxes are not created automatically anymore,
the new "-M" option must be used for that

- channel numbers (-Cc options) correspond always to channel numbers
of the hardware.

- the -u option isn't needed anymore

- increase the log verbosity so user errors are logged
without using -d

tested by many, help from jmc


Revision tags: OPENBSD_5_0_BASE
# 1.119 29-Jun-2011 ratchov

make -u flag per-device similarly to -a and -w


# 1.118 20-Jun-2011 ratchov

Make -aoff option apply to MIDI ports (-q) as well, ensuring the device
stays closed also if -q is used. As we're at it, add -a to midicat so
it behaves like aucat.


# 1.117 03-Jun-2011 ratchov

Allow device encoding to be set if -u is also used. Useful to degrade
quality to save bandwith when a remote audio device is used.


# 1.116 03-Jun-2011 ratchov

Reorganize the way command line options are parsed and stored.
No behaviour change.


# 1.115 26-May-2011 ratchov

add a new -w flag to control whether master volume is automatically
adjusted when new streams are connected and disconnected. Disabling
automatic volume adjustment makes sense if all streams are recorded
with properly lowered volumes.


# 1.114 10-May-2011 ratchov

fix option handling and enable TCP in midicat
from peters at schwertfisch.de, thanks!


# 1.113 28-Apr-2011 ratchov

Implement a new authentication method allowing aucat and midicat to
work over TCP, for instance, to expose the sound card of one machine
with other machines of the network.

The first client generates a 128-bit random number (aka the
session cookie), saves it in $HOME/.aucat_cookie and sends it to the
server. Successive clients load the cookie from $HOME/.aucat_cookie
and send it to the server but the server accepts only clients whose
cookie matches the session cookie. When all clients are gone, the
session is over, and another cookie could start a new session, and so
on.

TCP is enabled on the server with the new -L option, and on the client
side hostnames are specified with a new optional component in the
device name.

hints from damien, dlg and deraadt, tweaks from jmc


# 1.112 27-Apr-2011 ratchov

move amsg.h containing protocol defs from aucat side to libsndio side.

requested by deraadt


# 1.111 27-Apr-2011 deraadt

use more volatile sig_atomic_t in signal handlers
ok ratchov


# 1.110 19-Apr-2011 ratchov

don't hold a pointer to "listen" structure. Instead, when it must
be free()ed, iterate over the file_list, and find the structure
to free. This is safer and simpler. No behaviour change


# 1.109 16-Apr-2011 ratchov

for unix domain socket addresses use AUCAT_PATH and MIDICAT_PATH
macros instead of hardcoded strings. No object change


# 1.108 17-Mar-2011 ratchov

use a DEFAULT_RATE macro instead of hardcoding 44100, no object change


Revision tags: OPENBSD_4_9_BASE
# 1.107 05-Nov-2010 ratchov

unbreak compilation without DEBUG defined


# 1.106 21-Oct-2010 ratchov

use MODE_xxx and XRUN_xxx macros everywhere, and zap corresponding
AMSG_xxx macros, which in turns simplifies the code


# 1.105 08-Sep-2010 deraadt

aucat: getpwnam: No such file or directory
is not a proper error message.


# 1.104 20-Aug-2010 ratchov

polish the code: remove few unused #includes, add missing ones,
fix NULL vs 0, etc. No behaviour change.


# 1.103 19-Aug-2010 ratchov

fix NULL pointer deref in midicat option parsing when ``-'' is
used as filename


Revision tags: OPENBSD_4_8_BASE
# 1.102 31-Jul-2010 ratchov

remove 2 years old compatibility mode


# 1.101 10-Jul-2010 ratchov

In midi server mode, don't terminate the midi thru box if there are
no connections anymore.


# 1.100 10-Jul-2010 ratchov

fix parameter handling: don't try to open a ``default'' midi port
if no files are given on the command line


# 1.99 06-Jul-2010 jmc

various tweaks to previous; ok ratchov


# 1.98 06-Jul-2010 ratchov

Handle all streams the same way because there's no actual
difference between audio files and client connections. Clean up
the way command line options are handled and clarify this in the
manual page: stream parameters (-Ccehjmrtvx) must precede stream
definitions (-ios) and per-device parameters (-abz) and stream
definitions (-ios) must precede device definitions (-f). Since
there's no ``server'' and ``non-server'' modes anymore, make the
-l option just detach the process.

ok and help from jakemsr and jmc


# 1.97 29-Jun-2010 jakemsr

If the device isn't opened yet (as can be the case with '-a off'),
don't check if the midi control interface is idle
from ratchov


# 1.96 25-Jun-2010 ratchov

don't attempt to drain devices after they are destroyed, which
results in a use after free(). Catched by jakemsr@ with MALLOC_OPTIONS=J


# 1.95 20-Jun-2010 ratchov

fix midicat(1) device mode when only -i or only -o is used (ie
open the device input-only if -o is used, and output-only if -i is
used).


# 1.94 05-Jun-2010 ratchov

don't prime server buffers, because it's ugly and conceptually
complicated. Instead, request clients to provide enough samples
and start with buffers full.


# 1.93 05-Jun-2010 ratchov

Don't send the initial position as a clock tick with a negative delta.
Add a new ``initial position'' message which is simpler. No bahaviour
change, except effects of the protocol version crank.


# 1.92 04-Jun-2010 ratchov

Allow the audio device to be opened only while it's actually used.
This is necessary for uaudio devices, for instance to start aucat
before the device is plugged. Or to unplug a device whithout
having to restart aucat when another device is plugged. This is
controlled with the new -a option.

Allow multiple audio devices to be used concurently, i.e.
multiple ``-f devname'' options to be used; -f options must follow
per-device options, which is what we do for other options.


# 1.91 08-May-2010 ratchov

prime play buffers as soon as the device is opened, otherwise a
midi client could start it with empty buffers, in turn hurting
audio clients.


# 1.90 02-May-2010 ratchov

Clean up device handling code to clarify different initialization phases
and different device states. Split initialization in two phases:
first global variables are initialized then the audio hardware is opened.
Allow devices that don't support full-duplex to work in play-only or
record-only mode, even if ``-m play'' or ``-m rec'' are not specified.


# 1.89 02-May-2010 ratchov

Don't systematically fill with silence the mixer output. This
might result in extra samples being written to the device
when the mixer is closed.


# 1.88 24-Apr-2010 ratchov

if there are no files to play in legacy mode, print the usage message


# 1.87 24-Apr-2010 ratchov

rename ibuflist/obuflist -> ins/outs to cut long lines
from Thomas Pfaff, thanks


# 1.86 22-Apr-2010 ratchov

Allow multiple users to share the same aucat server. If aucat is
run by root, it binds a shared address to the socket, cranks the
process priority and drops privileges. sio_open(3) will try to
connect to the private socket first (if any), then to the shared
socket. Only one user may have connections to aucat at a given
time.

based on discussions with henning, pyr and others
ok jacek, deraadt


# 1.85 21-Apr-2010 ratchov

when creating the ``default'' subdevice, use last parameters of the
command line, instead of the device parameters.


# 1.84 21-Apr-2010 ratchov

adds the necessary bits to join channels (ex stereo->mono) or
expand channels (ex mono->stereo). It's switched on/off with
the ``-j'' option, (default is "on").


# 1.83 06-Apr-2010 ratchov

aucat (server):

- make the ``-m mode'' option per subdevice, allowing
subdevices to be play-only or rec-only even if the
server is full-duplex

- add ``monitoring'' mode (with ``-m mon''). This is a
record-only stream from which played streams can be
recorded (kind of ``record what you hear'').

- allow MIDI devices to be subscribed to the controlling
MIDI port of the server, ie what midicat does (with -f
option), but using the -q option.

- add flow control to the protocol, and serialize clock
ticks (sio_onmove() calls) and data chunks. This should
fix certain full-duplex programs, broken with ``magic''
block/buffer size combinations.

- enable 3 block latency which is the minimum theoretical.
Make -z and -b options correspond to device parameters.

- make sio_getcap(3) the same for aucat and sun backends,
ie return whatever is supported (``everything'' in the
aucat case, since everything is actulally supported).

aucat (player):

- enable ``-m mode'' option to select between monitoring
and recording when ``-o file'' is used.

- plug MIDI code to non-server codepath. The MIDI control
device is specified with the ``-q device'' option, as in
server mode.

- impliment lseek()'ing within files (controlled
through MIDI). Necessary to use aucat with a MIDI
sequencer.

midicat (thrubox):

- rename ``-f'' flag to ``-q'', so it has the
same name as in aucat (-f is still working)

ok jakemsr@, tweaks from jmc@


# 1.82 03-Apr-2010 ratchov

backout last change, committed by mistake, sorry...


# 1.81 03-Apr-2010 ratchov

doc fixes from jmc


Revision tags: OPENBSD_4_7_BASE
# 1.80 14-Jan-2010 ratchov

Use -C option to specify recorded channel range instead of -c which
is for played channels.

spotted by Jan Stary <hans at stare.cz>, thanks


# 1.79 11-Jan-2010 ratchov

When resuming from suspend, prime play buffers with silence,
otherwise the client would underrun by more than its own buffer
size, which currently is not allowed (causes the client to
deadlock).

Set the minimum client buffer size to two blocks. Single block
buffers don't work properly yet.

with help from jakemsr


# 1.78 10-Jan-2010 ratchov

Add traces to help tracking subtle timing bugs often causing
underuns and overruns; such bugs are hard to debug with ktrace
or a debugger. They are also handy to debug or monitor code
using aucat.

To enable traces, compile aucat with ``make DEBUG=-DDEBUG'' and
use the -d option multiple times.

ok jakemsr


# 1.77 05-Jan-2010 ratchov

Make the way .wav files are handled closer to the way sockets are
handled, which is conceptually simpler. No behaviour changes.


# 1.76 21-Nov-2009 ratchov

use strtonum(3) and strtol(3) instead of sscanf(3)
suggested by deraadt@


# 1.75 21-Nov-2009 deraadt

spelling


# 1.74 03-Nov-2009 ratchov

Allow any program using aucat to act as MMC slave and MTC master
transparently. Multiple audio applications can be started
synchronously from external software/hardware supporting the
standard Start/Stop/Relocate messages. The server clock is exposed
through MTC, allowing non-audio software/hardware to be
synchronized to audio applications.


# 1.73 27-Oct-2009 ratchov

add missing word in error message


# 1.72 10-Oct-2009 ratchov

don't use a references to the device file to check the current mode


# 1.71 10-Oct-2009 ratchov

make dev_done() common to audio and MIDI, and drop dev_thrudone()


# 1.70 10-Oct-2009 ratchov

use dev_done() for loopback devices too and remove dev_loopdone()


# 1.69 05-Oct-2009 ratchov

add -d flag. When started in server mode, if the -d flag is
used aucat and midicat don't daemonize and log on stderr.


# 1.68 27-Sep-2009 ratchov

remove all debug traces


# 1.67 26-Aug-2009 jmc

oops, forgot to update usage();


# 1.66 19-Aug-2009 ratchov

don't try to process -o arguments using the input file list, fixes
midicat crashing sometimes when -o is used


# 1.65 19-Aug-2009 ratchov

organize midi code like audio code, no functional change


# 1.64 17-Aug-2009 ratchov

allow midicat to take multiple -i and -o options


# 1.63 17-Aug-2009 ratchov

if one or more MIDI devices are specified in server mode, subscribe
them to the MIDI thru box


# 1.62 25-Jul-2009 ratchov

fix comments, remove no longer needed declaration, reorder includes
from Thomas Pfaff


# 1.61 25-Jul-2009 ratchov

Currently midi capable programs can control midi hardware, but
cannot cooperate with other programs. The aim of this change is
to allow any program to send midi data to other programs as they
were midi hardware. For instance, this change should solve the
longstanding problem of using a midi sequencer with software
synthesizers. More precisely:

- new midicat(1) utility (actually hardlink to aucat(1)).
it creates software midi thru boxes, allowing programs
to send midi messages to other programs as they were
midi(4) hardware.

- new midi api in libsndio (see mio_open(3)), to access
midi(4) devices and midicat(1) sockets in a uniform way.

- new device naming scheme <service>:<unit>[.<option>],
common to audio and midi.

- new sndio(7) manual describing concepts and naming

The current audio device naming still works, but people having
scripts or configuration files containing device names could read
the sndio(7) man page and slowly start updating device names.

discussed with jakemsr@ and deraadt@, help form jmc@


Revision tags: OPENBSD_4_6_BASE
# 1.60 27-Apr-2009 ratchov

make -v option apply also to implicit default socket


# 1.59 17-Mar-2009 jmc

- rename the arg to -b "nframes" and sync usage()
- grammar fix for the server section

from Thomas Pfaff
ok ratchov


Revision tags: OPENBSD_4_5_BASE
# 1.58 06-Feb-2009 ratchov

move error messages reported to user into main()


# 1.57 04-Feb-2009 ratchov

if there are too many connections, stop acceping new ones rather
than exit()ing with ``too many open files'' fatal error


# 1.56 04-Feb-2009 ratchov

daemonize when in server mode, suggested by many
ok jakemsr


# 1.55 03-Feb-2009 ratchov

in server mode, create /tmp/aucat-userid/ directory with
permissions 0700, and create sockets in it. This prevents
one local user to eavesdrop or disturb audio programs of
other users.

if you're using the ``-s socket'' option with an absolute
path as argument, please update it to use a socket name.

requested by many, bits from jakemsr and otto
ok jakemsr


# 1.54 25-Jan-2009 ratchov

don't set malloc_options to not interfer with /etc/malloc.conf
requested and explained by fgsch@, henning@, otto@


# 1.53 23-Jan-2009 ratchov

catch SIGHUP and SIGTERM too, to ensure that recorded files are
properly closed and sockets are cleaned up.


# 1.52 23-Jan-2009 ratchov

spacing + typo fix from Matt Jibson <matt.jibson(at)gmail.com>


# 1.51 10-Jan-2009 ratchov

add "loopback" mode in which input is connected to the output.
This is useful to mix, demultiplex, resample or reencode audio
files off-line.
tweak + ok jakemsr


# 1.50 29-Dec-2008 ratchov

make the code "more correct": add reference counters to aproc
structures so we can keep a pointer to the device. Beside some
simplifications, this allows to easily handle the situation
where the audio device desappears. No change in the behaviour.


# 1.49 26-Dec-2008 ratchov

*** empty log message ***


# 1.48 07-Dec-2008 ratchov

When resampling, don't require the sample frequency to be an integer.
This removes the arithmetic constraint between the sample frequency
and the block size and all the associated code. Now aucat can work
in server mode with any block size.
ok jakemsr


# 1.47 23-Nov-2008 ratchov

Use file input parameter as default device output and file output parameters
as default device input. That's what the man page says


# 1.46 20-Nov-2008 ratchov

take into account socket parameters into calculation of device
parameters; without this change aucat uses the device default
parameters and they are not necessarily usable for multi-stream
mode.


# 1.45 17-Nov-2008 jmc

- rename -s arg name to "socket"
- condense EXAMPLES into a single screenful

ok ratchov


# 1.44 17-Nov-2008 jmc

tweak previous man page changes, and sync usage();


# 1.43 17-Nov-2008 ratchov

allow aucat to run as server in play-only and record-only mode, so
it can be used on play-only, record-only and half-duplex devices.
ok jakemsr


# 1.42 16-Nov-2008 ratchov

allow aucat to listen on multiple sockets. Each socket carries its
channel and volume settings allowing multiple configuration to
coexist. Mostly useful for envy(4)-like devices, but can be used
to force different apps to use different settings.


# 1.41 16-Nov-2008 ratchov

add ``template'' parameters to sockets rather than using device
parameters. No functional change.


# 1.40 16-Nov-2008 ratchov

Make clients inherit the volume parameter when the -v option is used in
server mode. It gives the maximum volume a client may have. This wastes
dynamic range, but allows volume to stay constant when other clients
connect or disconnect.


# 1.39 12-Nov-2008 ratchov

when destroying the device, mark the mixer and the demultiplexer
for ``AUTOQUIT'' first, and then call file_eof() and friends.
fixes crashes sthen@ noticed on zaurus


# 1.38 11-Nov-2008 ratchov

if the device disappear the mixer and demultiplexer chains are
destroyed. If this happen then warn and exit, rather than crashing.


# 1.37 11-Nov-2008 ratchov

termninate non-server aucat when it's idle rather that using
the hackish {MIX,SUB}_AUTOQUIT features. This way we don't
have to care about possible references on destroyed objects.


# 1.36 11-Nov-2008 ratchov

when not in server mode, aucat terminates automatically using
the {MIX,SUB}_AUTAQUIT features. In this case we're not
allowed to directly touch dev_mix and dev_sub because they
can disappar. So disable the ``suspend'' and ``quit'' bits
when {MIX,SUB}_AUTAQUIT are used.


# 1.35 10-Nov-2008 ratchov

add a per-stream ``soft volume'' knob and the corresponding -v option.
The code will be useful later for the volume knob in the sndio API.


# 1.34 09-Nov-2008 ratchov

make aucat(1) stop automatically the audio(1) device if it's idle. This
way, when in server mode, it consumes no CPU if there are no clients.
Later, this will allow to start aucat(1) at session or system startup.


# 1.33 08-Nov-2008 ratchov

set the default device to /dev/audio for legacy mode.
From Thomas Pfaff <tpfaff _at_ agderlink.no> and tweaks from me


# 1.32 07-Nov-2008 ratchov

expose the block size in the sndio API by making par->round writable
and thus remove the ugly rate <-> block-size table from
sio_setpar(3). Handle the block size negociation in aucat(1), since
it has few constrains the code is overally simpler.
ok jakemsr@, major crank suggested by deraadt@


# 1.31 03-Nov-2008 ratchov

A small optimization: handle most N-channel <-> M-channel conversions
inside the mixer and the demultiplexer. This way, aucat will not
trigger the heavy conversion code when only channel conversions are
required. Cuts ~50% of the CPU usage on envy(4) devices, can improve
surround 4.0, 5.1 and 7.1 capable devices. No functionnal change.


# 1.30 26-Oct-2008 ratchov

zap `q' from the getopts string too,
requested by jmc@


# 1.29 26-Oct-2008 jmc

sync SYNOPSIS and usage();


# 1.28 26-Oct-2008 ratchov

add minimal server capability to aucat(1). When started in server
mode, it listens on an unix socket and mixes/demultiplexes any number
of full-duplex streams, doing necessary format conversions and
resampling on the fly.

programs can use the new libsa(3) library to play and record audio.
The library provides a very simple API to connect to the audio server;
if aucat(1) isn't running, it uses the audio(4) driver transparently
instead.


# 1.27 25-Aug-2008 sobrado

minor tweak for removal of "[-d devel]" (change done in r1.19 and r1.21).

ok ratchov@


# 1.26 14-Aug-2008 ratchov

move all device related stuff from aucat.c to a new dev.c file.
The new dev_xxx() routines expose a "high level" self-contained
interface to the device. At initialization, the device is opened
and two chains of aproc structures are created:

* a playback chain that exposes a (initially) empty mix
aproc to which the rest of the code can attach new
streams to be played

* record chain that exposes a (initially) empty sub aproc
to which the rest of the code can attach new stream to
to record

The rest of the code, has just to use dev_attach() routine to
attach streams. While we're at it, add a ``devops'' structure
containing pointers to the device-specific routines. This will
allow later to add support for other type of device than the
Sun API.

Also, write the .wav headers in file_del(), so put all header
related data in the file strucuture. This allows to close() the
file, as soon as wpipe_xxx() aproc terminates. This will be
useful for the server, because it will need to close() descripts
of closed connections immediately.

add mix_pushzero() routine to fill the mixer with silence. It
will be used to avoid the mixer to underrun when there are no
input streams. Since we always have at least one input stream
there's no behaviour change.

ok jakemsr


Revision tags: OPENBSD_4_4_BASE
# 1.25 02-Jun-2008 ratchov

document latest changes: -d flag is replaced by AUCAT_DEBUG
environment variable, new -xX options

bits from eric, ok jakemsr


# 1.24 02-Jun-2008 ratchov

currently, we start the device as soon as write() blocks; it's better
to continue filling the play buffer until it's full. This way, the
play buffer has fewer chances to underrun when the device is started.

ok jakemsr


# 1.23 02-Jun-2008 ratchov

if aucat is suspended, then the kernel continues running the device
(produces silence) and later, once aucat is resumed the kernel starts
dropping samples, it will try to drop as many samples as silence was
produced. So suspending breaks the aucat process permanently.

workaround this by blocking the signals ie disabling suspending of aucat
from the tty. The long term solution would be to catch SIGCONT and to stop
the device, resync/refill buffers and restart the device. That's really a
lot of work...

ok jakemsr


# 1.22 02-Jun-2008 ratchov

allow the user to choose the aucat behaviour when underruns/overruns
occur on a per-stream basis, using -Xx flags. There are 3 possible
policies:

- ignore : ignores underruns/overruns, for instance, this mode
could be used for creating simple pipes with utilities; like in
your last cdio diff.

- sync : insert/discard samples in order to keep all streams in
sync, useful for multi-tracker-like apps and/or to sync
midi/video/whatever on audio streams (this was the previous
behaviour)

- error : if overruns/underruns occur, consider it as fatal error
and kill the corresponding stream (without disturbing others).
Useful, for reliable recordings (and/or debugging aucat
itself:).

ok jakemsr


# 1.21 02-Jun-2008 ratchov

(remove -d, second part) use strtonum() instead of sscanf() and inlined
checks

suggested and ok jakemsr


# 1.20 02-Jun-2008 ratchov

since underruns/overruns are handled outside devices, remove code that
check for xruns in devices.

ok jakemsr


# 1.19 02-Jun-2008 ratchov

(remove -d, first part) replace -d flag by AUCAT_DEBUG environment
variable. This eases turning debugging on/off when aucat is started by
another program.

"i like the idea" jakemsr


# 1.18 26-May-2008 jmc

put -q in the right place;


# 1.17 26-May-2008 jakemsr

add -q option which enables "quiet" operation.

ok ratchov@


# 1.16 23-May-2008 jmc

various fixes for aucat.1, and sync usage(); ok ratchov


# 1.15 23-May-2008 ratchov

add support for:
- recording, full-duplex operation
- format conversions and resampling on the fly
- mixing on the fly of multiple inputs of different formats
- up to 16 channels, simplistic "routing" of channel ranges
- more linear encodings (in raw and wav files)
the old behaviour is fully preserved if none of the new -i and -o
options are used.

code and fixes from jakemsr@ and eric@, suggestions by others.
ok "go ahead" deraadt@


# 1.14 13-Apr-2008 jakemsr

- remove code that was rendered pointless by the last commit
- minor knf

from Pierre Riteau


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE
# 1.13 20-Mar-2007 uwe

Support for playing simple RIFF files (.wav) from Simon Effenberg, savar
at schuldeigen de, with some tweaks from me. ok millert, tests ckuethe,
liked by some disliked by nobody, but he's not even a real user :)


Revision tags: OPENBSD_4_1_BASE
# 1.12 20-Dec-2006 steven

remove unneeded #include

ok mbalmer@ deraadt@


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.11 20-Jan-2005 jaredy

- knf
- check read/write/etc. calls for errors
- use constants instead of hardcoded pathnames
- better variable types
- add usage output

suggestions from various, ok ian


# 1.10 13-Jan-2005 ian

Close what you open: obvious fix, ok jared@, more to follow


Revision tags: OPENBSD_3_5_BASE OPENBSD_3_6_BASE
# 1.9 20-Oct-2003 jmc

typos from Jared Yanovich, slightly ammended;


Revision tags: OPENBSD_3_4_BASE
# 1.8 10-Jul-2003 david

add missing includes
ok deraadt@ tedu@


# 1.7 02-Jul-2003 deraadt

protos


# 1.6 10-Jun-2003 deraadt

mostly ansi cleanup; pval ok


Revision tags: OPENBSD_3_3_BASE
# 1.5 09-Dec-2002 millert

From Andrushock, s/sucess/success/g


Revision tags: OPENBSD_2_8_BASE OPENBSD_2_9_BASE OPENBSD_3_0_BASE OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.4 01-Jul-2000 millert

Add -f option to specify audio device, honor AUDIODEVICE environment vairable,
and some KNF.


Revision tags: OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE
# 1.3 05-Jan-1997 kstailey

minor tweaks; htonl() -> ntohl(); added SEE ALSO


# 1.2 03-Jan-1997 kstailey

removed unnecessary test


# 1.1 02-Jan-1997 kstailey

new utility for playing sound files


# 1.179 01-Feb-2024 ratchov

aucat: Fix MIDI control of the levels of individual files

Change the order of slot_list to match the order of the -io options on
the command-line and fix the way slot_list searched using the MIDI
channel number of the CC-07 message.

Reported and analysed by Dirk-Wilhelm Peters <peters at schwertfisch.de>

Thanks!


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE OPENBSD_7_3_BASE OPENBSD_7_4_BASE
# 1.178 07-Mar-2022 ratchov

Switch internal sample representation to 24-bit fixed-point

Note that this also changes the default file encoding to 24-bit.


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.177 12-Jan-2021 naddy

const-ify lookup tables; ok ratchov@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.176 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.175 19-May-2019 ratchov

Fix integer overflow in block size calculation.

Fixes truncation of contents of files with rate above 64kHz
generate in off-line mode (i.e. -n option).


Revision tags: OPENBSD_6_5_BASE
# 1.174 21-Feb-2019 ratchov

Use reallocarray in place of malloc(a * b) to handle possible
overflow in multiplication of malloc argument.


Revision tags: OPENBSD_6_4_BASE
# 1.173 13-May-2018 ratchov

Add missing pledge(). From Jesper Wallin <jesper at ifconfig.se>.
Thanks!


Revision tags: OPENBSD_6_3_BASE
# 1.172 07-Nov-2017 ratchov

prefill with silence the buffer where cmap_copy() stores samples
rather than the file buffer. Fixes initialized data being treated as
recorded samples when resampling or format conversions are involved
but mono->stereo conversion is disabled.


# 1.171 03-Nov-2017 ratchov

Prefill rec buffer with silence. This is necessary because certain
channels don't get samples, for instance when mono->stereo conversion
is disabled.


Revision tags: OPENBSD_6_2_BASE
# 1.170 20-Jul-2017 ratchov

Fix integer overflow that would causes >12 hours files
to not properly relocate. Found with coverity.


Revision tags: OPENBSD_6_1_BASE
# 1.169 03-Jan-2017 ratchov

Assert we're not freeing buffers we didn't allocate (DEBUG mode).


# 1.168 03-Jan-2017 ratchov

Log play volume together with other play parameters.


# 1.167 30-Sep-2016 ratchov

Set the device rate to the calculated rate. Otherwise, the signal
may endup resampled twice.


# 1.166 30-Sep-2016 ratchov

Remove unnecessary includes. From Michael W. Bombardieri <mb at ii.net>.


# 1.165 27-Sep-2016 ratchov

Don't rely on the resampling code to calculate the number of samples
to process, as it may produce one extra sample (to handle accumulation
of fractional samples), which would cause access to one sample past
the end of the buffer and crash aucat. Fix this by limiting the number
of samples processed to a single block.

Found by and help from Michael W. Bombardieri <mb at ii.net>. Thanks.


Revision tags: OPENBSD_6_0_BASE
# 1.164 07-Jun-2016 ratchov

Add -g and -p options to control device and file start position.


# 1.163 07-Jun-2016 ratchov

Add resamp_getcnt() routine to calculate the exact number of samples
that would be consumed and produced by the sampler rate converter. Use
it to avoid partial samples that are not properly handled. Fixes
last samples of certain files causing aucat to abort.


# 1.162 02-Jun-2016 ratchov

remove forgotten debug printfs


# 1.161 02-Jun-2016 ratchov

set exit status to 1 if -n is used without -i and -o


# 1.160 31-May-2016 ratchov

Pass hr:min:sec:frame.cent to dev_mmcloc() directly and remove the
useless intermediate conversion from and to 2400-th of seconds.


# 1.159 31-May-2016 ratchov

To store MMC position, use samples (at device rate) as time units
instead of 2400-ths of second. No behaviour change.


# 1.158 28-May-2016 ratchov

Fix file block size rounding and ensure it's large enough to store a
full audio block.


# 1.157 27-May-2016 ratchov

When resampling, use the exact resampling factor instead of the ratio
between input and output block sizes. This was inherited from sndiod,
but is not required for files because they are continuous streams of
samples and do not need to be split in blocks of equal duration.

This change makes playback/recording rate match exactly the requested
sample rate.


# 1.156 27-May-2016 ratchov

Flush rec buffer if there's less than one block space left and refill
play buffer if there's less than one block of data left. This is the
correct condition in the general case. No behaviour change, as all
input/output is multiple of the block size.


# 1.155 27-May-2016 ratchov

Simplify slot_fill() and slot_flush(). No behaviour change


# 1.154 27-May-2016 ratchov

Make resamp_do() get the exact number input and output samples and
provide routines to calculate them. This way we don't rely on it
to calculate the bytes procuded/consumed anymore. No behaviour change.


# 1.153 26-May-2016 ratchov

Make format conversion routines return the number of frames
consumed from both input and output buffers. No behaviour change.


Revision tags: OPENBSD_5_9_BASE
# 1.152 29-Jan-2016 ratchov

Check that byte order is native only if we're using more than one
byte per sample, check that samples are lsb-aligned only if there's
padding. Fixes unsupported parameters not being detected if compiled
in 24-bit mode and sndiod is not running.


# 1.151 10-Jan-2016 ratchov

trailing spaces, fit in 80 columns


# 1.150 09-Nov-2015 mmcc

Remove xfree(). From Michael W. Bombardieri.

ok ratchov@


# 1.149 27-Aug-2015 ratchov

fix typos in comments and error messages


Revision tags: OPENBSD_5_8_BASE
# 1.148 16-May-2015 ratchov

Call slot_stop() after the play buffer is drained to properly cleanup
the slot. Fixes periodic glitches occurring after eof is reached, and
then playback restarted with a MMC-relocate and MMC-start.


Revision tags: OPENBSD_5_7_BASE
# 1.147 21-Jan-2015 jmc

fix macro breakage and update usage();


# 1.146 21-Jan-2015 ratchov

Simplify internals and rewrite file header parsing code:
- remove -M, -t, -w, -C, -x flags that don't make sense anymore
- make "-j off" the default (sndiod already does the job)
- don't limit the number of played/recorded files.
- add support for floating-point encoded files.
- add support for apple .aiff and sun/next .au files


# 1.145 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.144 18-Nov-2013 ratchov

remove unused variables


# 1.143 18-Nov-2013 ratchov

add missing prototypes


# 1.142 12-Nov-2013 ratchov

remove bogus legacy server-specific bits that cause confusion


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.141 03-Dec-2012 ratchov

Remove server capability description from the documentation and from
the usage string, and just say that for historic reasons aucat behaves
like sndiod.
with help from jmc@


# 1.140 23-Nov-2012 ratchov

Since there's no sndiod -> aucat link anymore, allow aucat to be run
in server mode even when not invoked as sndiod. This allows aucat and
the new sndiod daemon to live together in /usr/bin, which makes easy
to quickly compare sndiod and aucat (and spot possible regressions)
without having to modify /usr/bin.


# 1.139 27-Oct-2012 ratchov

don't stat & test sndio home directory since we never use it.


# 1.138 27-Oct-2012 ratchov

don't (re-)define SNDIO_USER and SNDIO_PRIO if they are already defined,
this way they can be defined at compilation time


Revision tags: OPENBSD_5_2_BASE
# 1.137 27-Jun-2012 ratchov

Uniformize error messages printing, use err() instead of fprintf()
when appropriate. From Michael W. Bombardieri <mb at ii.net>


# 1.136 19-Jun-2012 ratchov

Remove hidden options that are not needed any more,
from Remco <remco at d-compu.dyndns.org>. Thanks


# 1.135 23-May-2012 ratchov

Make the "default" string a valid device name that has the same effect
as NULL has. This will (hopefully) simplify ports where the user
passes the device string.


# 1.134 11-Apr-2012 ratchov

Use "unsigned int" rather than "unsigned". No object change.
suggested by deraadt@


Revision tags: OPENBSD_5_1_BASE
# 1.133 09-Feb-2012 ratchov

Crank the default buffer size to 160ms (ie 7680 frames at 48kHz).
Workarounds stuttering caused by something hogging the cpu in kernel
mode and preventing sndiod from running at time.
ok deraadt


# 1.132 26-Jan-2012 ratchov

Make the default rate 48kHz and the default block size 10ms. These
settings ensure video players and programs using MTC are smooth by
default. Thanks to all who tested.


# 1.131 10-Jan-2012 ratchov

When a sub-device is created (-s), check if a hardware
device was already created (-f) before trying to use the
default one, otherwise all sub-devices end up attached
to the default device. Noticed by peters at schwertfisch.de,
thanks!


# 1.130 09-Dec-2011 ratchov

fix usage string: remove -M from aucat and -l from aucat and sndiod
from jmc


# 1.129 09-Dec-2011 ratchov

add a new sndiod audio/midi daemon and hide aucat server-specific
options.


# 1.128 02-Dec-2011 ratchov

don't use a "server" flag, just check if we expose sub-devices


# 1.127 02-Dec-2011 ratchov

reuse midi-control code to implement midi thru boxes and remove
the old midithru implementation; less code, less bugs. As a side
effect, midi output doesn't implement running status "compression"
any more.


# 1.126 20-Nov-2011 ratchov

Move mmc/mtc and volume control bits from struct aproc to struct
dev. Allows volume settings to be saved while the device is kept
closed. Besides that, no behabiour changes.


# 1.125 15-Nov-2011 ratchov

Add a "device number" component in sndio(7) device names, allowing a
single aucat instance to handle all audio and MIDI services. Since
this partially breaks compatibility, this is a opportunitiy to fix few
other design mistakes (eg ':' being used by inet6, type name vs api
name confusion, etc..). This leads to the following names:

type[@hostname][,unit]/devnum[.option]

The device number is the minor device number for direct hardware
access (ie the 'N' in /dev/audioN). For aucat, this is the occurence
number of the -f (or -M) option.

There's a compatibility hook to keep old names working if only one
aucat server is running.


# 1.124 18-Oct-2011 ratchov

keep the device closed by default (when not used)


# 1.123 18-Oct-2011 ratchov

remove duplicate options in getopt() string


# 1.122 17-Oct-2011 ratchov

Remove midicat since aucat can now be used instead of midicat
with almost the same syntax (roughly an extra -M option).
Thru boxes are created with aucat, and corresponding MIDI port
names have the "aucat" prefix instead of "midithru". The old
device name will still work some time for backward compatibility.

ok deraadt


# 1.121 12-Oct-2011 jmc

- avoid line wrapping an example
- sort usage() for midicat
- tweak a bit of unclear wording

ok ratchov


# 1.120 12-Oct-2011 ratchov

Simplify and improve the way options are parsed and remove ~300 lines
of code that becomes unused. Few command line arguments changes are
required though:

- stream definitions (-ios) now must follow devices definitions they
are attached to (-fMn)

- the -n option is now a special "loopback" device and is thus used
like -f, eg it must precede streams

- in midicat, midi thru boxes are not created automatically anymore,
the new "-M" option must be used for that

- channel numbers (-Cc options) correspond always to channel numbers
of the hardware.

- the -u option isn't needed anymore

- increase the log verbosity so user errors are logged
without using -d

tested by many, help from jmc


Revision tags: OPENBSD_5_0_BASE
# 1.119 29-Jun-2011 ratchov

make -u flag per-device similarly to -a and -w


# 1.118 20-Jun-2011 ratchov

Make -aoff option apply to MIDI ports (-q) as well, ensuring the device
stays closed also if -q is used. As we're at it, add -a to midicat so
it behaves like aucat.


# 1.117 03-Jun-2011 ratchov

Allow device encoding to be set if -u is also used. Useful to degrade
quality to save bandwith when a remote audio device is used.


# 1.116 03-Jun-2011 ratchov

Reorganize the way command line options are parsed and stored.
No behaviour change.


# 1.115 26-May-2011 ratchov

add a new -w flag to control whether master volume is automatically
adjusted when new streams are connected and disconnected. Disabling
automatic volume adjustment makes sense if all streams are recorded
with properly lowered volumes.


# 1.114 10-May-2011 ratchov

fix option handling and enable TCP in midicat
from peters at schwertfisch.de, thanks!


# 1.113 28-Apr-2011 ratchov

Implement a new authentication method allowing aucat and midicat to
work over TCP, for instance, to expose the sound card of one machine
with other machines of the network.

The first client generates a 128-bit random number (aka the
session cookie), saves it in $HOME/.aucat_cookie and sends it to the
server. Successive clients load the cookie from $HOME/.aucat_cookie
and send it to the server but the server accepts only clients whose
cookie matches the session cookie. When all clients are gone, the
session is over, and another cookie could start a new session, and so
on.

TCP is enabled on the server with the new -L option, and on the client
side hostnames are specified with a new optional component in the
device name.

hints from damien, dlg and deraadt, tweaks from jmc


# 1.112 27-Apr-2011 ratchov

move amsg.h containing protocol defs from aucat side to libsndio side.

requested by deraadt


# 1.111 27-Apr-2011 deraadt

use more volatile sig_atomic_t in signal handlers
ok ratchov


# 1.110 19-Apr-2011 ratchov

don't hold a pointer to "listen" structure. Instead, when it must
be free()ed, iterate over the file_list, and find the structure
to free. This is safer and simpler. No behaviour change


# 1.109 16-Apr-2011 ratchov

for unix domain socket addresses use AUCAT_PATH and MIDICAT_PATH
macros instead of hardcoded strings. No object change


# 1.108 17-Mar-2011 ratchov

use a DEFAULT_RATE macro instead of hardcoding 44100, no object change


Revision tags: OPENBSD_4_9_BASE
# 1.107 05-Nov-2010 ratchov

unbreak compilation without DEBUG defined


# 1.106 21-Oct-2010 ratchov

use MODE_xxx and XRUN_xxx macros everywhere, and zap corresponding
AMSG_xxx macros, which in turns simplifies the code


# 1.105 08-Sep-2010 deraadt

aucat: getpwnam: No such file or directory
is not a proper error message.


# 1.104 20-Aug-2010 ratchov

polish the code: remove few unused #includes, add missing ones,
fix NULL vs 0, etc. No behaviour change.


# 1.103 19-Aug-2010 ratchov

fix NULL pointer deref in midicat option parsing when ``-'' is
used as filename


Revision tags: OPENBSD_4_8_BASE
# 1.102 31-Jul-2010 ratchov

remove 2 years old compatibility mode


# 1.101 10-Jul-2010 ratchov

In midi server mode, don't terminate the midi thru box if there are
no connections anymore.


# 1.100 10-Jul-2010 ratchov

fix parameter handling: don't try to open a ``default'' midi port
if no files are given on the command line


# 1.99 06-Jul-2010 jmc

various tweaks to previous; ok ratchov


# 1.98 06-Jul-2010 ratchov

Handle all streams the same way because there's no actual
difference between audio files and client connections. Clean up
the way command line options are handled and clarify this in the
manual page: stream parameters (-Ccehjmrtvx) must precede stream
definitions (-ios) and per-device parameters (-abz) and stream
definitions (-ios) must precede device definitions (-f). Since
there's no ``server'' and ``non-server'' modes anymore, make the
-l option just detach the process.

ok and help from jakemsr and jmc


# 1.97 29-Jun-2010 jakemsr

If the device isn't opened yet (as can be the case with '-a off'),
don't check if the midi control interface is idle
from ratchov


# 1.96 25-Jun-2010 ratchov

don't attempt to drain devices after they are destroyed, which
results in a use after free(). Catched by jakemsr@ with MALLOC_OPTIONS=J


# 1.95 20-Jun-2010 ratchov

fix midicat(1) device mode when only -i or only -o is used (ie
open the device input-only if -o is used, and output-only if -i is
used).


# 1.94 05-Jun-2010 ratchov

don't prime server buffers, because it's ugly and conceptually
complicated. Instead, request clients to provide enough samples
and start with buffers full.


# 1.93 05-Jun-2010 ratchov

Don't send the initial position as a clock tick with a negative delta.
Add a new ``initial position'' message which is simpler. No bahaviour
change, except effects of the protocol version crank.


# 1.92 04-Jun-2010 ratchov

Allow the audio device to be opened only while it's actually used.
This is necessary for uaudio devices, for instance to start aucat
before the device is plugged. Or to unplug a device whithout
having to restart aucat when another device is plugged. This is
controlled with the new -a option.

Allow multiple audio devices to be used concurently, i.e.
multiple ``-f devname'' options to be used; -f options must follow
per-device options, which is what we do for other options.


# 1.91 08-May-2010 ratchov

prime play buffers as soon as the device is opened, otherwise a
midi client could start it with empty buffers, in turn hurting
audio clients.


# 1.90 02-May-2010 ratchov

Clean up device handling code to clarify different initialization phases
and different device states. Split initialization in two phases:
first global variables are initialized then the audio hardware is opened.
Allow devices that don't support full-duplex to work in play-only or
record-only mode, even if ``-m play'' or ``-m rec'' are not specified.


# 1.89 02-May-2010 ratchov

Don't systematically fill with silence the mixer output. This
might result in extra samples being written to the device
when the mixer is closed.


# 1.88 24-Apr-2010 ratchov

if there are no files to play in legacy mode, print the usage message


# 1.87 24-Apr-2010 ratchov

rename ibuflist/obuflist -> ins/outs to cut long lines
from Thomas Pfaff, thanks


# 1.86 22-Apr-2010 ratchov

Allow multiple users to share the same aucat server. If aucat is
run by root, it binds a shared address to the socket, cranks the
process priority and drops privileges. sio_open(3) will try to
connect to the private socket first (if any), then to the shared
socket. Only one user may have connections to aucat at a given
time.

based on discussions with henning, pyr and others
ok jacek, deraadt


# 1.85 21-Apr-2010 ratchov

when creating the ``default'' subdevice, use last parameters of the
command line, instead of the device parameters.


# 1.84 21-Apr-2010 ratchov

adds the necessary bits to join channels (ex stereo->mono) or
expand channels (ex mono->stereo). It's switched on/off with
the ``-j'' option, (default is "on").


# 1.83 06-Apr-2010 ratchov

aucat (server):

- make the ``-m mode'' option per subdevice, allowing
subdevices to be play-only or rec-only even if the
server is full-duplex

- add ``monitoring'' mode (with ``-m mon''). This is a
record-only stream from which played streams can be
recorded (kind of ``record what you hear'').

- allow MIDI devices to be subscribed to the controlling
MIDI port of the server, ie what midicat does (with -f
option), but using the -q option.

- add flow control to the protocol, and serialize clock
ticks (sio_onmove() calls) and data chunks. This should
fix certain full-duplex programs, broken with ``magic''
block/buffer size combinations.

- enable 3 block latency which is the minimum theoretical.
Make -z and -b options correspond to device parameters.

- make sio_getcap(3) the same for aucat and sun backends,
ie return whatever is supported (``everything'' in the
aucat case, since everything is actulally supported).

aucat (player):

- enable ``-m mode'' option to select between monitoring
and recording when ``-o file'' is used.

- plug MIDI code to non-server codepath. The MIDI control
device is specified with the ``-q device'' option, as in
server mode.

- impliment lseek()'ing within files (controlled
through MIDI). Necessary to use aucat with a MIDI
sequencer.

midicat (thrubox):

- rename ``-f'' flag to ``-q'', so it has the
same name as in aucat (-f is still working)

ok jakemsr@, tweaks from jmc@


# 1.82 03-Apr-2010 ratchov

backout last change, committed by mistake, sorry...


# 1.81 03-Apr-2010 ratchov

doc fixes from jmc


Revision tags: OPENBSD_4_7_BASE
# 1.80 14-Jan-2010 ratchov

Use -C option to specify recorded channel range instead of -c which
is for played channels.

spotted by Jan Stary <hans at stare.cz>, thanks


# 1.79 11-Jan-2010 ratchov

When resuming from suspend, prime play buffers with silence,
otherwise the client would underrun by more than its own buffer
size, which currently is not allowed (causes the client to
deadlock).

Set the minimum client buffer size to two blocks. Single block
buffers don't work properly yet.

with help from jakemsr


# 1.78 10-Jan-2010 ratchov

Add traces to help tracking subtle timing bugs often causing
underuns and overruns; such bugs are hard to debug with ktrace
or a debugger. They are also handy to debug or monitor code
using aucat.

To enable traces, compile aucat with ``make DEBUG=-DDEBUG'' and
use the -d option multiple times.

ok jakemsr


# 1.77 05-Jan-2010 ratchov

Make the way .wav files are handled closer to the way sockets are
handled, which is conceptually simpler. No behaviour changes.


# 1.76 21-Nov-2009 ratchov

use strtonum(3) and strtol(3) instead of sscanf(3)
suggested by deraadt@


# 1.75 21-Nov-2009 deraadt

spelling


# 1.74 03-Nov-2009 ratchov

Allow any program using aucat to act as MMC slave and MTC master
transparently. Multiple audio applications can be started
synchronously from external software/hardware supporting the
standard Start/Stop/Relocate messages. The server clock is exposed
through MTC, allowing non-audio software/hardware to be
synchronized to audio applications.


# 1.73 27-Oct-2009 ratchov

add missing word in error message


# 1.72 10-Oct-2009 ratchov

don't use a references to the device file to check the current mode


# 1.71 10-Oct-2009 ratchov

make dev_done() common to audio and MIDI, and drop dev_thrudone()


# 1.70 10-Oct-2009 ratchov

use dev_done() for loopback devices too and remove dev_loopdone()


# 1.69 05-Oct-2009 ratchov

add -d flag. When started in server mode, if the -d flag is
used aucat and midicat don't daemonize and log on stderr.


# 1.68 27-Sep-2009 ratchov

remove all debug traces


# 1.67 26-Aug-2009 jmc

oops, forgot to update usage();


# 1.66 19-Aug-2009 ratchov

don't try to process -o arguments using the input file list, fixes
midicat crashing sometimes when -o is used


# 1.65 19-Aug-2009 ratchov

organize midi code like audio code, no functional change


# 1.64 17-Aug-2009 ratchov

allow midicat to take multiple -i and -o options


# 1.63 17-Aug-2009 ratchov

if one or more MIDI devices are specified in server mode, subscribe
them to the MIDI thru box


# 1.62 25-Jul-2009 ratchov

fix comments, remove no longer needed declaration, reorder includes
from Thomas Pfaff


# 1.61 25-Jul-2009 ratchov

Currently midi capable programs can control midi hardware, but
cannot cooperate with other programs. The aim of this change is
to allow any program to send midi data to other programs as they
were midi hardware. For instance, this change should solve the
longstanding problem of using a midi sequencer with software
synthesizers. More precisely:

- new midicat(1) utility (actually hardlink to aucat(1)).
it creates software midi thru boxes, allowing programs
to send midi messages to other programs as they were
midi(4) hardware.

- new midi api in libsndio (see mio_open(3)), to access
midi(4) devices and midicat(1) sockets in a uniform way.

- new device naming scheme <service>:<unit>[.<option>],
common to audio and midi.

- new sndio(7) manual describing concepts and naming

The current audio device naming still works, but people having
scripts or configuration files containing device names could read
the sndio(7) man page and slowly start updating device names.

discussed with jakemsr@ and deraadt@, help form jmc@


Revision tags: OPENBSD_4_6_BASE
# 1.60 27-Apr-2009 ratchov

make -v option apply also to implicit default socket


# 1.59 17-Mar-2009 jmc

- rename the arg to -b "nframes" and sync usage()
- grammar fix for the server section

from Thomas Pfaff
ok ratchov


Revision tags: OPENBSD_4_5_BASE
# 1.58 06-Feb-2009 ratchov

move error messages reported to user into main()


# 1.57 04-Feb-2009 ratchov

if there are too many connections, stop acceping new ones rather
than exit()ing with ``too many open files'' fatal error


# 1.56 04-Feb-2009 ratchov

daemonize when in server mode, suggested by many
ok jakemsr


# 1.55 03-Feb-2009 ratchov

in server mode, create /tmp/aucat-userid/ directory with
permissions 0700, and create sockets in it. This prevents
one local user to eavesdrop or disturb audio programs of
other users.

if you're using the ``-s socket'' option with an absolute
path as argument, please update it to use a socket name.

requested by many, bits from jakemsr and otto
ok jakemsr


# 1.54 25-Jan-2009 ratchov

don't set malloc_options to not interfer with /etc/malloc.conf
requested and explained by fgsch@, henning@, otto@


# 1.53 23-Jan-2009 ratchov

catch SIGHUP and SIGTERM too, to ensure that recorded files are
properly closed and sockets are cleaned up.


# 1.52 23-Jan-2009 ratchov

spacing + typo fix from Matt Jibson <matt.jibson(at)gmail.com>


# 1.51 10-Jan-2009 ratchov

add "loopback" mode in which input is connected to the output.
This is useful to mix, demultiplex, resample or reencode audio
files off-line.
tweak + ok jakemsr


# 1.50 29-Dec-2008 ratchov

make the code "more correct": add reference counters to aproc
structures so we can keep a pointer to the device. Beside some
simplifications, this allows to easily handle the situation
where the audio device desappears. No change in the behaviour.


# 1.49 26-Dec-2008 ratchov

*** empty log message ***


# 1.48 07-Dec-2008 ratchov

When resampling, don't require the sample frequency to be an integer.
This removes the arithmetic constraint between the sample frequency
and the block size and all the associated code. Now aucat can work
in server mode with any block size.
ok jakemsr


# 1.47 23-Nov-2008 ratchov

Use file input parameter as default device output and file output parameters
as default device input. That's what the man page says


# 1.46 20-Nov-2008 ratchov

take into account socket parameters into calculation of device
parameters; without this change aucat uses the device default
parameters and they are not necessarily usable for multi-stream
mode.


# 1.45 17-Nov-2008 jmc

- rename -s arg name to "socket"
- condense EXAMPLES into a single screenful

ok ratchov


# 1.44 17-Nov-2008 jmc

tweak previous man page changes, and sync usage();


# 1.43 17-Nov-2008 ratchov

allow aucat to run as server in play-only and record-only mode, so
it can be used on play-only, record-only and half-duplex devices.
ok jakemsr


# 1.42 16-Nov-2008 ratchov

allow aucat to listen on multiple sockets. Each socket carries its
channel and volume settings allowing multiple configuration to
coexist. Mostly useful for envy(4)-like devices, but can be used
to force different apps to use different settings.


# 1.41 16-Nov-2008 ratchov

add ``template'' parameters to sockets rather than using device
parameters. No functional change.


# 1.40 16-Nov-2008 ratchov

Make clients inherit the volume parameter when the -v option is used in
server mode. It gives the maximum volume a client may have. This wastes
dynamic range, but allows volume to stay constant when other clients
connect or disconnect.


# 1.39 12-Nov-2008 ratchov

when destroying the device, mark the mixer and the demultiplexer
for ``AUTOQUIT'' first, and then call file_eof() and friends.
fixes crashes sthen@ noticed on zaurus


# 1.38 11-Nov-2008 ratchov

if the device disappear the mixer and demultiplexer chains are
destroyed. If this happen then warn and exit, rather than crashing.


# 1.37 11-Nov-2008 ratchov

termninate non-server aucat when it's idle rather that using
the hackish {MIX,SUB}_AUTOQUIT features. This way we don't
have to care about possible references on destroyed objects.


# 1.36 11-Nov-2008 ratchov

when not in server mode, aucat terminates automatically using
the {MIX,SUB}_AUTAQUIT features. In this case we're not
allowed to directly touch dev_mix and dev_sub because they
can disappar. So disable the ``suspend'' and ``quit'' bits
when {MIX,SUB}_AUTAQUIT are used.


# 1.35 10-Nov-2008 ratchov

add a per-stream ``soft volume'' knob and the corresponding -v option.
The code will be useful later for the volume knob in the sndio API.


# 1.34 09-Nov-2008 ratchov

make aucat(1) stop automatically the audio(1) device if it's idle. This
way, when in server mode, it consumes no CPU if there are no clients.
Later, this will allow to start aucat(1) at session or system startup.


# 1.33 08-Nov-2008 ratchov

set the default device to /dev/audio for legacy mode.
From Thomas Pfaff <tpfaff _at_ agderlink.no> and tweaks from me


# 1.32 07-Nov-2008 ratchov

expose the block size in the sndio API by making par->round writable
and thus remove the ugly rate <-> block-size table from
sio_setpar(3). Handle the block size negociation in aucat(1), since
it has few constrains the code is overally simpler.
ok jakemsr@, major crank suggested by deraadt@


# 1.31 03-Nov-2008 ratchov

A small optimization: handle most N-channel <-> M-channel conversions
inside the mixer and the demultiplexer. This way, aucat will not
trigger the heavy conversion code when only channel conversions are
required. Cuts ~50% of the CPU usage on envy(4) devices, can improve
surround 4.0, 5.1 and 7.1 capable devices. No functionnal change.


# 1.30 26-Oct-2008 ratchov

zap `q' from the getopts string too,
requested by jmc@


# 1.29 26-Oct-2008 jmc

sync SYNOPSIS and usage();


# 1.28 26-Oct-2008 ratchov

add minimal server capability to aucat(1). When started in server
mode, it listens on an unix socket and mixes/demultiplexes any number
of full-duplex streams, doing necessary format conversions and
resampling on the fly.

programs can use the new libsa(3) library to play and record audio.
The library provides a very simple API to connect to the audio server;
if aucat(1) isn't running, it uses the audio(4) driver transparently
instead.


# 1.27 25-Aug-2008 sobrado

minor tweak for removal of "[-d devel]" (change done in r1.19 and r1.21).

ok ratchov@


# 1.26 14-Aug-2008 ratchov

move all device related stuff from aucat.c to a new dev.c file.
The new dev_xxx() routines expose a "high level" self-contained
interface to the device. At initialization, the device is opened
and two chains of aproc structures are created:

* a playback chain that exposes a (initially) empty mix
aproc to which the rest of the code can attach new
streams to be played

* record chain that exposes a (initially) empty sub aproc
to which the rest of the code can attach new stream to
to record

The rest of the code, has just to use dev_attach() routine to
attach streams. While we're at it, add a ``devops'' structure
containing pointers to the device-specific routines. This will
allow later to add support for other type of device than the
Sun API.

Also, write the .wav headers in file_del(), so put all header
related data in the file strucuture. This allows to close() the
file, as soon as wpipe_xxx() aproc terminates. This will be
useful for the server, because it will need to close() descripts
of closed connections immediately.

add mix_pushzero() routine to fill the mixer with silence. It
will be used to avoid the mixer to underrun when there are no
input streams. Since we always have at least one input stream
there's no behaviour change.

ok jakemsr


Revision tags: OPENBSD_4_4_BASE
# 1.25 02-Jun-2008 ratchov

document latest changes: -d flag is replaced by AUCAT_DEBUG
environment variable, new -xX options

bits from eric, ok jakemsr


# 1.24 02-Jun-2008 ratchov

currently, we start the device as soon as write() blocks; it's better
to continue filling the play buffer until it's full. This way, the
play buffer has fewer chances to underrun when the device is started.

ok jakemsr


# 1.23 02-Jun-2008 ratchov

if aucat is suspended, then the kernel continues running the device
(produces silence) and later, once aucat is resumed the kernel starts
dropping samples, it will try to drop as many samples as silence was
produced. So suspending breaks the aucat process permanently.

workaround this by blocking the signals ie disabling suspending of aucat
from the tty. The long term solution would be to catch SIGCONT and to stop
the device, resync/refill buffers and restart the device. That's really a
lot of work...

ok jakemsr


# 1.22 02-Jun-2008 ratchov

allow the user to choose the aucat behaviour when underruns/overruns
occur on a per-stream basis, using -Xx flags. There are 3 possible
policies:

- ignore : ignores underruns/overruns, for instance, this mode
could be used for creating simple pipes with utilities; like in
your last cdio diff.

- sync : insert/discard samples in order to keep all streams in
sync, useful for multi-tracker-like apps and/or to sync
midi/video/whatever on audio streams (this was the previous
behaviour)

- error : if overruns/underruns occur, consider it as fatal error
and kill the corresponding stream (without disturbing others).
Useful, for reliable recordings (and/or debugging aucat
itself:).

ok jakemsr


# 1.21 02-Jun-2008 ratchov

(remove -d, second part) use strtonum() instead of sscanf() and inlined
checks

suggested and ok jakemsr


# 1.20 02-Jun-2008 ratchov

since underruns/overruns are handled outside devices, remove code that
check for xruns in devices.

ok jakemsr


# 1.19 02-Jun-2008 ratchov

(remove -d, first part) replace -d flag by AUCAT_DEBUG environment
variable. This eases turning debugging on/off when aucat is started by
another program.

"i like the idea" jakemsr


# 1.18 26-May-2008 jmc

put -q in the right place;


# 1.17 26-May-2008 jakemsr

add -q option which enables "quiet" operation.

ok ratchov@


# 1.16 23-May-2008 jmc

various fixes for aucat.1, and sync usage(); ok ratchov


# 1.15 23-May-2008 ratchov

add support for:
- recording, full-duplex operation
- format conversions and resampling on the fly
- mixing on the fly of multiple inputs of different formats
- up to 16 channels, simplistic "routing" of channel ranges
- more linear encodings (in raw and wav files)
the old behaviour is fully preserved if none of the new -i and -o
options are used.

code and fixes from jakemsr@ and eric@, suggestions by others.
ok "go ahead" deraadt@


# 1.14 13-Apr-2008 jakemsr

- remove code that was rendered pointless by the last commit
- minor knf

from Pierre Riteau


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE
# 1.13 20-Mar-2007 uwe

Support for playing simple RIFF files (.wav) from Simon Effenberg, savar
at schuldeigen de, with some tweaks from me. ok millert, tests ckuethe,
liked by some disliked by nobody, but he's not even a real user :)


Revision tags: OPENBSD_4_1_BASE
# 1.12 20-Dec-2006 steven

remove unneeded #include

ok mbalmer@ deraadt@


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.11 20-Jan-2005 jaredy

- knf
- check read/write/etc. calls for errors
- use constants instead of hardcoded pathnames
- better variable types
- add usage output

suggestions from various, ok ian


# 1.10 13-Jan-2005 ian

Close what you open: obvious fix, ok jared@, more to follow


Revision tags: OPENBSD_3_5_BASE OPENBSD_3_6_BASE
# 1.9 20-Oct-2003 jmc

typos from Jared Yanovich, slightly ammended;


Revision tags: OPENBSD_3_4_BASE
# 1.8 10-Jul-2003 david

add missing includes
ok deraadt@ tedu@


# 1.7 02-Jul-2003 deraadt

protos


# 1.6 10-Jun-2003 deraadt

mostly ansi cleanup; pval ok


Revision tags: OPENBSD_3_3_BASE
# 1.5 09-Dec-2002 millert

From Andrushock, s/sucess/success/g


Revision tags: OPENBSD_2_8_BASE OPENBSD_2_9_BASE OPENBSD_3_0_BASE OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.4 01-Jul-2000 millert

Add -f option to specify audio device, honor AUDIODEVICE environment vairable,
and some KNF.


Revision tags: OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE
# 1.3 05-Jan-1997 kstailey

minor tweaks; htonl() -> ntohl(); added SEE ALSO


# 1.2 03-Jan-1997 kstailey

removed unnecessary test


# 1.1 02-Jan-1997 kstailey

new utility for playing sound files


# 1.178 07-Mar-2022 ratchov

Switch internal sample representation to 24-bit fixed-point

Note that this also changes the default file encoding to 24-bit.


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.177 12-Jan-2021 naddy

const-ify lookup tables; ok ratchov@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.176 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.175 19-May-2019 ratchov

Fix integer overflow in block size calculation.

Fixes truncation of contents of files with rate above 64kHz
generate in off-line mode (i.e. -n option).


Revision tags: OPENBSD_6_5_BASE
# 1.174 21-Feb-2019 ratchov

Use reallocarray in place of malloc(a * b) to handle possible
overflow in multiplication of malloc argument.


Revision tags: OPENBSD_6_4_BASE
# 1.173 13-May-2018 ratchov

Add missing pledge(). From Jesper Wallin <jesper at ifconfig.se>.
Thanks!


Revision tags: OPENBSD_6_3_BASE
# 1.172 07-Nov-2017 ratchov

prefill with silence the buffer where cmap_copy() stores samples
rather than the file buffer. Fixes initialized data being treated as
recorded samples when resampling or format conversions are involved
but mono->stereo conversion is disabled.


# 1.171 03-Nov-2017 ratchov

Prefill rec buffer with silence. This is necessary because certain
channels don't get samples, for instance when mono->stereo conversion
is disabled.


Revision tags: OPENBSD_6_2_BASE
# 1.170 20-Jul-2017 ratchov

Fix integer overflow that would causes >12 hours files
to not properly relocate. Found with coverity.


Revision tags: OPENBSD_6_1_BASE
# 1.169 03-Jan-2017 ratchov

Assert we're not freeing buffers we didn't allocate (DEBUG mode).


# 1.168 03-Jan-2017 ratchov

Log play volume together with other play parameters.


# 1.167 30-Sep-2016 ratchov

Set the device rate to the calculated rate. Otherwise, the signal
may endup resampled twice.


# 1.166 30-Sep-2016 ratchov

Remove unnecessary includes. From Michael W. Bombardieri <mb at ii.net>.


# 1.165 27-Sep-2016 ratchov

Don't rely on the resampling code to calculate the number of samples
to process, as it may produce one extra sample (to handle accumulation
of fractional samples), which would cause access to one sample past
the end of the buffer and crash aucat. Fix this by limiting the number
of samples processed to a single block.

Found by and help from Michael W. Bombardieri <mb at ii.net>. Thanks.


Revision tags: OPENBSD_6_0_BASE
# 1.164 07-Jun-2016 ratchov

Add -g and -p options to control device and file start position.


# 1.163 07-Jun-2016 ratchov

Add resamp_getcnt() routine to calculate the exact number of samples
that would be consumed and produced by the sampler rate converter. Use
it to avoid partial samples that are not properly handled. Fixes
last samples of certain files causing aucat to abort.


# 1.162 02-Jun-2016 ratchov

remove forgotten debug printfs


# 1.161 02-Jun-2016 ratchov

set exit status to 1 if -n is used without -i and -o


# 1.160 31-May-2016 ratchov

Pass hr:min:sec:frame.cent to dev_mmcloc() directly and remove the
useless intermediate conversion from and to 2400-th of seconds.


# 1.159 31-May-2016 ratchov

To store MMC position, use samples (at device rate) as time units
instead of 2400-ths of second. No behaviour change.


# 1.158 28-May-2016 ratchov

Fix file block size rounding and ensure it's large enough to store a
full audio block.


# 1.157 27-May-2016 ratchov

When resampling, use the exact resampling factor instead of the ratio
between input and output block sizes. This was inherited from sndiod,
but is not required for files because they are continuous streams of
samples and do not need to be split in blocks of equal duration.

This change makes playback/recording rate match exactly the requested
sample rate.


# 1.156 27-May-2016 ratchov

Flush rec buffer if there's less than one block space left and refill
play buffer if there's less than one block of data left. This is the
correct condition in the general case. No behaviour change, as all
input/output is multiple of the block size.


# 1.155 27-May-2016 ratchov

Simplify slot_fill() and slot_flush(). No behaviour change


# 1.154 27-May-2016 ratchov

Make resamp_do() get the exact number input and output samples and
provide routines to calculate them. This way we don't rely on it
to calculate the bytes procuded/consumed anymore. No behaviour change.


# 1.153 26-May-2016 ratchov

Make format conversion routines return the number of frames
consumed from both input and output buffers. No behaviour change.


Revision tags: OPENBSD_5_9_BASE
# 1.152 29-Jan-2016 ratchov

Check that byte order is native only if we're using more than one
byte per sample, check that samples are lsb-aligned only if there's
padding. Fixes unsupported parameters not being detected if compiled
in 24-bit mode and sndiod is not running.


# 1.151 10-Jan-2016 ratchov

trailing spaces, fit in 80 columns


# 1.150 09-Nov-2015 mmcc

Remove xfree(). From Michael W. Bombardieri.

ok ratchov@


# 1.149 27-Aug-2015 ratchov

fix typos in comments and error messages


Revision tags: OPENBSD_5_8_BASE
# 1.148 16-May-2015 ratchov

Call slot_stop() after the play buffer is drained to properly cleanup
the slot. Fixes periodic glitches occurring after eof is reached, and
then playback restarted with a MMC-relocate and MMC-start.


Revision tags: OPENBSD_5_7_BASE
# 1.147 21-Jan-2015 jmc

fix macro breakage and update usage();


# 1.146 21-Jan-2015 ratchov

Simplify internals and rewrite file header parsing code:
- remove -M, -t, -w, -C, -x flags that don't make sense anymore
- make "-j off" the default (sndiod already does the job)
- don't limit the number of played/recorded files.
- add support for floating-point encoded files.
- add support for apple .aiff and sun/next .au files


# 1.145 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.144 18-Nov-2013 ratchov

remove unused variables


# 1.143 18-Nov-2013 ratchov

add missing prototypes


# 1.142 12-Nov-2013 ratchov

remove bogus legacy server-specific bits that cause confusion


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.141 03-Dec-2012 ratchov

Remove server capability description from the documentation and from
the usage string, and just say that for historic reasons aucat behaves
like sndiod.
with help from jmc@


# 1.140 23-Nov-2012 ratchov

Since there's no sndiod -> aucat link anymore, allow aucat to be run
in server mode even when not invoked as sndiod. This allows aucat and
the new sndiod daemon to live together in /usr/bin, which makes easy
to quickly compare sndiod and aucat (and spot possible regressions)
without having to modify /usr/bin.


# 1.139 27-Oct-2012 ratchov

don't stat & test sndio home directory since we never use it.


# 1.138 27-Oct-2012 ratchov

don't (re-)define SNDIO_USER and SNDIO_PRIO if they are already defined,
this way they can be defined at compilation time


Revision tags: OPENBSD_5_2_BASE
# 1.137 27-Jun-2012 ratchov

Uniformize error messages printing, use err() instead of fprintf()
when appropriate. From Michael W. Bombardieri <mb at ii.net>


# 1.136 19-Jun-2012 ratchov

Remove hidden options that are not needed any more,
from Remco <remco at d-compu.dyndns.org>. Thanks


# 1.135 23-May-2012 ratchov

Make the "default" string a valid device name that has the same effect
as NULL has. This will (hopefully) simplify ports where the user
passes the device string.


# 1.134 11-Apr-2012 ratchov

Use "unsigned int" rather than "unsigned". No object change.
suggested by deraadt@


Revision tags: OPENBSD_5_1_BASE
# 1.133 09-Feb-2012 ratchov

Crank the default buffer size to 160ms (ie 7680 frames at 48kHz).
Workarounds stuttering caused by something hogging the cpu in kernel
mode and preventing sndiod from running at time.
ok deraadt


# 1.132 26-Jan-2012 ratchov

Make the default rate 48kHz and the default block size 10ms. These
settings ensure video players and programs using MTC are smooth by
default. Thanks to all who tested.


# 1.131 10-Jan-2012 ratchov

When a sub-device is created (-s), check if a hardware
device was already created (-f) before trying to use the
default one, otherwise all sub-devices end up attached
to the default device. Noticed by peters at schwertfisch.de,
thanks!


# 1.130 09-Dec-2011 ratchov

fix usage string: remove -M from aucat and -l from aucat and sndiod
from jmc


# 1.129 09-Dec-2011 ratchov

add a new sndiod audio/midi daemon and hide aucat server-specific
options.


# 1.128 02-Dec-2011 ratchov

don't use a "server" flag, just check if we expose sub-devices


# 1.127 02-Dec-2011 ratchov

reuse midi-control code to implement midi thru boxes and remove
the old midithru implementation; less code, less bugs. As a side
effect, midi output doesn't implement running status "compression"
any more.


# 1.126 20-Nov-2011 ratchov

Move mmc/mtc and volume control bits from struct aproc to struct
dev. Allows volume settings to be saved while the device is kept
closed. Besides that, no behabiour changes.


# 1.125 15-Nov-2011 ratchov

Add a "device number" component in sndio(7) device names, allowing a
single aucat instance to handle all audio and MIDI services. Since
this partially breaks compatibility, this is a opportunitiy to fix few
other design mistakes (eg ':' being used by inet6, type name vs api
name confusion, etc..). This leads to the following names:

type[@hostname][,unit]/devnum[.option]

The device number is the minor device number for direct hardware
access (ie the 'N' in /dev/audioN). For aucat, this is the occurence
number of the -f (or -M) option.

There's a compatibility hook to keep old names working if only one
aucat server is running.


# 1.124 18-Oct-2011 ratchov

keep the device closed by default (when not used)


# 1.123 18-Oct-2011 ratchov

remove duplicate options in getopt() string


# 1.122 17-Oct-2011 ratchov

Remove midicat since aucat can now be used instead of midicat
with almost the same syntax (roughly an extra -M option).
Thru boxes are created with aucat, and corresponding MIDI port
names have the "aucat" prefix instead of "midithru". The old
device name will still work some time for backward compatibility.

ok deraadt


# 1.121 12-Oct-2011 jmc

- avoid line wrapping an example
- sort usage() for midicat
- tweak a bit of unclear wording

ok ratchov


# 1.120 12-Oct-2011 ratchov

Simplify and improve the way options are parsed and remove ~300 lines
of code that becomes unused. Few command line arguments changes are
required though:

- stream definitions (-ios) now must follow devices definitions they
are attached to (-fMn)

- the -n option is now a special "loopback" device and is thus used
like -f, eg it must precede streams

- in midicat, midi thru boxes are not created automatically anymore,
the new "-M" option must be used for that

- channel numbers (-Cc options) correspond always to channel numbers
of the hardware.

- the -u option isn't needed anymore

- increase the log verbosity so user errors are logged
without using -d

tested by many, help from jmc


Revision tags: OPENBSD_5_0_BASE
# 1.119 29-Jun-2011 ratchov

make -u flag per-device similarly to -a and -w


# 1.118 20-Jun-2011 ratchov

Make -aoff option apply to MIDI ports (-q) as well, ensuring the device
stays closed also if -q is used. As we're at it, add -a to midicat so
it behaves like aucat.


# 1.117 03-Jun-2011 ratchov

Allow device encoding to be set if -u is also used. Useful to degrade
quality to save bandwith when a remote audio device is used.


# 1.116 03-Jun-2011 ratchov

Reorganize the way command line options are parsed and stored.
No behaviour change.


# 1.115 26-May-2011 ratchov

add a new -w flag to control whether master volume is automatically
adjusted when new streams are connected and disconnected. Disabling
automatic volume adjustment makes sense if all streams are recorded
with properly lowered volumes.


# 1.114 10-May-2011 ratchov

fix option handling and enable TCP in midicat
from peters at schwertfisch.de, thanks!


# 1.113 28-Apr-2011 ratchov

Implement a new authentication method allowing aucat and midicat to
work over TCP, for instance, to expose the sound card of one machine
with other machines of the network.

The first client generates a 128-bit random number (aka the
session cookie), saves it in $HOME/.aucat_cookie and sends it to the
server. Successive clients load the cookie from $HOME/.aucat_cookie
and send it to the server but the server accepts only clients whose
cookie matches the session cookie. When all clients are gone, the
session is over, and another cookie could start a new session, and so
on.

TCP is enabled on the server with the new -L option, and on the client
side hostnames are specified with a new optional component in the
device name.

hints from damien, dlg and deraadt, tweaks from jmc


# 1.112 27-Apr-2011 ratchov

move amsg.h containing protocol defs from aucat side to libsndio side.

requested by deraadt


# 1.111 27-Apr-2011 deraadt

use more volatile sig_atomic_t in signal handlers
ok ratchov


# 1.110 19-Apr-2011 ratchov

don't hold a pointer to "listen" structure. Instead, when it must
be free()ed, iterate over the file_list, and find the structure
to free. This is safer and simpler. No behaviour change


# 1.109 16-Apr-2011 ratchov

for unix domain socket addresses use AUCAT_PATH and MIDICAT_PATH
macros instead of hardcoded strings. No object change


# 1.108 17-Mar-2011 ratchov

use a DEFAULT_RATE macro instead of hardcoding 44100, no object change


Revision tags: OPENBSD_4_9_BASE
# 1.107 05-Nov-2010 ratchov

unbreak compilation without DEBUG defined


# 1.106 21-Oct-2010 ratchov

use MODE_xxx and XRUN_xxx macros everywhere, and zap corresponding
AMSG_xxx macros, which in turns simplifies the code


# 1.105 08-Sep-2010 deraadt

aucat: getpwnam: No such file or directory
is not a proper error message.


# 1.104 20-Aug-2010 ratchov

polish the code: remove few unused #includes, add missing ones,
fix NULL vs 0, etc. No behaviour change.


# 1.103 19-Aug-2010 ratchov

fix NULL pointer deref in midicat option parsing when ``-'' is
used as filename


Revision tags: OPENBSD_4_8_BASE
# 1.102 31-Jul-2010 ratchov

remove 2 years old compatibility mode


# 1.101 10-Jul-2010 ratchov

In midi server mode, don't terminate the midi thru box if there are
no connections anymore.


# 1.100 10-Jul-2010 ratchov

fix parameter handling: don't try to open a ``default'' midi port
if no files are given on the command line


# 1.99 06-Jul-2010 jmc

various tweaks to previous; ok ratchov


# 1.98 06-Jul-2010 ratchov

Handle all streams the same way because there's no actual
difference between audio files and client connections. Clean up
the way command line options are handled and clarify this in the
manual page: stream parameters (-Ccehjmrtvx) must precede stream
definitions (-ios) and per-device parameters (-abz) and stream
definitions (-ios) must precede device definitions (-f). Since
there's no ``server'' and ``non-server'' modes anymore, make the
-l option just detach the process.

ok and help from jakemsr and jmc


# 1.97 29-Jun-2010 jakemsr

If the device isn't opened yet (as can be the case with '-a off'),
don't check if the midi control interface is idle
from ratchov


# 1.96 25-Jun-2010 ratchov

don't attempt to drain devices after they are destroyed, which
results in a use after free(). Catched by jakemsr@ with MALLOC_OPTIONS=J


# 1.95 20-Jun-2010 ratchov

fix midicat(1) device mode when only -i or only -o is used (ie
open the device input-only if -o is used, and output-only if -i is
used).


# 1.94 05-Jun-2010 ratchov

don't prime server buffers, because it's ugly and conceptually
complicated. Instead, request clients to provide enough samples
and start with buffers full.


# 1.93 05-Jun-2010 ratchov

Don't send the initial position as a clock tick with a negative delta.
Add a new ``initial position'' message which is simpler. No bahaviour
change, except effects of the protocol version crank.


# 1.92 04-Jun-2010 ratchov

Allow the audio device to be opened only while it's actually used.
This is necessary for uaudio devices, for instance to start aucat
before the device is plugged. Or to unplug a device whithout
having to restart aucat when another device is plugged. This is
controlled with the new -a option.

Allow multiple audio devices to be used concurently, i.e.
multiple ``-f devname'' options to be used; -f options must follow
per-device options, which is what we do for other options.


# 1.91 08-May-2010 ratchov

prime play buffers as soon as the device is opened, otherwise a
midi client could start it with empty buffers, in turn hurting
audio clients.


# 1.90 02-May-2010 ratchov

Clean up device handling code to clarify different initialization phases
and different device states. Split initialization in two phases:
first global variables are initialized then the audio hardware is opened.
Allow devices that don't support full-duplex to work in play-only or
record-only mode, even if ``-m play'' or ``-m rec'' are not specified.


# 1.89 02-May-2010 ratchov

Don't systematically fill with silence the mixer output. This
might result in extra samples being written to the device
when the mixer is closed.


# 1.88 24-Apr-2010 ratchov

if there are no files to play in legacy mode, print the usage message


# 1.87 24-Apr-2010 ratchov

rename ibuflist/obuflist -> ins/outs to cut long lines
from Thomas Pfaff, thanks


# 1.86 22-Apr-2010 ratchov

Allow multiple users to share the same aucat server. If aucat is
run by root, it binds a shared address to the socket, cranks the
process priority and drops privileges. sio_open(3) will try to
connect to the private socket first (if any), then to the shared
socket. Only one user may have connections to aucat at a given
time.

based on discussions with henning, pyr and others
ok jacek, deraadt


# 1.85 21-Apr-2010 ratchov

when creating the ``default'' subdevice, use last parameters of the
command line, instead of the device parameters.


# 1.84 21-Apr-2010 ratchov

adds the necessary bits to join channels (ex stereo->mono) or
expand channels (ex mono->stereo). It's switched on/off with
the ``-j'' option, (default is "on").


# 1.83 06-Apr-2010 ratchov

aucat (server):

- make the ``-m mode'' option per subdevice, allowing
subdevices to be play-only or rec-only even if the
server is full-duplex

- add ``monitoring'' mode (with ``-m mon''). This is a
record-only stream from which played streams can be
recorded (kind of ``record what you hear'').

- allow MIDI devices to be subscribed to the controlling
MIDI port of the server, ie what midicat does (with -f
option), but using the -q option.

- add flow control to the protocol, and serialize clock
ticks (sio_onmove() calls) and data chunks. This should
fix certain full-duplex programs, broken with ``magic''
block/buffer size combinations.

- enable 3 block latency which is the minimum theoretical.
Make -z and -b options correspond to device parameters.

- make sio_getcap(3) the same for aucat and sun backends,
ie return whatever is supported (``everything'' in the
aucat case, since everything is actulally supported).

aucat (player):

- enable ``-m mode'' option to select between monitoring
and recording when ``-o file'' is used.

- plug MIDI code to non-server codepath. The MIDI control
device is specified with the ``-q device'' option, as in
server mode.

- impliment lseek()'ing within files (controlled
through MIDI). Necessary to use aucat with a MIDI
sequencer.

midicat (thrubox):

- rename ``-f'' flag to ``-q'', so it has the
same name as in aucat (-f is still working)

ok jakemsr@, tweaks from jmc@


# 1.82 03-Apr-2010 ratchov

backout last change, committed by mistake, sorry...


# 1.81 03-Apr-2010 ratchov

doc fixes from jmc


Revision tags: OPENBSD_4_7_BASE
# 1.80 14-Jan-2010 ratchov

Use -C option to specify recorded channel range instead of -c which
is for played channels.

spotted by Jan Stary <hans at stare.cz>, thanks


# 1.79 11-Jan-2010 ratchov

When resuming from suspend, prime play buffers with silence,
otherwise the client would underrun by more than its own buffer
size, which currently is not allowed (causes the client to
deadlock).

Set the minimum client buffer size to two blocks. Single block
buffers don't work properly yet.

with help from jakemsr


# 1.78 10-Jan-2010 ratchov

Add traces to help tracking subtle timing bugs often causing
underuns and overruns; such bugs are hard to debug with ktrace
or a debugger. They are also handy to debug or monitor code
using aucat.

To enable traces, compile aucat with ``make DEBUG=-DDEBUG'' and
use the -d option multiple times.

ok jakemsr


# 1.77 05-Jan-2010 ratchov

Make the way .wav files are handled closer to the way sockets are
handled, which is conceptually simpler. No behaviour changes.


# 1.76 21-Nov-2009 ratchov

use strtonum(3) and strtol(3) instead of sscanf(3)
suggested by deraadt@


# 1.75 21-Nov-2009 deraadt

spelling


# 1.74 03-Nov-2009 ratchov

Allow any program using aucat to act as MMC slave and MTC master
transparently. Multiple audio applications can be started
synchronously from external software/hardware supporting the
standard Start/Stop/Relocate messages. The server clock is exposed
through MTC, allowing non-audio software/hardware to be
synchronized to audio applications.


# 1.73 27-Oct-2009 ratchov

add missing word in error message


# 1.72 10-Oct-2009 ratchov

don't use a references to the device file to check the current mode


# 1.71 10-Oct-2009 ratchov

make dev_done() common to audio and MIDI, and drop dev_thrudone()


# 1.70 10-Oct-2009 ratchov

use dev_done() for loopback devices too and remove dev_loopdone()


# 1.69 05-Oct-2009 ratchov

add -d flag. When started in server mode, if the -d flag is
used aucat and midicat don't daemonize and log on stderr.


# 1.68 27-Sep-2009 ratchov

remove all debug traces


# 1.67 26-Aug-2009 jmc

oops, forgot to update usage();


# 1.66 19-Aug-2009 ratchov

don't try to process -o arguments using the input file list, fixes
midicat crashing sometimes when -o is used


# 1.65 19-Aug-2009 ratchov

organize midi code like audio code, no functional change


# 1.64 17-Aug-2009 ratchov

allow midicat to take multiple -i and -o options


# 1.63 17-Aug-2009 ratchov

if one or more MIDI devices are specified in server mode, subscribe
them to the MIDI thru box


# 1.62 25-Jul-2009 ratchov

fix comments, remove no longer needed declaration, reorder includes
from Thomas Pfaff


# 1.61 25-Jul-2009 ratchov

Currently midi capable programs can control midi hardware, but
cannot cooperate with other programs. The aim of this change is
to allow any program to send midi data to other programs as they
were midi hardware. For instance, this change should solve the
longstanding problem of using a midi sequencer with software
synthesizers. More precisely:

- new midicat(1) utility (actually hardlink to aucat(1)).
it creates software midi thru boxes, allowing programs
to send midi messages to other programs as they were
midi(4) hardware.

- new midi api in libsndio (see mio_open(3)), to access
midi(4) devices and midicat(1) sockets in a uniform way.

- new device naming scheme <service>:<unit>[.<option>],
common to audio and midi.

- new sndio(7) manual describing concepts and naming

The current audio device naming still works, but people having
scripts or configuration files containing device names could read
the sndio(7) man page and slowly start updating device names.

discussed with jakemsr@ and deraadt@, help form jmc@


Revision tags: OPENBSD_4_6_BASE
# 1.60 27-Apr-2009 ratchov

make -v option apply also to implicit default socket


# 1.59 17-Mar-2009 jmc

- rename the arg to -b "nframes" and sync usage()
- grammar fix for the server section

from Thomas Pfaff
ok ratchov


Revision tags: OPENBSD_4_5_BASE
# 1.58 06-Feb-2009 ratchov

move error messages reported to user into main()


# 1.57 04-Feb-2009 ratchov

if there are too many connections, stop acceping new ones rather
than exit()ing with ``too many open files'' fatal error


# 1.56 04-Feb-2009 ratchov

daemonize when in server mode, suggested by many
ok jakemsr


# 1.55 03-Feb-2009 ratchov

in server mode, create /tmp/aucat-userid/ directory with
permissions 0700, and create sockets in it. This prevents
one local user to eavesdrop or disturb audio programs of
other users.

if you're using the ``-s socket'' option with an absolute
path as argument, please update it to use a socket name.

requested by many, bits from jakemsr and otto
ok jakemsr


# 1.54 25-Jan-2009 ratchov

don't set malloc_options to not interfer with /etc/malloc.conf
requested and explained by fgsch@, henning@, otto@


# 1.53 23-Jan-2009 ratchov

catch SIGHUP and SIGTERM too, to ensure that recorded files are
properly closed and sockets are cleaned up.


# 1.52 23-Jan-2009 ratchov

spacing + typo fix from Matt Jibson <matt.jibson(at)gmail.com>


# 1.51 10-Jan-2009 ratchov

add "loopback" mode in which input is connected to the output.
This is useful to mix, demultiplex, resample or reencode audio
files off-line.
tweak + ok jakemsr


# 1.50 29-Dec-2008 ratchov

make the code "more correct": add reference counters to aproc
structures so we can keep a pointer to the device. Beside some
simplifications, this allows to easily handle the situation
where the audio device desappears. No change in the behaviour.


# 1.49 26-Dec-2008 ratchov

*** empty log message ***


# 1.48 07-Dec-2008 ratchov

When resampling, don't require the sample frequency to be an integer.
This removes the arithmetic constraint between the sample frequency
and the block size and all the associated code. Now aucat can work
in server mode with any block size.
ok jakemsr


# 1.47 23-Nov-2008 ratchov

Use file input parameter as default device output and file output parameters
as default device input. That's what the man page says


# 1.46 20-Nov-2008 ratchov

take into account socket parameters into calculation of device
parameters; without this change aucat uses the device default
parameters and they are not necessarily usable for multi-stream
mode.


# 1.45 17-Nov-2008 jmc

- rename -s arg name to "socket"
- condense EXAMPLES into a single screenful

ok ratchov


# 1.44 17-Nov-2008 jmc

tweak previous man page changes, and sync usage();


# 1.43 17-Nov-2008 ratchov

allow aucat to run as server in play-only and record-only mode, so
it can be used on play-only, record-only and half-duplex devices.
ok jakemsr


# 1.42 16-Nov-2008 ratchov

allow aucat to listen on multiple sockets. Each socket carries its
channel and volume settings allowing multiple configuration to
coexist. Mostly useful for envy(4)-like devices, but can be used
to force different apps to use different settings.


# 1.41 16-Nov-2008 ratchov

add ``template'' parameters to sockets rather than using device
parameters. No functional change.


# 1.40 16-Nov-2008 ratchov

Make clients inherit the volume parameter when the -v option is used in
server mode. It gives the maximum volume a client may have. This wastes
dynamic range, but allows volume to stay constant when other clients
connect or disconnect.


# 1.39 12-Nov-2008 ratchov

when destroying the device, mark the mixer and the demultiplexer
for ``AUTOQUIT'' first, and then call file_eof() and friends.
fixes crashes sthen@ noticed on zaurus


# 1.38 11-Nov-2008 ratchov

if the device disappear the mixer and demultiplexer chains are
destroyed. If this happen then warn and exit, rather than crashing.


# 1.37 11-Nov-2008 ratchov

termninate non-server aucat when it's idle rather that using
the hackish {MIX,SUB}_AUTOQUIT features. This way we don't
have to care about possible references on destroyed objects.


# 1.36 11-Nov-2008 ratchov

when not in server mode, aucat terminates automatically using
the {MIX,SUB}_AUTAQUIT features. In this case we're not
allowed to directly touch dev_mix and dev_sub because they
can disappar. So disable the ``suspend'' and ``quit'' bits
when {MIX,SUB}_AUTAQUIT are used.


# 1.35 10-Nov-2008 ratchov

add a per-stream ``soft volume'' knob and the corresponding -v option.
The code will be useful later for the volume knob in the sndio API.


# 1.34 09-Nov-2008 ratchov

make aucat(1) stop automatically the audio(1) device if it's idle. This
way, when in server mode, it consumes no CPU if there are no clients.
Later, this will allow to start aucat(1) at session or system startup.


# 1.33 08-Nov-2008 ratchov

set the default device to /dev/audio for legacy mode.
From Thomas Pfaff <tpfaff _at_ agderlink.no> and tweaks from me


# 1.32 07-Nov-2008 ratchov

expose the block size in the sndio API by making par->round writable
and thus remove the ugly rate <-> block-size table from
sio_setpar(3). Handle the block size negociation in aucat(1), since
it has few constrains the code is overally simpler.
ok jakemsr@, major crank suggested by deraadt@


# 1.31 03-Nov-2008 ratchov

A small optimization: handle most N-channel <-> M-channel conversions
inside the mixer and the demultiplexer. This way, aucat will not
trigger the heavy conversion code when only channel conversions are
required. Cuts ~50% of the CPU usage on envy(4) devices, can improve
surround 4.0, 5.1 and 7.1 capable devices. No functionnal change.


# 1.30 26-Oct-2008 ratchov

zap `q' from the getopts string too,
requested by jmc@


# 1.29 26-Oct-2008 jmc

sync SYNOPSIS and usage();


# 1.28 26-Oct-2008 ratchov

add minimal server capability to aucat(1). When started in server
mode, it listens on an unix socket and mixes/demultiplexes any number
of full-duplex streams, doing necessary format conversions and
resampling on the fly.

programs can use the new libsa(3) library to play and record audio.
The library provides a very simple API to connect to the audio server;
if aucat(1) isn't running, it uses the audio(4) driver transparently
instead.


# 1.27 25-Aug-2008 sobrado

minor tweak for removal of "[-d devel]" (change done in r1.19 and r1.21).

ok ratchov@


# 1.26 14-Aug-2008 ratchov

move all device related stuff from aucat.c to a new dev.c file.
The new dev_xxx() routines expose a "high level" self-contained
interface to the device. At initialization, the device is opened
and two chains of aproc structures are created:

* a playback chain that exposes a (initially) empty mix
aproc to which the rest of the code can attach new
streams to be played

* record chain that exposes a (initially) empty sub aproc
to which the rest of the code can attach new stream to
to record

The rest of the code, has just to use dev_attach() routine to
attach streams. While we're at it, add a ``devops'' structure
containing pointers to the device-specific routines. This will
allow later to add support for other type of device than the
Sun API.

Also, write the .wav headers in file_del(), so put all header
related data in the file strucuture. This allows to close() the
file, as soon as wpipe_xxx() aproc terminates. This will be
useful for the server, because it will need to close() descripts
of closed connections immediately.

add mix_pushzero() routine to fill the mixer with silence. It
will be used to avoid the mixer to underrun when there are no
input streams. Since we always have at least one input stream
there's no behaviour change.

ok jakemsr


Revision tags: OPENBSD_4_4_BASE
# 1.25 02-Jun-2008 ratchov

document latest changes: -d flag is replaced by AUCAT_DEBUG
environment variable, new -xX options

bits from eric, ok jakemsr


# 1.24 02-Jun-2008 ratchov

currently, we start the device as soon as write() blocks; it's better
to continue filling the play buffer until it's full. This way, the
play buffer has fewer chances to underrun when the device is started.

ok jakemsr


# 1.23 02-Jun-2008 ratchov

if aucat is suspended, then the kernel continues running the device
(produces silence) and later, once aucat is resumed the kernel starts
dropping samples, it will try to drop as many samples as silence was
produced. So suspending breaks the aucat process permanently.

workaround this by blocking the signals ie disabling suspending of aucat
from the tty. The long term solution would be to catch SIGCONT and to stop
the device, resync/refill buffers and restart the device. That's really a
lot of work...

ok jakemsr


# 1.22 02-Jun-2008 ratchov

allow the user to choose the aucat behaviour when underruns/overruns
occur on a per-stream basis, using -Xx flags. There are 3 possible
policies:

- ignore : ignores underruns/overruns, for instance, this mode
could be used for creating simple pipes with utilities; like in
your last cdio diff.

- sync : insert/discard samples in order to keep all streams in
sync, useful for multi-tracker-like apps and/or to sync
midi/video/whatever on audio streams (this was the previous
behaviour)

- error : if overruns/underruns occur, consider it as fatal error
and kill the corresponding stream (without disturbing others).
Useful, for reliable recordings (and/or debugging aucat
itself:).

ok jakemsr


# 1.21 02-Jun-2008 ratchov

(remove -d, second part) use strtonum() instead of sscanf() and inlined
checks

suggested and ok jakemsr


# 1.20 02-Jun-2008 ratchov

since underruns/overruns are handled outside devices, remove code that
check for xruns in devices.

ok jakemsr


# 1.19 02-Jun-2008 ratchov

(remove -d, first part) replace -d flag by AUCAT_DEBUG environment
variable. This eases turning debugging on/off when aucat is started by
another program.

"i like the idea" jakemsr


# 1.18 26-May-2008 jmc

put -q in the right place;


# 1.17 26-May-2008 jakemsr

add -q option which enables "quiet" operation.

ok ratchov@


# 1.16 23-May-2008 jmc

various fixes for aucat.1, and sync usage(); ok ratchov


# 1.15 23-May-2008 ratchov

add support for:
- recording, full-duplex operation
- format conversions and resampling on the fly
- mixing on the fly of multiple inputs of different formats
- up to 16 channels, simplistic "routing" of channel ranges
- more linear encodings (in raw and wav files)
the old behaviour is fully preserved if none of the new -i and -o
options are used.

code and fixes from jakemsr@ and eric@, suggestions by others.
ok "go ahead" deraadt@


# 1.14 13-Apr-2008 jakemsr

- remove code that was rendered pointless by the last commit
- minor knf

from Pierre Riteau


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE
# 1.13 20-Mar-2007 uwe

Support for playing simple RIFF files (.wav) from Simon Effenberg, savar
at schuldeigen de, with some tweaks from me. ok millert, tests ckuethe,
liked by some disliked by nobody, but he's not even a real user :)


Revision tags: OPENBSD_4_1_BASE
# 1.12 20-Dec-2006 steven

remove unneeded #include

ok mbalmer@ deraadt@


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.11 20-Jan-2005 jaredy

- knf
- check read/write/etc. calls for errors
- use constants instead of hardcoded pathnames
- better variable types
- add usage output

suggestions from various, ok ian


# 1.10 13-Jan-2005 ian

Close what you open: obvious fix, ok jared@, more to follow


Revision tags: OPENBSD_3_5_BASE OPENBSD_3_6_BASE
# 1.9 20-Oct-2003 jmc

typos from Jared Yanovich, slightly ammended;


Revision tags: OPENBSD_3_4_BASE
# 1.8 10-Jul-2003 david

add missing includes
ok deraadt@ tedu@


# 1.7 02-Jul-2003 deraadt

protos


# 1.6 10-Jun-2003 deraadt

mostly ansi cleanup; pval ok


Revision tags: OPENBSD_3_3_BASE
# 1.5 09-Dec-2002 millert

From Andrushock, s/sucess/success/g


Revision tags: OPENBSD_2_8_BASE OPENBSD_2_9_BASE OPENBSD_3_0_BASE OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.4 01-Jul-2000 millert

Add -f option to specify audio device, honor AUDIODEVICE environment vairable,
and some KNF.


Revision tags: OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE
# 1.3 05-Jan-1997 kstailey

minor tweaks; htonl() -> ntohl(); added SEE ALSO


# 1.2 03-Jan-1997 kstailey

removed unnecessary test


# 1.1 02-Jan-1997 kstailey

new utility for playing sound files


# 1.177 12-Jan-2021 naddy

const-ify lookup tables; ok ratchov@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.176 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.175 19-May-2019 ratchov

Fix integer overflow in block size calculation.

Fixes truncation of contents of files with rate above 64kHz
generate in off-line mode (i.e. -n option).


Revision tags: OPENBSD_6_5_BASE
# 1.174 21-Feb-2019 ratchov

Use reallocarray in place of malloc(a * b) to handle possible
overflow in multiplication of malloc argument.


Revision tags: OPENBSD_6_4_BASE
# 1.173 13-May-2018 ratchov

Add missing pledge(). From Jesper Wallin <jesper at ifconfig.se>.
Thanks!


Revision tags: OPENBSD_6_3_BASE
# 1.172 07-Nov-2017 ratchov

prefill with silence the buffer where cmap_copy() stores samples
rather than the file buffer. Fixes initialized data being treated as
recorded samples when resampling or format conversions are involved
but mono->stereo conversion is disabled.


# 1.171 03-Nov-2017 ratchov

Prefill rec buffer with silence. This is necessary because certain
channels don't get samples, for instance when mono->stereo conversion
is disabled.


Revision tags: OPENBSD_6_2_BASE
# 1.170 20-Jul-2017 ratchov

Fix integer overflow that would causes >12 hours files
to not properly relocate. Found with coverity.


Revision tags: OPENBSD_6_1_BASE
# 1.169 03-Jan-2017 ratchov

Assert we're not freeing buffers we didn't allocate (DEBUG mode).


# 1.168 03-Jan-2017 ratchov

Log play volume together with other play parameters.


# 1.167 30-Sep-2016 ratchov

Set the device rate to the calculated rate. Otherwise, the signal
may endup resampled twice.


# 1.166 30-Sep-2016 ratchov

Remove unnecessary includes. From Michael W. Bombardieri <mb at ii.net>.


# 1.165 27-Sep-2016 ratchov

Don't rely on the resampling code to calculate the number of samples
to process, as it may produce one extra sample (to handle accumulation
of fractional samples), which would cause access to one sample past
the end of the buffer and crash aucat. Fix this by limiting the number
of samples processed to a single block.

Found by and help from Michael W. Bombardieri <mb at ii.net>. Thanks.


Revision tags: OPENBSD_6_0_BASE
# 1.164 07-Jun-2016 ratchov

Add -g and -p options to control device and file start position.


# 1.163 07-Jun-2016 ratchov

Add resamp_getcnt() routine to calculate the exact number of samples
that would be consumed and produced by the sampler rate converter. Use
it to avoid partial samples that are not properly handled. Fixes
last samples of certain files causing aucat to abort.


# 1.162 02-Jun-2016 ratchov

remove forgotten debug printfs


# 1.161 02-Jun-2016 ratchov

set exit status to 1 if -n is used without -i and -o


# 1.160 31-May-2016 ratchov

Pass hr:min:sec:frame.cent to dev_mmcloc() directly and remove the
useless intermediate conversion from and to 2400-th of seconds.


# 1.159 31-May-2016 ratchov

To store MMC position, use samples (at device rate) as time units
instead of 2400-ths of second. No behaviour change.


# 1.158 28-May-2016 ratchov

Fix file block size rounding and ensure it's large enough to store a
full audio block.


# 1.157 27-May-2016 ratchov

When resampling, use the exact resampling factor instead of the ratio
between input and output block sizes. This was inherited from sndiod,
but is not required for files because they are continuous streams of
samples and do not need to be split in blocks of equal duration.

This change makes playback/recording rate match exactly the requested
sample rate.


# 1.156 27-May-2016 ratchov

Flush rec buffer if there's less than one block space left and refill
play buffer if there's less than one block of data left. This is the
correct condition in the general case. No behaviour change, as all
input/output is multiple of the block size.


# 1.155 27-May-2016 ratchov

Simplify slot_fill() and slot_flush(). No behaviour change


# 1.154 27-May-2016 ratchov

Make resamp_do() get the exact number input and output samples and
provide routines to calculate them. This way we don't rely on it
to calculate the bytes procuded/consumed anymore. No behaviour change.


# 1.153 26-May-2016 ratchov

Make format conversion routines return the number of frames
consumed from both input and output buffers. No behaviour change.


Revision tags: OPENBSD_5_9_BASE
# 1.152 29-Jan-2016 ratchov

Check that byte order is native only if we're using more than one
byte per sample, check that samples are lsb-aligned only if there's
padding. Fixes unsupported parameters not being detected if compiled
in 24-bit mode and sndiod is not running.


# 1.151 10-Jan-2016 ratchov

trailing spaces, fit in 80 columns


# 1.150 09-Nov-2015 mmcc

Remove xfree(). From Michael W. Bombardieri.

ok ratchov@


# 1.149 27-Aug-2015 ratchov

fix typos in comments and error messages


Revision tags: OPENBSD_5_8_BASE
# 1.148 16-May-2015 ratchov

Call slot_stop() after the play buffer is drained to properly cleanup
the slot. Fixes periodic glitches occurring after eof is reached, and
then playback restarted with a MMC-relocate and MMC-start.


Revision tags: OPENBSD_5_7_BASE
# 1.147 21-Jan-2015 jmc

fix macro breakage and update usage();


# 1.146 21-Jan-2015 ratchov

Simplify internals and rewrite file header parsing code:
- remove -M, -t, -w, -C, -x flags that don't make sense anymore
- make "-j off" the default (sndiod already does the job)
- don't limit the number of played/recorded files.
- add support for floating-point encoded files.
- add support for apple .aiff and sun/next .au files


# 1.145 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.144 18-Nov-2013 ratchov

remove unused variables


# 1.143 18-Nov-2013 ratchov

add missing prototypes


# 1.142 12-Nov-2013 ratchov

remove bogus legacy server-specific bits that cause confusion


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.141 03-Dec-2012 ratchov

Remove server capability description from the documentation and from
the usage string, and just say that for historic reasons aucat behaves
like sndiod.
with help from jmc@


# 1.140 23-Nov-2012 ratchov

Since there's no sndiod -> aucat link anymore, allow aucat to be run
in server mode even when not invoked as sndiod. This allows aucat and
the new sndiod daemon to live together in /usr/bin, which makes easy
to quickly compare sndiod and aucat (and spot possible regressions)
without having to modify /usr/bin.


# 1.139 27-Oct-2012 ratchov

don't stat & test sndio home directory since we never use it.


# 1.138 27-Oct-2012 ratchov

don't (re-)define SNDIO_USER and SNDIO_PRIO if they are already defined,
this way they can be defined at compilation time


Revision tags: OPENBSD_5_2_BASE
# 1.137 27-Jun-2012 ratchov

Uniformize error messages printing, use err() instead of fprintf()
when appropriate. From Michael W. Bombardieri <mb at ii.net>


# 1.136 19-Jun-2012 ratchov

Remove hidden options that are not needed any more,
from Remco <remco at d-compu.dyndns.org>. Thanks


# 1.135 23-May-2012 ratchov

Make the "default" string a valid device name that has the same effect
as NULL has. This will (hopefully) simplify ports where the user
passes the device string.


# 1.134 11-Apr-2012 ratchov

Use "unsigned int" rather than "unsigned". No object change.
suggested by deraadt@


Revision tags: OPENBSD_5_1_BASE
# 1.133 09-Feb-2012 ratchov

Crank the default buffer size to 160ms (ie 7680 frames at 48kHz).
Workarounds stuttering caused by something hogging the cpu in kernel
mode and preventing sndiod from running at time.
ok deraadt


# 1.132 26-Jan-2012 ratchov

Make the default rate 48kHz and the default block size 10ms. These
settings ensure video players and programs using MTC are smooth by
default. Thanks to all who tested.


# 1.131 10-Jan-2012 ratchov

When a sub-device is created (-s), check if a hardware
device was already created (-f) before trying to use the
default one, otherwise all sub-devices end up attached
to the default device. Noticed by peters at schwertfisch.de,
thanks!


# 1.130 09-Dec-2011 ratchov

fix usage string: remove -M from aucat and -l from aucat and sndiod
from jmc


# 1.129 09-Dec-2011 ratchov

add a new sndiod audio/midi daemon and hide aucat server-specific
options.


# 1.128 02-Dec-2011 ratchov

don't use a "server" flag, just check if we expose sub-devices


# 1.127 02-Dec-2011 ratchov

reuse midi-control code to implement midi thru boxes and remove
the old midithru implementation; less code, less bugs. As a side
effect, midi output doesn't implement running status "compression"
any more.


# 1.126 20-Nov-2011 ratchov

Move mmc/mtc and volume control bits from struct aproc to struct
dev. Allows volume settings to be saved while the device is kept
closed. Besides that, no behabiour changes.


# 1.125 15-Nov-2011 ratchov

Add a "device number" component in sndio(7) device names, allowing a
single aucat instance to handle all audio and MIDI services. Since
this partially breaks compatibility, this is a opportunitiy to fix few
other design mistakes (eg ':' being used by inet6, type name vs api
name confusion, etc..). This leads to the following names:

type[@hostname][,unit]/devnum[.option]

The device number is the minor device number for direct hardware
access (ie the 'N' in /dev/audioN). For aucat, this is the occurence
number of the -f (or -M) option.

There's a compatibility hook to keep old names working if only one
aucat server is running.


# 1.124 18-Oct-2011 ratchov

keep the device closed by default (when not used)


# 1.123 18-Oct-2011 ratchov

remove duplicate options in getopt() string


# 1.122 17-Oct-2011 ratchov

Remove midicat since aucat can now be used instead of midicat
with almost the same syntax (roughly an extra -M option).
Thru boxes are created with aucat, and corresponding MIDI port
names have the "aucat" prefix instead of "midithru". The old
device name will still work some time for backward compatibility.

ok deraadt


# 1.121 12-Oct-2011 jmc

- avoid line wrapping an example
- sort usage() for midicat
- tweak a bit of unclear wording

ok ratchov


# 1.120 12-Oct-2011 ratchov

Simplify and improve the way options are parsed and remove ~300 lines
of code that becomes unused. Few command line arguments changes are
required though:

- stream definitions (-ios) now must follow devices definitions they
are attached to (-fMn)

- the -n option is now a special "loopback" device and is thus used
like -f, eg it must precede streams

- in midicat, midi thru boxes are not created automatically anymore,
the new "-M" option must be used for that

- channel numbers (-Cc options) correspond always to channel numbers
of the hardware.

- the -u option isn't needed anymore

- increase the log verbosity so user errors are logged
without using -d

tested by many, help from jmc


Revision tags: OPENBSD_5_0_BASE
# 1.119 29-Jun-2011 ratchov

make -u flag per-device similarly to -a and -w


# 1.118 20-Jun-2011 ratchov

Make -aoff option apply to MIDI ports (-q) as well, ensuring the device
stays closed also if -q is used. As we're at it, add -a to midicat so
it behaves like aucat.


# 1.117 03-Jun-2011 ratchov

Allow device encoding to be set if -u is also used. Useful to degrade
quality to save bandwith when a remote audio device is used.


# 1.116 03-Jun-2011 ratchov

Reorganize the way command line options are parsed and stored.
No behaviour change.


# 1.115 26-May-2011 ratchov

add a new -w flag to control whether master volume is automatically
adjusted when new streams are connected and disconnected. Disabling
automatic volume adjustment makes sense if all streams are recorded
with properly lowered volumes.


# 1.114 10-May-2011 ratchov

fix option handling and enable TCP in midicat
from peters at schwertfisch.de, thanks!


# 1.113 28-Apr-2011 ratchov

Implement a new authentication method allowing aucat and midicat to
work over TCP, for instance, to expose the sound card of one machine
with other machines of the network.

The first client generates a 128-bit random number (aka the
session cookie), saves it in $HOME/.aucat_cookie and sends it to the
server. Successive clients load the cookie from $HOME/.aucat_cookie
and send it to the server but the server accepts only clients whose
cookie matches the session cookie. When all clients are gone, the
session is over, and another cookie could start a new session, and so
on.

TCP is enabled on the server with the new -L option, and on the client
side hostnames are specified with a new optional component in the
device name.

hints from damien, dlg and deraadt, tweaks from jmc


# 1.112 27-Apr-2011 ratchov

move amsg.h containing protocol defs from aucat side to libsndio side.

requested by deraadt


# 1.111 27-Apr-2011 deraadt

use more volatile sig_atomic_t in signal handlers
ok ratchov


# 1.110 19-Apr-2011 ratchov

don't hold a pointer to "listen" structure. Instead, when it must
be free()ed, iterate over the file_list, and find the structure
to free. This is safer and simpler. No behaviour change


# 1.109 16-Apr-2011 ratchov

for unix domain socket addresses use AUCAT_PATH and MIDICAT_PATH
macros instead of hardcoded strings. No object change


# 1.108 17-Mar-2011 ratchov

use a DEFAULT_RATE macro instead of hardcoding 44100, no object change


Revision tags: OPENBSD_4_9_BASE
# 1.107 05-Nov-2010 ratchov

unbreak compilation without DEBUG defined


# 1.106 21-Oct-2010 ratchov

use MODE_xxx and XRUN_xxx macros everywhere, and zap corresponding
AMSG_xxx macros, which in turns simplifies the code


# 1.105 08-Sep-2010 deraadt

aucat: getpwnam: No such file or directory
is not a proper error message.


# 1.104 20-Aug-2010 ratchov

polish the code: remove few unused #includes, add missing ones,
fix NULL vs 0, etc. No behaviour change.


# 1.103 19-Aug-2010 ratchov

fix NULL pointer deref in midicat option parsing when ``-'' is
used as filename


Revision tags: OPENBSD_4_8_BASE
# 1.102 31-Jul-2010 ratchov

remove 2 years old compatibility mode


# 1.101 10-Jul-2010 ratchov

In midi server mode, don't terminate the midi thru box if there are
no connections anymore.


# 1.100 10-Jul-2010 ratchov

fix parameter handling: don't try to open a ``default'' midi port
if no files are given on the command line


# 1.99 06-Jul-2010 jmc

various tweaks to previous; ok ratchov


# 1.98 06-Jul-2010 ratchov

Handle all streams the same way because there's no actual
difference between audio files and client connections. Clean up
the way command line options are handled and clarify this in the
manual page: stream parameters (-Ccehjmrtvx) must precede stream
definitions (-ios) and per-device parameters (-abz) and stream
definitions (-ios) must precede device definitions (-f). Since
there's no ``server'' and ``non-server'' modes anymore, make the
-l option just detach the process.

ok and help from jakemsr and jmc


# 1.97 29-Jun-2010 jakemsr

If the device isn't opened yet (as can be the case with '-a off'),
don't check if the midi control interface is idle
from ratchov


# 1.96 25-Jun-2010 ratchov

don't attempt to drain devices after they are destroyed, which
results in a use after free(). Catched by jakemsr@ with MALLOC_OPTIONS=J


# 1.95 20-Jun-2010 ratchov

fix midicat(1) device mode when only -i or only -o is used (ie
open the device input-only if -o is used, and output-only if -i is
used).


# 1.94 05-Jun-2010 ratchov

don't prime server buffers, because it's ugly and conceptually
complicated. Instead, request clients to provide enough samples
and start with buffers full.


# 1.93 05-Jun-2010 ratchov

Don't send the initial position as a clock tick with a negative delta.
Add a new ``initial position'' message which is simpler. No bahaviour
change, except effects of the protocol version crank.


# 1.92 04-Jun-2010 ratchov

Allow the audio device to be opened only while it's actually used.
This is necessary for uaudio devices, for instance to start aucat
before the device is plugged. Or to unplug a device whithout
having to restart aucat when another device is plugged. This is
controlled with the new -a option.

Allow multiple audio devices to be used concurently, i.e.
multiple ``-f devname'' options to be used; -f options must follow
per-device options, which is what we do for other options.


# 1.91 08-May-2010 ratchov

prime play buffers as soon as the device is opened, otherwise a
midi client could start it with empty buffers, in turn hurting
audio clients.


# 1.90 02-May-2010 ratchov

Clean up device handling code to clarify different initialization phases
and different device states. Split initialization in two phases:
first global variables are initialized then the audio hardware is opened.
Allow devices that don't support full-duplex to work in play-only or
record-only mode, even if ``-m play'' or ``-m rec'' are not specified.


# 1.89 02-May-2010 ratchov

Don't systematically fill with silence the mixer output. This
might result in extra samples being written to the device
when the mixer is closed.


# 1.88 24-Apr-2010 ratchov

if there are no files to play in legacy mode, print the usage message


# 1.87 24-Apr-2010 ratchov

rename ibuflist/obuflist -> ins/outs to cut long lines
from Thomas Pfaff, thanks


# 1.86 22-Apr-2010 ratchov

Allow multiple users to share the same aucat server. If aucat is
run by root, it binds a shared address to the socket, cranks the
process priority and drops privileges. sio_open(3) will try to
connect to the private socket first (if any), then to the shared
socket. Only one user may have connections to aucat at a given
time.

based on discussions with henning, pyr and others
ok jacek, deraadt


# 1.85 21-Apr-2010 ratchov

when creating the ``default'' subdevice, use last parameters of the
command line, instead of the device parameters.


# 1.84 21-Apr-2010 ratchov

adds the necessary bits to join channels (ex stereo->mono) or
expand channels (ex mono->stereo). It's switched on/off with
the ``-j'' option, (default is "on").


# 1.83 06-Apr-2010 ratchov

aucat (server):

- make the ``-m mode'' option per subdevice, allowing
subdevices to be play-only or rec-only even if the
server is full-duplex

- add ``monitoring'' mode (with ``-m mon''). This is a
record-only stream from which played streams can be
recorded (kind of ``record what you hear'').

- allow MIDI devices to be subscribed to the controlling
MIDI port of the server, ie what midicat does (with -f
option), but using the -q option.

- add flow control to the protocol, and serialize clock
ticks (sio_onmove() calls) and data chunks. This should
fix certain full-duplex programs, broken with ``magic''
block/buffer size combinations.

- enable 3 block latency which is the minimum theoretical.
Make -z and -b options correspond to device parameters.

- make sio_getcap(3) the same for aucat and sun backends,
ie return whatever is supported (``everything'' in the
aucat case, since everything is actulally supported).

aucat (player):

- enable ``-m mode'' option to select between monitoring
and recording when ``-o file'' is used.

- plug MIDI code to non-server codepath. The MIDI control
device is specified with the ``-q device'' option, as in
server mode.

- impliment lseek()'ing within files (controlled
through MIDI). Necessary to use aucat with a MIDI
sequencer.

midicat (thrubox):

- rename ``-f'' flag to ``-q'', so it has the
same name as in aucat (-f is still working)

ok jakemsr@, tweaks from jmc@


# 1.82 03-Apr-2010 ratchov

backout last change, committed by mistake, sorry...


# 1.81 03-Apr-2010 ratchov

doc fixes from jmc


Revision tags: OPENBSD_4_7_BASE
# 1.80 14-Jan-2010 ratchov

Use -C option to specify recorded channel range instead of -c which
is for played channels.

spotted by Jan Stary <hans at stare.cz>, thanks


# 1.79 11-Jan-2010 ratchov

When resuming from suspend, prime play buffers with silence,
otherwise the client would underrun by more than its own buffer
size, which currently is not allowed (causes the client to
deadlock).

Set the minimum client buffer size to two blocks. Single block
buffers don't work properly yet.

with help from jakemsr


# 1.78 10-Jan-2010 ratchov

Add traces to help tracking subtle timing bugs often causing
underuns and overruns; such bugs are hard to debug with ktrace
or a debugger. They are also handy to debug or monitor code
using aucat.

To enable traces, compile aucat with ``make DEBUG=-DDEBUG'' and
use the -d option multiple times.

ok jakemsr


# 1.77 05-Jan-2010 ratchov

Make the way .wav files are handled closer to the way sockets are
handled, which is conceptually simpler. No behaviour changes.


# 1.76 21-Nov-2009 ratchov

use strtonum(3) and strtol(3) instead of sscanf(3)
suggested by deraadt@


# 1.75 21-Nov-2009 deraadt

spelling


# 1.74 03-Nov-2009 ratchov

Allow any program using aucat to act as MMC slave and MTC master
transparently. Multiple audio applications can be started
synchronously from external software/hardware supporting the
standard Start/Stop/Relocate messages. The server clock is exposed
through MTC, allowing non-audio software/hardware to be
synchronized to audio applications.


# 1.73 27-Oct-2009 ratchov

add missing word in error message


# 1.72 10-Oct-2009 ratchov

don't use a references to the device file to check the current mode


# 1.71 10-Oct-2009 ratchov

make dev_done() common to audio and MIDI, and drop dev_thrudone()


# 1.70 10-Oct-2009 ratchov

use dev_done() for loopback devices too and remove dev_loopdone()


# 1.69 05-Oct-2009 ratchov

add -d flag. When started in server mode, if the -d flag is
used aucat and midicat don't daemonize and log on stderr.


# 1.68 27-Sep-2009 ratchov

remove all debug traces


# 1.67 26-Aug-2009 jmc

oops, forgot to update usage();


# 1.66 19-Aug-2009 ratchov

don't try to process -o arguments using the input file list, fixes
midicat crashing sometimes when -o is used


# 1.65 19-Aug-2009 ratchov

organize midi code like audio code, no functional change


# 1.64 17-Aug-2009 ratchov

allow midicat to take multiple -i and -o options


# 1.63 17-Aug-2009 ratchov

if one or more MIDI devices are specified in server mode, subscribe
them to the MIDI thru box


# 1.62 25-Jul-2009 ratchov

fix comments, remove no longer needed declaration, reorder includes
from Thomas Pfaff


# 1.61 25-Jul-2009 ratchov

Currently midi capable programs can control midi hardware, but
cannot cooperate with other programs. The aim of this change is
to allow any program to send midi data to other programs as they
were midi hardware. For instance, this change should solve the
longstanding problem of using a midi sequencer with software
synthesizers. More precisely:

- new midicat(1) utility (actually hardlink to aucat(1)).
it creates software midi thru boxes, allowing programs
to send midi messages to other programs as they were
midi(4) hardware.

- new midi api in libsndio (see mio_open(3)), to access
midi(4) devices and midicat(1) sockets in a uniform way.

- new device naming scheme <service>:<unit>[.<option>],
common to audio and midi.

- new sndio(7) manual describing concepts and naming

The current audio device naming still works, but people having
scripts or configuration files containing device names could read
the sndio(7) man page and slowly start updating device names.

discussed with jakemsr@ and deraadt@, help form jmc@


Revision tags: OPENBSD_4_6_BASE
# 1.60 27-Apr-2009 ratchov

make -v option apply also to implicit default socket


# 1.59 17-Mar-2009 jmc

- rename the arg to -b "nframes" and sync usage()
- grammar fix for the server section

from Thomas Pfaff
ok ratchov


Revision tags: OPENBSD_4_5_BASE
# 1.58 06-Feb-2009 ratchov

move error messages reported to user into main()


# 1.57 04-Feb-2009 ratchov

if there are too many connections, stop acceping new ones rather
than exit()ing with ``too many open files'' fatal error


# 1.56 04-Feb-2009 ratchov

daemonize when in server mode, suggested by many
ok jakemsr


# 1.55 03-Feb-2009 ratchov

in server mode, create /tmp/aucat-userid/ directory with
permissions 0700, and create sockets in it. This prevents
one local user to eavesdrop or disturb audio programs of
other users.

if you're using the ``-s socket'' option with an absolute
path as argument, please update it to use a socket name.

requested by many, bits from jakemsr and otto
ok jakemsr


# 1.54 25-Jan-2009 ratchov

don't set malloc_options to not interfer with /etc/malloc.conf
requested and explained by fgsch@, henning@, otto@


# 1.53 23-Jan-2009 ratchov

catch SIGHUP and SIGTERM too, to ensure that recorded files are
properly closed and sockets are cleaned up.


# 1.52 23-Jan-2009 ratchov

spacing + typo fix from Matt Jibson <matt.jibson(at)gmail.com>


# 1.51 10-Jan-2009 ratchov

add "loopback" mode in which input is connected to the output.
This is useful to mix, demultiplex, resample or reencode audio
files off-line.
tweak + ok jakemsr


# 1.50 29-Dec-2008 ratchov

make the code "more correct": add reference counters to aproc
structures so we can keep a pointer to the device. Beside some
simplifications, this allows to easily handle the situation
where the audio device desappears. No change in the behaviour.


# 1.49 26-Dec-2008 ratchov

*** empty log message ***


# 1.48 07-Dec-2008 ratchov

When resampling, don't require the sample frequency to be an integer.
This removes the arithmetic constraint between the sample frequency
and the block size and all the associated code. Now aucat can work
in server mode with any block size.
ok jakemsr


# 1.47 23-Nov-2008 ratchov

Use file input parameter as default device output and file output parameters
as default device input. That's what the man page says


# 1.46 20-Nov-2008 ratchov

take into account socket parameters into calculation of device
parameters; without this change aucat uses the device default
parameters and they are not necessarily usable for multi-stream
mode.


# 1.45 17-Nov-2008 jmc

- rename -s arg name to "socket"
- condense EXAMPLES into a single screenful

ok ratchov


# 1.44 17-Nov-2008 jmc

tweak previous man page changes, and sync usage();


# 1.43 17-Nov-2008 ratchov

allow aucat to run as server in play-only and record-only mode, so
it can be used on play-only, record-only and half-duplex devices.
ok jakemsr


# 1.42 16-Nov-2008 ratchov

allow aucat to listen on multiple sockets. Each socket carries its
channel and volume settings allowing multiple configuration to
coexist. Mostly useful for envy(4)-like devices, but can be used
to force different apps to use different settings.


# 1.41 16-Nov-2008 ratchov

add ``template'' parameters to sockets rather than using device
parameters. No functional change.


# 1.40 16-Nov-2008 ratchov

Make clients inherit the volume parameter when the -v option is used in
server mode. It gives the maximum volume a client may have. This wastes
dynamic range, but allows volume to stay constant when other clients
connect or disconnect.


# 1.39 12-Nov-2008 ratchov

when destroying the device, mark the mixer and the demultiplexer
for ``AUTOQUIT'' first, and then call file_eof() and friends.
fixes crashes sthen@ noticed on zaurus


# 1.38 11-Nov-2008 ratchov

if the device disappear the mixer and demultiplexer chains are
destroyed. If this happen then warn and exit, rather than crashing.


# 1.37 11-Nov-2008 ratchov

termninate non-server aucat when it's idle rather that using
the hackish {MIX,SUB}_AUTOQUIT features. This way we don't
have to care about possible references on destroyed objects.


# 1.36 11-Nov-2008 ratchov

when not in server mode, aucat terminates automatically using
the {MIX,SUB}_AUTAQUIT features. In this case we're not
allowed to directly touch dev_mix and dev_sub because they
can disappar. So disable the ``suspend'' and ``quit'' bits
when {MIX,SUB}_AUTAQUIT are used.


# 1.35 10-Nov-2008 ratchov

add a per-stream ``soft volume'' knob and the corresponding -v option.
The code will be useful later for the volume knob in the sndio API.


# 1.34 09-Nov-2008 ratchov

make aucat(1) stop automatically the audio(1) device if it's idle. This
way, when in server mode, it consumes no CPU if there are no clients.
Later, this will allow to start aucat(1) at session or system startup.


# 1.33 08-Nov-2008 ratchov

set the default device to /dev/audio for legacy mode.
From Thomas Pfaff <tpfaff _at_ agderlink.no> and tweaks from me


# 1.32 07-Nov-2008 ratchov

expose the block size in the sndio API by making par->round writable
and thus remove the ugly rate <-> block-size table from
sio_setpar(3). Handle the block size negociation in aucat(1), since
it has few constrains the code is overally simpler.
ok jakemsr@, major crank suggested by deraadt@


# 1.31 03-Nov-2008 ratchov

A small optimization: handle most N-channel <-> M-channel conversions
inside the mixer and the demultiplexer. This way, aucat will not
trigger the heavy conversion code when only channel conversions are
required. Cuts ~50% of the CPU usage on envy(4) devices, can improve
surround 4.0, 5.1 and 7.1 capable devices. No functionnal change.


# 1.30 26-Oct-2008 ratchov

zap `q' from the getopts string too,
requested by jmc@


# 1.29 26-Oct-2008 jmc

sync SYNOPSIS and usage();


# 1.28 26-Oct-2008 ratchov

add minimal server capability to aucat(1). When started in server
mode, it listens on an unix socket and mixes/demultiplexes any number
of full-duplex streams, doing necessary format conversions and
resampling on the fly.

programs can use the new libsa(3) library to play and record audio.
The library provides a very simple API to connect to the audio server;
if aucat(1) isn't running, it uses the audio(4) driver transparently
instead.


# 1.27 25-Aug-2008 sobrado

minor tweak for removal of "[-d devel]" (change done in r1.19 and r1.21).

ok ratchov@


# 1.26 14-Aug-2008 ratchov

move all device related stuff from aucat.c to a new dev.c file.
The new dev_xxx() routines expose a "high level" self-contained
interface to the device. At initialization, the device is opened
and two chains of aproc structures are created:

* a playback chain that exposes a (initially) empty mix
aproc to which the rest of the code can attach new
streams to be played

* record chain that exposes a (initially) empty sub aproc
to which the rest of the code can attach new stream to
to record

The rest of the code, has just to use dev_attach() routine to
attach streams. While we're at it, add a ``devops'' structure
containing pointers to the device-specific routines. This will
allow later to add support for other type of device than the
Sun API.

Also, write the .wav headers in file_del(), so put all header
related data in the file strucuture. This allows to close() the
file, as soon as wpipe_xxx() aproc terminates. This will be
useful for the server, because it will need to close() descripts
of closed connections immediately.

add mix_pushzero() routine to fill the mixer with silence. It
will be used to avoid the mixer to underrun when there are no
input streams. Since we always have at least one input stream
there's no behaviour change.

ok jakemsr


Revision tags: OPENBSD_4_4_BASE
# 1.25 02-Jun-2008 ratchov

document latest changes: -d flag is replaced by AUCAT_DEBUG
environment variable, new -xX options

bits from eric, ok jakemsr


# 1.24 02-Jun-2008 ratchov

currently, we start the device as soon as write() blocks; it's better
to continue filling the play buffer until it's full. This way, the
play buffer has fewer chances to underrun when the device is started.

ok jakemsr


# 1.23 02-Jun-2008 ratchov

if aucat is suspended, then the kernel continues running the device
(produces silence) and later, once aucat is resumed the kernel starts
dropping samples, it will try to drop as many samples as silence was
produced. So suspending breaks the aucat process permanently.

workaround this by blocking the signals ie disabling suspending of aucat
from the tty. The long term solution would be to catch SIGCONT and to stop
the device, resync/refill buffers and restart the device. That's really a
lot of work...

ok jakemsr


# 1.22 02-Jun-2008 ratchov

allow the user to choose the aucat behaviour when underruns/overruns
occur on a per-stream basis, using -Xx flags. There are 3 possible
policies:

- ignore : ignores underruns/overruns, for instance, this mode
could be used for creating simple pipes with utilities; like in
your last cdio diff.

- sync : insert/discard samples in order to keep all streams in
sync, useful for multi-tracker-like apps and/or to sync
midi/video/whatever on audio streams (this was the previous
behaviour)

- error : if overruns/underruns occur, consider it as fatal error
and kill the corresponding stream (without disturbing others).
Useful, for reliable recordings (and/or debugging aucat
itself:).

ok jakemsr


# 1.21 02-Jun-2008 ratchov

(remove -d, second part) use strtonum() instead of sscanf() and inlined
checks

suggested and ok jakemsr


# 1.20 02-Jun-2008 ratchov

since underruns/overruns are handled outside devices, remove code that
check for xruns in devices.

ok jakemsr


# 1.19 02-Jun-2008 ratchov

(remove -d, first part) replace -d flag by AUCAT_DEBUG environment
variable. This eases turning debugging on/off when aucat is started by
another program.

"i like the idea" jakemsr


# 1.18 26-May-2008 jmc

put -q in the right place;


# 1.17 26-May-2008 jakemsr

add -q option which enables "quiet" operation.

ok ratchov@


# 1.16 23-May-2008 jmc

various fixes for aucat.1, and sync usage(); ok ratchov


# 1.15 23-May-2008 ratchov

add support for:
- recording, full-duplex operation
- format conversions and resampling on the fly
- mixing on the fly of multiple inputs of different formats
- up to 16 channels, simplistic "routing" of channel ranges
- more linear encodings (in raw and wav files)
the old behaviour is fully preserved if none of the new -i and -o
options are used.

code and fixes from jakemsr@ and eric@, suggestions by others.
ok "go ahead" deraadt@


# 1.14 13-Apr-2008 jakemsr

- remove code that was rendered pointless by the last commit
- minor knf

from Pierre Riteau


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE
# 1.13 20-Mar-2007 uwe

Support for playing simple RIFF files (.wav) from Simon Effenberg, savar
at schuldeigen de, with some tweaks from me. ok millert, tests ckuethe,
liked by some disliked by nobody, but he's not even a real user :)


Revision tags: OPENBSD_4_1_BASE
# 1.12 20-Dec-2006 steven

remove unneeded #include

ok mbalmer@ deraadt@


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.11 20-Jan-2005 jaredy

- knf
- check read/write/etc. calls for errors
- use constants instead of hardcoded pathnames
- better variable types
- add usage output

suggestions from various, ok ian


# 1.10 13-Jan-2005 ian

Close what you open: obvious fix, ok jared@, more to follow


Revision tags: OPENBSD_3_5_BASE OPENBSD_3_6_BASE
# 1.9 20-Oct-2003 jmc

typos from Jared Yanovich, slightly ammended;


Revision tags: OPENBSD_3_4_BASE
# 1.8 10-Jul-2003 david

add missing includes
ok deraadt@ tedu@


# 1.7 02-Jul-2003 deraadt

protos


# 1.6 10-Jun-2003 deraadt

mostly ansi cleanup; pval ok


Revision tags: OPENBSD_3_3_BASE
# 1.5 09-Dec-2002 millert

From Andrushock, s/sucess/success/g


Revision tags: OPENBSD_2_8_BASE OPENBSD_2_9_BASE OPENBSD_3_0_BASE OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.4 01-Jul-2000 millert

Add -f option to specify audio device, honor AUDIODEVICE environment vairable,
and some KNF.


Revision tags: OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE
# 1.3 05-Jan-1997 kstailey

minor tweaks; htonl() -> ntohl(); added SEE ALSO


# 1.2 03-Jan-1997 kstailey

removed unnecessary test


# 1.1 02-Jan-1997 kstailey

new utility for playing sound files


# 1.176 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


# 1.175 19-May-2019 ratchov

Fix integer overflow in block size calculation.

Fixes truncation of contents of files with rate above 64kHz
generate in off-line mode (i.e. -n option).


Revision tags: OPENBSD_6_5_BASE
# 1.174 21-Feb-2019 ratchov

Use reallocarray in place of malloc(a * b) to handle possible
overflow in multiplication of malloc argument.


Revision tags: OPENBSD_6_4_BASE
# 1.173 13-May-2018 ratchov

Add missing pledge(). From Jesper Wallin <jesper at ifconfig.se>.
Thanks!


Revision tags: OPENBSD_6_3_BASE
# 1.172 07-Nov-2017 ratchov

prefill with silence the buffer where cmap_copy() stores samples
rather than the file buffer. Fixes initialized data being treated as
recorded samples when resampling or format conversions are involved
but mono->stereo conversion is disabled.


# 1.171 03-Nov-2017 ratchov

Prefill rec buffer with silence. This is necessary because certain
channels don't get samples, for instance when mono->stereo conversion
is disabled.


Revision tags: OPENBSD_6_2_BASE
# 1.170 20-Jul-2017 ratchov

Fix integer overflow that would causes >12 hours files
to not properly relocate. Found with coverity.


Revision tags: OPENBSD_6_1_BASE
# 1.169 03-Jan-2017 ratchov

Assert we're not freeing buffers we didn't allocate (DEBUG mode).


# 1.168 03-Jan-2017 ratchov

Log play volume together with other play parameters.


# 1.167 30-Sep-2016 ratchov

Set the device rate to the calculated rate. Otherwise, the signal
may endup resampled twice.


# 1.166 30-Sep-2016 ratchov

Remove unnecessary includes. From Michael W. Bombardieri <mb at ii.net>.


# 1.165 27-Sep-2016 ratchov

Don't rely on the resampling code to calculate the number of samples
to process, as it may produce one extra sample (to handle accumulation
of fractional samples), which would cause access to one sample past
the end of the buffer and crash aucat. Fix this by limiting the number
of samples processed to a single block.

Found by and help from Michael W. Bombardieri <mb at ii.net>. Thanks.


Revision tags: OPENBSD_6_0_BASE
# 1.164 07-Jun-2016 ratchov

Add -g and -p options to control device and file start position.


# 1.163 07-Jun-2016 ratchov

Add resamp_getcnt() routine to calculate the exact number of samples
that would be consumed and produced by the sampler rate converter. Use
it to avoid partial samples that are not properly handled. Fixes
last samples of certain files causing aucat to abort.


# 1.162 02-Jun-2016 ratchov

remove forgotten debug printfs


# 1.161 02-Jun-2016 ratchov

set exit status to 1 if -n is used without -i and -o


# 1.160 31-May-2016 ratchov

Pass hr:min:sec:frame.cent to dev_mmcloc() directly and remove the
useless intermediate conversion from and to 2400-th of seconds.


# 1.159 31-May-2016 ratchov

To store MMC position, use samples (at device rate) as time units
instead of 2400-ths of second. No behaviour change.


# 1.158 28-May-2016 ratchov

Fix file block size rounding and ensure it's large enough to store a
full audio block.


# 1.157 27-May-2016 ratchov

When resampling, use the exact resampling factor instead of the ratio
between input and output block sizes. This was inherited from sndiod,
but is not required for files because they are continuous streams of
samples and do not need to be split in blocks of equal duration.

This change makes playback/recording rate match exactly the requested
sample rate.


# 1.156 27-May-2016 ratchov

Flush rec buffer if there's less than one block space left and refill
play buffer if there's less than one block of data left. This is the
correct condition in the general case. No behaviour change, as all
input/output is multiple of the block size.


# 1.155 27-May-2016 ratchov

Simplify slot_fill() and slot_flush(). No behaviour change


# 1.154 27-May-2016 ratchov

Make resamp_do() get the exact number input and output samples and
provide routines to calculate them. This way we don't rely on it
to calculate the bytes procuded/consumed anymore. No behaviour change.


# 1.153 26-May-2016 ratchov

Make format conversion routines return the number of frames
consumed from both input and output buffers. No behaviour change.


Revision tags: OPENBSD_5_9_BASE
# 1.152 29-Jan-2016 ratchov

Check that byte order is native only if we're using more than one
byte per sample, check that samples are lsb-aligned only if there's
padding. Fixes unsupported parameters not being detected if compiled
in 24-bit mode and sndiod is not running.


# 1.151 10-Jan-2016 ratchov

trailing spaces, fit in 80 columns


# 1.150 09-Nov-2015 mmcc

Remove xfree(). From Michael W. Bombardieri.

ok ratchov@


# 1.149 27-Aug-2015 ratchov

fix typos in comments and error messages


Revision tags: OPENBSD_5_8_BASE
# 1.148 16-May-2015 ratchov

Call slot_stop() after the play buffer is drained to properly cleanup
the slot. Fixes periodic glitches occurring after eof is reached, and
then playback restarted with a MMC-relocate and MMC-start.


Revision tags: OPENBSD_5_7_BASE
# 1.147 21-Jan-2015 jmc

fix macro breakage and update usage();


# 1.146 21-Jan-2015 ratchov

Simplify internals and rewrite file header parsing code:
- remove -M, -t, -w, -C, -x flags that don't make sense anymore
- make "-j off" the default (sndiod already does the job)
- don't limit the number of played/recorded files.
- add support for floating-point encoded files.
- add support for apple .aiff and sun/next .au files


# 1.145 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.144 18-Nov-2013 ratchov

remove unused variables


# 1.143 18-Nov-2013 ratchov

add missing prototypes


# 1.142 12-Nov-2013 ratchov

remove bogus legacy server-specific bits that cause confusion


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.141 03-Dec-2012 ratchov

Remove server capability description from the documentation and from
the usage string, and just say that for historic reasons aucat behaves
like sndiod.
with help from jmc@


# 1.140 23-Nov-2012 ratchov

Since there's no sndiod -> aucat link anymore, allow aucat to be run
in server mode even when not invoked as sndiod. This allows aucat and
the new sndiod daemon to live together in /usr/bin, which makes easy
to quickly compare sndiod and aucat (and spot possible regressions)
without having to modify /usr/bin.


# 1.139 27-Oct-2012 ratchov

don't stat & test sndio home directory since we never use it.


# 1.138 27-Oct-2012 ratchov

don't (re-)define SNDIO_USER and SNDIO_PRIO if they are already defined,
this way they can be defined at compilation time


Revision tags: OPENBSD_5_2_BASE
# 1.137 27-Jun-2012 ratchov

Uniformize error messages printing, use err() instead of fprintf()
when appropriate. From Michael W. Bombardieri <mb at ii.net>


# 1.136 19-Jun-2012 ratchov

Remove hidden options that are not needed any more,
from Remco <remco at d-compu.dyndns.org>. Thanks


# 1.135 23-May-2012 ratchov

Make the "default" string a valid device name that has the same effect
as NULL has. This will (hopefully) simplify ports where the user
passes the device string.


# 1.134 11-Apr-2012 ratchov

Use "unsigned int" rather than "unsigned". No object change.
suggested by deraadt@


Revision tags: OPENBSD_5_1_BASE
# 1.133 09-Feb-2012 ratchov

Crank the default buffer size to 160ms (ie 7680 frames at 48kHz).
Workarounds stuttering caused by something hogging the cpu in kernel
mode and preventing sndiod from running at time.
ok deraadt


# 1.132 26-Jan-2012 ratchov

Make the default rate 48kHz and the default block size 10ms. These
settings ensure video players and programs using MTC are smooth by
default. Thanks to all who tested.


# 1.131 10-Jan-2012 ratchov

When a sub-device is created (-s), check if a hardware
device was already created (-f) before trying to use the
default one, otherwise all sub-devices end up attached
to the default device. Noticed by peters at schwertfisch.de,
thanks!


# 1.130 09-Dec-2011 ratchov

fix usage string: remove -M from aucat and -l from aucat and sndiod
from jmc


# 1.129 09-Dec-2011 ratchov

add a new sndiod audio/midi daemon and hide aucat server-specific
options.


# 1.128 02-Dec-2011 ratchov

don't use a "server" flag, just check if we expose sub-devices


# 1.127 02-Dec-2011 ratchov

reuse midi-control code to implement midi thru boxes and remove
the old midithru implementation; less code, less bugs. As a side
effect, midi output doesn't implement running status "compression"
any more.


# 1.126 20-Nov-2011 ratchov

Move mmc/mtc and volume control bits from struct aproc to struct
dev. Allows volume settings to be saved while the device is kept
closed. Besides that, no behabiour changes.


# 1.125 15-Nov-2011 ratchov

Add a "device number" component in sndio(7) device names, allowing a
single aucat instance to handle all audio and MIDI services. Since
this partially breaks compatibility, this is a opportunitiy to fix few
other design mistakes (eg ':' being used by inet6, type name vs api
name confusion, etc..). This leads to the following names:

type[@hostname][,unit]/devnum[.option]

The device number is the minor device number for direct hardware
access (ie the 'N' in /dev/audioN). For aucat, this is the occurence
number of the -f (or -M) option.

There's a compatibility hook to keep old names working if only one
aucat server is running.


# 1.124 18-Oct-2011 ratchov

keep the device closed by default (when not used)


# 1.123 18-Oct-2011 ratchov

remove duplicate options in getopt() string


# 1.122 17-Oct-2011 ratchov

Remove midicat since aucat can now be used instead of midicat
with almost the same syntax (roughly an extra -M option).
Thru boxes are created with aucat, and corresponding MIDI port
names have the "aucat" prefix instead of "midithru". The old
device name will still work some time for backward compatibility.

ok deraadt


# 1.121 12-Oct-2011 jmc

- avoid line wrapping an example
- sort usage() for midicat
- tweak a bit of unclear wording

ok ratchov


# 1.120 12-Oct-2011 ratchov

Simplify and improve the way options are parsed and remove ~300 lines
of code that becomes unused. Few command line arguments changes are
required though:

- stream definitions (-ios) now must follow devices definitions they
are attached to (-fMn)

- the -n option is now a special "loopback" device and is thus used
like -f, eg it must precede streams

- in midicat, midi thru boxes are not created automatically anymore,
the new "-M" option must be used for that

- channel numbers (-Cc options) correspond always to channel numbers
of the hardware.

- the -u option isn't needed anymore

- increase the log verbosity so user errors are logged
without using -d

tested by many, help from jmc


Revision tags: OPENBSD_5_0_BASE
# 1.119 29-Jun-2011 ratchov

make -u flag per-device similarly to -a and -w


# 1.118 20-Jun-2011 ratchov

Make -aoff option apply to MIDI ports (-q) as well, ensuring the device
stays closed also if -q is used. As we're at it, add -a to midicat so
it behaves like aucat.


# 1.117 03-Jun-2011 ratchov

Allow device encoding to be set if -u is also used. Useful to degrade
quality to save bandwith when a remote audio device is used.


# 1.116 03-Jun-2011 ratchov

Reorganize the way command line options are parsed and stored.
No behaviour change.


# 1.115 26-May-2011 ratchov

add a new -w flag to control whether master volume is automatically
adjusted when new streams are connected and disconnected. Disabling
automatic volume adjustment makes sense if all streams are recorded
with properly lowered volumes.


# 1.114 10-May-2011 ratchov

fix option handling and enable TCP in midicat
from peters at schwertfisch.de, thanks!


# 1.113 28-Apr-2011 ratchov

Implement a new authentication method allowing aucat and midicat to
work over TCP, for instance, to expose the sound card of one machine
with other machines of the network.

The first client generates a 128-bit random number (aka the
session cookie), saves it in $HOME/.aucat_cookie and sends it to the
server. Successive clients load the cookie from $HOME/.aucat_cookie
and send it to the server but the server accepts only clients whose
cookie matches the session cookie. When all clients are gone, the
session is over, and another cookie could start a new session, and so
on.

TCP is enabled on the server with the new -L option, and on the client
side hostnames are specified with a new optional component in the
device name.

hints from damien, dlg and deraadt, tweaks from jmc


# 1.112 27-Apr-2011 ratchov

move amsg.h containing protocol defs from aucat side to libsndio side.

requested by deraadt


# 1.111 27-Apr-2011 deraadt

use more volatile sig_atomic_t in signal handlers
ok ratchov


# 1.110 19-Apr-2011 ratchov

don't hold a pointer to "listen" structure. Instead, when it must
be free()ed, iterate over the file_list, and find the structure
to free. This is safer and simpler. No behaviour change


# 1.109 16-Apr-2011 ratchov

for unix domain socket addresses use AUCAT_PATH and MIDICAT_PATH
macros instead of hardcoded strings. No object change


# 1.108 17-Mar-2011 ratchov

use a DEFAULT_RATE macro instead of hardcoding 44100, no object change


Revision tags: OPENBSD_4_9_BASE
# 1.107 05-Nov-2010 ratchov

unbreak compilation without DEBUG defined


# 1.106 21-Oct-2010 ratchov

use MODE_xxx and XRUN_xxx macros everywhere, and zap corresponding
AMSG_xxx macros, which in turns simplifies the code


# 1.105 08-Sep-2010 deraadt

aucat: getpwnam: No such file or directory
is not a proper error message.


# 1.104 20-Aug-2010 ratchov

polish the code: remove few unused #includes, add missing ones,
fix NULL vs 0, etc. No behaviour change.


# 1.103 19-Aug-2010 ratchov

fix NULL pointer deref in midicat option parsing when ``-'' is
used as filename


Revision tags: OPENBSD_4_8_BASE
# 1.102 31-Jul-2010 ratchov

remove 2 years old compatibility mode


# 1.101 10-Jul-2010 ratchov

In midi server mode, don't terminate the midi thru box if there are
no connections anymore.


# 1.100 10-Jul-2010 ratchov

fix parameter handling: don't try to open a ``default'' midi port
if no files are given on the command line


# 1.99 06-Jul-2010 jmc

various tweaks to previous; ok ratchov


# 1.98 06-Jul-2010 ratchov

Handle all streams the same way because there's no actual
difference between audio files and client connections. Clean up
the way command line options are handled and clarify this in the
manual page: stream parameters (-Ccehjmrtvx) must precede stream
definitions (-ios) and per-device parameters (-abz) and stream
definitions (-ios) must precede device definitions (-f). Since
there's no ``server'' and ``non-server'' modes anymore, make the
-l option just detach the process.

ok and help from jakemsr and jmc


# 1.97 29-Jun-2010 jakemsr

If the device isn't opened yet (as can be the case with '-a off'),
don't check if the midi control interface is idle
from ratchov


# 1.96 25-Jun-2010 ratchov

don't attempt to drain devices after they are destroyed, which
results in a use after free(). Catched by jakemsr@ with MALLOC_OPTIONS=J


# 1.95 20-Jun-2010 ratchov

fix midicat(1) device mode when only -i or only -o is used (ie
open the device input-only if -o is used, and output-only if -i is
used).


# 1.94 05-Jun-2010 ratchov

don't prime server buffers, because it's ugly and conceptually
complicated. Instead, request clients to provide enough samples
and start with buffers full.


# 1.93 05-Jun-2010 ratchov

Don't send the initial position as a clock tick with a negative delta.
Add a new ``initial position'' message which is simpler. No bahaviour
change, except effects of the protocol version crank.


# 1.92 04-Jun-2010 ratchov

Allow the audio device to be opened only while it's actually used.
This is necessary for uaudio devices, for instance to start aucat
before the device is plugged. Or to unplug a device whithout
having to restart aucat when another device is plugged. This is
controlled with the new -a option.

Allow multiple audio devices to be used concurently, i.e.
multiple ``-f devname'' options to be used; -f options must follow
per-device options, which is what we do for other options.


# 1.91 08-May-2010 ratchov

prime play buffers as soon as the device is opened, otherwise a
midi client could start it with empty buffers, in turn hurting
audio clients.


# 1.90 02-May-2010 ratchov

Clean up device handling code to clarify different initialization phases
and different device states. Split initialization in two phases:
first global variables are initialized then the audio hardware is opened.
Allow devices that don't support full-duplex to work in play-only or
record-only mode, even if ``-m play'' or ``-m rec'' are not specified.


# 1.89 02-May-2010 ratchov

Don't systematically fill with silence the mixer output. This
might result in extra samples being written to the device
when the mixer is closed.


# 1.88 24-Apr-2010 ratchov

if there are no files to play in legacy mode, print the usage message


# 1.87 24-Apr-2010 ratchov

rename ibuflist/obuflist -> ins/outs to cut long lines
from Thomas Pfaff, thanks


# 1.86 22-Apr-2010 ratchov

Allow multiple users to share the same aucat server. If aucat is
run by root, it binds a shared address to the socket, cranks the
process priority and drops privileges. sio_open(3) will try to
connect to the private socket first (if any), then to the shared
socket. Only one user may have connections to aucat at a given
time.

based on discussions with henning, pyr and others
ok jacek, deraadt


# 1.85 21-Apr-2010 ratchov

when creating the ``default'' subdevice, use last parameters of the
command line, instead of the device parameters.


# 1.84 21-Apr-2010 ratchov

adds the necessary bits to join channels (ex stereo->mono) or
expand channels (ex mono->stereo). It's switched on/off with
the ``-j'' option, (default is "on").


# 1.83 06-Apr-2010 ratchov

aucat (server):

- make the ``-m mode'' option per subdevice, allowing
subdevices to be play-only or rec-only even if the
server is full-duplex

- add ``monitoring'' mode (with ``-m mon''). This is a
record-only stream from which played streams can be
recorded (kind of ``record what you hear'').

- allow MIDI devices to be subscribed to the controlling
MIDI port of the server, ie what midicat does (with -f
option), but using the -q option.

- add flow control to the protocol, and serialize clock
ticks (sio_onmove() calls) and data chunks. This should
fix certain full-duplex programs, broken with ``magic''
block/buffer size combinations.

- enable 3 block latency which is the minimum theoretical.
Make -z and -b options correspond to device parameters.

- make sio_getcap(3) the same for aucat and sun backends,
ie return whatever is supported (``everything'' in the
aucat case, since everything is actulally supported).

aucat (player):

- enable ``-m mode'' option to select between monitoring
and recording when ``-o file'' is used.

- plug MIDI code to non-server codepath. The MIDI control
device is specified with the ``-q device'' option, as in
server mode.

- impliment lseek()'ing within files (controlled
through MIDI). Necessary to use aucat with a MIDI
sequencer.

midicat (thrubox):

- rename ``-f'' flag to ``-q'', so it has the
same name as in aucat (-f is still working)

ok jakemsr@, tweaks from jmc@


# 1.82 03-Apr-2010 ratchov

backout last change, committed by mistake, sorry...


# 1.81 03-Apr-2010 ratchov

doc fixes from jmc


Revision tags: OPENBSD_4_7_BASE
# 1.80 14-Jan-2010 ratchov

Use -C option to specify recorded channel range instead of -c which
is for played channels.

spotted by Jan Stary <hans at stare.cz>, thanks


# 1.79 11-Jan-2010 ratchov

When resuming from suspend, prime play buffers with silence,
otherwise the client would underrun by more than its own buffer
size, which currently is not allowed (causes the client to
deadlock).

Set the minimum client buffer size to two blocks. Single block
buffers don't work properly yet.

with help from jakemsr


# 1.78 10-Jan-2010 ratchov

Add traces to help tracking subtle timing bugs often causing
underuns and overruns; such bugs are hard to debug with ktrace
or a debugger. They are also handy to debug or monitor code
using aucat.

To enable traces, compile aucat with ``make DEBUG=-DDEBUG'' and
use the -d option multiple times.

ok jakemsr


# 1.77 05-Jan-2010 ratchov

Make the way .wav files are handled closer to the way sockets are
handled, which is conceptually simpler. No behaviour changes.


# 1.76 21-Nov-2009 ratchov

use strtonum(3) and strtol(3) instead of sscanf(3)
suggested by deraadt@


# 1.75 21-Nov-2009 deraadt

spelling


# 1.74 03-Nov-2009 ratchov

Allow any program using aucat to act as MMC slave and MTC master
transparently. Multiple audio applications can be started
synchronously from external software/hardware supporting the
standard Start/Stop/Relocate messages. The server clock is exposed
through MTC, allowing non-audio software/hardware to be
synchronized to audio applications.


# 1.73 27-Oct-2009 ratchov

add missing word in error message


# 1.72 10-Oct-2009 ratchov

don't use a references to the device file to check the current mode


# 1.71 10-Oct-2009 ratchov

make dev_done() common to audio and MIDI, and drop dev_thrudone()


# 1.70 10-Oct-2009 ratchov

use dev_done() for loopback devices too and remove dev_loopdone()


# 1.69 05-Oct-2009 ratchov

add -d flag. When started in server mode, if the -d flag is
used aucat and midicat don't daemonize and log on stderr.


# 1.68 27-Sep-2009 ratchov

remove all debug traces


# 1.67 26-Aug-2009 jmc

oops, forgot to update usage();


# 1.66 19-Aug-2009 ratchov

don't try to process -o arguments using the input file list, fixes
midicat crashing sometimes when -o is used


# 1.65 19-Aug-2009 ratchov

organize midi code like audio code, no functional change


# 1.64 17-Aug-2009 ratchov

allow midicat to take multiple -i and -o options


# 1.63 17-Aug-2009 ratchov

if one or more MIDI devices are specified in server mode, subscribe
them to the MIDI thru box


# 1.62 25-Jul-2009 ratchov

fix comments, remove no longer needed declaration, reorder includes
from Thomas Pfaff


# 1.61 25-Jul-2009 ratchov

Currently midi capable programs can control midi hardware, but
cannot cooperate with other programs. The aim of this change is
to allow any program to send midi data to other programs as they
were midi hardware. For instance, this change should solve the
longstanding problem of using a midi sequencer with software
synthesizers. More precisely:

- new midicat(1) utility (actually hardlink to aucat(1)).
it creates software midi thru boxes, allowing programs
to send midi messages to other programs as they were
midi(4) hardware.

- new midi api in libsndio (see mio_open(3)), to access
midi(4) devices and midicat(1) sockets in a uniform way.

- new device naming scheme <service>:<unit>[.<option>],
common to audio and midi.

- new sndio(7) manual describing concepts and naming

The current audio device naming still works, but people having
scripts or configuration files containing device names could read
the sndio(7) man page and slowly start updating device names.

discussed with jakemsr@ and deraadt@, help form jmc@


Revision tags: OPENBSD_4_6_BASE
# 1.60 27-Apr-2009 ratchov

make -v option apply also to implicit default socket


# 1.59 17-Mar-2009 jmc

- rename the arg to -b "nframes" and sync usage()
- grammar fix for the server section

from Thomas Pfaff
ok ratchov


Revision tags: OPENBSD_4_5_BASE
# 1.58 06-Feb-2009 ratchov

move error messages reported to user into main()


# 1.57 04-Feb-2009 ratchov

if there are too many connections, stop acceping new ones rather
than exit()ing with ``too many open files'' fatal error


# 1.56 04-Feb-2009 ratchov

daemonize when in server mode, suggested by many
ok jakemsr


# 1.55 03-Feb-2009 ratchov

in server mode, create /tmp/aucat-userid/ directory with
permissions 0700, and create sockets in it. This prevents
one local user to eavesdrop or disturb audio programs of
other users.

if you're using the ``-s socket'' option with an absolute
path as argument, please update it to use a socket name.

requested by many, bits from jakemsr and otto
ok jakemsr


# 1.54 25-Jan-2009 ratchov

don't set malloc_options to not interfer with /etc/malloc.conf
requested and explained by fgsch@, henning@, otto@


# 1.53 23-Jan-2009 ratchov

catch SIGHUP and SIGTERM too, to ensure that recorded files are
properly closed and sockets are cleaned up.


# 1.52 23-Jan-2009 ratchov

spacing + typo fix from Matt Jibson <matt.jibson(at)gmail.com>


# 1.51 10-Jan-2009 ratchov

add "loopback" mode in which input is connected to the output.
This is useful to mix, demultiplex, resample or reencode audio
files off-line.
tweak + ok jakemsr


# 1.50 29-Dec-2008 ratchov

make the code "more correct": add reference counters to aproc
structures so we can keep a pointer to the device. Beside some
simplifications, this allows to easily handle the situation
where the audio device desappears. No change in the behaviour.


# 1.49 26-Dec-2008 ratchov

*** empty log message ***


# 1.48 07-Dec-2008 ratchov

When resampling, don't require the sample frequency to be an integer.
This removes the arithmetic constraint between the sample frequency
and the block size and all the associated code. Now aucat can work
in server mode with any block size.
ok jakemsr


# 1.47 23-Nov-2008 ratchov

Use file input parameter as default device output and file output parameters
as default device input. That's what the man page says


# 1.46 20-Nov-2008 ratchov

take into account socket parameters into calculation of device
parameters; without this change aucat uses the device default
parameters and they are not necessarily usable for multi-stream
mode.


# 1.45 17-Nov-2008 jmc

- rename -s arg name to "socket"
- condense EXAMPLES into a single screenful

ok ratchov


# 1.44 17-Nov-2008 jmc

tweak previous man page changes, and sync usage();


# 1.43 17-Nov-2008 ratchov

allow aucat to run as server in play-only and record-only mode, so
it can be used on play-only, record-only and half-duplex devices.
ok jakemsr


# 1.42 16-Nov-2008 ratchov

allow aucat to listen on multiple sockets. Each socket carries its
channel and volume settings allowing multiple configuration to
coexist. Mostly useful for envy(4)-like devices, but can be used
to force different apps to use different settings.


# 1.41 16-Nov-2008 ratchov

add ``template'' parameters to sockets rather than using device
parameters. No functional change.


# 1.40 16-Nov-2008 ratchov

Make clients inherit the volume parameter when the -v option is used in
server mode. It gives the maximum volume a client may have. This wastes
dynamic range, but allows volume to stay constant when other clients
connect or disconnect.


# 1.39 12-Nov-2008 ratchov

when destroying the device, mark the mixer and the demultiplexer
for ``AUTOQUIT'' first, and then call file_eof() and friends.
fixes crashes sthen@ noticed on zaurus


# 1.38 11-Nov-2008 ratchov

if the device disappear the mixer and demultiplexer chains are
destroyed. If this happen then warn and exit, rather than crashing.


# 1.37 11-Nov-2008 ratchov

termninate non-server aucat when it's idle rather that using
the hackish {MIX,SUB}_AUTOQUIT features. This way we don't
have to care about possible references on destroyed objects.


# 1.36 11-Nov-2008 ratchov

when not in server mode, aucat terminates automatically using
the {MIX,SUB}_AUTAQUIT features. In this case we're not
allowed to directly touch dev_mix and dev_sub because they
can disappar. So disable the ``suspend'' and ``quit'' bits
when {MIX,SUB}_AUTAQUIT are used.


# 1.35 10-Nov-2008 ratchov

add a per-stream ``soft volume'' knob and the corresponding -v option.
The code will be useful later for the volume knob in the sndio API.


# 1.34 09-Nov-2008 ratchov

make aucat(1) stop automatically the audio(1) device if it's idle. This
way, when in server mode, it consumes no CPU if there are no clients.
Later, this will allow to start aucat(1) at session or system startup.


# 1.33 08-Nov-2008 ratchov

set the default device to /dev/audio for legacy mode.
From Thomas Pfaff <tpfaff _at_ agderlink.no> and tweaks from me


# 1.32 07-Nov-2008 ratchov

expose the block size in the sndio API by making par->round writable
and thus remove the ugly rate <-> block-size table from
sio_setpar(3). Handle the block size negociation in aucat(1), since
it has few constrains the code is overally simpler.
ok jakemsr@, major crank suggested by deraadt@


# 1.31 03-Nov-2008 ratchov

A small optimization: handle most N-channel <-> M-channel conversions
inside the mixer and the demultiplexer. This way, aucat will not
trigger the heavy conversion code when only channel conversions are
required. Cuts ~50% of the CPU usage on envy(4) devices, can improve
surround 4.0, 5.1 and 7.1 capable devices. No functionnal change.


# 1.30 26-Oct-2008 ratchov

zap `q' from the getopts string too,
requested by jmc@


# 1.29 26-Oct-2008 jmc

sync SYNOPSIS and usage();


# 1.28 26-Oct-2008 ratchov

add minimal server capability to aucat(1). When started in server
mode, it listens on an unix socket and mixes/demultiplexes any number
of full-duplex streams, doing necessary format conversions and
resampling on the fly.

programs can use the new libsa(3) library to play and record audio.
The library provides a very simple API to connect to the audio server;
if aucat(1) isn't running, it uses the audio(4) driver transparently
instead.


# 1.27 25-Aug-2008 sobrado

minor tweak for removal of "[-d devel]" (change done in r1.19 and r1.21).

ok ratchov@


# 1.26 14-Aug-2008 ratchov

move all device related stuff from aucat.c to a new dev.c file.
The new dev_xxx() routines expose a "high level" self-contained
interface to the device. At initialization, the device is opened
and two chains of aproc structures are created:

* a playback chain that exposes a (initially) empty mix
aproc to which the rest of the code can attach new
streams to be played

* record chain that exposes a (initially) empty sub aproc
to which the rest of the code can attach new stream to
to record

The rest of the code, has just to use dev_attach() routine to
attach streams. While we're at it, add a ``devops'' structure
containing pointers to the device-specific routines. This will
allow later to add support for other type of device than the
Sun API.

Also, write the .wav headers in file_del(), so put all header
related data in the file strucuture. This allows to close() the
file, as soon as wpipe_xxx() aproc terminates. This will be
useful for the server, because it will need to close() descripts
of closed connections immediately.

add mix_pushzero() routine to fill the mixer with silence. It
will be used to avoid the mixer to underrun when there are no
input streams. Since we always have at least one input stream
there's no behaviour change.

ok jakemsr


Revision tags: OPENBSD_4_4_BASE
# 1.25 02-Jun-2008 ratchov

document latest changes: -d flag is replaced by AUCAT_DEBUG
environment variable, new -xX options

bits from eric, ok jakemsr


# 1.24 02-Jun-2008 ratchov

currently, we start the device as soon as write() blocks; it's better
to continue filling the play buffer until it's full. This way, the
play buffer has fewer chances to underrun when the device is started.

ok jakemsr


# 1.23 02-Jun-2008 ratchov

if aucat is suspended, then the kernel continues running the device
(produces silence) and later, once aucat is resumed the kernel starts
dropping samples, it will try to drop as many samples as silence was
produced. So suspending breaks the aucat process permanently.

workaround this by blocking the signals ie disabling suspending of aucat
from the tty. The long term solution would be to catch SIGCONT and to stop
the device, resync/refill buffers and restart the device. That's really a
lot of work...

ok jakemsr


# 1.22 02-Jun-2008 ratchov

allow the user to choose the aucat behaviour when underruns/overruns
occur on a per-stream basis, using -Xx flags. There are 3 possible
policies:

- ignore : ignores underruns/overruns, for instance, this mode
could be used for creating simple pipes with utilities; like in
your last cdio diff.

- sync : insert/discard samples in order to keep all streams in
sync, useful for multi-tracker-like apps and/or to sync
midi/video/whatever on audio streams (this was the previous
behaviour)

- error : if overruns/underruns occur, consider it as fatal error
and kill the corresponding stream (without disturbing others).
Useful, for reliable recordings (and/or debugging aucat
itself:).

ok jakemsr


# 1.21 02-Jun-2008 ratchov

(remove -d, second part) use strtonum() instead of sscanf() and inlined
checks

suggested and ok jakemsr


# 1.20 02-Jun-2008 ratchov

since underruns/overruns are handled outside devices, remove code that
check for xruns in devices.

ok jakemsr


# 1.19 02-Jun-2008 ratchov

(remove -d, first part) replace -d flag by AUCAT_DEBUG environment
variable. This eases turning debugging on/off when aucat is started by
another program.

"i like the idea" jakemsr


# 1.18 26-May-2008 jmc

put -q in the right place;


# 1.17 26-May-2008 jakemsr

add -q option which enables "quiet" operation.

ok ratchov@


# 1.16 23-May-2008 jmc

various fixes for aucat.1, and sync usage(); ok ratchov


# 1.15 23-May-2008 ratchov

add support for:
- recording, full-duplex operation
- format conversions and resampling on the fly
- mixing on the fly of multiple inputs of different formats
- up to 16 channels, simplistic "routing" of channel ranges
- more linear encodings (in raw and wav files)
the old behaviour is fully preserved if none of the new -i and -o
options are used.

code and fixes from jakemsr@ and eric@, suggestions by others.
ok "go ahead" deraadt@


# 1.14 13-Apr-2008 jakemsr

- remove code that was rendered pointless by the last commit
- minor knf

from Pierre Riteau


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE
# 1.13 20-Mar-2007 uwe

Support for playing simple RIFF files (.wav) from Simon Effenberg, savar
at schuldeigen de, with some tweaks from me. ok millert, tests ckuethe,
liked by some disliked by nobody, but he's not even a real user :)


Revision tags: OPENBSD_4_1_BASE
# 1.12 20-Dec-2006 steven

remove unneeded #include

ok mbalmer@ deraadt@


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.11 20-Jan-2005 jaredy

- knf
- check read/write/etc. calls for errors
- use constants instead of hardcoded pathnames
- better variable types
- add usage output

suggestions from various, ok ian


# 1.10 13-Jan-2005 ian

Close what you open: obvious fix, ok jared@, more to follow


Revision tags: OPENBSD_3_5_BASE OPENBSD_3_6_BASE
# 1.9 20-Oct-2003 jmc

typos from Jared Yanovich, slightly ammended;


Revision tags: OPENBSD_3_4_BASE
# 1.8 10-Jul-2003 david

add missing includes
ok deraadt@ tedu@


# 1.7 02-Jul-2003 deraadt

protos


# 1.6 10-Jun-2003 deraadt

mostly ansi cleanup; pval ok


Revision tags: OPENBSD_3_3_BASE
# 1.5 09-Dec-2002 millert

From Andrushock, s/sucess/success/g


Revision tags: OPENBSD_2_8_BASE OPENBSD_2_9_BASE OPENBSD_3_0_BASE OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.4 01-Jul-2000 millert

Add -f option to specify audio device, honor AUDIODEVICE environment vairable,
and some KNF.


Revision tags: OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE
# 1.3 05-Jan-1997 kstailey

minor tweaks; htonl() -> ntohl(); added SEE ALSO


# 1.2 03-Jan-1997 kstailey

removed unnecessary test


# 1.1 02-Jan-1997 kstailey

new utility for playing sound files


# 1.175 19-May-2019 ratchov

Fix integer overflow in block size calculation.

Fixes truncation of contents of files with rate above 64kHz
generate in off-line mode (i.e. -n option).


Revision tags: OPENBSD_6_5_BASE
# 1.174 21-Feb-2019 ratchov

Use reallocarray in place of malloc(a * b) to handle possible
overflow in multiplication of malloc argument.


Revision tags: OPENBSD_6_4_BASE
# 1.173 13-May-2018 ratchov

Add missing pledge(). From Jesper Wallin <jesper at ifconfig.se>.
Thanks!


Revision tags: OPENBSD_6_3_BASE
# 1.172 07-Nov-2017 ratchov

prefill with silence the buffer where cmap_copy() stores samples
rather than the file buffer. Fixes initialized data being treated as
recorded samples when resampling or format conversions are involved
but mono->stereo conversion is disabled.


# 1.171 03-Nov-2017 ratchov

Prefill rec buffer with silence. This is necessary because certain
channels don't get samples, for instance when mono->stereo conversion
is disabled.


Revision tags: OPENBSD_6_2_BASE
# 1.170 20-Jul-2017 ratchov

Fix integer overflow that would causes >12 hours files
to not properly relocate. Found with coverity.


Revision tags: OPENBSD_6_1_BASE
# 1.169 03-Jan-2017 ratchov

Assert we're not freeing buffers we didn't allocate (DEBUG mode).


# 1.168 03-Jan-2017 ratchov

Log play volume together with other play parameters.


# 1.167 30-Sep-2016 ratchov

Set the device rate to the calculated rate. Otherwise, the signal
may endup resampled twice.


# 1.166 30-Sep-2016 ratchov

Remove unnecessary includes. From Michael W. Bombardieri <mb at ii.net>.


# 1.165 27-Sep-2016 ratchov

Don't rely on the resampling code to calculate the number of samples
to process, as it may produce one extra sample (to handle accumulation
of fractional samples), which would cause access to one sample past
the end of the buffer and crash aucat. Fix this by limiting the number
of samples processed to a single block.

Found by and help from Michael W. Bombardieri <mb at ii.net>. Thanks.


Revision tags: OPENBSD_6_0_BASE
# 1.164 07-Jun-2016 ratchov

Add -g and -p options to control device and file start position.


# 1.163 07-Jun-2016 ratchov

Add resamp_getcnt() routine to calculate the exact number of samples
that would be consumed and produced by the sampler rate converter. Use
it to avoid partial samples that are not properly handled. Fixes
last samples of certain files causing aucat to abort.


# 1.162 02-Jun-2016 ratchov

remove forgotten debug printfs


# 1.161 02-Jun-2016 ratchov

set exit status to 1 if -n is used without -i and -o


# 1.160 31-May-2016 ratchov

Pass hr:min:sec:frame.cent to dev_mmcloc() directly and remove the
useless intermediate conversion from and to 2400-th of seconds.


# 1.159 31-May-2016 ratchov

To store MMC position, use samples (at device rate) as time units
instead of 2400-ths of second. No behaviour change.


# 1.158 28-May-2016 ratchov

Fix file block size rounding and ensure it's large enough to store a
full audio block.


# 1.157 27-May-2016 ratchov

When resampling, use the exact resampling factor instead of the ratio
between input and output block sizes. This was inherited from sndiod,
but is not required for files because they are continuous streams of
samples and do not need to be split in blocks of equal duration.

This change makes playback/recording rate match exactly the requested
sample rate.


# 1.156 27-May-2016 ratchov

Flush rec buffer if there's less than one block space left and refill
play buffer if there's less than one block of data left. This is the
correct condition in the general case. No behaviour change, as all
input/output is multiple of the block size.


# 1.155 27-May-2016 ratchov

Simplify slot_fill() and slot_flush(). No behaviour change


# 1.154 27-May-2016 ratchov

Make resamp_do() get the exact number input and output samples and
provide routines to calculate them. This way we don't rely on it
to calculate the bytes procuded/consumed anymore. No behaviour change.


# 1.153 26-May-2016 ratchov

Make format conversion routines return the number of frames
consumed from both input and output buffers. No behaviour change.


Revision tags: OPENBSD_5_9_BASE
# 1.152 29-Jan-2016 ratchov

Check that byte order is native only if we're using more than one
byte per sample, check that samples are lsb-aligned only if there's
padding. Fixes unsupported parameters not being detected if compiled
in 24-bit mode and sndiod is not running.


# 1.151 10-Jan-2016 ratchov

trailing spaces, fit in 80 columns


# 1.150 09-Nov-2015 mmcc

Remove xfree(). From Michael W. Bombardieri.

ok ratchov@


# 1.149 27-Aug-2015 ratchov

fix typos in comments and error messages


Revision tags: OPENBSD_5_8_BASE
# 1.148 16-May-2015 ratchov

Call slot_stop() after the play buffer is drained to properly cleanup
the slot. Fixes periodic glitches occurring after eof is reached, and
then playback restarted with a MMC-relocate and MMC-start.


Revision tags: OPENBSD_5_7_BASE
# 1.147 21-Jan-2015 jmc

fix macro breakage and update usage();


# 1.146 21-Jan-2015 ratchov

Simplify internals and rewrite file header parsing code:
- remove -M, -t, -w, -C, -x flags that don't make sense anymore
- make "-j off" the default (sndiod already does the job)
- don't limit the number of played/recorded files.
- add support for floating-point encoded files.
- add support for apple .aiff and sun/next .au files


# 1.145 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.144 18-Nov-2013 ratchov

remove unused variables


# 1.143 18-Nov-2013 ratchov

add missing prototypes


# 1.142 12-Nov-2013 ratchov

remove bogus legacy server-specific bits that cause confusion


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.141 03-Dec-2012 ratchov

Remove server capability description from the documentation and from
the usage string, and just say that for historic reasons aucat behaves
like sndiod.
with help from jmc@


# 1.140 23-Nov-2012 ratchov

Since there's no sndiod -> aucat link anymore, allow aucat to be run
in server mode even when not invoked as sndiod. This allows aucat and
the new sndiod daemon to live together in /usr/bin, which makes easy
to quickly compare sndiod and aucat (and spot possible regressions)
without having to modify /usr/bin.


# 1.139 27-Oct-2012 ratchov

don't stat & test sndio home directory since we never use it.


# 1.138 27-Oct-2012 ratchov

don't (re-)define SNDIO_USER and SNDIO_PRIO if they are already defined,
this way they can be defined at compilation time


Revision tags: OPENBSD_5_2_BASE
# 1.137 27-Jun-2012 ratchov

Uniformize error messages printing, use err() instead of fprintf()
when appropriate. From Michael W. Bombardieri <mb at ii.net>


# 1.136 19-Jun-2012 ratchov

Remove hidden options that are not needed any more,
from Remco <remco at d-compu.dyndns.org>. Thanks


# 1.135 23-May-2012 ratchov

Make the "default" string a valid device name that has the same effect
as NULL has. This will (hopefully) simplify ports where the user
passes the device string.


# 1.134 11-Apr-2012 ratchov

Use "unsigned int" rather than "unsigned". No object change.
suggested by deraadt@


Revision tags: OPENBSD_5_1_BASE
# 1.133 09-Feb-2012 ratchov

Crank the default buffer size to 160ms (ie 7680 frames at 48kHz).
Workarounds stuttering caused by something hogging the cpu in kernel
mode and preventing sndiod from running at time.
ok deraadt


# 1.132 26-Jan-2012 ratchov

Make the default rate 48kHz and the default block size 10ms. These
settings ensure video players and programs using MTC are smooth by
default. Thanks to all who tested.


# 1.131 10-Jan-2012 ratchov

When a sub-device is created (-s), check if a hardware
device was already created (-f) before trying to use the
default one, otherwise all sub-devices end up attached
to the default device. Noticed by peters at schwertfisch.de,
thanks!


# 1.130 09-Dec-2011 ratchov

fix usage string: remove -M from aucat and -l from aucat and sndiod
from jmc


# 1.129 09-Dec-2011 ratchov

add a new sndiod audio/midi daemon and hide aucat server-specific
options.


# 1.128 02-Dec-2011 ratchov

don't use a "server" flag, just check if we expose sub-devices


# 1.127 02-Dec-2011 ratchov

reuse midi-control code to implement midi thru boxes and remove
the old midithru implementation; less code, less bugs. As a side
effect, midi output doesn't implement running status "compression"
any more.


# 1.126 20-Nov-2011 ratchov

Move mmc/mtc and volume control bits from struct aproc to struct
dev. Allows volume settings to be saved while the device is kept
closed. Besides that, no behabiour changes.


# 1.125 15-Nov-2011 ratchov

Add a "device number" component in sndio(7) device names, allowing a
single aucat instance to handle all audio and MIDI services. Since
this partially breaks compatibility, this is a opportunitiy to fix few
other design mistakes (eg ':' being used by inet6, type name vs api
name confusion, etc..). This leads to the following names:

type[@hostname][,unit]/devnum[.option]

The device number is the minor device number for direct hardware
access (ie the 'N' in /dev/audioN). For aucat, this is the occurence
number of the -f (or -M) option.

There's a compatibility hook to keep old names working if only one
aucat server is running.


# 1.124 18-Oct-2011 ratchov

keep the device closed by default (when not used)


# 1.123 18-Oct-2011 ratchov

remove duplicate options in getopt() string


# 1.122 17-Oct-2011 ratchov

Remove midicat since aucat can now be used instead of midicat
with almost the same syntax (roughly an extra -M option).
Thru boxes are created with aucat, and corresponding MIDI port
names have the "aucat" prefix instead of "midithru". The old
device name will still work some time for backward compatibility.

ok deraadt


# 1.121 12-Oct-2011 jmc

- avoid line wrapping an example
- sort usage() for midicat
- tweak a bit of unclear wording

ok ratchov


# 1.120 12-Oct-2011 ratchov

Simplify and improve the way options are parsed and remove ~300 lines
of code that becomes unused. Few command line arguments changes are
required though:

- stream definitions (-ios) now must follow devices definitions they
are attached to (-fMn)

- the -n option is now a special "loopback" device and is thus used
like -f, eg it must precede streams

- in midicat, midi thru boxes are not created automatically anymore,
the new "-M" option must be used for that

- channel numbers (-Cc options) correspond always to channel numbers
of the hardware.

- the -u option isn't needed anymore

- increase the log verbosity so user errors are logged
without using -d

tested by many, help from jmc


Revision tags: OPENBSD_5_0_BASE
# 1.119 29-Jun-2011 ratchov

make -u flag per-device similarly to -a and -w


# 1.118 20-Jun-2011 ratchov

Make -aoff option apply to MIDI ports (-q) as well, ensuring the device
stays closed also if -q is used. As we're at it, add -a to midicat so
it behaves like aucat.


# 1.117 03-Jun-2011 ratchov

Allow device encoding to be set if -u is also used. Useful to degrade
quality to save bandwith when a remote audio device is used.


# 1.116 03-Jun-2011 ratchov

Reorganize the way command line options are parsed and stored.
No behaviour change.


# 1.115 26-May-2011 ratchov

add a new -w flag to control whether master volume is automatically
adjusted when new streams are connected and disconnected. Disabling
automatic volume adjustment makes sense if all streams are recorded
with properly lowered volumes.


# 1.114 10-May-2011 ratchov

fix option handling and enable TCP in midicat
from peters at schwertfisch.de, thanks!


# 1.113 28-Apr-2011 ratchov

Implement a new authentication method allowing aucat and midicat to
work over TCP, for instance, to expose the sound card of one machine
with other machines of the network.

The first client generates a 128-bit random number (aka the
session cookie), saves it in $HOME/.aucat_cookie and sends it to the
server. Successive clients load the cookie from $HOME/.aucat_cookie
and send it to the server but the server accepts only clients whose
cookie matches the session cookie. When all clients are gone, the
session is over, and another cookie could start a new session, and so
on.

TCP is enabled on the server with the new -L option, and on the client
side hostnames are specified with a new optional component in the
device name.

hints from damien, dlg and deraadt, tweaks from jmc


# 1.112 27-Apr-2011 ratchov

move amsg.h containing protocol defs from aucat side to libsndio side.

requested by deraadt


# 1.111 27-Apr-2011 deraadt

use more volatile sig_atomic_t in signal handlers
ok ratchov


# 1.110 19-Apr-2011 ratchov

don't hold a pointer to "listen" structure. Instead, when it must
be free()ed, iterate over the file_list, and find the structure
to free. This is safer and simpler. No behaviour change


# 1.109 16-Apr-2011 ratchov

for unix domain socket addresses use AUCAT_PATH and MIDICAT_PATH
macros instead of hardcoded strings. No object change


# 1.108 17-Mar-2011 ratchov

use a DEFAULT_RATE macro instead of hardcoding 44100, no object change


Revision tags: OPENBSD_4_9_BASE
# 1.107 05-Nov-2010 ratchov

unbreak compilation without DEBUG defined


# 1.106 21-Oct-2010 ratchov

use MODE_xxx and XRUN_xxx macros everywhere, and zap corresponding
AMSG_xxx macros, which in turns simplifies the code


# 1.105 08-Sep-2010 deraadt

aucat: getpwnam: No such file or directory
is not a proper error message.


# 1.104 20-Aug-2010 ratchov

polish the code: remove few unused #includes, add missing ones,
fix NULL vs 0, etc. No behaviour change.


# 1.103 19-Aug-2010 ratchov

fix NULL pointer deref in midicat option parsing when ``-'' is
used as filename


Revision tags: OPENBSD_4_8_BASE
# 1.102 31-Jul-2010 ratchov

remove 2 years old compatibility mode


# 1.101 10-Jul-2010 ratchov

In midi server mode, don't terminate the midi thru box if there are
no connections anymore.


# 1.100 10-Jul-2010 ratchov

fix parameter handling: don't try to open a ``default'' midi port
if no files are given on the command line


# 1.99 06-Jul-2010 jmc

various tweaks to previous; ok ratchov


# 1.98 06-Jul-2010 ratchov

Handle all streams the same way because there's no actual
difference between audio files and client connections. Clean up
the way command line options are handled and clarify this in the
manual page: stream parameters (-Ccehjmrtvx) must precede stream
definitions (-ios) and per-device parameters (-abz) and stream
definitions (-ios) must precede device definitions (-f). Since
there's no ``server'' and ``non-server'' modes anymore, make the
-l option just detach the process.

ok and help from jakemsr and jmc


# 1.97 29-Jun-2010 jakemsr

If the device isn't opened yet (as can be the case with '-a off'),
don't check if the midi control interface is idle
from ratchov


# 1.96 25-Jun-2010 ratchov

don't attempt to drain devices after they are destroyed, which
results in a use after free(). Catched by jakemsr@ with MALLOC_OPTIONS=J


# 1.95 20-Jun-2010 ratchov

fix midicat(1) device mode when only -i or only -o is used (ie
open the device input-only if -o is used, and output-only if -i is
used).


# 1.94 05-Jun-2010 ratchov

don't prime server buffers, because it's ugly and conceptually
complicated. Instead, request clients to provide enough samples
and start with buffers full.


# 1.93 05-Jun-2010 ratchov

Don't send the initial position as a clock tick with a negative delta.
Add a new ``initial position'' message which is simpler. No bahaviour
change, except effects of the protocol version crank.


# 1.92 04-Jun-2010 ratchov

Allow the audio device to be opened only while it's actually used.
This is necessary for uaudio devices, for instance to start aucat
before the device is plugged. Or to unplug a device whithout
having to restart aucat when another device is plugged. This is
controlled with the new -a option.

Allow multiple audio devices to be used concurently, i.e.
multiple ``-f devname'' options to be used; -f options must follow
per-device options, which is what we do for other options.


# 1.91 08-May-2010 ratchov

prime play buffers as soon as the device is opened, otherwise a
midi client could start it with empty buffers, in turn hurting
audio clients.


# 1.90 02-May-2010 ratchov

Clean up device handling code to clarify different initialization phases
and different device states. Split initialization in two phases:
first global variables are initialized then the audio hardware is opened.
Allow devices that don't support full-duplex to work in play-only or
record-only mode, even if ``-m play'' or ``-m rec'' are not specified.


# 1.89 02-May-2010 ratchov

Don't systematically fill with silence the mixer output. This
might result in extra samples being written to the device
when the mixer is closed.


# 1.88 24-Apr-2010 ratchov

if there are no files to play in legacy mode, print the usage message


# 1.87 24-Apr-2010 ratchov

rename ibuflist/obuflist -> ins/outs to cut long lines
from Thomas Pfaff, thanks


# 1.86 22-Apr-2010 ratchov

Allow multiple users to share the same aucat server. If aucat is
run by root, it binds a shared address to the socket, cranks the
process priority and drops privileges. sio_open(3) will try to
connect to the private socket first (if any), then to the shared
socket. Only one user may have connections to aucat at a given
time.

based on discussions with henning, pyr and others
ok jacek, deraadt


# 1.85 21-Apr-2010 ratchov

when creating the ``default'' subdevice, use last parameters of the
command line, instead of the device parameters.


# 1.84 21-Apr-2010 ratchov

adds the necessary bits to join channels (ex stereo->mono) or
expand channels (ex mono->stereo). It's switched on/off with
the ``-j'' option, (default is "on").


# 1.83 06-Apr-2010 ratchov

aucat (server):

- make the ``-m mode'' option per subdevice, allowing
subdevices to be play-only or rec-only even if the
server is full-duplex

- add ``monitoring'' mode (with ``-m mon''). This is a
record-only stream from which played streams can be
recorded (kind of ``record what you hear'').

- allow MIDI devices to be subscribed to the controlling
MIDI port of the server, ie what midicat does (with -f
option), but using the -q option.

- add flow control to the protocol, and serialize clock
ticks (sio_onmove() calls) and data chunks. This should
fix certain full-duplex programs, broken with ``magic''
block/buffer size combinations.

- enable 3 block latency which is the minimum theoretical.
Make -z and -b options correspond to device parameters.

- make sio_getcap(3) the same for aucat and sun backends,
ie return whatever is supported (``everything'' in the
aucat case, since everything is actulally supported).

aucat (player):

- enable ``-m mode'' option to select between monitoring
and recording when ``-o file'' is used.

- plug MIDI code to non-server codepath. The MIDI control
device is specified with the ``-q device'' option, as in
server mode.

- impliment lseek()'ing within files (controlled
through MIDI). Necessary to use aucat with a MIDI
sequencer.

midicat (thrubox):

- rename ``-f'' flag to ``-q'', so it has the
same name as in aucat (-f is still working)

ok jakemsr@, tweaks from jmc@


# 1.82 03-Apr-2010 ratchov

backout last change, committed by mistake, sorry...


# 1.81 03-Apr-2010 ratchov

doc fixes from jmc


Revision tags: OPENBSD_4_7_BASE
# 1.80 14-Jan-2010 ratchov

Use -C option to specify recorded channel range instead of -c which
is for played channels.

spotted by Jan Stary <hans at stare.cz>, thanks


# 1.79 11-Jan-2010 ratchov

When resuming from suspend, prime play buffers with silence,
otherwise the client would underrun by more than its own buffer
size, which currently is not allowed (causes the client to
deadlock).

Set the minimum client buffer size to two blocks. Single block
buffers don't work properly yet.

with help from jakemsr


# 1.78 10-Jan-2010 ratchov

Add traces to help tracking subtle timing bugs often causing
underuns and overruns; such bugs are hard to debug with ktrace
or a debugger. They are also handy to debug or monitor code
using aucat.

To enable traces, compile aucat with ``make DEBUG=-DDEBUG'' and
use the -d option multiple times.

ok jakemsr


# 1.77 05-Jan-2010 ratchov

Make the way .wav files are handled closer to the way sockets are
handled, which is conceptually simpler. No behaviour changes.


# 1.76 21-Nov-2009 ratchov

use strtonum(3) and strtol(3) instead of sscanf(3)
suggested by deraadt@


# 1.75 21-Nov-2009 deraadt

spelling


# 1.74 03-Nov-2009 ratchov

Allow any program using aucat to act as MMC slave and MTC master
transparently. Multiple audio applications can be started
synchronously from external software/hardware supporting the
standard Start/Stop/Relocate messages. The server clock is exposed
through MTC, allowing non-audio software/hardware to be
synchronized to audio applications.


# 1.73 27-Oct-2009 ratchov

add missing word in error message


# 1.72 10-Oct-2009 ratchov

don't use a references to the device file to check the current mode


# 1.71 10-Oct-2009 ratchov

make dev_done() common to audio and MIDI, and drop dev_thrudone()


# 1.70 10-Oct-2009 ratchov

use dev_done() for loopback devices too and remove dev_loopdone()


# 1.69 05-Oct-2009 ratchov

add -d flag. When started in server mode, if the -d flag is
used aucat and midicat don't daemonize and log on stderr.


# 1.68 27-Sep-2009 ratchov

remove all debug traces


# 1.67 26-Aug-2009 jmc

oops, forgot to update usage();


# 1.66 19-Aug-2009 ratchov

don't try to process -o arguments using the input file list, fixes
midicat crashing sometimes when -o is used


# 1.65 19-Aug-2009 ratchov

organize midi code like audio code, no functional change


# 1.64 17-Aug-2009 ratchov

allow midicat to take multiple -i and -o options


# 1.63 17-Aug-2009 ratchov

if one or more MIDI devices are specified in server mode, subscribe
them to the MIDI thru box


# 1.62 25-Jul-2009 ratchov

fix comments, remove no longer needed declaration, reorder includes
from Thomas Pfaff


# 1.61 25-Jul-2009 ratchov

Currently midi capable programs can control midi hardware, but
cannot cooperate with other programs. The aim of this change is
to allow any program to send midi data to other programs as they
were midi hardware. For instance, this change should solve the
longstanding problem of using a midi sequencer with software
synthesizers. More precisely:

- new midicat(1) utility (actually hardlink to aucat(1)).
it creates software midi thru boxes, allowing programs
to send midi messages to other programs as they were
midi(4) hardware.

- new midi api in libsndio (see mio_open(3)), to access
midi(4) devices and midicat(1) sockets in a uniform way.

- new device naming scheme <service>:<unit>[.<option>],
common to audio and midi.

- new sndio(7) manual describing concepts and naming

The current audio device naming still works, but people having
scripts or configuration files containing device names could read
the sndio(7) man page and slowly start updating device names.

discussed with jakemsr@ and deraadt@, help form jmc@


Revision tags: OPENBSD_4_6_BASE
# 1.60 27-Apr-2009 ratchov

make -v option apply also to implicit default socket


# 1.59 17-Mar-2009 jmc

- rename the arg to -b "nframes" and sync usage()
- grammar fix for the server section

from Thomas Pfaff
ok ratchov


Revision tags: OPENBSD_4_5_BASE
# 1.58 06-Feb-2009 ratchov

move error messages reported to user into main()


# 1.57 04-Feb-2009 ratchov

if there are too many connections, stop acceping new ones rather
than exit()ing with ``too many open files'' fatal error


# 1.56 04-Feb-2009 ratchov

daemonize when in server mode, suggested by many
ok jakemsr


# 1.55 03-Feb-2009 ratchov

in server mode, create /tmp/aucat-userid/ directory with
permissions 0700, and create sockets in it. This prevents
one local user to eavesdrop or disturb audio programs of
other users.

if you're using the ``-s socket'' option with an absolute
path as argument, please update it to use a socket name.

requested by many, bits from jakemsr and otto
ok jakemsr


# 1.54 25-Jan-2009 ratchov

don't set malloc_options to not interfer with /etc/malloc.conf
requested and explained by fgsch@, henning@, otto@


# 1.53 23-Jan-2009 ratchov

catch SIGHUP and SIGTERM too, to ensure that recorded files are
properly closed and sockets are cleaned up.


# 1.52 23-Jan-2009 ratchov

spacing + typo fix from Matt Jibson <matt.jibson(at)gmail.com>


# 1.51 10-Jan-2009 ratchov

add "loopback" mode in which input is connected to the output.
This is useful to mix, demultiplex, resample or reencode audio
files off-line.
tweak + ok jakemsr


# 1.50 29-Dec-2008 ratchov

make the code "more correct": add reference counters to aproc
structures so we can keep a pointer to the device. Beside some
simplifications, this allows to easily handle the situation
where the audio device desappears. No change in the behaviour.


# 1.49 26-Dec-2008 ratchov

*** empty log message ***


# 1.48 07-Dec-2008 ratchov

When resampling, don't require the sample frequency to be an integer.
This removes the arithmetic constraint between the sample frequency
and the block size and all the associated code. Now aucat can work
in server mode with any block size.
ok jakemsr


# 1.47 23-Nov-2008 ratchov

Use file input parameter as default device output and file output parameters
as default device input. That's what the man page says


# 1.46 20-Nov-2008 ratchov

take into account socket parameters into calculation of device
parameters; without this change aucat uses the device default
parameters and they are not necessarily usable for multi-stream
mode.


# 1.45 17-Nov-2008 jmc

- rename -s arg name to "socket"
- condense EXAMPLES into a single screenful

ok ratchov


# 1.44 17-Nov-2008 jmc

tweak previous man page changes, and sync usage();


# 1.43 17-Nov-2008 ratchov

allow aucat to run as server in play-only and record-only mode, so
it can be used on play-only, record-only and half-duplex devices.
ok jakemsr


# 1.42 16-Nov-2008 ratchov

allow aucat to listen on multiple sockets. Each socket carries its
channel and volume settings allowing multiple configuration to
coexist. Mostly useful for envy(4)-like devices, but can be used
to force different apps to use different settings.


# 1.41 16-Nov-2008 ratchov

add ``template'' parameters to sockets rather than using device
parameters. No functional change.


# 1.40 16-Nov-2008 ratchov

Make clients inherit the volume parameter when the -v option is used in
server mode. It gives the maximum volume a client may have. This wastes
dynamic range, but allows volume to stay constant when other clients
connect or disconnect.


# 1.39 12-Nov-2008 ratchov

when destroying the device, mark the mixer and the demultiplexer
for ``AUTOQUIT'' first, and then call file_eof() and friends.
fixes crashes sthen@ noticed on zaurus


# 1.38 11-Nov-2008 ratchov

if the device disappear the mixer and demultiplexer chains are
destroyed. If this happen then warn and exit, rather than crashing.


# 1.37 11-Nov-2008 ratchov

termninate non-server aucat when it's idle rather that using
the hackish {MIX,SUB}_AUTOQUIT features. This way we don't
have to care about possible references on destroyed objects.


# 1.36 11-Nov-2008 ratchov

when not in server mode, aucat terminates automatically using
the {MIX,SUB}_AUTAQUIT features. In this case we're not
allowed to directly touch dev_mix and dev_sub because they
can disappar. So disable the ``suspend'' and ``quit'' bits
when {MIX,SUB}_AUTAQUIT are used.


# 1.35 10-Nov-2008 ratchov

add a per-stream ``soft volume'' knob and the corresponding -v option.
The code will be useful later for the volume knob in the sndio API.


# 1.34 09-Nov-2008 ratchov

make aucat(1) stop automatically the audio(1) device if it's idle. This
way, when in server mode, it consumes no CPU if there are no clients.
Later, this will allow to start aucat(1) at session or system startup.


# 1.33 08-Nov-2008 ratchov

set the default device to /dev/audio for legacy mode.
From Thomas Pfaff <tpfaff _at_ agderlink.no> and tweaks from me


# 1.32 07-Nov-2008 ratchov

expose the block size in the sndio API by making par->round writable
and thus remove the ugly rate <-> block-size table from
sio_setpar(3). Handle the block size negociation in aucat(1), since
it has few constrains the code is overally simpler.
ok jakemsr@, major crank suggested by deraadt@


# 1.31 03-Nov-2008 ratchov

A small optimization: handle most N-channel <-> M-channel conversions
inside the mixer and the demultiplexer. This way, aucat will not
trigger the heavy conversion code when only channel conversions are
required. Cuts ~50% of the CPU usage on envy(4) devices, can improve
surround 4.0, 5.1 and 7.1 capable devices. No functionnal change.


# 1.30 26-Oct-2008 ratchov

zap `q' from the getopts string too,
requested by jmc@


# 1.29 26-Oct-2008 jmc

sync SYNOPSIS and usage();


# 1.28 26-Oct-2008 ratchov

add minimal server capability to aucat(1). When started in server
mode, it listens on an unix socket and mixes/demultiplexes any number
of full-duplex streams, doing necessary format conversions and
resampling on the fly.

programs can use the new libsa(3) library to play and record audio.
The library provides a very simple API to connect to the audio server;
if aucat(1) isn't running, it uses the audio(4) driver transparently
instead.


# 1.27 25-Aug-2008 sobrado

minor tweak for removal of "[-d devel]" (change done in r1.19 and r1.21).

ok ratchov@


# 1.26 14-Aug-2008 ratchov

move all device related stuff from aucat.c to a new dev.c file.
The new dev_xxx() routines expose a "high level" self-contained
interface to the device. At initialization, the device is opened
and two chains of aproc structures are created:

* a playback chain that exposes a (initially) empty mix
aproc to which the rest of the code can attach new
streams to be played

* record chain that exposes a (initially) empty sub aproc
to which the rest of the code can attach new stream to
to record

The rest of the code, has just to use dev_attach() routine to
attach streams. While we're at it, add a ``devops'' structure
containing pointers to the device-specific routines. This will
allow later to add support for other type of device than the
Sun API.

Also, write the .wav headers in file_del(), so put all header
related data in the file strucuture. This allows to close() the
file, as soon as wpipe_xxx() aproc terminates. This will be
useful for the server, because it will need to close() descripts
of closed connections immediately.

add mix_pushzero() routine to fill the mixer with silence. It
will be used to avoid the mixer to underrun when there are no
input streams. Since we always have at least one input stream
there's no behaviour change.

ok jakemsr


Revision tags: OPENBSD_4_4_BASE
# 1.25 02-Jun-2008 ratchov

document latest changes: -d flag is replaced by AUCAT_DEBUG
environment variable, new -xX options

bits from eric, ok jakemsr


# 1.24 02-Jun-2008 ratchov

currently, we start the device as soon as write() blocks; it's better
to continue filling the play buffer until it's full. This way, the
play buffer has fewer chances to underrun when the device is started.

ok jakemsr


# 1.23 02-Jun-2008 ratchov

if aucat is suspended, then the kernel continues running the device
(produces silence) and later, once aucat is resumed the kernel starts
dropping samples, it will try to drop as many samples as silence was
produced. So suspending breaks the aucat process permanently.

workaround this by blocking the signals ie disabling suspending of aucat
from the tty. The long term solution would be to catch SIGCONT and to stop
the device, resync/refill buffers and restart the device. That's really a
lot of work...

ok jakemsr


# 1.22 02-Jun-2008 ratchov

allow the user to choose the aucat behaviour when underruns/overruns
occur on a per-stream basis, using -Xx flags. There are 3 possible
policies:

- ignore : ignores underruns/overruns, for instance, this mode
could be used for creating simple pipes with utilities; like in
your last cdio diff.

- sync : insert/discard samples in order to keep all streams in
sync, useful for multi-tracker-like apps and/or to sync
midi/video/whatever on audio streams (this was the previous
behaviour)

- error : if overruns/underruns occur, consider it as fatal error
and kill the corresponding stream (without disturbing others).
Useful, for reliable recordings (and/or debugging aucat
itself:).

ok jakemsr


# 1.21 02-Jun-2008 ratchov

(remove -d, second part) use strtonum() instead of sscanf() and inlined
checks

suggested and ok jakemsr


# 1.20 02-Jun-2008 ratchov

since underruns/overruns are handled outside devices, remove code that
check for xruns in devices.

ok jakemsr


# 1.19 02-Jun-2008 ratchov

(remove -d, first part) replace -d flag by AUCAT_DEBUG environment
variable. This eases turning debugging on/off when aucat is started by
another program.

"i like the idea" jakemsr


# 1.18 26-May-2008 jmc

put -q in the right place;


# 1.17 26-May-2008 jakemsr

add -q option which enables "quiet" operation.

ok ratchov@


# 1.16 23-May-2008 jmc

various fixes for aucat.1, and sync usage(); ok ratchov


# 1.15 23-May-2008 ratchov

add support for:
- recording, full-duplex operation
- format conversions and resampling on the fly
- mixing on the fly of multiple inputs of different formats
- up to 16 channels, simplistic "routing" of channel ranges
- more linear encodings (in raw and wav files)
the old behaviour is fully preserved if none of the new -i and -o
options are used.

code and fixes from jakemsr@ and eric@, suggestions by others.
ok "go ahead" deraadt@


# 1.14 13-Apr-2008 jakemsr

- remove code that was rendered pointless by the last commit
- minor knf

from Pierre Riteau


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE
# 1.13 20-Mar-2007 uwe

Support for playing simple RIFF files (.wav) from Simon Effenberg, savar
at schuldeigen de, with some tweaks from me. ok millert, tests ckuethe,
liked by some disliked by nobody, but he's not even a real user :)


Revision tags: OPENBSD_4_1_BASE
# 1.12 20-Dec-2006 steven

remove unneeded #include

ok mbalmer@ deraadt@


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.11 20-Jan-2005 jaredy

- knf
- check read/write/etc. calls for errors
- use constants instead of hardcoded pathnames
- better variable types
- add usage output

suggestions from various, ok ian


# 1.10 13-Jan-2005 ian

Close what you open: obvious fix, ok jared@, more to follow


Revision tags: OPENBSD_3_5_BASE OPENBSD_3_6_BASE
# 1.9 20-Oct-2003 jmc

typos from Jared Yanovich, slightly ammended;


Revision tags: OPENBSD_3_4_BASE
# 1.8 10-Jul-2003 david

add missing includes
ok deraadt@ tedu@


# 1.7 02-Jul-2003 deraadt

protos


# 1.6 10-Jun-2003 deraadt

mostly ansi cleanup; pval ok


Revision tags: OPENBSD_3_3_BASE
# 1.5 09-Dec-2002 millert

From Andrushock, s/sucess/success/g


Revision tags: OPENBSD_2_8_BASE OPENBSD_2_9_BASE OPENBSD_3_0_BASE OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.4 01-Jul-2000 millert

Add -f option to specify audio device, honor AUDIODEVICE environment vairable,
and some KNF.


Revision tags: OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE
# 1.3 05-Jan-1997 kstailey

minor tweaks; htonl() -> ntohl(); added SEE ALSO


# 1.2 03-Jan-1997 kstailey

removed unnecessary test


# 1.1 02-Jan-1997 kstailey

new utility for playing sound files


# 1.174 21-Feb-2019 ratchov

Use reallocarray in place of malloc(a * b) to handle possible
overflow in multiplication of malloc argument.


Revision tags: OPENBSD_6_4_BASE
# 1.173 13-May-2018 ratchov

Add missing pledge(). From Jesper Wallin <jesper at ifconfig.se>.
Thanks!


Revision tags: OPENBSD_6_3_BASE
# 1.172 07-Nov-2017 ratchov

prefill with silence the buffer where cmap_copy() stores samples
rather than the file buffer. Fixes initialized data being treated as
recorded samples when resampling or format conversions are involved
but mono->stereo conversion is disabled.


# 1.171 03-Nov-2017 ratchov

Prefill rec buffer with silence. This is necessary because certain
channels don't get samples, for instance when mono->stereo conversion
is disabled.


Revision tags: OPENBSD_6_2_BASE
# 1.170 20-Jul-2017 ratchov

Fix integer overflow that would causes >12 hours files
to not properly relocate. Found with coverity.


Revision tags: OPENBSD_6_1_BASE
# 1.169 03-Jan-2017 ratchov

Assert we're not freeing buffers we didn't allocate (DEBUG mode).


# 1.168 03-Jan-2017 ratchov

Log play volume together with other play parameters.


# 1.167 30-Sep-2016 ratchov

Set the device rate to the calculated rate. Otherwise, the signal
may endup resampled twice.


# 1.166 30-Sep-2016 ratchov

Remove unnecessary includes. From Michael W. Bombardieri <mb at ii.net>.


# 1.165 27-Sep-2016 ratchov

Don't rely on the resampling code to calculate the number of samples
to process, as it may produce one extra sample (to handle accumulation
of fractional samples), which would cause access to one sample past
the end of the buffer and crash aucat. Fix this by limiting the number
of samples processed to a single block.

Found by and help from Michael W. Bombardieri <mb at ii.net>. Thanks.


Revision tags: OPENBSD_6_0_BASE
# 1.164 07-Jun-2016 ratchov

Add -g and -p options to control device and file start position.


# 1.163 07-Jun-2016 ratchov

Add resamp_getcnt() routine to calculate the exact number of samples
that would be consumed and produced by the sampler rate converter. Use
it to avoid partial samples that are not properly handled. Fixes
last samples of certain files causing aucat to abort.


# 1.162 02-Jun-2016 ratchov

remove forgotten debug printfs


# 1.161 02-Jun-2016 ratchov

set exit status to 1 if -n is used without -i and -o


# 1.160 31-May-2016 ratchov

Pass hr:min:sec:frame.cent to dev_mmcloc() directly and remove the
useless intermediate conversion from and to 2400-th of seconds.


# 1.159 31-May-2016 ratchov

To store MMC position, use samples (at device rate) as time units
instead of 2400-ths of second. No behaviour change.


# 1.158 28-May-2016 ratchov

Fix file block size rounding and ensure it's large enough to store a
full audio block.


# 1.157 27-May-2016 ratchov

When resampling, use the exact resampling factor instead of the ratio
between input and output block sizes. This was inherited from sndiod,
but is not required for files because they are continuous streams of
samples and do not need to be split in blocks of equal duration.

This change makes playback/recording rate match exactly the requested
sample rate.


# 1.156 27-May-2016 ratchov

Flush rec buffer if there's less than one block space left and refill
play buffer if there's less than one block of data left. This is the
correct condition in the general case. No behaviour change, as all
input/output is multiple of the block size.


# 1.155 27-May-2016 ratchov

Simplify slot_fill() and slot_flush(). No behaviour change


# 1.154 27-May-2016 ratchov

Make resamp_do() get the exact number input and output samples and
provide routines to calculate them. This way we don't rely on it
to calculate the bytes procuded/consumed anymore. No behaviour change.


# 1.153 26-May-2016 ratchov

Make format conversion routines return the number of frames
consumed from both input and output buffers. No behaviour change.


Revision tags: OPENBSD_5_9_BASE
# 1.152 29-Jan-2016 ratchov

Check that byte order is native only if we're using more than one
byte per sample, check that samples are lsb-aligned only if there's
padding. Fixes unsupported parameters not being detected if compiled
in 24-bit mode and sndiod is not running.


# 1.151 10-Jan-2016 ratchov

trailing spaces, fit in 80 columns


# 1.150 09-Nov-2015 mmcc

Remove xfree(). From Michael W. Bombardieri.

ok ratchov@


# 1.149 27-Aug-2015 ratchov

fix typos in comments and error messages


Revision tags: OPENBSD_5_8_BASE
# 1.148 16-May-2015 ratchov

Call slot_stop() after the play buffer is drained to properly cleanup
the slot. Fixes periodic glitches occurring after eof is reached, and
then playback restarted with a MMC-relocate and MMC-start.


Revision tags: OPENBSD_5_7_BASE
# 1.147 21-Jan-2015 jmc

fix macro breakage and update usage();


# 1.146 21-Jan-2015 ratchov

Simplify internals and rewrite file header parsing code:
- remove -M, -t, -w, -C, -x flags that don't make sense anymore
- make "-j off" the default (sndiod already does the job)
- don't limit the number of played/recorded files.
- add support for floating-point encoded files.
- add support for apple .aiff and sun/next .au files


# 1.145 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.144 18-Nov-2013 ratchov

remove unused variables


# 1.143 18-Nov-2013 ratchov

add missing prototypes


# 1.142 12-Nov-2013 ratchov

remove bogus legacy server-specific bits that cause confusion


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.141 03-Dec-2012 ratchov

Remove server capability description from the documentation and from
the usage string, and just say that for historic reasons aucat behaves
like sndiod.
with help from jmc@


# 1.140 23-Nov-2012 ratchov

Since there's no sndiod -> aucat link anymore, allow aucat to be run
in server mode even when not invoked as sndiod. This allows aucat and
the new sndiod daemon to live together in /usr/bin, which makes easy
to quickly compare sndiod and aucat (and spot possible regressions)
without having to modify /usr/bin.


# 1.139 27-Oct-2012 ratchov

don't stat & test sndio home directory since we never use it.


# 1.138 27-Oct-2012 ratchov

don't (re-)define SNDIO_USER and SNDIO_PRIO if they are already defined,
this way they can be defined at compilation time


Revision tags: OPENBSD_5_2_BASE
# 1.137 27-Jun-2012 ratchov

Uniformize error messages printing, use err() instead of fprintf()
when appropriate. From Michael W. Bombardieri <mb at ii.net>


# 1.136 19-Jun-2012 ratchov

Remove hidden options that are not needed any more,
from Remco <remco at d-compu.dyndns.org>. Thanks


# 1.135 23-May-2012 ratchov

Make the "default" string a valid device name that has the same effect
as NULL has. This will (hopefully) simplify ports where the user
passes the device string.


# 1.134 11-Apr-2012 ratchov

Use "unsigned int" rather than "unsigned". No object change.
suggested by deraadt@


Revision tags: OPENBSD_5_1_BASE
# 1.133 09-Feb-2012 ratchov

Crank the default buffer size to 160ms (ie 7680 frames at 48kHz).
Workarounds stuttering caused by something hogging the cpu in kernel
mode and preventing sndiod from running at time.
ok deraadt


# 1.132 26-Jan-2012 ratchov

Make the default rate 48kHz and the default block size 10ms. These
settings ensure video players and programs using MTC are smooth by
default. Thanks to all who tested.


# 1.131 10-Jan-2012 ratchov

When a sub-device is created (-s), check if a hardware
device was already created (-f) before trying to use the
default one, otherwise all sub-devices end up attached
to the default device. Noticed by peters at schwertfisch.de,
thanks!


# 1.130 09-Dec-2011 ratchov

fix usage string: remove -M from aucat and -l from aucat and sndiod
from jmc


# 1.129 09-Dec-2011 ratchov

add a new sndiod audio/midi daemon and hide aucat server-specific
options.


# 1.128 02-Dec-2011 ratchov

don't use a "server" flag, just check if we expose sub-devices


# 1.127 02-Dec-2011 ratchov

reuse midi-control code to implement midi thru boxes and remove
the old midithru implementation; less code, less bugs. As a side
effect, midi output doesn't implement running status "compression"
any more.


# 1.126 20-Nov-2011 ratchov

Move mmc/mtc and volume control bits from struct aproc to struct
dev. Allows volume settings to be saved while the device is kept
closed. Besides that, no behabiour changes.


# 1.125 15-Nov-2011 ratchov

Add a "device number" component in sndio(7) device names, allowing a
single aucat instance to handle all audio and MIDI services. Since
this partially breaks compatibility, this is a opportunitiy to fix few
other design mistakes (eg ':' being used by inet6, type name vs api
name confusion, etc..). This leads to the following names:

type[@hostname][,unit]/devnum[.option]

The device number is the minor device number for direct hardware
access (ie the 'N' in /dev/audioN). For aucat, this is the occurence
number of the -f (or -M) option.

There's a compatibility hook to keep old names working if only one
aucat server is running.


# 1.124 18-Oct-2011 ratchov

keep the device closed by default (when not used)


# 1.123 18-Oct-2011 ratchov

remove duplicate options in getopt() string


# 1.122 17-Oct-2011 ratchov

Remove midicat since aucat can now be used instead of midicat
with almost the same syntax (roughly an extra -M option).
Thru boxes are created with aucat, and corresponding MIDI port
names have the "aucat" prefix instead of "midithru". The old
device name will still work some time for backward compatibility.

ok deraadt


# 1.121 12-Oct-2011 jmc

- avoid line wrapping an example
- sort usage() for midicat
- tweak a bit of unclear wording

ok ratchov


# 1.120 12-Oct-2011 ratchov

Simplify and improve the way options are parsed and remove ~300 lines
of code that becomes unused. Few command line arguments changes are
required though:

- stream definitions (-ios) now must follow devices definitions they
are attached to (-fMn)

- the -n option is now a special "loopback" device and is thus used
like -f, eg it must precede streams

- in midicat, midi thru boxes are not created automatically anymore,
the new "-M" option must be used for that

- channel numbers (-Cc options) correspond always to channel numbers
of the hardware.

- the -u option isn't needed anymore

- increase the log verbosity so user errors are logged
without using -d

tested by many, help from jmc


Revision tags: OPENBSD_5_0_BASE
# 1.119 29-Jun-2011 ratchov

make -u flag per-device similarly to -a and -w


# 1.118 20-Jun-2011 ratchov

Make -aoff option apply to MIDI ports (-q) as well, ensuring the device
stays closed also if -q is used. As we're at it, add -a to midicat so
it behaves like aucat.


# 1.117 03-Jun-2011 ratchov

Allow device encoding to be set if -u is also used. Useful to degrade
quality to save bandwith when a remote audio device is used.


# 1.116 03-Jun-2011 ratchov

Reorganize the way command line options are parsed and stored.
No behaviour change.


# 1.115 26-May-2011 ratchov

add a new -w flag to control whether master volume is automatically
adjusted when new streams are connected and disconnected. Disabling
automatic volume adjustment makes sense if all streams are recorded
with properly lowered volumes.


# 1.114 10-May-2011 ratchov

fix option handling and enable TCP in midicat
from peters at schwertfisch.de, thanks!


# 1.113 28-Apr-2011 ratchov

Implement a new authentication method allowing aucat and midicat to
work over TCP, for instance, to expose the sound card of one machine
with other machines of the network.

The first client generates a 128-bit random number (aka the
session cookie), saves it in $HOME/.aucat_cookie and sends it to the
server. Successive clients load the cookie from $HOME/.aucat_cookie
and send it to the server but the server accepts only clients whose
cookie matches the session cookie. When all clients are gone, the
session is over, and another cookie could start a new session, and so
on.

TCP is enabled on the server with the new -L option, and on the client
side hostnames are specified with a new optional component in the
device name.

hints from damien, dlg and deraadt, tweaks from jmc


# 1.112 27-Apr-2011 ratchov

move amsg.h containing protocol defs from aucat side to libsndio side.

requested by deraadt


# 1.111 27-Apr-2011 deraadt

use more volatile sig_atomic_t in signal handlers
ok ratchov


# 1.110 19-Apr-2011 ratchov

don't hold a pointer to "listen" structure. Instead, when it must
be free()ed, iterate over the file_list, and find the structure
to free. This is safer and simpler. No behaviour change


# 1.109 16-Apr-2011 ratchov

for unix domain socket addresses use AUCAT_PATH and MIDICAT_PATH
macros instead of hardcoded strings. No object change


# 1.108 17-Mar-2011 ratchov

use a DEFAULT_RATE macro instead of hardcoding 44100, no object change


Revision tags: OPENBSD_4_9_BASE
# 1.107 05-Nov-2010 ratchov

unbreak compilation without DEBUG defined


# 1.106 21-Oct-2010 ratchov

use MODE_xxx and XRUN_xxx macros everywhere, and zap corresponding
AMSG_xxx macros, which in turns simplifies the code


# 1.105 08-Sep-2010 deraadt

aucat: getpwnam: No such file or directory
is not a proper error message.


# 1.104 20-Aug-2010 ratchov

polish the code: remove few unused #includes, add missing ones,
fix NULL vs 0, etc. No behaviour change.


# 1.103 19-Aug-2010 ratchov

fix NULL pointer deref in midicat option parsing when ``-'' is
used as filename


Revision tags: OPENBSD_4_8_BASE
# 1.102 31-Jul-2010 ratchov

remove 2 years old compatibility mode


# 1.101 10-Jul-2010 ratchov

In midi server mode, don't terminate the midi thru box if there are
no connections anymore.


# 1.100 10-Jul-2010 ratchov

fix parameter handling: don't try to open a ``default'' midi port
if no files are given on the command line


# 1.99 06-Jul-2010 jmc

various tweaks to previous; ok ratchov


# 1.98 06-Jul-2010 ratchov

Handle all streams the same way because there's no actual
difference between audio files and client connections. Clean up
the way command line options are handled and clarify this in the
manual page: stream parameters (-Ccehjmrtvx) must precede stream
definitions (-ios) and per-device parameters (-abz) and stream
definitions (-ios) must precede device definitions (-f). Since
there's no ``server'' and ``non-server'' modes anymore, make the
-l option just detach the process.

ok and help from jakemsr and jmc


# 1.97 29-Jun-2010 jakemsr

If the device isn't opened yet (as can be the case with '-a off'),
don't check if the midi control interface is idle
from ratchov


# 1.96 25-Jun-2010 ratchov

don't attempt to drain devices after they are destroyed, which
results in a use after free(). Catched by jakemsr@ with MALLOC_OPTIONS=J


# 1.95 20-Jun-2010 ratchov

fix midicat(1) device mode when only -i or only -o is used (ie
open the device input-only if -o is used, and output-only if -i is
used).


# 1.94 05-Jun-2010 ratchov

don't prime server buffers, because it's ugly and conceptually
complicated. Instead, request clients to provide enough samples
and start with buffers full.


# 1.93 05-Jun-2010 ratchov

Don't send the initial position as a clock tick with a negative delta.
Add a new ``initial position'' message which is simpler. No bahaviour
change, except effects of the protocol version crank.


# 1.92 04-Jun-2010 ratchov

Allow the audio device to be opened only while it's actually used.
This is necessary for uaudio devices, for instance to start aucat
before the device is plugged. Or to unplug a device whithout
having to restart aucat when another device is plugged. This is
controlled with the new -a option.

Allow multiple audio devices to be used concurently, i.e.
multiple ``-f devname'' options to be used; -f options must follow
per-device options, which is what we do for other options.


# 1.91 08-May-2010 ratchov

prime play buffers as soon as the device is opened, otherwise a
midi client could start it with empty buffers, in turn hurting
audio clients.


# 1.90 02-May-2010 ratchov

Clean up device handling code to clarify different initialization phases
and different device states. Split initialization in two phases:
first global variables are initialized then the audio hardware is opened.
Allow devices that don't support full-duplex to work in play-only or
record-only mode, even if ``-m play'' or ``-m rec'' are not specified.


# 1.89 02-May-2010 ratchov

Don't systematically fill with silence the mixer output. This
might result in extra samples being written to the device
when the mixer is closed.


# 1.88 24-Apr-2010 ratchov

if there are no files to play in legacy mode, print the usage message


# 1.87 24-Apr-2010 ratchov

rename ibuflist/obuflist -> ins/outs to cut long lines
from Thomas Pfaff, thanks


# 1.86 22-Apr-2010 ratchov

Allow multiple users to share the same aucat server. If aucat is
run by root, it binds a shared address to the socket, cranks the
process priority and drops privileges. sio_open(3) will try to
connect to the private socket first (if any), then to the shared
socket. Only one user may have connections to aucat at a given
time.

based on discussions with henning, pyr and others
ok jacek, deraadt


# 1.85 21-Apr-2010 ratchov

when creating the ``default'' subdevice, use last parameters of the
command line, instead of the device parameters.


# 1.84 21-Apr-2010 ratchov

adds the necessary bits to join channels (ex stereo->mono) or
expand channels (ex mono->stereo). It's switched on/off with
the ``-j'' option, (default is "on").


# 1.83 06-Apr-2010 ratchov

aucat (server):

- make the ``-m mode'' option per subdevice, allowing
subdevices to be play-only or rec-only even if the
server is full-duplex

- add ``monitoring'' mode (with ``-m mon''). This is a
record-only stream from which played streams can be
recorded (kind of ``record what you hear'').

- allow MIDI devices to be subscribed to the controlling
MIDI port of the server, ie what midicat does (with -f
option), but using the -q option.

- add flow control to the protocol, and serialize clock
ticks (sio_onmove() calls) and data chunks. This should
fix certain full-duplex programs, broken with ``magic''
block/buffer size combinations.

- enable 3 block latency which is the minimum theoretical.
Make -z and -b options correspond to device parameters.

- make sio_getcap(3) the same for aucat and sun backends,
ie return whatever is supported (``everything'' in the
aucat case, since everything is actulally supported).

aucat (player):

- enable ``-m mode'' option to select between monitoring
and recording when ``-o file'' is used.

- plug MIDI code to non-server codepath. The MIDI control
device is specified with the ``-q device'' option, as in
server mode.

- impliment lseek()'ing within files (controlled
through MIDI). Necessary to use aucat with a MIDI
sequencer.

midicat (thrubox):

- rename ``-f'' flag to ``-q'', so it has the
same name as in aucat (-f is still working)

ok jakemsr@, tweaks from jmc@


# 1.82 03-Apr-2010 ratchov

backout last change, committed by mistake, sorry...


# 1.81 03-Apr-2010 ratchov

doc fixes from jmc


Revision tags: OPENBSD_4_7_BASE
# 1.80 14-Jan-2010 ratchov

Use -C option to specify recorded channel range instead of -c which
is for played channels.

spotted by Jan Stary <hans at stare.cz>, thanks


# 1.79 11-Jan-2010 ratchov

When resuming from suspend, prime play buffers with silence,
otherwise the client would underrun by more than its own buffer
size, which currently is not allowed (causes the client to
deadlock).

Set the minimum client buffer size to two blocks. Single block
buffers don't work properly yet.

with help from jakemsr


# 1.78 10-Jan-2010 ratchov

Add traces to help tracking subtle timing bugs often causing
underuns and overruns; such bugs are hard to debug with ktrace
or a debugger. They are also handy to debug or monitor code
using aucat.

To enable traces, compile aucat with ``make DEBUG=-DDEBUG'' and
use the -d option multiple times.

ok jakemsr


# 1.77 05-Jan-2010 ratchov

Make the way .wav files are handled closer to the way sockets are
handled, which is conceptually simpler. No behaviour changes.


# 1.76 21-Nov-2009 ratchov

use strtonum(3) and strtol(3) instead of sscanf(3)
suggested by deraadt@


# 1.75 21-Nov-2009 deraadt

spelling


# 1.74 03-Nov-2009 ratchov

Allow any program using aucat to act as MMC slave and MTC master
transparently. Multiple audio applications can be started
synchronously from external software/hardware supporting the
standard Start/Stop/Relocate messages. The server clock is exposed
through MTC, allowing non-audio software/hardware to be
synchronized to audio applications.


# 1.73 27-Oct-2009 ratchov

add missing word in error message


# 1.72 10-Oct-2009 ratchov

don't use a references to the device file to check the current mode


# 1.71 10-Oct-2009 ratchov

make dev_done() common to audio and MIDI, and drop dev_thrudone()


# 1.70 10-Oct-2009 ratchov

use dev_done() for loopback devices too and remove dev_loopdone()


# 1.69 05-Oct-2009 ratchov

add -d flag. When started in server mode, if the -d flag is
used aucat and midicat don't daemonize and log on stderr.


# 1.68 27-Sep-2009 ratchov

remove all debug traces


# 1.67 26-Aug-2009 jmc

oops, forgot to update usage();


# 1.66 19-Aug-2009 ratchov

don't try to process -o arguments using the input file list, fixes
midicat crashing sometimes when -o is used


# 1.65 19-Aug-2009 ratchov

organize midi code like audio code, no functional change


# 1.64 17-Aug-2009 ratchov

allow midicat to take multiple -i and -o options


# 1.63 17-Aug-2009 ratchov

if one or more MIDI devices are specified in server mode, subscribe
them to the MIDI thru box


# 1.62 25-Jul-2009 ratchov

fix comments, remove no longer needed declaration, reorder includes
from Thomas Pfaff


# 1.61 25-Jul-2009 ratchov

Currently midi capable programs can control midi hardware, but
cannot cooperate with other programs. The aim of this change is
to allow any program to send midi data to other programs as they
were midi hardware. For instance, this change should solve the
longstanding problem of using a midi sequencer with software
synthesizers. More precisely:

- new midicat(1) utility (actually hardlink to aucat(1)).
it creates software midi thru boxes, allowing programs
to send midi messages to other programs as they were
midi(4) hardware.

- new midi api in libsndio (see mio_open(3)), to access
midi(4) devices and midicat(1) sockets in a uniform way.

- new device naming scheme <service>:<unit>[.<option>],
common to audio and midi.

- new sndio(7) manual describing concepts and naming

The current audio device naming still works, but people having
scripts or configuration files containing device names could read
the sndio(7) man page and slowly start updating device names.

discussed with jakemsr@ and deraadt@, help form jmc@


Revision tags: OPENBSD_4_6_BASE
# 1.60 27-Apr-2009 ratchov

make -v option apply also to implicit default socket


# 1.59 17-Mar-2009 jmc

- rename the arg to -b "nframes" and sync usage()
- grammar fix for the server section

from Thomas Pfaff
ok ratchov


Revision tags: OPENBSD_4_5_BASE
# 1.58 06-Feb-2009 ratchov

move error messages reported to user into main()


# 1.57 04-Feb-2009 ratchov

if there are too many connections, stop acceping new ones rather
than exit()ing with ``too many open files'' fatal error


# 1.56 04-Feb-2009 ratchov

daemonize when in server mode, suggested by many
ok jakemsr


# 1.55 03-Feb-2009 ratchov

in server mode, create /tmp/aucat-userid/ directory with
permissions 0700, and create sockets in it. This prevents
one local user to eavesdrop or disturb audio programs of
other users.

if you're using the ``-s socket'' option with an absolute
path as argument, please update it to use a socket name.

requested by many, bits from jakemsr and otto
ok jakemsr


# 1.54 25-Jan-2009 ratchov

don't set malloc_options to not interfer with /etc/malloc.conf
requested and explained by fgsch@, henning@, otto@


# 1.53 23-Jan-2009 ratchov

catch SIGHUP and SIGTERM too, to ensure that recorded files are
properly closed and sockets are cleaned up.


# 1.52 23-Jan-2009 ratchov

spacing + typo fix from Matt Jibson <matt.jibson(at)gmail.com>


# 1.51 10-Jan-2009 ratchov

add "loopback" mode in which input is connected to the output.
This is useful to mix, demultiplex, resample or reencode audio
files off-line.
tweak + ok jakemsr


# 1.50 29-Dec-2008 ratchov

make the code "more correct": add reference counters to aproc
structures so we can keep a pointer to the device. Beside some
simplifications, this allows to easily handle the situation
where the audio device desappears. No change in the behaviour.


# 1.49 26-Dec-2008 ratchov

*** empty log message ***


# 1.48 07-Dec-2008 ratchov

When resampling, don't require the sample frequency to be an integer.
This removes the arithmetic constraint between the sample frequency
and the block size and all the associated code. Now aucat can work
in server mode with any block size.
ok jakemsr


# 1.47 23-Nov-2008 ratchov

Use file input parameter as default device output and file output parameters
as default device input. That's what the man page says


# 1.46 20-Nov-2008 ratchov

take into account socket parameters into calculation of device
parameters; without this change aucat uses the device default
parameters and they are not necessarily usable for multi-stream
mode.


# 1.45 17-Nov-2008 jmc

- rename -s arg name to "socket"
- condense EXAMPLES into a single screenful

ok ratchov


# 1.44 17-Nov-2008 jmc

tweak previous man page changes, and sync usage();


# 1.43 17-Nov-2008 ratchov

allow aucat to run as server in play-only and record-only mode, so
it can be used on play-only, record-only and half-duplex devices.
ok jakemsr


# 1.42 16-Nov-2008 ratchov

allow aucat to listen on multiple sockets. Each socket carries its
channel and volume settings allowing multiple configuration to
coexist. Mostly useful for envy(4)-like devices, but can be used
to force different apps to use different settings.


# 1.41 16-Nov-2008 ratchov

add ``template'' parameters to sockets rather than using device
parameters. No functional change.


# 1.40 16-Nov-2008 ratchov

Make clients inherit the volume parameter when the -v option is used in
server mode. It gives the maximum volume a client may have. This wastes
dynamic range, but allows volume to stay constant when other clients
connect or disconnect.


# 1.39 12-Nov-2008 ratchov

when destroying the device, mark the mixer and the demultiplexer
for ``AUTOQUIT'' first, and then call file_eof() and friends.
fixes crashes sthen@ noticed on zaurus


# 1.38 11-Nov-2008 ratchov

if the device disappear the mixer and demultiplexer chains are
destroyed. If this happen then warn and exit, rather than crashing.


# 1.37 11-Nov-2008 ratchov

termninate non-server aucat when it's idle rather that using
the hackish {MIX,SUB}_AUTOQUIT features. This way we don't
have to care about possible references on destroyed objects.


# 1.36 11-Nov-2008 ratchov

when not in server mode, aucat terminates automatically using
the {MIX,SUB}_AUTAQUIT features. In this case we're not
allowed to directly touch dev_mix and dev_sub because they
can disappar. So disable the ``suspend'' and ``quit'' bits
when {MIX,SUB}_AUTAQUIT are used.


# 1.35 10-Nov-2008 ratchov

add a per-stream ``soft volume'' knob and the corresponding -v option.
The code will be useful later for the volume knob in the sndio API.


# 1.34 09-Nov-2008 ratchov

make aucat(1) stop automatically the audio(1) device if it's idle. This
way, when in server mode, it consumes no CPU if there are no clients.
Later, this will allow to start aucat(1) at session or system startup.


# 1.33 08-Nov-2008 ratchov

set the default device to /dev/audio for legacy mode.
From Thomas Pfaff <tpfaff _at_ agderlink.no> and tweaks from me


# 1.32 07-Nov-2008 ratchov

expose the block size in the sndio API by making par->round writable
and thus remove the ugly rate <-> block-size table from
sio_setpar(3). Handle the block size negociation in aucat(1), since
it has few constrains the code is overally simpler.
ok jakemsr@, major crank suggested by deraadt@


# 1.31 03-Nov-2008 ratchov

A small optimization: handle most N-channel <-> M-channel conversions
inside the mixer and the demultiplexer. This way, aucat will not
trigger the heavy conversion code when only channel conversions are
required. Cuts ~50% of the CPU usage on envy(4) devices, can improve
surround 4.0, 5.1 and 7.1 capable devices. No functionnal change.


# 1.30 26-Oct-2008 ratchov

zap `q' from the getopts string too,
requested by jmc@


# 1.29 26-Oct-2008 jmc

sync SYNOPSIS and usage();


# 1.28 26-Oct-2008 ratchov

add minimal server capability to aucat(1). When started in server
mode, it listens on an unix socket and mixes/demultiplexes any number
of full-duplex streams, doing necessary format conversions and
resampling on the fly.

programs can use the new libsa(3) library to play and record audio.
The library provides a very simple API to connect to the audio server;
if aucat(1) isn't running, it uses the audio(4) driver transparently
instead.


# 1.27 25-Aug-2008 sobrado

minor tweak for removal of "[-d devel]" (change done in r1.19 and r1.21).

ok ratchov@


# 1.26 14-Aug-2008 ratchov

move all device related stuff from aucat.c to a new dev.c file.
The new dev_xxx() routines expose a "high level" self-contained
interface to the device. At initialization, the device is opened
and two chains of aproc structures are created:

* a playback chain that exposes a (initially) empty mix
aproc to which the rest of the code can attach new
streams to be played

* record chain that exposes a (initially) empty sub aproc
to which the rest of the code can attach new stream to
to record

The rest of the code, has just to use dev_attach() routine to
attach streams. While we're at it, add a ``devops'' structure
containing pointers to the device-specific routines. This will
allow later to add support for other type of device than the
Sun API.

Also, write the .wav headers in file_del(), so put all header
related data in the file strucuture. This allows to close() the
file, as soon as wpipe_xxx() aproc terminates. This will be
useful for the server, because it will need to close() descripts
of closed connections immediately.

add mix_pushzero() routine to fill the mixer with silence. It
will be used to avoid the mixer to underrun when there are no
input streams. Since we always have at least one input stream
there's no behaviour change.

ok jakemsr


Revision tags: OPENBSD_4_4_BASE
# 1.25 02-Jun-2008 ratchov

document latest changes: -d flag is replaced by AUCAT_DEBUG
environment variable, new -xX options

bits from eric, ok jakemsr


# 1.24 02-Jun-2008 ratchov

currently, we start the device as soon as write() blocks; it's better
to continue filling the play buffer until it's full. This way, the
play buffer has fewer chances to underrun when the device is started.

ok jakemsr


# 1.23 02-Jun-2008 ratchov

if aucat is suspended, then the kernel continues running the device
(produces silence) and later, once aucat is resumed the kernel starts
dropping samples, it will try to drop as many samples as silence was
produced. So suspending breaks the aucat process permanently.

workaround this by blocking the signals ie disabling suspending of aucat
from the tty. The long term solution would be to catch SIGCONT and to stop
the device, resync/refill buffers and restart the device. That's really a
lot of work...

ok jakemsr


# 1.22 02-Jun-2008 ratchov

allow the user to choose the aucat behaviour when underruns/overruns
occur on a per-stream basis, using -Xx flags. There are 3 possible
policies:

- ignore : ignores underruns/overruns, for instance, this mode
could be used for creating simple pipes with utilities; like in
your last cdio diff.

- sync : insert/discard samples in order to keep all streams in
sync, useful for multi-tracker-like apps and/or to sync
midi/video/whatever on audio streams (this was the previous
behaviour)

- error : if overruns/underruns occur, consider it as fatal error
and kill the corresponding stream (without disturbing others).
Useful, for reliable recordings (and/or debugging aucat
itself:).

ok jakemsr


# 1.21 02-Jun-2008 ratchov

(remove -d, second part) use strtonum() instead of sscanf() and inlined
checks

suggested and ok jakemsr


# 1.20 02-Jun-2008 ratchov

since underruns/overruns are handled outside devices, remove code that
check for xruns in devices.

ok jakemsr


# 1.19 02-Jun-2008 ratchov

(remove -d, first part) replace -d flag by AUCAT_DEBUG environment
variable. This eases turning debugging on/off when aucat is started by
another program.

"i like the idea" jakemsr


# 1.18 26-May-2008 jmc

put -q in the right place;


# 1.17 26-May-2008 jakemsr

add -q option which enables "quiet" operation.

ok ratchov@


# 1.16 23-May-2008 jmc

various fixes for aucat.1, and sync usage(); ok ratchov


# 1.15 23-May-2008 ratchov

add support for:
- recording, full-duplex operation
- format conversions and resampling on the fly
- mixing on the fly of multiple inputs of different formats
- up to 16 channels, simplistic "routing" of channel ranges
- more linear encodings (in raw and wav files)
the old behaviour is fully preserved if none of the new -i and -o
options are used.

code and fixes from jakemsr@ and eric@, suggestions by others.
ok "go ahead" deraadt@


# 1.14 13-Apr-2008 jakemsr

- remove code that was rendered pointless by the last commit
- minor knf

from Pierre Riteau


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE
# 1.13 20-Mar-2007 uwe

Support for playing simple RIFF files (.wav) from Simon Effenberg, savar
at schuldeigen de, with some tweaks from me. ok millert, tests ckuethe,
liked by some disliked by nobody, but he's not even a real user :)


Revision tags: OPENBSD_4_1_BASE
# 1.12 20-Dec-2006 steven

remove unneeded #include

ok mbalmer@ deraadt@


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.11 20-Jan-2005 jaredy

- knf
- check read/write/etc. calls for errors
- use constants instead of hardcoded pathnames
- better variable types
- add usage output

suggestions from various, ok ian


# 1.10 13-Jan-2005 ian

Close what you open: obvious fix, ok jared@, more to follow


Revision tags: OPENBSD_3_5_BASE OPENBSD_3_6_BASE
# 1.9 20-Oct-2003 jmc

typos from Jared Yanovich, slightly ammended;


Revision tags: OPENBSD_3_4_BASE
# 1.8 10-Jul-2003 david

add missing includes
ok deraadt@ tedu@


# 1.7 02-Jul-2003 deraadt

protos


# 1.6 10-Jun-2003 deraadt

mostly ansi cleanup; pval ok


Revision tags: OPENBSD_3_3_BASE
# 1.5 09-Dec-2002 millert

From Andrushock, s/sucess/success/g


Revision tags: OPENBSD_2_8_BASE OPENBSD_2_9_BASE OPENBSD_3_0_BASE OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.4 01-Jul-2000 millert

Add -f option to specify audio device, honor AUDIODEVICE environment vairable,
and some KNF.


Revision tags: OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE
# 1.3 05-Jan-1997 kstailey

minor tweaks; htonl() -> ntohl(); added SEE ALSO


# 1.2 03-Jan-1997 kstailey

removed unnecessary test


# 1.1 02-Jan-1997 kstailey

new utility for playing sound files


# 1.173 13-May-2018 ratchov

Add missing pledge(). From Jesper Wallin <jesper at ifconfig.se>.
Thanks!


Revision tags: OPENBSD_6_3_BASE
# 1.172 07-Nov-2017 ratchov

prefill with silence the buffer where cmap_copy() stores samples
rather than the file buffer. Fixes initialized data being treated as
recorded samples when resampling or format conversions are involved
but mono->stereo conversion is disabled.


# 1.171 03-Nov-2017 ratchov

Prefill rec buffer with silence. This is necessary because certain
channels don't get samples, for instance when mono->stereo conversion
is disabled.


Revision tags: OPENBSD_6_2_BASE
# 1.170 20-Jul-2017 ratchov

Fix integer overflow that would causes >12 hours files
to not properly relocate. Found with coverity.


Revision tags: OPENBSD_6_1_BASE
# 1.169 03-Jan-2017 ratchov

Assert we're not freeing buffers we didn't allocate (DEBUG mode).


# 1.168 03-Jan-2017 ratchov

Log play volume together with other play parameters.


# 1.167 30-Sep-2016 ratchov

Set the device rate to the calculated rate. Otherwise, the signal
may endup resampled twice.


# 1.166 30-Sep-2016 ratchov

Remove unnecessary includes. From Michael W. Bombardieri <mb at ii.net>.


# 1.165 27-Sep-2016 ratchov

Don't rely on the resampling code to calculate the number of samples
to process, as it may produce one extra sample (to handle accumulation
of fractional samples), which would cause access to one sample past
the end of the buffer and crash aucat. Fix this by limiting the number
of samples processed to a single block.

Found by and help from Michael W. Bombardieri <mb at ii.net>. Thanks.


Revision tags: OPENBSD_6_0_BASE
# 1.164 07-Jun-2016 ratchov

Add -g and -p options to control device and file start position.


# 1.163 07-Jun-2016 ratchov

Add resamp_getcnt() routine to calculate the exact number of samples
that would be consumed and produced by the sampler rate converter. Use
it to avoid partial samples that are not properly handled. Fixes
last samples of certain files causing aucat to abort.


# 1.162 02-Jun-2016 ratchov

remove forgotten debug printfs


# 1.161 02-Jun-2016 ratchov

set exit status to 1 if -n is used without -i and -o


# 1.160 31-May-2016 ratchov

Pass hr:min:sec:frame.cent to dev_mmcloc() directly and remove the
useless intermediate conversion from and to 2400-th of seconds.


# 1.159 31-May-2016 ratchov

To store MMC position, use samples (at device rate) as time units
instead of 2400-ths of second. No behaviour change.


# 1.158 28-May-2016 ratchov

Fix file block size rounding and ensure it's large enough to store a
full audio block.


# 1.157 27-May-2016 ratchov

When resampling, use the exact resampling factor instead of the ratio
between input and output block sizes. This was inherited from sndiod,
but is not required for files because they are continuous streams of
samples and do not need to be split in blocks of equal duration.

This change makes playback/recording rate match exactly the requested
sample rate.


# 1.156 27-May-2016 ratchov

Flush rec buffer if there's less than one block space left and refill
play buffer if there's less than one block of data left. This is the
correct condition in the general case. No behaviour change, as all
input/output is multiple of the block size.


# 1.155 27-May-2016 ratchov

Simplify slot_fill() and slot_flush(). No behaviour change


# 1.154 27-May-2016 ratchov

Make resamp_do() get the exact number input and output samples and
provide routines to calculate them. This way we don't rely on it
to calculate the bytes procuded/consumed anymore. No behaviour change.


# 1.153 26-May-2016 ratchov

Make format conversion routines return the number of frames
consumed from both input and output buffers. No behaviour change.


Revision tags: OPENBSD_5_9_BASE
# 1.152 29-Jan-2016 ratchov

Check that byte order is native only if we're using more than one
byte per sample, check that samples are lsb-aligned only if there's
padding. Fixes unsupported parameters not being detected if compiled
in 24-bit mode and sndiod is not running.


# 1.151 10-Jan-2016 ratchov

trailing spaces, fit in 80 columns


# 1.150 09-Nov-2015 mmcc

Remove xfree(). From Michael W. Bombardieri.

ok ratchov@


# 1.149 27-Aug-2015 ratchov

fix typos in comments and error messages


Revision tags: OPENBSD_5_8_BASE
# 1.148 16-May-2015 ratchov

Call slot_stop() after the play buffer is drained to properly cleanup
the slot. Fixes periodic glitches occurring after eof is reached, and
then playback restarted with a MMC-relocate and MMC-start.


Revision tags: OPENBSD_5_7_BASE
# 1.147 21-Jan-2015 jmc

fix macro breakage and update usage();


# 1.146 21-Jan-2015 ratchov

Simplify internals and rewrite file header parsing code:
- remove -M, -t, -w, -C, -x flags that don't make sense anymore
- make "-j off" the default (sndiod already does the job)
- don't limit the number of played/recorded files.
- add support for floating-point encoded files.
- add support for apple .aiff and sun/next .au files


# 1.145 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.144 18-Nov-2013 ratchov

remove unused variables


# 1.143 18-Nov-2013 ratchov

add missing prototypes


# 1.142 12-Nov-2013 ratchov

remove bogus legacy server-specific bits that cause confusion


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.141 03-Dec-2012 ratchov

Remove server capability description from the documentation and from
the usage string, and just say that for historic reasons aucat behaves
like sndiod.
with help from jmc@


# 1.140 23-Nov-2012 ratchov

Since there's no sndiod -> aucat link anymore, allow aucat to be run
in server mode even when not invoked as sndiod. This allows aucat and
the new sndiod daemon to live together in /usr/bin, which makes easy
to quickly compare sndiod and aucat (and spot possible regressions)
without having to modify /usr/bin.


# 1.139 27-Oct-2012 ratchov

don't stat & test sndio home directory since we never use it.


# 1.138 27-Oct-2012 ratchov

don't (re-)define SNDIO_USER and SNDIO_PRIO if they are already defined,
this way they can be defined at compilation time


Revision tags: OPENBSD_5_2_BASE
# 1.137 27-Jun-2012 ratchov

Uniformize error messages printing, use err() instead of fprintf()
when appropriate. From Michael W. Bombardieri <mb at ii.net>


# 1.136 19-Jun-2012 ratchov

Remove hidden options that are not needed any more,
from Remco <remco at d-compu.dyndns.org>. Thanks


# 1.135 23-May-2012 ratchov

Make the "default" string a valid device name that has the same effect
as NULL has. This will (hopefully) simplify ports where the user
passes the device string.


# 1.134 11-Apr-2012 ratchov

Use "unsigned int" rather than "unsigned". No object change.
suggested by deraadt@


Revision tags: OPENBSD_5_1_BASE
# 1.133 09-Feb-2012 ratchov

Crank the default buffer size to 160ms (ie 7680 frames at 48kHz).
Workarounds stuttering caused by something hogging the cpu in kernel
mode and preventing sndiod from running at time.
ok deraadt


# 1.132 26-Jan-2012 ratchov

Make the default rate 48kHz and the default block size 10ms. These
settings ensure video players and programs using MTC are smooth by
default. Thanks to all who tested.


# 1.131 10-Jan-2012 ratchov

When a sub-device is created (-s), check if a hardware
device was already created (-f) before trying to use the
default one, otherwise all sub-devices end up attached
to the default device. Noticed by peters at schwertfisch.de,
thanks!


# 1.130 09-Dec-2011 ratchov

fix usage string: remove -M from aucat and -l from aucat and sndiod
from jmc


# 1.129 09-Dec-2011 ratchov

add a new sndiod audio/midi daemon and hide aucat server-specific
options.


# 1.128 02-Dec-2011 ratchov

don't use a "server" flag, just check if we expose sub-devices


# 1.127 02-Dec-2011 ratchov

reuse midi-control code to implement midi thru boxes and remove
the old midithru implementation; less code, less bugs. As a side
effect, midi output doesn't implement running status "compression"
any more.


# 1.126 20-Nov-2011 ratchov

Move mmc/mtc and volume control bits from struct aproc to struct
dev. Allows volume settings to be saved while the device is kept
closed. Besides that, no behabiour changes.


# 1.125 15-Nov-2011 ratchov

Add a "device number" component in sndio(7) device names, allowing a
single aucat instance to handle all audio and MIDI services. Since
this partially breaks compatibility, this is a opportunitiy to fix few
other design mistakes (eg ':' being used by inet6, type name vs api
name confusion, etc..). This leads to the following names:

type[@hostname][,unit]/devnum[.option]

The device number is the minor device number for direct hardware
access (ie the 'N' in /dev/audioN). For aucat, this is the occurence
number of the -f (or -M) option.

There's a compatibility hook to keep old names working if only one
aucat server is running.


# 1.124 18-Oct-2011 ratchov

keep the device closed by default (when not used)


# 1.123 18-Oct-2011 ratchov

remove duplicate options in getopt() string


# 1.122 17-Oct-2011 ratchov

Remove midicat since aucat can now be used instead of midicat
with almost the same syntax (roughly an extra -M option).
Thru boxes are created with aucat, and corresponding MIDI port
names have the "aucat" prefix instead of "midithru". The old
device name will still work some time for backward compatibility.

ok deraadt


# 1.121 12-Oct-2011 jmc

- avoid line wrapping an example
- sort usage() for midicat
- tweak a bit of unclear wording

ok ratchov


# 1.120 12-Oct-2011 ratchov

Simplify and improve the way options are parsed and remove ~300 lines
of code that becomes unused. Few command line arguments changes are
required though:

- stream definitions (-ios) now must follow devices definitions they
are attached to (-fMn)

- the -n option is now a special "loopback" device and is thus used
like -f, eg it must precede streams

- in midicat, midi thru boxes are not created automatically anymore,
the new "-M" option must be used for that

- channel numbers (-Cc options) correspond always to channel numbers
of the hardware.

- the -u option isn't needed anymore

- increase the log verbosity so user errors are logged
without using -d

tested by many, help from jmc


Revision tags: OPENBSD_5_0_BASE
# 1.119 29-Jun-2011 ratchov

make -u flag per-device similarly to -a and -w


# 1.118 20-Jun-2011 ratchov

Make -aoff option apply to MIDI ports (-q) as well, ensuring the device
stays closed also if -q is used. As we're at it, add -a to midicat so
it behaves like aucat.


# 1.117 03-Jun-2011 ratchov

Allow device encoding to be set if -u is also used. Useful to degrade
quality to save bandwith when a remote audio device is used.


# 1.116 03-Jun-2011 ratchov

Reorganize the way command line options are parsed and stored.
No behaviour change.


# 1.115 26-May-2011 ratchov

add a new -w flag to control whether master volume is automatically
adjusted when new streams are connected and disconnected. Disabling
automatic volume adjustment makes sense if all streams are recorded
with properly lowered volumes.


# 1.114 10-May-2011 ratchov

fix option handling and enable TCP in midicat
from peters at schwertfisch.de, thanks!


# 1.113 28-Apr-2011 ratchov

Implement a new authentication method allowing aucat and midicat to
work over TCP, for instance, to expose the sound card of one machine
with other machines of the network.

The first client generates a 128-bit random number (aka the
session cookie), saves it in $HOME/.aucat_cookie and sends it to the
server. Successive clients load the cookie from $HOME/.aucat_cookie
and send it to the server but the server accepts only clients whose
cookie matches the session cookie. When all clients are gone, the
session is over, and another cookie could start a new session, and so
on.

TCP is enabled on the server with the new -L option, and on the client
side hostnames are specified with a new optional component in the
device name.

hints from damien, dlg and deraadt, tweaks from jmc


# 1.112 27-Apr-2011 ratchov

move amsg.h containing protocol defs from aucat side to libsndio side.

requested by deraadt


# 1.111 27-Apr-2011 deraadt

use more volatile sig_atomic_t in signal handlers
ok ratchov


# 1.110 19-Apr-2011 ratchov

don't hold a pointer to "listen" structure. Instead, when it must
be free()ed, iterate over the file_list, and find the structure
to free. This is safer and simpler. No behaviour change


# 1.109 16-Apr-2011 ratchov

for unix domain socket addresses use AUCAT_PATH and MIDICAT_PATH
macros instead of hardcoded strings. No object change


# 1.108 17-Mar-2011 ratchov

use a DEFAULT_RATE macro instead of hardcoding 44100, no object change


Revision tags: OPENBSD_4_9_BASE
# 1.107 05-Nov-2010 ratchov

unbreak compilation without DEBUG defined


# 1.106 21-Oct-2010 ratchov

use MODE_xxx and XRUN_xxx macros everywhere, and zap corresponding
AMSG_xxx macros, which in turns simplifies the code


# 1.105 08-Sep-2010 deraadt

aucat: getpwnam: No such file or directory
is not a proper error message.


# 1.104 20-Aug-2010 ratchov

polish the code: remove few unused #includes, add missing ones,
fix NULL vs 0, etc. No behaviour change.


# 1.103 19-Aug-2010 ratchov

fix NULL pointer deref in midicat option parsing when ``-'' is
used as filename


Revision tags: OPENBSD_4_8_BASE
# 1.102 31-Jul-2010 ratchov

remove 2 years old compatibility mode


# 1.101 10-Jul-2010 ratchov

In midi server mode, don't terminate the midi thru box if there are
no connections anymore.


# 1.100 10-Jul-2010 ratchov

fix parameter handling: don't try to open a ``default'' midi port
if no files are given on the command line


# 1.99 06-Jul-2010 jmc

various tweaks to previous; ok ratchov


# 1.98 06-Jul-2010 ratchov

Handle all streams the same way because there's no actual
difference between audio files and client connections. Clean up
the way command line options are handled and clarify this in the
manual page: stream parameters (-Ccehjmrtvx) must precede stream
definitions (-ios) and per-device parameters (-abz) and stream
definitions (-ios) must precede device definitions (-f). Since
there's no ``server'' and ``non-server'' modes anymore, make the
-l option just detach the process.

ok and help from jakemsr and jmc


# 1.97 29-Jun-2010 jakemsr

If the device isn't opened yet (as can be the case with '-a off'),
don't check if the midi control interface is idle
from ratchov


# 1.96 25-Jun-2010 ratchov

don't attempt to drain devices after they are destroyed, which
results in a use after free(). Catched by jakemsr@ with MALLOC_OPTIONS=J


# 1.95 20-Jun-2010 ratchov

fix midicat(1) device mode when only -i or only -o is used (ie
open the device input-only if -o is used, and output-only if -i is
used).


# 1.94 05-Jun-2010 ratchov

don't prime server buffers, because it's ugly and conceptually
complicated. Instead, request clients to provide enough samples
and start with buffers full.


# 1.93 05-Jun-2010 ratchov

Don't send the initial position as a clock tick with a negative delta.
Add a new ``initial position'' message which is simpler. No bahaviour
change, except effects of the protocol version crank.


# 1.92 04-Jun-2010 ratchov

Allow the audio device to be opened only while it's actually used.
This is necessary for uaudio devices, for instance to start aucat
before the device is plugged. Or to unplug a device whithout
having to restart aucat when another device is plugged. This is
controlled with the new -a option.

Allow multiple audio devices to be used concurently, i.e.
multiple ``-f devname'' options to be used; -f options must follow
per-device options, which is what we do for other options.


# 1.91 08-May-2010 ratchov

prime play buffers as soon as the device is opened, otherwise a
midi client could start it with empty buffers, in turn hurting
audio clients.


# 1.90 02-May-2010 ratchov

Clean up device handling code to clarify different initialization phases
and different device states. Split initialization in two phases:
first global variables are initialized then the audio hardware is opened.
Allow devices that don't support full-duplex to work in play-only or
record-only mode, even if ``-m play'' or ``-m rec'' are not specified.


# 1.89 02-May-2010 ratchov

Don't systematically fill with silence the mixer output. This
might result in extra samples being written to the device
when the mixer is closed.


# 1.88 24-Apr-2010 ratchov

if there are no files to play in legacy mode, print the usage message


# 1.87 24-Apr-2010 ratchov

rename ibuflist/obuflist -> ins/outs to cut long lines
from Thomas Pfaff, thanks


# 1.86 22-Apr-2010 ratchov

Allow multiple users to share the same aucat server. If aucat is
run by root, it binds a shared address to the socket, cranks the
process priority and drops privileges. sio_open(3) will try to
connect to the private socket first (if any), then to the shared
socket. Only one user may have connections to aucat at a given
time.

based on discussions with henning, pyr and others
ok jacek, deraadt


# 1.85 21-Apr-2010 ratchov

when creating the ``default'' subdevice, use last parameters of the
command line, instead of the device parameters.


# 1.84 21-Apr-2010 ratchov

adds the necessary bits to join channels (ex stereo->mono) or
expand channels (ex mono->stereo). It's switched on/off with
the ``-j'' option, (default is "on").


# 1.83 06-Apr-2010 ratchov

aucat (server):

- make the ``-m mode'' option per subdevice, allowing
subdevices to be play-only or rec-only even if the
server is full-duplex

- add ``monitoring'' mode (with ``-m mon''). This is a
record-only stream from which played streams can be
recorded (kind of ``record what you hear'').

- allow MIDI devices to be subscribed to the controlling
MIDI port of the server, ie what midicat does (with -f
option), but using the -q option.

- add flow control to the protocol, and serialize clock
ticks (sio_onmove() calls) and data chunks. This should
fix certain full-duplex programs, broken with ``magic''
block/buffer size combinations.

- enable 3 block latency which is the minimum theoretical.
Make -z and -b options correspond to device parameters.

- make sio_getcap(3) the same for aucat and sun backends,
ie return whatever is supported (``everything'' in the
aucat case, since everything is actulally supported).

aucat (player):

- enable ``-m mode'' option to select between monitoring
and recording when ``-o file'' is used.

- plug MIDI code to non-server codepath. The MIDI control
device is specified with the ``-q device'' option, as in
server mode.

- impliment lseek()'ing within files (controlled
through MIDI). Necessary to use aucat with a MIDI
sequencer.

midicat (thrubox):

- rename ``-f'' flag to ``-q'', so it has the
same name as in aucat (-f is still working)

ok jakemsr@, tweaks from jmc@


# 1.82 03-Apr-2010 ratchov

backout last change, committed by mistake, sorry...


# 1.81 03-Apr-2010 ratchov

doc fixes from jmc


Revision tags: OPENBSD_4_7_BASE
# 1.80 14-Jan-2010 ratchov

Use -C option to specify recorded channel range instead of -c which
is for played channels.

spotted by Jan Stary <hans at stare.cz>, thanks


# 1.79 11-Jan-2010 ratchov

When resuming from suspend, prime play buffers with silence,
otherwise the client would underrun by more than its own buffer
size, which currently is not allowed (causes the client to
deadlock).

Set the minimum client buffer size to two blocks. Single block
buffers don't work properly yet.

with help from jakemsr


# 1.78 10-Jan-2010 ratchov

Add traces to help tracking subtle timing bugs often causing
underuns and overruns; such bugs are hard to debug with ktrace
or a debugger. They are also handy to debug or monitor code
using aucat.

To enable traces, compile aucat with ``make DEBUG=-DDEBUG'' and
use the -d option multiple times.

ok jakemsr


# 1.77 05-Jan-2010 ratchov

Make the way .wav files are handled closer to the way sockets are
handled, which is conceptually simpler. No behaviour changes.


# 1.76 21-Nov-2009 ratchov

use strtonum(3) and strtol(3) instead of sscanf(3)
suggested by deraadt@


# 1.75 21-Nov-2009 deraadt

spelling


# 1.74 03-Nov-2009 ratchov

Allow any program using aucat to act as MMC slave and MTC master
transparently. Multiple audio applications can be started
synchronously from external software/hardware supporting the
standard Start/Stop/Relocate messages. The server clock is exposed
through MTC, allowing non-audio software/hardware to be
synchronized to audio applications.


# 1.73 27-Oct-2009 ratchov

add missing word in error message


# 1.72 10-Oct-2009 ratchov

don't use a references to the device file to check the current mode


# 1.71 10-Oct-2009 ratchov

make dev_done() common to audio and MIDI, and drop dev_thrudone()


# 1.70 10-Oct-2009 ratchov

use dev_done() for loopback devices too and remove dev_loopdone()


# 1.69 05-Oct-2009 ratchov

add -d flag. When started in server mode, if the -d flag is
used aucat and midicat don't daemonize and log on stderr.


# 1.68 27-Sep-2009 ratchov

remove all debug traces


# 1.67 26-Aug-2009 jmc

oops, forgot to update usage();


# 1.66 19-Aug-2009 ratchov

don't try to process -o arguments using the input file list, fixes
midicat crashing sometimes when -o is used


# 1.65 19-Aug-2009 ratchov

organize midi code like audio code, no functional change


# 1.64 17-Aug-2009 ratchov

allow midicat to take multiple -i and -o options


# 1.63 17-Aug-2009 ratchov

if one or more MIDI devices are specified in server mode, subscribe
them to the MIDI thru box


# 1.62 25-Jul-2009 ratchov

fix comments, remove no longer needed declaration, reorder includes
from Thomas Pfaff


# 1.61 25-Jul-2009 ratchov

Currently midi capable programs can control midi hardware, but
cannot cooperate with other programs. The aim of this change is
to allow any program to send midi data to other programs as they
were midi hardware. For instance, this change should solve the
longstanding problem of using a midi sequencer with software
synthesizers. More precisely:

- new midicat(1) utility (actually hardlink to aucat(1)).
it creates software midi thru boxes, allowing programs
to send midi messages to other programs as they were
midi(4) hardware.

- new midi api in libsndio (see mio_open(3)), to access
midi(4) devices and midicat(1) sockets in a uniform way.

- new device naming scheme <service>:<unit>[.<option>],
common to audio and midi.

- new sndio(7) manual describing concepts and naming

The current audio device naming still works, but people having
scripts or configuration files containing device names could read
the sndio(7) man page and slowly start updating device names.

discussed with jakemsr@ and deraadt@, help form jmc@


Revision tags: OPENBSD_4_6_BASE
# 1.60 27-Apr-2009 ratchov

make -v option apply also to implicit default socket


# 1.59 17-Mar-2009 jmc

- rename the arg to -b "nframes" and sync usage()
- grammar fix for the server section

from Thomas Pfaff
ok ratchov


Revision tags: OPENBSD_4_5_BASE
# 1.58 06-Feb-2009 ratchov

move error messages reported to user into main()


# 1.57 04-Feb-2009 ratchov

if there are too many connections, stop acceping new ones rather
than exit()ing with ``too many open files'' fatal error


# 1.56 04-Feb-2009 ratchov

daemonize when in server mode, suggested by many
ok jakemsr


# 1.55 03-Feb-2009 ratchov

in server mode, create /tmp/aucat-userid/ directory with
permissions 0700, and create sockets in it. This prevents
one local user to eavesdrop or disturb audio programs of
other users.

if you're using the ``-s socket'' option with an absolute
path as argument, please update it to use a socket name.

requested by many, bits from jakemsr and otto
ok jakemsr


# 1.54 25-Jan-2009 ratchov

don't set malloc_options to not interfer with /etc/malloc.conf
requested and explained by fgsch@, henning@, otto@


# 1.53 23-Jan-2009 ratchov

catch SIGHUP and SIGTERM too, to ensure that recorded files are
properly closed and sockets are cleaned up.


# 1.52 23-Jan-2009 ratchov

spacing + typo fix from Matt Jibson <matt.jibson(at)gmail.com>


# 1.51 10-Jan-2009 ratchov

add "loopback" mode in which input is connected to the output.
This is useful to mix, demultiplex, resample or reencode audio
files off-line.
tweak + ok jakemsr


# 1.50 29-Dec-2008 ratchov

make the code "more correct": add reference counters to aproc
structures so we can keep a pointer to the device. Beside some
simplifications, this allows to easily handle the situation
where the audio device desappears. No change in the behaviour.


# 1.49 26-Dec-2008 ratchov

*** empty log message ***


# 1.48 07-Dec-2008 ratchov

When resampling, don't require the sample frequency to be an integer.
This removes the arithmetic constraint between the sample frequency
and the block size and all the associated code. Now aucat can work
in server mode with any block size.
ok jakemsr


# 1.47 23-Nov-2008 ratchov

Use file input parameter as default device output and file output parameters
as default device input. That's what the man page says


# 1.46 20-Nov-2008 ratchov

take into account socket parameters into calculation of device
parameters; without this change aucat uses the device default
parameters and they are not necessarily usable for multi-stream
mode.


# 1.45 17-Nov-2008 jmc

- rename -s arg name to "socket"
- condense EXAMPLES into a single screenful

ok ratchov


# 1.44 17-Nov-2008 jmc

tweak previous man page changes, and sync usage();


# 1.43 17-Nov-2008 ratchov

allow aucat to run as server in play-only and record-only mode, so
it can be used on play-only, record-only and half-duplex devices.
ok jakemsr


# 1.42 16-Nov-2008 ratchov

allow aucat to listen on multiple sockets. Each socket carries its
channel and volume settings allowing multiple configuration to
coexist. Mostly useful for envy(4)-like devices, but can be used
to force different apps to use different settings.


# 1.41 16-Nov-2008 ratchov

add ``template'' parameters to sockets rather than using device
parameters. No functional change.


# 1.40 16-Nov-2008 ratchov

Make clients inherit the volume parameter when the -v option is used in
server mode. It gives the maximum volume a client may have. This wastes
dynamic range, but allows volume to stay constant when other clients
connect or disconnect.


# 1.39 12-Nov-2008 ratchov

when destroying the device, mark the mixer and the demultiplexer
for ``AUTOQUIT'' first, and then call file_eof() and friends.
fixes crashes sthen@ noticed on zaurus


# 1.38 11-Nov-2008 ratchov

if the device disappear the mixer and demultiplexer chains are
destroyed. If this happen then warn and exit, rather than crashing.


# 1.37 11-Nov-2008 ratchov

termninate non-server aucat when it's idle rather that using
the hackish {MIX,SUB}_AUTOQUIT features. This way we don't
have to care about possible references on destroyed objects.


# 1.36 11-Nov-2008 ratchov

when not in server mode, aucat terminates automatically using
the {MIX,SUB}_AUTAQUIT features. In this case we're not
allowed to directly touch dev_mix and dev_sub because they
can disappar. So disable the ``suspend'' and ``quit'' bits
when {MIX,SUB}_AUTAQUIT are used.


# 1.35 10-Nov-2008 ratchov

add a per-stream ``soft volume'' knob and the corresponding -v option.
The code will be useful later for the volume knob in the sndio API.


# 1.34 09-Nov-2008 ratchov

make aucat(1) stop automatically the audio(1) device if it's idle. This
way, when in server mode, it consumes no CPU if there are no clients.
Later, this will allow to start aucat(1) at session or system startup.


# 1.33 08-Nov-2008 ratchov

set the default device to /dev/audio for legacy mode.
From Thomas Pfaff <tpfaff _at_ agderlink.no> and tweaks from me


# 1.32 07-Nov-2008 ratchov

expose the block size in the sndio API by making par->round writable
and thus remove the ugly rate <-> block-size table from
sio_setpar(3). Handle the block size negociation in aucat(1), since
it has few constrains the code is overally simpler.
ok jakemsr@, major crank suggested by deraadt@


# 1.31 03-Nov-2008 ratchov

A small optimization: handle most N-channel <-> M-channel conversions
inside the mixer and the demultiplexer. This way, aucat will not
trigger the heavy conversion code when only channel conversions are
required. Cuts ~50% of the CPU usage on envy(4) devices, can improve
surround 4.0, 5.1 and 7.1 capable devices. No functionnal change.


# 1.30 26-Oct-2008 ratchov

zap `q' from the getopts string too,
requested by jmc@


# 1.29 26-Oct-2008 jmc

sync SYNOPSIS and usage();


# 1.28 26-Oct-2008 ratchov

add minimal server capability to aucat(1). When started in server
mode, it listens on an unix socket and mixes/demultiplexes any number
of full-duplex streams, doing necessary format conversions and
resampling on the fly.

programs can use the new libsa(3) library to play and record audio.
The library provides a very simple API to connect to the audio server;
if aucat(1) isn't running, it uses the audio(4) driver transparently
instead.


# 1.27 25-Aug-2008 sobrado

minor tweak for removal of "[-d devel]" (change done in r1.19 and r1.21).

ok ratchov@


# 1.26 14-Aug-2008 ratchov

move all device related stuff from aucat.c to a new dev.c file.
The new dev_xxx() routines expose a "high level" self-contained
interface to the device. At initialization, the device is opened
and two chains of aproc structures are created:

* a playback chain that exposes a (initially) empty mix
aproc to which the rest of the code can attach new
streams to be played

* record chain that exposes a (initially) empty sub aproc
to which the rest of the code can attach new stream to
to record

The rest of the code, has just to use dev_attach() routine to
attach streams. While we're at it, add a ``devops'' structure
containing pointers to the device-specific routines. This will
allow later to add support for other type of device than the
Sun API.

Also, write the .wav headers in file_del(), so put all header
related data in the file strucuture. This allows to close() the
file, as soon as wpipe_xxx() aproc terminates. This will be
useful for the server, because it will need to close() descripts
of closed connections immediately.

add mix_pushzero() routine to fill the mixer with silence. It
will be used to avoid the mixer to underrun when there are no
input streams. Since we always have at least one input stream
there's no behaviour change.

ok jakemsr


Revision tags: OPENBSD_4_4_BASE
# 1.25 02-Jun-2008 ratchov

document latest changes: -d flag is replaced by AUCAT_DEBUG
environment variable, new -xX options

bits from eric, ok jakemsr


# 1.24 02-Jun-2008 ratchov

currently, we start the device as soon as write() blocks; it's better
to continue filling the play buffer until it's full. This way, the
play buffer has fewer chances to underrun when the device is started.

ok jakemsr


# 1.23 02-Jun-2008 ratchov

if aucat is suspended, then the kernel continues running the device
(produces silence) and later, once aucat is resumed the kernel starts
dropping samples, it will try to drop as many samples as silence was
produced. So suspending breaks the aucat process permanently.

workaround this by blocking the signals ie disabling suspending of aucat
from the tty. The long term solution would be to catch SIGCONT and to stop
the device, resync/refill buffers and restart the device. That's really a
lot of work...

ok jakemsr


# 1.22 02-Jun-2008 ratchov

allow the user to choose the aucat behaviour when underruns/overruns
occur on a per-stream basis, using -Xx flags. There are 3 possible
policies:

- ignore : ignores underruns/overruns, for instance, this mode
could be used for creating simple pipes with utilities; like in
your last cdio diff.

- sync : insert/discard samples in order to keep all streams in
sync, useful for multi-tracker-like apps and/or to sync
midi/video/whatever on audio streams (this was the previous
behaviour)

- error : if overruns/underruns occur, consider it as fatal error
and kill the corresponding stream (without disturbing others).
Useful, for reliable recordings (and/or debugging aucat
itself:).

ok jakemsr


# 1.21 02-Jun-2008 ratchov

(remove -d, second part) use strtonum() instead of sscanf() and inlined
checks

suggested and ok jakemsr


# 1.20 02-Jun-2008 ratchov

since underruns/overruns are handled outside devices, remove code that
check for xruns in devices.

ok jakemsr


# 1.19 02-Jun-2008 ratchov

(remove -d, first part) replace -d flag by AUCAT_DEBUG environment
variable. This eases turning debugging on/off when aucat is started by
another program.

"i like the idea" jakemsr


# 1.18 26-May-2008 jmc

put -q in the right place;


# 1.17 26-May-2008 jakemsr

add -q option which enables "quiet" operation.

ok ratchov@


# 1.16 23-May-2008 jmc

various fixes for aucat.1, and sync usage(); ok ratchov


# 1.15 23-May-2008 ratchov

add support for:
- recording, full-duplex operation
- format conversions and resampling on the fly
- mixing on the fly of multiple inputs of different formats
- up to 16 channels, simplistic "routing" of channel ranges
- more linear encodings (in raw and wav files)
the old behaviour is fully preserved if none of the new -i and -o
options are used.

code and fixes from jakemsr@ and eric@, suggestions by others.
ok "go ahead" deraadt@


# 1.14 13-Apr-2008 jakemsr

- remove code that was rendered pointless by the last commit
- minor knf

from Pierre Riteau


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE
# 1.13 20-Mar-2007 uwe

Support for playing simple RIFF files (.wav) from Simon Effenberg, savar
at schuldeigen de, with some tweaks from me. ok millert, tests ckuethe,
liked by some disliked by nobody, but he's not even a real user :)


Revision tags: OPENBSD_4_1_BASE
# 1.12 20-Dec-2006 steven

remove unneeded #include

ok mbalmer@ deraadt@


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.11 20-Jan-2005 jaredy

- knf
- check read/write/etc. calls for errors
- use constants instead of hardcoded pathnames
- better variable types
- add usage output

suggestions from various, ok ian


# 1.10 13-Jan-2005 ian

Close what you open: obvious fix, ok jared@, more to follow


Revision tags: OPENBSD_3_5_BASE OPENBSD_3_6_BASE
# 1.9 20-Oct-2003 jmc

typos from Jared Yanovich, slightly ammended;


Revision tags: OPENBSD_3_4_BASE
# 1.8 10-Jul-2003 david

add missing includes
ok deraadt@ tedu@


# 1.7 02-Jul-2003 deraadt

protos


# 1.6 10-Jun-2003 deraadt

mostly ansi cleanup; pval ok


Revision tags: OPENBSD_3_3_BASE
# 1.5 09-Dec-2002 millert

From Andrushock, s/sucess/success/g


Revision tags: OPENBSD_2_8_BASE OPENBSD_2_9_BASE OPENBSD_3_0_BASE OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.4 01-Jul-2000 millert

Add -f option to specify audio device, honor AUDIODEVICE environment vairable,
and some KNF.


Revision tags: OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE
# 1.3 05-Jan-1997 kstailey

minor tweaks; htonl() -> ntohl(); added SEE ALSO


# 1.2 03-Jan-1997 kstailey

removed unnecessary test


# 1.1 02-Jan-1997 kstailey

new utility for playing sound files


# 1.172 07-Nov-2017 ratchov

prefill with silence the buffer where cmap_copy() stores samples
rather than the file buffer. Fixes initialized data being treated as
recorded samples when resampling or format conversions are involved
but mono->stereo conversion is disabled.


# 1.171 03-Nov-2017 ratchov

Prefill rec buffer with silence. This is necessary because certain
channels don't get samples, for instance when mono->stereo conversion
is disabled.


Revision tags: OPENBSD_6_2_BASE
# 1.170 20-Jul-2017 ratchov

Fix integer overflow that would causes >12 hours files
to not properly relocate. Found with coverity.


Revision tags: OPENBSD_6_1_BASE
# 1.169 03-Jan-2017 ratchov

Assert we're not freeing buffers we didn't allocate (DEBUG mode).


# 1.168 03-Jan-2017 ratchov

Log play volume together with other play parameters.


# 1.167 30-Sep-2016 ratchov

Set the device rate to the calculated rate. Otherwise, the signal
may endup resampled twice.


# 1.166 30-Sep-2016 ratchov

Remove unnecessary includes. From Michael W. Bombardieri <mb at ii.net>.


# 1.165 27-Sep-2016 ratchov

Don't rely on the resampling code to calculate the number of samples
to process, as it may produce one extra sample (to handle accumulation
of fractional samples), which would cause access to one sample past
the end of the buffer and crash aucat. Fix this by limiting the number
of samples processed to a single block.

Found by and help from Michael W. Bombardieri <mb at ii.net>. Thanks.


Revision tags: OPENBSD_6_0_BASE
# 1.164 07-Jun-2016 ratchov

Add -g and -p options to control device and file start position.


# 1.163 07-Jun-2016 ratchov

Add resamp_getcnt() routine to calculate the exact number of samples
that would be consumed and produced by the sampler rate converter. Use
it to avoid partial samples that are not properly handled. Fixes
last samples of certain files causing aucat to abort.


# 1.162 02-Jun-2016 ratchov

remove forgotten debug printfs


# 1.161 02-Jun-2016 ratchov

set exit status to 1 if -n is used without -i and -o


# 1.160 31-May-2016 ratchov

Pass hr:min:sec:frame.cent to dev_mmcloc() directly and remove the
useless intermediate conversion from and to 2400-th of seconds.


# 1.159 31-May-2016 ratchov

To store MMC position, use samples (at device rate) as time units
instead of 2400-ths of second. No behaviour change.


# 1.158 28-May-2016 ratchov

Fix file block size rounding and ensure it's large enough to store a
full audio block.


# 1.157 27-May-2016 ratchov

When resampling, use the exact resampling factor instead of the ratio
between input and output block sizes. This was inherited from sndiod,
but is not required for files because they are continuous streams of
samples and do not need to be split in blocks of equal duration.

This change makes playback/recording rate match exactly the requested
sample rate.


# 1.156 27-May-2016 ratchov

Flush rec buffer if there's less than one block space left and refill
play buffer if there's less than one block of data left. This is the
correct condition in the general case. No behaviour change, as all
input/output is multiple of the block size.


# 1.155 27-May-2016 ratchov

Simplify slot_fill() and slot_flush(). No behaviour change


# 1.154 27-May-2016 ratchov

Make resamp_do() get the exact number input and output samples and
provide routines to calculate them. This way we don't rely on it
to calculate the bytes procuded/consumed anymore. No behaviour change.


# 1.153 26-May-2016 ratchov

Make format conversion routines return the number of frames
consumed from both input and output buffers. No behaviour change.


Revision tags: OPENBSD_5_9_BASE
# 1.152 29-Jan-2016 ratchov

Check that byte order is native only if we're using more than one
byte per sample, check that samples are lsb-aligned only if there's
padding. Fixes unsupported parameters not being detected if compiled
in 24-bit mode and sndiod is not running.


# 1.151 10-Jan-2016 ratchov

trailing spaces, fit in 80 columns


# 1.150 09-Nov-2015 mmcc

Remove xfree(). From Michael W. Bombardieri.

ok ratchov@


# 1.149 27-Aug-2015 ratchov

fix typos in comments and error messages


Revision tags: OPENBSD_5_8_BASE
# 1.148 16-May-2015 ratchov

Call slot_stop() after the play buffer is drained to properly cleanup
the slot. Fixes periodic glitches occurring after eof is reached, and
then playback restarted with a MMC-relocate and MMC-start.


Revision tags: OPENBSD_5_7_BASE
# 1.147 21-Jan-2015 jmc

fix macro breakage and update usage();


# 1.146 21-Jan-2015 ratchov

Simplify internals and rewrite file header parsing code:
- remove -M, -t, -w, -C, -x flags that don't make sense anymore
- make "-j off" the default (sndiod already does the job)
- don't limit the number of played/recorded files.
- add support for floating-point encoded files.
- add support for apple .aiff and sun/next .au files


# 1.145 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.144 18-Nov-2013 ratchov

remove unused variables


# 1.143 18-Nov-2013 ratchov

add missing prototypes


# 1.142 12-Nov-2013 ratchov

remove bogus legacy server-specific bits that cause confusion


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.141 03-Dec-2012 ratchov

Remove server capability description from the documentation and from
the usage string, and just say that for historic reasons aucat behaves
like sndiod.
with help from jmc@


# 1.140 23-Nov-2012 ratchov

Since there's no sndiod -> aucat link anymore, allow aucat to be run
in server mode even when not invoked as sndiod. This allows aucat and
the new sndiod daemon to live together in /usr/bin, which makes easy
to quickly compare sndiod and aucat (and spot possible regressions)
without having to modify /usr/bin.


# 1.139 27-Oct-2012 ratchov

don't stat & test sndio home directory since we never use it.


# 1.138 27-Oct-2012 ratchov

don't (re-)define SNDIO_USER and SNDIO_PRIO if they are already defined,
this way they can be defined at compilation time


Revision tags: OPENBSD_5_2_BASE
# 1.137 27-Jun-2012 ratchov

Uniformize error messages printing, use err() instead of fprintf()
when appropriate. From Michael W. Bombardieri <mb at ii.net>


# 1.136 19-Jun-2012 ratchov

Remove hidden options that are not needed any more,
from Remco <remco at d-compu.dyndns.org>. Thanks


# 1.135 23-May-2012 ratchov

Make the "default" string a valid device name that has the same effect
as NULL has. This will (hopefully) simplify ports where the user
passes the device string.


# 1.134 11-Apr-2012 ratchov

Use "unsigned int" rather than "unsigned". No object change.
suggested by deraadt@


Revision tags: OPENBSD_5_1_BASE
# 1.133 09-Feb-2012 ratchov

Crank the default buffer size to 160ms (ie 7680 frames at 48kHz).
Workarounds stuttering caused by something hogging the cpu in kernel
mode and preventing sndiod from running at time.
ok deraadt


# 1.132 26-Jan-2012 ratchov

Make the default rate 48kHz and the default block size 10ms. These
settings ensure video players and programs using MTC are smooth by
default. Thanks to all who tested.


# 1.131 10-Jan-2012 ratchov

When a sub-device is created (-s), check if a hardware
device was already created (-f) before trying to use the
default one, otherwise all sub-devices end up attached
to the default device. Noticed by peters at schwertfisch.de,
thanks!


# 1.130 09-Dec-2011 ratchov

fix usage string: remove -M from aucat and -l from aucat and sndiod
from jmc


# 1.129 09-Dec-2011 ratchov

add a new sndiod audio/midi daemon and hide aucat server-specific
options.


# 1.128 02-Dec-2011 ratchov

don't use a "server" flag, just check if we expose sub-devices


# 1.127 02-Dec-2011 ratchov

reuse midi-control code to implement midi thru boxes and remove
the old midithru implementation; less code, less bugs. As a side
effect, midi output doesn't implement running status "compression"
any more.


# 1.126 20-Nov-2011 ratchov

Move mmc/mtc and volume control bits from struct aproc to struct
dev. Allows volume settings to be saved while the device is kept
closed. Besides that, no behabiour changes.


# 1.125 15-Nov-2011 ratchov

Add a "device number" component in sndio(7) device names, allowing a
single aucat instance to handle all audio and MIDI services. Since
this partially breaks compatibility, this is a opportunitiy to fix few
other design mistakes (eg ':' being used by inet6, type name vs api
name confusion, etc..). This leads to the following names:

type[@hostname][,unit]/devnum[.option]

The device number is the minor device number for direct hardware
access (ie the 'N' in /dev/audioN). For aucat, this is the occurence
number of the -f (or -M) option.

There's a compatibility hook to keep old names working if only one
aucat server is running.


# 1.124 18-Oct-2011 ratchov

keep the device closed by default (when not used)


# 1.123 18-Oct-2011 ratchov

remove duplicate options in getopt() string


# 1.122 17-Oct-2011 ratchov

Remove midicat since aucat can now be used instead of midicat
with almost the same syntax (roughly an extra -M option).
Thru boxes are created with aucat, and corresponding MIDI port
names have the "aucat" prefix instead of "midithru". The old
device name will still work some time for backward compatibility.

ok deraadt


# 1.121 12-Oct-2011 jmc

- avoid line wrapping an example
- sort usage() for midicat
- tweak a bit of unclear wording

ok ratchov


# 1.120 12-Oct-2011 ratchov

Simplify and improve the way options are parsed and remove ~300 lines
of code that becomes unused. Few command line arguments changes are
required though:

- stream definitions (-ios) now must follow devices definitions they
are attached to (-fMn)

- the -n option is now a special "loopback" device and is thus used
like -f, eg it must precede streams

- in midicat, midi thru boxes are not created automatically anymore,
the new "-M" option must be used for that

- channel numbers (-Cc options) correspond always to channel numbers
of the hardware.

- the -u option isn't needed anymore

- increase the log verbosity so user errors are logged
without using -d

tested by many, help from jmc


Revision tags: OPENBSD_5_0_BASE
# 1.119 29-Jun-2011 ratchov

make -u flag per-device similarly to -a and -w


# 1.118 20-Jun-2011 ratchov

Make -aoff option apply to MIDI ports (-q) as well, ensuring the device
stays closed also if -q is used. As we're at it, add -a to midicat so
it behaves like aucat.


# 1.117 03-Jun-2011 ratchov

Allow device encoding to be set if -u is also used. Useful to degrade
quality to save bandwith when a remote audio device is used.


# 1.116 03-Jun-2011 ratchov

Reorganize the way command line options are parsed and stored.
No behaviour change.


# 1.115 26-May-2011 ratchov

add a new -w flag to control whether master volume is automatically
adjusted when new streams are connected and disconnected. Disabling
automatic volume adjustment makes sense if all streams are recorded
with properly lowered volumes.


# 1.114 10-May-2011 ratchov

fix option handling and enable TCP in midicat
from peters at schwertfisch.de, thanks!


# 1.113 28-Apr-2011 ratchov

Implement a new authentication method allowing aucat and midicat to
work over TCP, for instance, to expose the sound card of one machine
with other machines of the network.

The first client generates a 128-bit random number (aka the
session cookie), saves it in $HOME/.aucat_cookie and sends it to the
server. Successive clients load the cookie from $HOME/.aucat_cookie
and send it to the server but the server accepts only clients whose
cookie matches the session cookie. When all clients are gone, the
session is over, and another cookie could start a new session, and so
on.

TCP is enabled on the server with the new -L option, and on the client
side hostnames are specified with a new optional component in the
device name.

hints from damien, dlg and deraadt, tweaks from jmc


# 1.112 27-Apr-2011 ratchov

move amsg.h containing protocol defs from aucat side to libsndio side.

requested by deraadt


# 1.111 27-Apr-2011 deraadt

use more volatile sig_atomic_t in signal handlers
ok ratchov


# 1.110 19-Apr-2011 ratchov

don't hold a pointer to "listen" structure. Instead, when it must
be free()ed, iterate over the file_list, and find the structure
to free. This is safer and simpler. No behaviour change


# 1.109 16-Apr-2011 ratchov

for unix domain socket addresses use AUCAT_PATH and MIDICAT_PATH
macros instead of hardcoded strings. No object change


# 1.108 17-Mar-2011 ratchov

use a DEFAULT_RATE macro instead of hardcoding 44100, no object change


Revision tags: OPENBSD_4_9_BASE
# 1.107 05-Nov-2010 ratchov

unbreak compilation without DEBUG defined


# 1.106 21-Oct-2010 ratchov

use MODE_xxx and XRUN_xxx macros everywhere, and zap corresponding
AMSG_xxx macros, which in turns simplifies the code


# 1.105 08-Sep-2010 deraadt

aucat: getpwnam: No such file or directory
is not a proper error message.


# 1.104 20-Aug-2010 ratchov

polish the code: remove few unused #includes, add missing ones,
fix NULL vs 0, etc. No behaviour change.


# 1.103 19-Aug-2010 ratchov

fix NULL pointer deref in midicat option parsing when ``-'' is
used as filename


Revision tags: OPENBSD_4_8_BASE
# 1.102 31-Jul-2010 ratchov

remove 2 years old compatibility mode


# 1.101 10-Jul-2010 ratchov

In midi server mode, don't terminate the midi thru box if there are
no connections anymore.


# 1.100 10-Jul-2010 ratchov

fix parameter handling: don't try to open a ``default'' midi port
if no files are given on the command line


# 1.99 06-Jul-2010 jmc

various tweaks to previous; ok ratchov


# 1.98 06-Jul-2010 ratchov

Handle all streams the same way because there's no actual
difference between audio files and client connections. Clean up
the way command line options are handled and clarify this in the
manual page: stream parameters (-Ccehjmrtvx) must precede stream
definitions (-ios) and per-device parameters (-abz) and stream
definitions (-ios) must precede device definitions (-f). Since
there's no ``server'' and ``non-server'' modes anymore, make the
-l option just detach the process.

ok and help from jakemsr and jmc


# 1.97 29-Jun-2010 jakemsr

If the device isn't opened yet (as can be the case with '-a off'),
don't check if the midi control interface is idle
from ratchov


# 1.96 25-Jun-2010 ratchov

don't attempt to drain devices after they are destroyed, which
results in a use after free(). Catched by jakemsr@ with MALLOC_OPTIONS=J


# 1.95 20-Jun-2010 ratchov

fix midicat(1) device mode when only -i or only -o is used (ie
open the device input-only if -o is used, and output-only if -i is
used).


# 1.94 05-Jun-2010 ratchov

don't prime server buffers, because it's ugly and conceptually
complicated. Instead, request clients to provide enough samples
and start with buffers full.


# 1.93 05-Jun-2010 ratchov

Don't send the initial position as a clock tick with a negative delta.
Add a new ``initial position'' message which is simpler. No bahaviour
change, except effects of the protocol version crank.


# 1.92 04-Jun-2010 ratchov

Allow the audio device to be opened only while it's actually used.
This is necessary for uaudio devices, for instance to start aucat
before the device is plugged. Or to unplug a device whithout
having to restart aucat when another device is plugged. This is
controlled with the new -a option.

Allow multiple audio devices to be used concurently, i.e.
multiple ``-f devname'' options to be used; -f options must follow
per-device options, which is what we do for other options.


# 1.91 08-May-2010 ratchov

prime play buffers as soon as the device is opened, otherwise a
midi client could start it with empty buffers, in turn hurting
audio clients.


# 1.90 02-May-2010 ratchov

Clean up device handling code to clarify different initialization phases
and different device states. Split initialization in two phases:
first global variables are initialized then the audio hardware is opened.
Allow devices that don't support full-duplex to work in play-only or
record-only mode, even if ``-m play'' or ``-m rec'' are not specified.


# 1.89 02-May-2010 ratchov

Don't systematically fill with silence the mixer output. This
might result in extra samples being written to the device
when the mixer is closed.


# 1.88 24-Apr-2010 ratchov

if there are no files to play in legacy mode, print the usage message


# 1.87 24-Apr-2010 ratchov

rename ibuflist/obuflist -> ins/outs to cut long lines
from Thomas Pfaff, thanks


# 1.86 22-Apr-2010 ratchov

Allow multiple users to share the same aucat server. If aucat is
run by root, it binds a shared address to the socket, cranks the
process priority and drops privileges. sio_open(3) will try to
connect to the private socket first (if any), then to the shared
socket. Only one user may have connections to aucat at a given
time.

based on discussions with henning, pyr and others
ok jacek, deraadt


# 1.85 21-Apr-2010 ratchov

when creating the ``default'' subdevice, use last parameters of the
command line, instead of the device parameters.


# 1.84 21-Apr-2010 ratchov

adds the necessary bits to join channels (ex stereo->mono) or
expand channels (ex mono->stereo). It's switched on/off with
the ``-j'' option, (default is "on").


# 1.83 06-Apr-2010 ratchov

aucat (server):

- make the ``-m mode'' option per subdevice, allowing
subdevices to be play-only or rec-only even if the
server is full-duplex

- add ``monitoring'' mode (with ``-m mon''). This is a
record-only stream from which played streams can be
recorded (kind of ``record what you hear'').

- allow MIDI devices to be subscribed to the controlling
MIDI port of the server, ie what midicat does (with -f
option), but using the -q option.

- add flow control to the protocol, and serialize clock
ticks (sio_onmove() calls) and data chunks. This should
fix certain full-duplex programs, broken with ``magic''
block/buffer size combinations.

- enable 3 block latency which is the minimum theoretical.
Make -z and -b options correspond to device parameters.

- make sio_getcap(3) the same for aucat and sun backends,
ie return whatever is supported (``everything'' in the
aucat case, since everything is actulally supported).

aucat (player):

- enable ``-m mode'' option to select between monitoring
and recording when ``-o file'' is used.

- plug MIDI code to non-server codepath. The MIDI control
device is specified with the ``-q device'' option, as in
server mode.

- impliment lseek()'ing within files (controlled
through MIDI). Necessary to use aucat with a MIDI
sequencer.

midicat (thrubox):

- rename ``-f'' flag to ``-q'', so it has the
same name as in aucat (-f is still working)

ok jakemsr@, tweaks from jmc@


# 1.82 03-Apr-2010 ratchov

backout last change, committed by mistake, sorry...


# 1.81 03-Apr-2010 ratchov

doc fixes from jmc


Revision tags: OPENBSD_4_7_BASE
# 1.80 14-Jan-2010 ratchov

Use -C option to specify recorded channel range instead of -c which
is for played channels.

spotted by Jan Stary <hans at stare.cz>, thanks


# 1.79 11-Jan-2010 ratchov

When resuming from suspend, prime play buffers with silence,
otherwise the client would underrun by more than its own buffer
size, which currently is not allowed (causes the client to
deadlock).

Set the minimum client buffer size to two blocks. Single block
buffers don't work properly yet.

with help from jakemsr


# 1.78 10-Jan-2010 ratchov

Add traces to help tracking subtle timing bugs often causing
underuns and overruns; such bugs are hard to debug with ktrace
or a debugger. They are also handy to debug or monitor code
using aucat.

To enable traces, compile aucat with ``make DEBUG=-DDEBUG'' and
use the -d option multiple times.

ok jakemsr


# 1.77 05-Jan-2010 ratchov

Make the way .wav files are handled closer to the way sockets are
handled, which is conceptually simpler. No behaviour changes.


# 1.76 21-Nov-2009 ratchov

use strtonum(3) and strtol(3) instead of sscanf(3)
suggested by deraadt@


# 1.75 21-Nov-2009 deraadt

spelling


# 1.74 03-Nov-2009 ratchov

Allow any program using aucat to act as MMC slave and MTC master
transparently. Multiple audio applications can be started
synchronously from external software/hardware supporting the
standard Start/Stop/Relocate messages. The server clock is exposed
through MTC, allowing non-audio software/hardware to be
synchronized to audio applications.


# 1.73 27-Oct-2009 ratchov

add missing word in error message


# 1.72 10-Oct-2009 ratchov

don't use a references to the device file to check the current mode


# 1.71 10-Oct-2009 ratchov

make dev_done() common to audio and MIDI, and drop dev_thrudone()


# 1.70 10-Oct-2009 ratchov

use dev_done() for loopback devices too and remove dev_loopdone()


# 1.69 05-Oct-2009 ratchov

add -d flag. When started in server mode, if the -d flag is
used aucat and midicat don't daemonize and log on stderr.


# 1.68 27-Sep-2009 ratchov

remove all debug traces


# 1.67 26-Aug-2009 jmc

oops, forgot to update usage();


# 1.66 19-Aug-2009 ratchov

don't try to process -o arguments using the input file list, fixes
midicat crashing sometimes when -o is used


# 1.65 19-Aug-2009 ratchov

organize midi code like audio code, no functional change


# 1.64 17-Aug-2009 ratchov

allow midicat to take multiple -i and -o options


# 1.63 17-Aug-2009 ratchov

if one or more MIDI devices are specified in server mode, subscribe
them to the MIDI thru box


# 1.62 25-Jul-2009 ratchov

fix comments, remove no longer needed declaration, reorder includes
from Thomas Pfaff


# 1.61 25-Jul-2009 ratchov

Currently midi capable programs can control midi hardware, but
cannot cooperate with other programs. The aim of this change is
to allow any program to send midi data to other programs as they
were midi hardware. For instance, this change should solve the
longstanding problem of using a midi sequencer with software
synthesizers. More precisely:

- new midicat(1) utility (actually hardlink to aucat(1)).
it creates software midi thru boxes, allowing programs
to send midi messages to other programs as they were
midi(4) hardware.

- new midi api in libsndio (see mio_open(3)), to access
midi(4) devices and midicat(1) sockets in a uniform way.

- new device naming scheme <service>:<unit>[.<option>],
common to audio and midi.

- new sndio(7) manual describing concepts and naming

The current audio device naming still works, but people having
scripts or configuration files containing device names could read
the sndio(7) man page and slowly start updating device names.

discussed with jakemsr@ and deraadt@, help form jmc@


Revision tags: OPENBSD_4_6_BASE
# 1.60 27-Apr-2009 ratchov

make -v option apply also to implicit default socket


# 1.59 17-Mar-2009 jmc

- rename the arg to -b "nframes" and sync usage()
- grammar fix for the server section

from Thomas Pfaff
ok ratchov


Revision tags: OPENBSD_4_5_BASE
# 1.58 06-Feb-2009 ratchov

move error messages reported to user into main()


# 1.57 04-Feb-2009 ratchov

if there are too many connections, stop acceping new ones rather
than exit()ing with ``too many open files'' fatal error


# 1.56 04-Feb-2009 ratchov

daemonize when in server mode, suggested by many
ok jakemsr


# 1.55 03-Feb-2009 ratchov

in server mode, create /tmp/aucat-userid/ directory with
permissions 0700, and create sockets in it. This prevents
one local user to eavesdrop or disturb audio programs of
other users.

if you're using the ``-s socket'' option with an absolute
path as argument, please update it to use a socket name.

requested by many, bits from jakemsr and otto
ok jakemsr


# 1.54 25-Jan-2009 ratchov

don't set malloc_options to not interfer with /etc/malloc.conf
requested and explained by fgsch@, henning@, otto@


# 1.53 23-Jan-2009 ratchov

catch SIGHUP and SIGTERM too, to ensure that recorded files are
properly closed and sockets are cleaned up.


# 1.52 23-Jan-2009 ratchov

spacing + typo fix from Matt Jibson <matt.jibson(at)gmail.com>


# 1.51 10-Jan-2009 ratchov

add "loopback" mode in which input is connected to the output.
This is useful to mix, demultiplex, resample or reencode audio
files off-line.
tweak + ok jakemsr


# 1.50 29-Dec-2008 ratchov

make the code "more correct": add reference counters to aproc
structures so we can keep a pointer to the device. Beside some
simplifications, this allows to easily handle the situation
where the audio device desappears. No change in the behaviour.


# 1.49 26-Dec-2008 ratchov

*** empty log message ***


# 1.48 07-Dec-2008 ratchov

When resampling, don't require the sample frequency to be an integer.
This removes the arithmetic constraint between the sample frequency
and the block size and all the associated code. Now aucat can work
in server mode with any block size.
ok jakemsr


# 1.47 23-Nov-2008 ratchov

Use file input parameter as default device output and file output parameters
as default device input. That's what the man page says


# 1.46 20-Nov-2008 ratchov

take into account socket parameters into calculation of device
parameters; without this change aucat uses the device default
parameters and they are not necessarily usable for multi-stream
mode.


# 1.45 17-Nov-2008 jmc

- rename -s arg name to "socket"
- condense EXAMPLES into a single screenful

ok ratchov


# 1.44 17-Nov-2008 jmc

tweak previous man page changes, and sync usage();


# 1.43 17-Nov-2008 ratchov

allow aucat to run as server in play-only and record-only mode, so
it can be used on play-only, record-only and half-duplex devices.
ok jakemsr


# 1.42 16-Nov-2008 ratchov

allow aucat to listen on multiple sockets. Each socket carries its
channel and volume settings allowing multiple configuration to
coexist. Mostly useful for envy(4)-like devices, but can be used
to force different apps to use different settings.


# 1.41 16-Nov-2008 ratchov

add ``template'' parameters to sockets rather than using device
parameters. No functional change.


# 1.40 16-Nov-2008 ratchov

Make clients inherit the volume parameter when the -v option is used in
server mode. It gives the maximum volume a client may have. This wastes
dynamic range, but allows volume to stay constant when other clients
connect or disconnect.


# 1.39 12-Nov-2008 ratchov

when destroying the device, mark the mixer and the demultiplexer
for ``AUTOQUIT'' first, and then call file_eof() and friends.
fixes crashes sthen@ noticed on zaurus


# 1.38 11-Nov-2008 ratchov

if the device disappear the mixer and demultiplexer chains are
destroyed. If this happen then warn and exit, rather than crashing.


# 1.37 11-Nov-2008 ratchov

termninate non-server aucat when it's idle rather that using
the hackish {MIX,SUB}_AUTOQUIT features. This way we don't
have to care about possible references on destroyed objects.


# 1.36 11-Nov-2008 ratchov

when not in server mode, aucat terminates automatically using
the {MIX,SUB}_AUTAQUIT features. In this case we're not
allowed to directly touch dev_mix and dev_sub because they
can disappar. So disable the ``suspend'' and ``quit'' bits
when {MIX,SUB}_AUTAQUIT are used.


# 1.35 10-Nov-2008 ratchov

add a per-stream ``soft volume'' knob and the corresponding -v option.
The code will be useful later for the volume knob in the sndio API.


# 1.34 09-Nov-2008 ratchov

make aucat(1) stop automatically the audio(1) device if it's idle. This
way, when in server mode, it consumes no CPU if there are no clients.
Later, this will allow to start aucat(1) at session or system startup.


# 1.33 08-Nov-2008 ratchov

set the default device to /dev/audio for legacy mode.
From Thomas Pfaff <tpfaff _at_ agderlink.no> and tweaks from me


# 1.32 07-Nov-2008 ratchov

expose the block size in the sndio API by making par->round writable
and thus remove the ugly rate <-> block-size table from
sio_setpar(3). Handle the block size negociation in aucat(1), since
it has few constrains the code is overally simpler.
ok jakemsr@, major crank suggested by deraadt@


# 1.31 03-Nov-2008 ratchov

A small optimization: handle most N-channel <-> M-channel conversions
inside the mixer and the demultiplexer. This way, aucat will not
trigger the heavy conversion code when only channel conversions are
required. Cuts ~50% of the CPU usage on envy(4) devices, can improve
surround 4.0, 5.1 and 7.1 capable devices. No functionnal change.


# 1.30 26-Oct-2008 ratchov

zap `q' from the getopts string too,
requested by jmc@


# 1.29 26-Oct-2008 jmc

sync SYNOPSIS and usage();


# 1.28 26-Oct-2008 ratchov

add minimal server capability to aucat(1). When started in server
mode, it listens on an unix socket and mixes/demultiplexes any number
of full-duplex streams, doing necessary format conversions and
resampling on the fly.

programs can use the new libsa(3) library to play and record audio.
The library provides a very simple API to connect to the audio server;
if aucat(1) isn't running, it uses the audio(4) driver transparently
instead.


# 1.27 25-Aug-2008 sobrado

minor tweak for removal of "[-d devel]" (change done in r1.19 and r1.21).

ok ratchov@


# 1.26 14-Aug-2008 ratchov

move all device related stuff from aucat.c to a new dev.c file.
The new dev_xxx() routines expose a "high level" self-contained
interface to the device. At initialization, the device is opened
and two chains of aproc structures are created:

* a playback chain that exposes a (initially) empty mix
aproc to which the rest of the code can attach new
streams to be played

* record chain that exposes a (initially) empty sub aproc
to which the rest of the code can attach new stream to
to record

The rest of the code, has just to use dev_attach() routine to
attach streams. While we're at it, add a ``devops'' structure
containing pointers to the device-specific routines. This will
allow later to add support for other type of device than the
Sun API.

Also, write the .wav headers in file_del(), so put all header
related data in the file strucuture. This allows to close() the
file, as soon as wpipe_xxx() aproc terminates. This will be
useful for the server, because it will need to close() descripts
of closed connections immediately.

add mix_pushzero() routine to fill the mixer with silence. It
will be used to avoid the mixer to underrun when there are no
input streams. Since we always have at least one input stream
there's no behaviour change.

ok jakemsr


Revision tags: OPENBSD_4_4_BASE
# 1.25 02-Jun-2008 ratchov

document latest changes: -d flag is replaced by AUCAT_DEBUG
environment variable, new -xX options

bits from eric, ok jakemsr


# 1.24 02-Jun-2008 ratchov

currently, we start the device as soon as write() blocks; it's better
to continue filling the play buffer until it's full. This way, the
play buffer has fewer chances to underrun when the device is started.

ok jakemsr


# 1.23 02-Jun-2008 ratchov

if aucat is suspended, then the kernel continues running the device
(produces silence) and later, once aucat is resumed the kernel starts
dropping samples, it will try to drop as many samples as silence was
produced. So suspending breaks the aucat process permanently.

workaround this by blocking the signals ie disabling suspending of aucat
from the tty. The long term solution would be to catch SIGCONT and to stop
the device, resync/refill buffers and restart the device. That's really a
lot of work...

ok jakemsr


# 1.22 02-Jun-2008 ratchov

allow the user to choose the aucat behaviour when underruns/overruns
occur on a per-stream basis, using -Xx flags. There are 3 possible
policies:

- ignore : ignores underruns/overruns, for instance, this mode
could be used for creating simple pipes with utilities; like in
your last cdio diff.

- sync : insert/discard samples in order to keep all streams in
sync, useful for multi-tracker-like apps and/or to sync
midi/video/whatever on audio streams (this was the previous
behaviour)

- error : if overruns/underruns occur, consider it as fatal error
and kill the corresponding stream (without disturbing others).
Useful, for reliable recordings (and/or debugging aucat
itself:).

ok jakemsr


# 1.21 02-Jun-2008 ratchov

(remove -d, second part) use strtonum() instead of sscanf() and inlined
checks

suggested and ok jakemsr


# 1.20 02-Jun-2008 ratchov

since underruns/overruns are handled outside devices, remove code that
check for xruns in devices.

ok jakemsr


# 1.19 02-Jun-2008 ratchov

(remove -d, first part) replace -d flag by AUCAT_DEBUG environment
variable. This eases turning debugging on/off when aucat is started by
another program.

"i like the idea" jakemsr


# 1.18 26-May-2008 jmc

put -q in the right place;


# 1.17 26-May-2008 jakemsr

add -q option which enables "quiet" operation.

ok ratchov@


# 1.16 23-May-2008 jmc

various fixes for aucat.1, and sync usage(); ok ratchov


# 1.15 23-May-2008 ratchov

add support for:
- recording, full-duplex operation
- format conversions and resampling on the fly
- mixing on the fly of multiple inputs of different formats
- up to 16 channels, simplistic "routing" of channel ranges
- more linear encodings (in raw and wav files)
the old behaviour is fully preserved if none of the new -i and -o
options are used.

code and fixes from jakemsr@ and eric@, suggestions by others.
ok "go ahead" deraadt@


# 1.14 13-Apr-2008 jakemsr

- remove code that was rendered pointless by the last commit
- minor knf

from Pierre Riteau


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE
# 1.13 20-Mar-2007 uwe

Support for playing simple RIFF files (.wav) from Simon Effenberg, savar
at schuldeigen de, with some tweaks from me. ok millert, tests ckuethe,
liked by some disliked by nobody, but he's not even a real user :)


Revision tags: OPENBSD_4_1_BASE
# 1.12 20-Dec-2006 steven

remove unneeded #include

ok mbalmer@ deraadt@


Revision tags: OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.11 20-Jan-2005 jaredy

- knf
- check read/write/etc. calls for errors
- use constants instead of hardcoded pathnames
- better variable types
- add usage output

suggestions from various, ok ian


# 1.10 13-Jan-2005 ian

Close what you open: obvious fix, ok jared@, more to follow


Revision tags: OPENBSD_3_5_BASE OPENBSD_3_6_BASE
# 1.9 20-Oct-2003 jmc

typos from Jared Yanovich, slightly ammended;


Revision tags: OPENBSD_3_4_BASE
# 1.8 10-Jul-2003 david

add missing includes
ok deraadt@ tedu@


# 1.7 02-Jul-2003 deraadt

protos


# 1.6 10-Jun-2003 deraadt

mostly ansi cleanup; pval ok


Revision tags: OPENBSD_3_3_BASE
# 1.5 09-Dec-2002 millert

From Andrushock, s/sucess/success/g


Revision tags: OPENBSD_2_8_BASE OPENBSD_2_9_BASE OPENBSD_3_0_BASE OPENBSD_3_1_BASE OPENBSD_3_2_BASE
# 1.4 01-Jul-2000 millert

Add -f option to specify audio device, honor AUDIODEVICE environment vairable,
and some KNF.


Revision tags: OPENBSD_2_1_BASE OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE
# 1.3 05-Jan-1997 kstailey

minor tweaks; htonl() -> ntohl(); added SEE ALSO


# 1.2 03-Jan-1997 kstailey

removed unnecessary test


# 1.1 02-Jan-1997 kstailey

new utility for playing sound files