| 1 |
<chapter id="Using"> |
| 2 |
|
| 3 |
<title>Using Csound</title> |
| 4 |
|
| 5 |
<para>Csound can be operated in a variety of modes and |
| 6 |
configurations. The original method for running Csound was as a console |
| 7 |
program (DOS prompt for Windows, Terminal for Mac OS X). This, of course, |
| 8 |
still works. Running |
| 9 |
<application>csound</application> without any arguments prints out a |
| 10 |
list of command-line options, which are more fully explained in the |
| 11 |
<link linkend="CommandFlagsCategory"><citetitle>Command Line Flags |
| 12 |
(by Category)</citetitle></link> section. |
| 13 |
Normally, the user executes something |
| 14 |
like: |
| 15 |
<programlisting>csound myfile.csd</programlisting> |
| 16 |
or separate orchestra (orc) and score (sco) files can be used: |
| 17 |
<programlisting>csound myorchestra.orc myscore.sco</programlisting> |
| 18 |
You can find many .csd files in the examples folder. Most opcode entries in this manual also include simple .csd files showing the usage of the opcode. |
| 19 |
</para> |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
<para> |
| 29 |
Csound also has several ways of producing output. It can: |
| 30 |
<itemizedlist> |
| 31 |
<listitem><simpara>Read and write soundfiles (off-line rendering) - Using the <link linkend="FlagsCatMinusLowerO"><citetitle>-o</citetitle></link> and <link linkend="FlagsCatMinusLowerI"><citetitle>-i</citetitle></link> flags specifying an output filename.</simpara> |
| 32 |
</listitem> |
| 33 |
<listitem><simpara> |
| 34 |
Read and write digital audio using a sound card (real-time rendering) - Using the <link linkend="FlagsCatMinusLowerOdac"><citetitle>-odac</citetitle></link> and <link linkend="FlagsCatMinusLowerIadc"><citetitle>-iadc</citetitle></link> flags</simpara> |
| 35 |
</listitem> |
| 36 |
<listitem><simpara>Read and write MIDI files (non-realtime) - Using the <link linkend="FlagsCatMinusUpperF"><citetitle>-F</citetitle></link> and <link linkend="FlagsCatMinusMidiOutFile"><citetitle>--midioutfile</citetitle></link> flags.</simpara> |
| 37 |
</listitem> |
| 38 |
<listitem><simpara> |
| 39 |
Read and write MIDI using a MIDI interface and controller (real-time control) - Using the <link linkend="FlagsCatMinusUpperM"><citetitle>-M</citetitle></link> and <link linkend="FlagsCatMinusUpperQ"><citetitle>-Q</citetitle></link> flags.</simpara> |
| 40 |
</listitem> |
| 41 |
</itemizedlist> |
| 42 |
</para> |
| 43 |
<section id="ConsoleOutput"> |
| 44 |
<title>Csound's Console Output</title> |
| 45 |
<para> |
| 46 |
When Csound runs, it prints a text output to the console, which shows data about the Csound run. A Console output looks something like this: |
| 47 |
<programlisting>time resolution is 0.455 ns |
| 48 |
PortMIDI real time MIDI plugin for Csound |
| 49 |
virtual_keyboard real time MIDI plugin for Csound |
| 50 |
PortAudio real-time audio module for Csound |
| 51 |
0dBFS level = 32768.0 |
| 52 |
Csound version 5.10 beta (float samples) Apr 19 2009 |
| 53 |
libsndfile-1.0.17 |
| 54 |
Reading options from $HOME/.csoundrc |
| 55 |
UnifiedCSD: oscil.csd |
| 56 |
STARTING FILE |
| 57 |
Creating options |
| 58 |
Creating orchestra |
| 59 |
Creating score |
| 60 |
orchname: /tmp/csound-XYACV6.orc |
| 61 |
scorename: /tmp/csound-IYtLAJ.sco |
| 62 |
rtaudio: ALSA module enabled |
| 63 |
rtmidi: PortMIDI module enabled |
| 64 |
orch compiler: |
| 65 |
17 lines read |
| 66 |
instr 1 |
| 67 |
Elapsed time at end of orchestra compile: real: 0.129s, CPU: 0.020s |
| 68 |
sorting score ... |
| 69 |
... done |
| 70 |
Elapsed time at end of score sort: real: 0.130s, CPU: 0.020s |
| 71 |
Csound version 5.10 beta (float samples) Apr 19 2009 |
| 72 |
displays suppressed |
| 73 |
0dBFS level = 32768.0 |
| 74 |
orch now loaded |
| 75 |
audio buffered in 256 sample-frame blocks |
| 76 |
ALSA input: total buffer size: 1024, period size: 256 |
| 77 |
reading 1024-byte blks of shorts from adc (RAW) |
| 78 |
ALSA output: total buffer size: 1024, period size: 256 |
| 79 |
writing 1024-byte blks of shorts to dac |
| 80 |
SECTION 1: |
| 81 |
ftable 1: |
| 82 |
new alloc for instr 1: |
| 83 |
B 0.000 .. 2.000 T 2.000 TT 2.000 M: 10000.0 10000.0 |
| 84 |
Score finished in csoundPerform(). |
| 85 |
inactive allocs returned to freespace |
| 86 |
end of score. overall amps: 10000.0 10000.0 |
| 87 |
overall samples out of range: 0 0 |
| 88 |
0 errors in performance |
| 89 |
Elapsed time at end of performance: real: 2.341s, CPU: 0.050s |
| 90 |
345 1024-byte soundblks of shorts written to dac |
| 91 |
Removing temporary file /tmp/csound-CoVcrm.srt ... |
| 92 |
Removing temporary file /tmp/csound-IYtLAJ.sco ... |
| 93 |
Removing temporary file /tmp/csound-XYACV6.orc ...</programlisting> |
| 94 |
The console output of Csound is quite verbose, particularly before the actual performance (like version, plugins loaded, etc.). Performance actually started when the console printed: |
| 95 |
<programlisting>SECTION 1:</programlisting> |
| 96 |
In this particular run, the lines: |
| 97 |
<programlisting>new alloc for instr 1: |
| 98 |
B 0.000 .. 2.000 T 2.000 TT 2.000 M: 10000.0 10000.0</programlisting> |
| 99 |
Show that a single note for instrument 1, that lasted 2 seconds starting at time 0.000, was produced with an amplitude of 10000 for both channel 1 and 2. An important section of the console output is: |
| 100 |
<programlisting>end of score. overall amps: 10000.0 10000.0 |
| 101 |
overall samples out of range: 0 0</programlisting> |
| 102 |
Which shows the overall amplitude and the number of samples which were clipped because they were out of range. |
| 103 |
</para> |
| 104 |
|
| 105 |
<para> |
| 106 |
The line: |
| 107 |
<programlisting>Elapsed time at end of performance: real: 2.341s, CPU: 0.050s</programlisting> |
| 108 |
Shows the clock time and the CPU time it takes for the processor to complete the task. If CPU time is lower than clock time it means the csd can run in realtime (unless it has some sections which are extremely CPU intensive). The "real time" figure is the total running time and it is larger because it acounts for disk access. module loading, etc. (CPU time is strictly number-crunching time). If you have a sound that lasts for 100s and it takes 5s to generate it offline, it means that you are taking around 5% of CPU, and that it runs on 0.05 of realtime. |
| 109 |
</para> |
| 110 |
</section> |
| 111 |
|
| 112 |
&oviewdesign; |
| 113 |
|
| 114 |
<section id="UsingRealTime"> |
| 115 |
|
| 116 |
<title>Real-Time Audio</title> |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
<para> |
| 121 |
The <link linkend="FlagsCatMinusLowerI"><citetitle>-i</citetitle></link> and <link linkend="FlagsCatMinusLowerO"><citetitle>-o</citetitle></link> flags can are used to specify realtime output instead of the ordinary non-realtime file output. You should use <literal>-o dac</literal> for realtime output and <literal>-i adc</literal> for realtime input. Naturally, you can use either one or both if your hardware supports it. You can also specify the hardware you want to use by appending a device number or name to the flag (See <link linkend="FlagsCatMinusLowerI"><citetitle>-i</citetitle></link> and <link linkend="FlagsCatMinusLowerO"><citetitle>-o</citetitle></link>). |
| 122 |
</para> |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
<bridgehead> |
| 130 |
Period & Buffer Sizes</bridgehead> |
| 131 |
<para>Period and buffer sizes will vary greatly from one machine to |
| 132 |
another. Lower buffer sizes will result in lower latency, but might |
| 133 |
cause breakups or clicks in the audio. The Csound |
| 134 |
flags which control period and buffer sizes are <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link> and <link linkend="FlagsCatMinusUpperB"><citetitle>-B</citetitle></link>, |
| 135 |
respectively. Buffer size is hardware dependant, and some |
| 136 |
experimentation may be necessary to find the optimal balance between |
| 137 |
low latency performance and uninterrupted audio output. The values |
| 138 |
given to <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link> and <link linkend="FlagsCatMinusUpperB"><citetitle>-B</citetitle></link> should be powers of two, and the value of <link linkend="FlagsCatMinusUpperB"><citetitle>-B</citetitle></link> |
| 139 |
should be at least one power of two higher than that of <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link>. |
| 140 |
</para> |
| 141 |
<para>Currently, with <literal><link linkend="FlagsCatMinusUpperB"><citetitle>-B</citetitle></link></literal> set to 512, audio output latency |
| 142 |
is about 12 milliseconds, fast enough for reasonably responsive |
| 143 |
keyboad playing. Even shorter latencies, are feasible on some systems.</para> |
| 144 |
<bridgehead>Control Rate</bridgehead> |
| 145 |
<para> |
| 146 |
Low values for ksmps will in general give a higher quality of |
| 147 |
synthesis, but will consume more system resources. There is no hard |
| 148 |
and fast rule for setting ksmps - different orchestras will require |
| 149 |
different control rates. A waveguide instrument will need a ksmps of |
| 150 |
1 (and may not be suitable for realtime use), whereas a simple FM |
| 151 |
synth may be run with a higher ksmps without noticeable degradation of |
| 152 |
sound. If the FM synth were to be used to play a monophonic bassline, |
| 153 |
a very low ksmps may be used, however more complex note clusters will |
| 154 |
require a higher ksmps. A well-tuned Linux system should be capable of |
| 155 |
running even complex polyphonic synths with ksmps values as low as 4 |
| 156 |
or 8. If full duplex audio is required, <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link> must be an integer |
| 157 |
multiple of ksmps. Bearing this in mind, a rule of thumb might be to |
| 158 |
only use powers of two for ksmps. |
| 159 |
</para> |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
</section> |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
<section id="UsingOptimizing"> |
| 174 |
<title>Optimizing Audio I/O Latency</title> |
| 175 |
|
| 176 |
<para> |
| 177 |
To achieve the lowest latency possible without audio break ups, a combination of variables needs to be tweaked. The final values will be platform and system dependent, and will also depend on the complexity of the audio calculations performed. You need to adjust <link linkend="ksmps"><citetitle>ksmps</citetitle></link> in the orchestra, as well as the software (<link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link>) and harware buffer (<link linkend="FlagsCatMinusUpperB"><citetitle>-B</citetitle></link>) sizes. |
| 178 |
</para> |
| 179 |
<para> |
| 180 |
Usually the simplest solution is the following: |
| 181 |
</para> |
| 182 |
<orderedlist> |
| 183 |
<listitem> |
| 184 |
<para>Set <link linkend="ksmps"><citetitle>ksmps</citetitle></link> to a value with a good tradeoff between quality and performance, without adjusting <link linkend="FlagsCatMinusUpperB"><citetitle>-B</citetitle></link> at all.</para> |
| 185 |
</listitem> |
| 186 |
<listitem> |
| 187 |
<para>Set <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link> to a negative power of two of this value.</para> |
| 188 |
<para> |
| 189 |
To get the optimal values, start with something you think is going to be too low, ie -1, and then continue "upwards", -2, -4 and so on, until you stop getting x-runs (glitches). The real value of <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link> will be the absolute value of <emphasis>-b * ksmps</emphasis>. |
| 190 |
</para> |
| 191 |
</listitem> |
| 192 |
<listitem> |
| 193 |
<para> |
| 194 |
Reduce the hardware buffer (<link linkend="FlagsCatMinusUpperB"><citetitle>-B</citetitle></link>). Bring it down from the default (1024 on Linux, 4096 on Mac OS X, 16384 on Windows), halving it each time, until you start to get x-runs (glitches) again. Then take it back up again until performance is continuous. |
| 195 |
</para> |
| 196 |
</listitem> |
| 197 |
</orderedlist> |
| 198 |
<para> |
| 199 |
This process assumes you have a 16-bit soundcard. If you have a 24-bit soundcard, then <link linkend="FlagsCatMinusUpperB"><citetitle>-B</citetitle></link> should be 3/2, or 3 times <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link>, rather than 2 or 4 times. Csound works with 32-bit floats, or 64-bit doubles whereas most soundcards are 16 or 24-bit integer. <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link> is the internal buffer, so it's dealing with the 32 or 64-bit side of things, whereas <link linkend="FlagsCatMinusUpperB"><citetitle>-B</citetitle></link> is the hardware buffer, so it's dealing with the 16 or 24-bit side. The csound default for floats is <emphasis>-B = 4 * -b</emphasis>. This is a sane value for a 16 bit card. You can usually get away with <emphasis>-B = 2 * -b</emphasis>, but this is the absolute minimum. For example, if you set <emphasis>-b1024 -B2048</emphasis>, csound will tell you that: |
| 200 |
<screen>audio buffered in 1024 sample-frame blocks |
| 201 |
writing 4096-byte blocks to dac</screen> |
| 202 |
</para> |
| 203 |
<para> |
| 204 |
4096 bytes is 32768 bits. 32768/32 = 1024, our sample-frame size, 1024 * 32/16 = 2048, our buffer size. Were we to reduce the value of <link linkend="FlagsCatMinusUpperB"><citetitle>-B</citetitle></link>, we would need to reduce the value of <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link> by a corresponding amount in order to continue to write 16-bit integers to dac. The minimum size of <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link> is <emphasis>(-B * bitrate)/32</emphasis>. That is to say that the minimum ratio of <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link> to <link linkend="FlagsCatMinusUpperB"><citetitle>-B</citetitle></link> should be: |
| 205 |
<itemizedlist> |
| 206 |
<listitem> |
| 207 |
<simpara>16-bit: 1:2</simpara> |
| 208 |
</listitem> |
| 209 |
<listitem> |
| 210 |
<simpara>24-bit: 2:3</simpara> |
| 211 |
</listitem> |
| 212 |
<listitem> |
| 213 |
<simpara>32-bit: 1:1</simpara> |
| 214 |
</listitem> |
| 215 |
</itemizedlist> |
| 216 |
</para> |
| 217 |
<para> |
| 218 |
While there is no theoretical maximum ratio, it makes no sense to have a very high ratio here, as the software buffer has to fill the hardware buffer before returning. If the ratio is high, it will take a long time, defeating the purpose of setting a small value for <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link>. |
| 219 |
</para> |
| 220 |
<para> |
| 221 |
The value of <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link> is something that will need to be varied depending on the complexity of the instrument you're working with, but because it's intimately related to the value of ksmps, it's better to synchronise it with <link linkend="ksmps"><citetitle>ksmps</citetitle></link> and go from there. One way to do it is to decide how long the release on your envelopes might need to be at maximum (for desired effect), set the release on all envelopes to maximum, give yourself a generous value for <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link>, and then play. If it breaks up, double ksmps, repeat until smooth, then bring the value of <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link> down as far as possible. |
| 222 |
</para> |
| 223 |
<para> |
| 224 |
The value of <link linkend="FlagsCatMinusUpperB"><citetitle>-B</citetitle></link> is primarily determined by operating system and soundcard. Figure out (using above method) how low you can go, and use that value (or one higher for safety). If you |
| 225 |
have problems you'll know that it's probably because of an inappropriate value for ksmps, too low a value for <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link>, or denormals (see <link linkend="denorm"><citetitle>denorm</citetitle></link>). |
| 226 |
</para> |
| 227 |
</section> |
| 228 |
</chapter> |
| 229 |
|