Configuring Audio & Tuning Audio Performance
Selecting Audio Devices and Drivers
Csound relates to the various inputs and outputs of sound devices installed on your computer as a numbered list. If you wish to send or receive audio to or from a specific audio connection you will need to know the number by which Csound knows it. If you are not sure of what that is you can trick Csound into providing you with a list of available devices by trying to run Csound using an obviously out of range device number, like this:
<CsoundSynthesizer> <CsOptions> -iadc999 -odac999 </CsOptions> <CsInstruments> ;Example by Andrés Cabrera instr 1 endin </CsInstruments> <CsScore> e </CsScore> </CsoundSynthesizer>
The input and output devices will be listed seperately.1 Specify your input device with the -iadc flag and the number of your input device, and your output device with the -odac flag and the number of your output device. For instance, if you select one of the devices from the list above both, for input and output, you may include something like
in the <CsOptions> section of you .csd file.
The RT (= real-time) output module can be set with the -+rtaudio flag. If you don't use this flag, the PortAudio driver will be used. Other possible drivers are jack and alsa (Linux), mme (Windows) or CoreAudio (Mac). So, this sets your audio driver to mme instead of Port Audio:
Tuning Performance and Latency
Live performance and latency depend mainly on the sizes of the software and the hardware buffers. They can be set in the <CsOptions> using the -B flag for the hardware buffer, and the -b flag for the software buffer.2 For instance, this statement sets the hardware buffer size to 512 samples and the software buffer size to 128 sample:
The other factor which affects Csound's live performance is the ksmps value which is set in the header of the <CsInstruments> section. By this value, you define how many samples are processed every Csound control cycle.
Try your realtime performance with -B512, -b128 and ksmps=32.3 With a software buffer of 128 samples, a hardware buffer of 512 and a sample rate of 44100 you will have around 12ms latency, which is usable for live keyboard playing. If you have problems with either the latency or the performance, tweak the values as described here.
To define the audio hardware used for realtime performance, open the configuration dialog. In the "Run" Tab, you can choose your audio interface, and the preferred driver. You can select input and output devices from a list if you press the buttons to the right of the text boxes for input and output names. Software and hardware buffer sizes can be set at the top of this dialogue box.
Csound Can Produce Extreme Dynamic Range!
Csound can produce extreme dynamic range, so keep an eye on the level you are sending to your output. The number which describes the level of 0 dB, can be set in Csound by the 0dbfs assignment in the <CsInstruments> header. There is no limitation, if you set 0dbfs = 1 and send a value of 32000, this can damage your ears and speakers!
Using Live Audio Input and Output
To process audio from an external source (for example a microphone), use the inch opcode to access any of the inputs of your audio input device. For the output, outch gives you all necessary flexibility. The following example takes a live audio input and transforms its sound using ring modulation. The Csound Console should output five times per second the input amplitude level.
<CsoundSynthesizer> <CsOptions> ;CHANGE YOUR INPUT AND OUTPUT DEVICE NUMBER HERE IF NECESSARY! -iadc0 -odac0 -B512 -b128 </CsOptions> <CsInstruments> ;Example by Joachim Heintz sr = 44100 ;set sample rate to 44100 Hz ksmps = 32 ;number of samples per control cycle nchnls = 2 ;use two audio channels 0dbfs = 1 ;set maximum level as 1 giSine ftgen 0, 0, 2^10, 10, 1 ;table with sine wave instr 1 aIn inch 1 ;take input from channel 1 kInLev downsamp aIn ;convert audio input to control signal printk .2, abs(kInLev) ;make modulator frequency oscillate 200 to 1000 Hz kModFreq poscil 400, 1/2, giSine kModFreq = kModFreq+600 aMod poscil 1, kModFreq, giSine ;modulator signal aRM = aIn * aMod ;ring modulation outch 1, aRM, 2, aRM ;output to channel 1 and 2 endin </CsInstruments> <CsScore> i 1 0 3600 </CsScore> </CsoundSynthesizer>
Live Audio is frequently used with live devices like widgets or MIDI. In CsoundQt, you can find several examples in Examples -> Getting Started -> Realtime Interaction.
- You may have to run -iadc999 and -odac999 seperately.^
- As Victor Lazzarini explains (mail to Joachim Heintz, 19 march 2013), the role of -b and -B varies between the Audio Modules:
"1. For portaudio, -B is only used to suggest a latency to the backend, whereas -b is used to set the actual buffersize.
2. For coreaudio, -B is used as the size of the internal circular buffer, and -b is used for the actual IO buffer size.
3. For jack, -B
is used to determine the number of buffers used in conjunction with -b^ , num = (N + M + 1) / M. -b is the size of each buffer.
4. For alsa, -B is the size of the buffer size, -b is the period size (a buffer is divided into periods).
5. For pulse, -b is the actual buffersize passed to the device, -B is not used.
In other words, -B is not too significant in 1), not used in 5), but has a part to play in 2), 3) and 4), which is functionally similar."
- It is always preferable to use power-of-two values for ksmps (which is the same as "block size" in PureData or "vector size" in Max). Just with ksmps = 1, 2, 4, 8, 16 ... you will take advantage of the "full duplex" audio, which provides best real time audio. Make sure your ksmps divides your buffer size with no remainder. So, for -b 128, you can use ksmps = 128, 64, 32, 16, 8, 4, 2 or 1.^