Debian Codegraph @

overview/realtimelinux.xml

raw symbols SCIP
1 <section id="RealTimeLinux">
2 <title>Realtime I/O on Linux</title>
3 <!-- Most of this file was contributed by Jonathan Murphy on July 2006-->
4 <para>
5 Under Linux, the default portaudio/portmidi settings will result in
6 higher latency than that which can be achieved using ALSA and/or
7 JACK (see a separate manual section on this).
8 The portaudio/portmidi plugins are audio and MIDI servers, which provide an
9 interface to the ALSA drivers, in a manner which is in some respects
10 similar but fundamentally different from that provided by JACK.
11 </para>
12
13 <note>If you are able to run Csound as the root user, using the "--sched"
14 flag will dramatically improve realtime performance, when using ALSA,
15 however you may hang your system if you do something stupid. DO NOT
16 use "--sched" if you are using JACK for audio output. JACK controls
17 scheduling for the audio applications connected to it, and also tries
18 to run at the highest possible priority. If the "--sched" flag is
19 used, Csound and JACK will be competing rather than cooperating,
20 resulting in extremely poor performance.
21 </note>
22
23 <bridgehead>Using ALSA</bridgehead>
24 <para>
25 The highest level of control and the lowest possible level of latency
26 are to be achieved using the ALSA plugins in combination with the <link linkend="IndexCatMinusSched"><citetitle>--sched</citetitle></link>
27 flag. Using <link linkend="IndexCatMinusSched"><citetitle>--sched</citetitle></link> requires that Csound be run as the root user, which may be impossible or undesirable in some
28 circumstances.
29 </para>
30
31 <para>
32 The ALSA plugins require the "name" of a "card" and a "device". Unless
33 you have named your "cards" in ~/.asoundrc (or /etc/asound.conf), the
34 "names" will actually be numbers. In order to obtain a list of the
35 possible configurations, use the command line utilities "aplay",
36 "arecord" and "amidi". These utilities are included with most Linux
37 distros, or can be downloaded and built from source here:
38 </para>
39 <para><ulink url="ftp://ftp.alsa-project.org/pub/utils/">ftp://ftp.alsa-project.org/pub/utils/</ulink></para>
40
41 <note>
42 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 :
43 <literallayout>
44 # ALSA module ordering for soundcard
45 options snd slots=snd_ice1712
46 </literallayout>
47 </note>
48
49 <bridgehead>Audio Output</bridgehead>
50 <para>Running the following command:</para>
51 <screen>
52 aplay -l</screen>
53 <para>will give you a list of the audio playback devices available on your
54 system. Typically this list will look something like:
55 </para>
56 <literallayout>[....]
57 **** List of PLAYBACK Hardware Devices ****
58 card 0: A5451 [ALI 5451], device 0: ALI 5451 [ALI 5451]
59 [....]
60 </literallayout>
61 <para>If you have more than one card on your system, or if there is more
62 than one device on your card, the list will of course be more
63 complicated, however in all cases the information that is pertinent is
64 the number/name of the card/device. In order to use the above
65 soundcard for audio output, the following flag would be added to the
66 Csound command line, ~/.csoundrc, or the &lt;CsOptions&gt;section of a
67 CSD:
68 </para>
69 <screen>
70 -+rtaudio=alsa -o dac</screen>
71
72 <bridgehead>Output with dmix</bridgehead>
73 <para>
74 If you would like to use Csound with dmix and your soundcard does not
75 support hardware mixing of audio streams, special care is needed in setting
76 up of software (-b) and hardware (-B) buffers. If you get a message from
77 Csound's ALSA driver that looks like the following:
78 </para>
79 <screen>
80 ALSA: -B 8192 not allowed on this device; use 7526 instead</screen>
81 <para>
82 there is a good chance that you may be using dmix. If you are using dmix,
83 the -b and -B settings of Csound must be synced the period_size and
84 buffer_size of dmix respectively, using a ratio of the sr for the Csound
85 project to the sample rate that dmix is set up to. The following formula
86 will determine what settings to use for Csound given the settings of dmix:
87 </para>
88 <screen>
89 -b = (csound_sr/dmix_sample_rate) * dmix_period_size
90 -B = (csound_sr/dmix_sample_rate) * dmix_buffer_size</screen>
91 <para>
92 For example, if dmix is set to 48000 sample rate, a period_size of 1024,
93 and a buffer_size of 8192, when running a Csound project with sr=48000,
94 the settings for buffers should be "-b 1024 -B8192". If the sr=24000,
95 the settings for buffers should be "-b 512 -B4096".
96 </para>
97 <para>
98 Because of this relationship, if a Csound project's sr does not evenly
99 divide into the sample_rate used by dmix, then it may be difficult if not
100 imposible to set the correct setting for -b and -B due to rounding errors.
101 It is suggested then that if you are using sample rates different than
102 what your setting is for dmix, then you may want to configure dmix to
103 have a period_size and buffer_size that can be evenly divided by the ratio
104 between the csound sr and dmix sample_rate. For example, to run a project
105 with sr=16000, the following dmix setting:
106 </para>
107 <screen>
108 pcm.amix {
109 type dmix
110 ipc_key 50557
111 slave {
112 pcm "hw:0,0"
113 period_time 0
114 #period_size 1024
115 #buffer_size 8192
116 period_size 1536
117 buffer_size 12288
118 }
119 bindings {
120 0 0
121 1 1
122 }
123 }
124
125 # route ALSA software through pcm.amix
126 pcm.!default {
127 type plug
128 slave.pcm "amix"
129 }</screen>
130 <para> with period_size 1536 and buffer_size 12288 will divide nicely by 3
131 (the ratio of the csound sr to the dmix sample_rate) to get "-b 512 -B4096"
132 ( (16000/48000) * 1536 = 512, (16000/48000) * 12288 = 4096 ).
133
134 </para>
135 <note>
136 <title>Note</title>
137 <para>
138 For most soundcards that this affects, the default sample rate for the
139 card will be 48000 and the defaults for dmix will be 1024 and 8192.
140 </para>
141 </note>
142
143 <bridgehead>Audio Input</bridgehead>
144 <para>
145 Typically the same card will be used for both input and output, so to
146 continue using the foregoing example, the flag:
147 </para>
148 <screen>
149 -i adc:hw:0,0</screen>
150 <para>would be added for audio input from Card 0 Device 0. To use the
151 default card employ one of the following flags, with the forementioned
152 warning that this will not necessarily work:
153 </para>
154 <screen>
155 -i adc</screen>
156 <para>If you wish to use a different card or device for input, running the
157 following utility from the command line will provide a list of input
158 devices:
159 </para>
160 <screen>
161 arecord -l</screen>
162 <para>If, by way of an example, you wanted to use a USB audio interface,
163 which is the second "card" in your system, for output, but wanted to
164 use your internal soundcard, the first card in your setup, for input,
165 you would put the following flags somewhere useful:
166 </para>
167 <screen>
168 -+rtaudio=alsa -i adc:hw:0,0 -o dac:hw:1,0</screen>
169 <para>If you wanted to use the second device on your USB interface, to send
170 audio to a specific channel, for instance, you would use the following
171 flags:
172 </para>
173 <screen>
174 -+rtaudio=alsa -i adc:hw:0,0 -o dac:hw:1,1</screen>
175
176 <bridgehead>Using Pulseaudio</bridgehead>
177 <para>Support for <ulink url="http://www.pulseaudio.org/">Pulseaudio</ulink> was added in Csound 5.09. You can specify the following settings:</para>
178 <orderedlist>
179 <listitem><simpara>Sink names: it's possible to use a number instead of the full name, so
180 -odac:1 would select your second device (count starts at 0).</simpara>
181 </listitem>
182 <listitem><simpara>Server name: it's possible to connect to a specific server by using
183 -+server=&lt;server_string&gt;
184 where server_string is a name of a server or a more
185 complex server selection string (see <ulink url="http://www.pulseaudio.org/">pulseaudio.org</ulink> on
186 server strings). This should be network transparent and should allow connections to remote machines.</simpara>
187 </listitem>
188 <listitem><simpara>Stream names: it is possible to label the streams generated by csound, by
189 using
190 -+output_stream=&lt;stream-name&gt;
191 and
192 -+input_stream=&lt;stream-name&gt;</simpara>
193 </listitem>
194 </orderedlist>
195 <para>Here's an example command line:</para>
196 <literallayout>csound -odac:1 examples/trapped.csd -+rtaudio=pulse -+server=unix:/tmp/pulse-victor/native -+output_stream=trapped</literallayout>
197
198
199 <bridgehead>MIDI</bridgehead>
200 <para>3 Midi drivers are available: PortMidi, Raw Midi and Alsa Sequencer (since version 5.18).
201 </para>
202 <para>Csound will use PortMidi as the default MIDI module. With PortMIDI, DEVICE is either a number for single port input, 'a' for listening to all input ports, or 'm' for mapping ports to MIDI channels above 16. In this case, device 0 uses 1-16, device 1 uses 17-32, ... device N uses (N+1)*channel. The options 'a' and 'm' are also convenient when you don't have devices as they will not generate an error.
203 </para>
204 <screen>
205 -+rtmidi=PortMIDI -Ma -Q 1</screen>
206
207 <bridgehead>MIDI Input (Raw Midi driver)</bridgehead>
208 <para>In order to enable your orchestra to receive MIDI input you can use VirMIDI or MIDIThru, whichever you prefer. Setting up these
209 virtual MIDI ports is a topic that has been covered extensively
210 elsewhere, see
211 <ulink url="http://www.midi-howto.com/">The Linux MIDI how-to</ulink>
212 or browse your distro's documentation or the ALSA documentation for
213 instructions on how to install and configure either VirMIDI or
214 MIDIThru (seqdummy). Once you have done so run:
215 </para>
216 <screen>
217 amidi -l</screen>
218 <para>for a list of available devices. Typically this will look something
219 like the following:
220 </para>
221 <literallayout> [....]
222 Device Name
223 hw:1,0 Virtual Raw MIDI (16 subdevices)
224 hw:1,1 Virtual Raw MIDI (16 subdevices)
225 hw:1,2 Virtual Raw MIDI (16 subdevices)
226 hw:1,3 Virtual Raw MIDI (16 subdevices)
227 hw:2,0,0 PCR MIDI
228 hw:2,0,1 PCR 1</literallayout>
229 <para>In this example, Csound can connect to any of the four available
230 Virtual Raw MIDI ports, where it will listen for MIDI input. The
231 following flag instructs Csound to listen on the first of these ports:
232 </para>
233 <screen>
234 -+rtmidi=alsa -Mhw:1,0</screen>
235 <para>
236 You will then need to connect your hardware or software controller to
237 the port which is hosting your Csound synthesizer. The simplest way to do
238 this is using the "aconnect" utility. Run:
239 </para>
240 <screen>
241 aconnect -li</screen>
242 <para>for a list of available input devices, and:</para>
243 <screen>
244 aconnect -lo</screen>
245 <para>for a list of available output devices (including the port to which
246 Csound has been connected). These should give something like the following:
247 </para>
248 <literallayout>
249 #aconnect -li
250 client 0: 'System' [type=kernel]
251 0 'Timer '
252 1 'Announce '
253 Connecting To: 15:0
254 client 20: 'Virtual Raw MIDI 1-0' [type=kernel]
255 0 'VirMIDI 1-0 '
256 client 21: 'Virtual Raw MIDI 1-1' [type=kernel]
257 0 'VirMIDI 1-1 '
258 client 22: 'Virtual Raw MIDI 1-2' [type=kernel]
259 0 'VirMIDI 1-2 '
260 client 23: 'Virtual Raw MIDI 1-3' [type=kernel]
261 0 'VirMIDI 1-3 '
262 client 24: 'PCR' [type=kernel]
263 0 'PCR MIDI '
264 1 'PCR 1 '
265 2 'PCR 2 '</literallayout>
266 <literallayout>
267 #aconnect -lo
268 client 20: 'Virtual Raw MIDI 1-0' [type=kernel]
269 0 'VirMIDI 1-0 '
270 client 21: 'Virtual Raw MIDI 1-1' [type=kernel]
271 0 'VirMIDI 1-1 '
272 client 22: 'Virtual Raw MIDI 1-2' [type=kernel]
273 0 'VirMIDI 1-2 '
274 client 23: 'Virtual Raw MIDI 1-3' [type=kernel]
275 0 'VirMIDI 1-3 '
276 client 24: 'PCR' [type=kernel]
277 0 'PCR MIDI '
278 1 'PCR 1 '</literallayout>
279 <para>
280 In the following example, the USB keyboard which is listed above as
281 client 24 will be connected to a Csound synthesizer which is listening
282 on the first VirMIDI port. The keyboard has three output ports. The
283 first (24:0) transmits messages received on the MIDI in port, the
284 second (24:1) transmits keyboard and controller messages, and the
285 third (24:2) transmits system exclusive messages. The following
286 command connects the second port of the keyboard to the Csound
287 synthesizer:</para>
288 <screen>
289 aconnect 24:1 20:0</screen>
290 <para>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 <emphasis>aconnect</emphasis>, 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.</para>
291 <bridgehead>MIDI Output (Raw Midi driver)</bridgehead>
292 <para>Csound can be connected to any device which shows up on the ALSA
293 sequencer list of output ports, obtained by "amidi -l" as above. In
294 order to connect a Csound synthesizer to the MIDI out port of the
295 keyboard listed above, the following flag would be used:</para>
296 <screen>
297 -Qhw:2,0,0</screen>
298
299 <bridgehead>MIDI Input and Output (Midi Sequencer driver)</bridgehead>
300 <para>This driver is to be preferred over the Raw Midi driver. It has these advantages:
301 <itemizedlist>
302 <listitem>
303 <simpara>Multiple concurrent access.</simpara>
304 </listitem>
305 <listitem>
306 <simpara>Scheduled by priority queues.</simpara>
307 </listitem>
308 <listitem>
309 <simpara>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).</simpara>
310 </listitem>
311 </itemizedlist>
312 </para>
313
314 <para>The following command will call the Midi Sequencer. Here it listens to midi port 20. The midi output port is also 20:
315 </para>
316 <screen>
317 -+rtmidi=alsaseq -M20 -Q20 </screen>
318
319 <para>Csound will automatically create its own ALSA sequencer port. For a list of available devices, use the following command: </para>
320 <screen>
321 aconnect -i -o </screen>
322 <para>This will create output that will look something
323 like the following:
324 </para>
325 <literallayout> client 0: 'System' [type=kernel]
326 0 'Timer '
327 1 'Announce '
328 client 14: 'Midi Through' [type=kernel]
329 0 'Midi Through Port-0'
330 client 20: 'M Audio Delta 1010' [type=kernel]
331 0 'M Audio Delta 1010 MIDI'
332 client 130: 'Csound' [type=user]
333 0 'Csound </literallayout>
334 <para>The output of Csound will contain lines like:</para>
335 <literallayout>
336 ...........
337 ALSASEQ: opened MIDI output sequencer
338 ALSASEQ: created output port 'Csound' 130:0
339 ALSASEQ: connected to 20:0
340 ..............
341 ...........
342 ALSASEQ: opened MIDI input sequencer
343 ALSASEQ: created input port 'Csound' 130:0
344 ALSASEQ: connected from 20:0
345 ..............
346 </literallayout>
347
348 </section>