Debian Codegraph @

overview/using.xml

raw symbols SCIP
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 <para>
21 There are also many <link linkend="OviewFrontEnds"><citetitle>Front-Ends</citetitle></link>
22 which can be used to run
23 <application>csound</application>. A
24 <link linkend="OviewFrontEnds"><citetitle>Front-End</citetitle></link> is a graphical program
25 that eases the process of running csound, and sometimes provides editing
26 and composing functions.
27 </para>
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 <note>This output is from csound5; later versions may differ a bit.</note>
47 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:
48 <programlisting>time resolution is 0.455 ns
49 PortMIDI real time MIDI plugin for Csound
50 virtual_keyboard real time MIDI plugin for Csound
51 PortAudio real-time audio module for Csound
52 0dBFS level = 32768.0
53 Csound version 5.10 beta (float samples) Apr 19 2009
54 libsndfile-1.0.17
55 Reading options from $HOME/.csoundrc
56 UnifiedCSD: oscil.csd
57 STARTING FILE
58 Creating options
59 Creating orchestra
60 Creating score
61 orchname: /tmp/csound-XYACV6.orc
62 scorename: /tmp/csound-IYtLAJ.sco
63 rtaudio: ALSA module enabled
64 rtmidi: PortMIDI module enabled
65 orch compiler:
66 17 lines read
67 instr 1
68 Elapsed time at end of orchestra compile: real: 0.129s, CPU: 0.020s
69 sorting score ...
70 ... done
71 Elapsed time at end of score sort: real: 0.130s, CPU: 0.020s
72 Csound version 5.10 beta (float samples) Apr 19 2009
73 displays suppressed
74 0dBFS level = 32768.0
75 orch now loaded
76 audio buffered in 256 sample-frame blocks
77 ALSA input: total buffer size: 1024, period size: 256
78 reading 1024-byte blks of shorts from adc (RAW)
79 ALSA output: total buffer size: 1024, period size: 256
80 writing 1024-byte blks of shorts to dac
81 SECTION 1:
82 ftable 1:
83 new alloc for instr 1:
84 B 0.000 .. 2.000 T 2.000 TT 2.000 M: 10000.0 10000.0
85 Score finished in csoundPerform().
86 inactive allocs returned to freespace
87 end of score. overall amps: 10000.0 10000.0
88 overall samples out of range: 0 0
89 0 errors in performance
90 Elapsed time at end of performance: real: 2.341s, CPU: 0.050s
91 345 1024-byte soundblks of shorts written to dac
92 Removing temporary file /tmp/csound-CoVcrm.srt ...
93 Removing temporary file /tmp/csound-IYtLAJ.sco ...
94 Removing temporary file /tmp/csound-XYACV6.orc ...</programlisting>
95 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:
96 <programlisting>SECTION 1:</programlisting>
97 In this particular run, the lines:
98 <programlisting>new alloc for instr 1:
99 B 0.000 .. 2.000 T 2.000 TT 2.000 M: 10000.0 10000.0</programlisting>
100 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:
101 <programlisting>end of score. overall amps: 10000.0 10000.0
102 overall samples out of range: 0 0</programlisting>
103 Which shows the overall amplitude and the number of samples which were clipped because they were out of range.
104 </para>
105 <!-- The following paragraph adapted from an email by Victor Lazzarini in the Csound mailing list -->
106 <para>
107 The line:
108 <programlisting>Elapsed time at end of performance: real: 2.341s, CPU: 0.050s</programlisting>
109 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.
110 </para>
111 </section>
112
113 &oviewdesign;
114
115 <section id="UsingRealTime">
116
117 <title>Real-Time Audio</title>
118 <para>
119 The following information applies mostly to csound being run directly from the command line. Front-ends implement these features in different ways, but knowledge of them is necessary in some of them.
120 </para>
121 <para>
122 The <link linkend="FlagsCatMinusLowerI"><citetitle>-i</citetitle></link> and <link linkend="FlagsCatMinusLowerO"><citetitle>-o</citetitle></link> flags can be 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>).
123 </para>
124 <para>
125 You might also need to use the <link linkend="FlagsCatMinusPlusRtaudio"><citetitle>-+rtaudio</citetitle></link> flag to specify the driver interface to be used. Csound defaults to using Portaudio, which is cross-plaform and reliable, but for better performance, you might need to use ALSA or JACK on linux, and CoreAudio on Mac. You can use ASIO on Windows if your version of Portaudio has been compiled with ASIO support.
126 </para>
127 <para>
128 You can see a list of available devices by giving a device number which is out of range, for instance <literal>-o dac99</literal>. This will also reveal if you have ASIO available if you are using PortAudio.
129 </para>
130 <bridgehead>
131 Period &amp; Buffer Sizes</bridgehead>
132 <para>Period and buffer sizes will vary greatly from one machine to
133 another. Lower buffer sizes will result in lower latency, but might
134 cause breakups or clicks in the audio. The Csound
135 flags which control period and buffer sizes are <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link> and <link linkend="FlagsCatMinusUpperB"><citetitle>-B</citetitle></link>,
136 respectively. Buffer size is hardware dependant, and some
137 experimentation may be necessary to find the optimal balance between
138 low latency performance and uninterrupted audio output. The values
139 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>
140 should be at least one power of two higher than that of <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link>.
141 </para>
142 <para>Currently, with <literal><link linkend="FlagsCatMinusUpperB"><citetitle>-B</citetitle></link></literal> set to 512, audio output latency
143 is about 12 milliseconds, fast enough for reasonably responsive
144 keyboad playing. Even shorter latencies, are feasible on some systems.</para>
145 <bridgehead>Control Rate</bridgehead>
146 <para>
147 Low values for ksmps will in general give a higher quality of
148 synthesis, but will consume more system resources. There is no hard
149 and fast rule for setting ksmps - different orchestras will require
150 different control rates. A waveguide instrument will need a ksmps of
151 1 (and may not be suitable for realtime use), whereas a simple FM
152 synth may be run with a higher ksmps without noticeable degradation of
153 sound. If the FM synth were to be used to play a monophonic bassline,
154 a very low ksmps may be used, however more complex note clusters will
155 require a higher ksmps. A well-tuned Linux system should be capable of
156 running even complex polyphonic synths with ksmps values as low as 4
157 or 8. If full duplex audio is required, <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link> must be an integer
158 multiple of ksmps. Bearing this in mind, a rule of thumb might be to
159 only use powers of two for ksmps.
160 </para>
161 <para>
162 Some settings differ according to platform. See next pages for information for each platform.
163 </para>
164
165 <section id="UsingOptimizing">
166 <title>Optimizing Audio I/O Latency</title>
167 <!-- Written by Jonathan Murphy-->
168 <para>
169 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.
170 </para>
171 <para>
172 Usually the simplest solution is the following:
173 </para>
174 <orderedlist>
175 <listitem>
176 <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>
177 </listitem>
178 <listitem>
179 <para>Set <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link> to a negative power of two of this value.</para>
180 <para>
181 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>.
182 </para>
183 </listitem>
184 <listitem>
185 <para>
186 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.
187 </para>
188 </listitem>
189 </orderedlist>
190 <para>
191 This process assumes you have a 16-bit soundcard. If you have a 24-bit
192 soundcard, then
193 <link linkend="FlagsCatMinusUpperB"><citetitle>-B</citetitle></link>
194 should be 3/2, or 3 times
195 <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link>,
196 rather than 2 or 4 times. Csound works with 32-bit floats, or 64-bit
197 doubles whereas most soundcards are 16 or 24-bit integer.
198 <link linkend="FlagsCatMinusLowerB"><citetitle>-b</citetitle></link> is
199 the internal buffer, so it's dealing with the 32 or 64-bit side of things,
200 whereas <link linkend="FlagsCatMinusUpperB"><citetitle>-B</citetitle></link>
201 is the hardware buffer, so it's dealing with the 16 or 24-bit side. The
202 csound default for floats is <emphasis>-B = 4 * -b</emphasis>. This is a
203 sane value for a 16 bit card. You can usually get away with
204 <emphasis>-B = 2 * -b</emphasis>, but this is the absolute minimum. For
205 example, if you set <emphasis>-b1024 -B2048</emphasis>, csound will tell
206 you that:
207 <screen>audio buffered in 1024 sample-frame blocks
208 writing 4096-byte blocks to dac</screen>
209 </para>
210 <para>
211 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:
212 <itemizedlist>
213 <listitem>
214 <simpara>16-bit: 1:2</simpara>
215 </listitem>
216 <listitem>
217 <simpara>24-bit: 2:3</simpara>
218 </listitem>
219 <listitem>
220 <simpara>32-bit: 1:1</simpara>
221 </listitem>
222 </itemizedlist>
223 </para>
224 <para>
225 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>.
226 </para>
227 <para>
228 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.
229 </para>
230 <para>
231 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
232 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>).
233 </para>
234 </section>
235
236 </section>
237
238 &oviewrealtimelinux;
239 &oviewrealtimemac;
240 &oviewrealtimewindows;
241 &oviewrealtimejack;
242
243 </chapter>
244