{ — Begins a non-sectional, nestable loop.
The { and } statements can be used to repeat a group of score statements. These loops do not constitute independent score sections and thus may repeat events within the same section. Multiple loops may overlap in time or be nested within each other.
p1 -- Number of times to repeat the loop.
p2 -- A macro name that is automatically defined at the beginning of the loop and whose value is advanced with each repetition (optional). The initial value is zero and the final value is (p1 - 1).
The { statement is used in conjunction with the } statement to define repeating groups of other score events. A score loop begins with the { statement which defines the number of repetitions and a unique macro name that will contain the current loop counter. The body of a loop can contain any number of other events (including sectional breaks) and is terminated by a } statement on its own line. The } statement takes no parameters.
The use of the term "loop" here does not imply any sort of temporal succession to the loop iterations. In other words, the p2 values of the events inside of the loop are not automatically incremented by the length of the loop in each repetition. This is actually an advantage since it allows groups of simulataneous events to be easily defined as well. The loop macro can be used along with score expressions to increase the start times of events or to vary the events in any other way desired for each iteration. The macro is incremented by one for each repetition. Note that unlike the r statement, the value of the macro the first time through the loop is zero (0), not one (1). Therefore the final value is one less than the number of repetitions.
Score loops are a very powerful tool. While similar to the section repeat facility (the r statement), their chief advantage is that the score events in successive iterations of the loop are not separated by a section termination. Thus, it is possible to create multiple loops that overlap in time. Loops also can be nested within each other to a depth of 39 levels.
Warning | |
---|---|
Because of serious problems of interaction with macro expansion, loops must start and end in the same file, and not in a macro. |
Here are some examples of the { and } statements.
Example 1273. Sequentially repeat a three-note phrase four times.
{ 4 CNT i1 [0.00 + 0.75 * $CNT.] 0.2 220 i1 [0.25 + 0.75 * $CNT.] . 440 i1 [0.50 + 0.75 * $CNT.] . 880 }is interpreted as
i1 0.00 0.2 220 i1 0.25 . 440 i1 0.50 . 880 i1 0.75 0.2 220 i1 1.00 . 440 i1 1.25 . 880 i1 1.50 0.2 220 i1 1.75 . 440 i1 2.00 . 880 i1 2.25 0.2 220 i1 2.50 . 440 i1 2.75 . 880
Example 1274. Create a group of simultaneous harmonic partials.
{ 8 PARTIAL i1 0 1 [100 * ($PARTIAL. + 1)] }is interpreted as
i1 0 1 100 i1 0 1 200 i1 0 1 300 i1 0 1 400 i1 0 1 500 i1 0 1 600 i1 0 1 700 i1 0 1 800
Here is a full example of the { and } statements. Play leftbrace.csd
Example 1275. An example of nested loops to create several inharmonic sine clusters.
<CsoundSynthesizer> <CsOptions> ; Select audio/midi flags here according to platform ; Audio out Audio in -odac -iadc ;;;RT audio I/O ; For Non-realtime ouput leave only the line below: ; -o abs.wav -W ;;; for file output any platform </CsOptions> <CsInstruments> nchnls = 2 gaReverbSend init 0 ; a simple sine wave partial instr 1 idur = p3 iamp = p4 ifreq = p5 aenv linseg 0.0, 0.1*idur, iamp, 0.6*idur, iamp, 0.3*idur, 0.0 aosc oscili aenv, ifreq, 1 vincr gaReverbSend, aosc endin ; global reverb instrument instr 2 al, ar reverbsc gaReverbSend, gaReverbSend, 0.85, 12000 outs gaReverbSend+al, gaReverbSend+ar clear gaReverbSend endin </CsInstruments> <CsScore> f1 0 4096 10 1 { 4 CNT { 8 PARTIAL ; start time duration amplitude frequency i1 [0.5 * $CNT.] [1 + ($CNT * 0.2)] [500 + (~ * 200)] [800 + (200 * $CNT.) + ($PARTIAL. * 20)] } } i2 0 6 e </CsScore> </CsoundSynthesizer>