Realtime I/O on Linux

Under Linux, the default portaudio/portmidi settings will result in
higher latency than that which can be achieved using ALSA and/or
JACK (see a separate manual section on this).
The portaudio/portmidi plugins are audio and MIDI servers, which provide an
interface to the ALSA drivers, in a manner which is in some respects
similar but fundamentally different from that provided by JACK.

Using ALSA

The highest level of control and the lowest possible level of latency
are to be achieved using the ALSA plugins in combination with the --sched
flag. Using --sched requires that Csound be run as the root user, which may be impossible or undesirable in some
circumstances.

The ALSA plugins require the "name" of a "card" and a "device". Unless
you have named your "cards" in ~/.asoundrc (or /etc/asound.conf), the
"names" will actually be numbers. In order to obtain a list of the
possible configurations, use the command line utilities "aplay",
"arecord" and "amidi". These utilities are included with most Linux
distros, or can be downloaded and built from source here:

On every boot, the soundcard may have a different hardware order number, especially when there are more soundcards in the system. This can be awkward as every time you have to set the right number again. You can assign a fixed order by adding some lines to /etc/modprobe.d/alsa-base-conf, for example for a card with the ice1712 chip :

# ALSA module ordering for soundcard
options snd slots=snd_ice1712

Audio Output

Running the following command:

aplay -l

will give you a list of the audio playback devices available on your
system. Typically this list will look something like:

If you have more than one card on your system, or if there is more
than one device on your card, the list will of course be more
complicated, however in all cases the information that is pertinent is
the number/name of the card/device. In order to use the above
soundcard for audio output, the following flag would be added to the
Csound command line, ~/.csoundrc, or the <CsOptions>section of a
CSD:

-+rtaudio=alsa -o dac

Output with dmix

If you would like to use Csound with dmix and your soundcard does not
support hardware mixing of audio streams, special care is needed in setting
up of software (-b) and hardware (-B) buffers. If you get a message from
Csound's ALSA driver that looks like the following:

ALSA: -B 8192 not allowed on this device; use 7526 instead

there is a good chance that you may be using dmix. If you are using dmix,
the -b and -B settings of Csound must be synced the period_size and
buffer_size of dmix respectively, using a ratio of the sr for the Csound
project to the sample rate that dmix is set up to. The following formula
will determine what settings to use for Csound given the settings of dmix:

For example, if dmix is set to 48000 sample rate, a period_size of 1024,
and a buffer_size of 8192, when running a Csound project with sr=48000,
the settings for buffers should be "-b 1024 -B8192". If the sr=24000,
the settings for buffers should be "-b 512 -B4096".

Because of this relationship, if a Csound project's sr does not evenly
divide into the sample_rate used by dmix, then it may be difficult if not
imposible to set the correct setting for -b and -B due to rounding errors.
It is suggested then that if you are using sample rates different than
what your setting is for dmix, then you may want to configure dmix to
have a period_size and buffer_size that can be evenly divided by the ratio
between the csound sr and dmix sample_rate. For example, to run a project
with sr=16000, the following dmix setting:

For most soundcards that this affects, the default sample rate for the
card will be 48000 and the defaults for dmix will be 1024 and 8192.

Audio Input

Typically the same card will be used for both input and output, so to
continue using the foregoing example, the flag:

-i adc:hw:0,0

would be added for audio input from Card 0 Device 0. To use the
default card employ one of the following flags, with the forementioned
warning that this will not necessarily work:

-i adc

If you wish to use a different card or device for input, running the
following utility from the command line will provide a list of input
devices:

arecord -l

If, by way of an example, you wanted to use a USB audio interface,
which is the second "card" in your system, for output, but wanted to
use your internal soundcard, the first card in your setup, for input,
you would put the following flags somewhere useful:

-+rtaudio=alsa -i adc:hw:0,0 -o dac:hw:1,0

If you wanted to use the second device on your USB interface, to send
audio to a specific channel, for instance, you would use the following
flags:

-+rtaudio=alsa -i adc:hw:0,0 -o dac:hw:1,1

MIDI

2 Midi drivers are available:

Raw Midi.

Alsa Sequencer (since version 5.18).

MIDI Input (Raw Midi driver)

In order to enable your orchestra to receive MIDI input you can use VirMIDI or MIDIThru, whichever you prefer. Setting up these
virtual MIDI ports is a topic that has been covered extensively
elsewhere, see
The Linux MIDI how-to
or browse your distro's documentation or the ALSA documentation for
instructions on how to install and configure either VirMIDI or
MIDIThru (seqdummy). Once you have done so run:

amidi -l

for a list of available devices. Typically this will look something
like the following:

In the following example, the USB keyboard which is listed above as
client 24 will be connected to a Csound synthesizer which is listening
on the first VirMIDI port. The keyboard has three output ports. The
first (24:0) transmits messages received on the MIDI in port, the
second (24:1) transmits keyboard and controller messages, and the
third (24:2) transmits system exclusive messages. The following
command connects the second port of the keyboard to the Csound
synthesizer:

aconnect 24:1 20:0

Remember that Csound acts as a raw MIDI device and is not an ALSA sequencer client. This means that Csound will not appear in MIDI device listings and will not be available for use directly with aconnect, so you must connect to a virtual device (like 'virtual raw MIDI' or 'MIDI through') for persistent connections, or conect directly to the destination using command line flags.

MIDI Output (Raw Midi driver)

Csound can be connected to any device which shows up on the ALSA
sequencer list of output ports, obtained by "amidi -l" as above. In
order to connect a Csound synthesizer to the MIDI out port of the
keyboard listed above, the following flag would be used:

-Qhw:2,0,0

MIDI Input and Output (Midi Sequencer driver)

This driver is to be preferred over the Raw Midi driver. It has these advantages:

Multiple concurrent access.

Scheduled by priority queues.

Real-time event dispatching i.e., the role of the Midi Sequencer is to deliver events at the right time (sequence) to the right destination (device).

The following command will call the Midi Sequencer. Here it listens to midi port 20. The midi output port is also 20:

-+rtmidi=alsaseq -M20 -Q20

Csound will automatically create its own ALSA sequencer port. For a list of available devices, use the following command:

Scheduling

If you are able to run Csound as the root user, using the "--sched"
flag will dramatically improve realtime performance, when using ALSA,
however you may hang your system if you do something stupid. DO NOT
use "--sched" if you are using JACK for audio output. JACK controls
scheduling for the audio applications connected to it, and also tries
to run at the highest possible priority. If the "--sched" flag is
used, Csound and JACK will be competing rather than cooperating,
resulting in extremely poor performance.

Using Pulseaudio

Support for Pulseaudio was added in Csound 5.09. You can specify the following settings:

Sink names: it's possible to use a number instead of the full name, so
-odac:1 would select your second device (count starts at 0).

Server name: it's possible to connect to a specific server by using
-+server=<server_string>
where server_string is a name of a server or a more
complex server selection string (see pulseaudio.org on
server strings). This should be network transparent and should allow connections to remote machines.

Stream names: it is possible to label the streams generated by csound, by
using
-+output_stream=<stream-name>
and
-+input_stream=<stream-name>