MIO_OPEN(3)
๐ MIO_OPEN(3) โ Library Functions Manual ๐
okay bestie, so this is the page for the sndio MIDI stream functions, and I'm gonna hold your hand through every single one of them. grab a snack. ๐
โจ NAME โจ
mio_open, mio_close, mio_read, mio_write, mio_nfds, mio_pollfd, mio_revents, mio_eof โ the sndio interface to MIDI streams ๐น
๐ SYNOPSIS ๐
#include <sndio.h>
struct mio_hdl *
mio_open(const char *name, unsigned int mode, int nbio_flag);
void
mio_close(struct mio_hdl *hdl);
size_t
mio_read(struct mio_hdl *hdl, void *addr, size_t nbytes);
size_t
mio_write(struct mio_hdl *hdl, const void *addr, size_t nbytes);
int
mio_nfds(struct mio_hdl *hdl);
int
mio_pollfd(struct mio_hdl *hdl, struct pollfd *pfd, int events);
int
mio_revents(struct mio_hdl *hdl, struct pollfd *pfd);
int
mio_eof(struct mio_hdl *hdl);
๐ DESCRIPTION ๐
The sndio library lets your user processes get at midi(4) hardware and sndiod(8) MIDI thru boxes and control ports โ all in one uniform way. one API, every MIDI thing, slay. ๐ฅ
๐ Opening and closing a MIDI stream ๐
First things first: your app must call mio_open() to get a handle representing the brand-new stream. that handle then gets passed as the hdl argument to basically every other function, it's giving main character energy fr fr.
The name parameter is the device string discussed in sndio(7). If your program is using a single device and isn't providing a device chooser, set it to MIO_PORTANY so the user can pick it with the MIDIDEVICE environment variable. let them choose, it's their moment. ๐
The mode parameter sets the direction of the stream. the supported ones are:
MIO_OUTโ the stream is output-only; whatever you write to it gets sent to the hardware or to other programs. ๐คMIO_INโ the stream is input-only; data coming in from the hardware or other programs has to be read out of the stream. ๐ฅMIO_IN | MIO_OUTโ the stream sends and receives data. use this one instead of callingmio_open()twice, bestie, seriously, don't be extra. โจ
If the nbio_flag argument is true (i.e. non-zero), then mio_read() and mio_write() (see below ๐) will be non-blocking.
The mio_close() function closes the stream and frees all allocated resources tied to the libsndio handle. clean up after yourself, icon behaviour. ๐ซถ
๐ฆ Sending and receiving data ๐ฆ
When input mode is selected, you must call mio_read() to retrieve the received data โ and you gotta call it often enough that the internal buffers don't overrun. it stores at most nbytes bytes at the addr location. Unless the nbio_flag flag is set, it will block until data becomes available, and it returns zero only on error. ๐ญ
When output mode is selected, mio_write() can be called to hand over data to transmit. Unless the nbio_flag is set, mio_write() will block until the requested amount of data is written. patient king. ๐
๐ธ Non-blocking mode operation ๐ธ
If nbio_flag is set on mio_open(), then mio_read() and mio_write() will never block; if there's no data available, they return zero immediately. no waiting, no drama.
To avoid busy loops when you're in non-blocking mode, you can use the poll(2) system call to check whether data can be read from or written to the stream. The mio_pollfd() function prepares the array pfd of pollfd structures for use with poll(2). The optimal size of that pfd array โ which you, the caller, must pre-allocate โ is provided by the mio_nfds() function. do the math first, okay? ๐งฎ
poll(2) will sleep until any of the events requested with mio_pollfd() have occurred. Events are represented as a bit-mask of the POLLIN and POLLOUT constants. The events that actually woke poll(2) up can be obtained with the mio_revents() function. If POLLIN is set, mio_read() can be called without blocking. If POLLOUT is set, mio_write() can be called without blocking. POLLHUP may be set if an error occurs โ even if you never requested it with mio_pollfd(). sneaky. ๐
๐ Error handling ๐
Errors related to the MIDI subsystem (like hardware errors or dropped connections) and programming errors (such as calling mio_read() on a play-only stream) are considered fatal. Once an error occurs, all functions that take a mio_hdl argument stop working (i.e. they always return 0) โ with the exception of mio_close() and mio_eof(), which are the only survivors. it's giving apocalypse. ๐ญ
โ ๏ธ fr fr: after a fatal error, don't expect anything else to work.mio_close()andmio_eof()are literally the only ones still picking up the phone. ๐
โญ RETURN VALUES โญ
mio_open()โ returns the newly created handle on success, orNULLon failure. ๐mio_pollfd()โ returns the number ofpollfdstructures filled.mio_nfds()โ returns the number ofpollfdstructures the caller must preallocate so thatmio_pollfd()will never overrun.mio_revents()โ returns the bit-mask set bypoll(2)in thepfdarray ofpollfdstructures.mio_read()andmio_write()โ return the number of bytes transferred.mio_eof()โ returns0if there's no pending error, and a non-zero value if there's an error.
๐ ENVIRONMENT ๐
SNDIO_DEBUGโ the debug level: may be a value between0and2. turn it up when things get weird. ๐
๐ SEE ALSO ๐
poll(2), midi(4), sndio(7), sndiod(8)
๐ HISTORY ๐
These functions first appeared in OpenBSD 4.7. vintage. โจ
๐ AUTHORS ๐
Alexandre Ratchov <ratchov@openbsd.org>
and that's the whole page bestie, you ate it up ๐
no cap, you're basically a MIDI stream expert now. this was the Debian build of MIO_OPEN(3), tagged $Mdocdate$ in the footer โ mwah, go write something iconic with it ๐ซถ๐๐
๐ฆ where this page comes from
- alpine 3.24: sndio-doc 1.10.0-r4 ยท ISC
- arch rolling: sndio 1.10.0-1 ยท ISC
- debian bookworm: libsndio-dev 1.9.0-0.3+b2 ยท ISC
- debian trixie: libsndio-dev 1.10.0-0.1 ยท ISC
- nixos 26.05: sndio 1.10.0
- nixpkgs unstable: sndio 1.10.0
- opensuse-leap 16.0: sndio-devel 1.10.0-bp160.1.7 ยท ISC
- opensuse-leap 16.0: sndio-devel 1.10.0-bp160.1.7 ยท ISC
- opensuse-leap 16.0: sndio-devel 1.10.0-bp160.1.7 ยท ISC
- opensuse-leap 16.0: sndio-devel 1.10.0-bp160.1.7 ยท ISC
- ubuntu jammy: libsndio-dev 1.8.1-1.1 ยท ISC
- ubuntu noble: libsndio-dev 1.9.0-0.3build3 ยท ISC
- ubuntu resolute: libsndio-dev 1.10.0-0.2 ยท ISC
- ubuntu stonking: libsndio-dev 1.10.0-0.2 ยท ISC
- Copyright (c) 2007 Alexandre Ratchov <alex@caoua.org>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms