Skip to content

metro2

Trigger Metronome with Swing and Accents.

Syntax

ktrig  = metro2( kfreq, kswing [, iamp, initphase])
ktrig  metro2  kfreq, kswing [, iamp, initphase]

Initialization

iamp (optional, default=1) -- offbeat trigger amplitude

initphase (optional, default=0) -- initial phase within a main-beat/offbeat pair, normally between 0 and 1. It must be finite and nonnegative. Values of 1 or more are reduced to their fractional part with a warning.

Performance

ktrig - output trigger signal

kfreq - frequency of trigger bangs in cps. It must be finite and nonnegative. A main-beat/offbeat pair takes 2/kfreq seconds when the frequency is positive and constant.

kswing - offbeat position within each pair, in the 0 to 1 range. A value of 0.5 places the offbeat midway between main beats; larger values delay it and smaller values bring it forward.

metro2 adds swing and accents to metro. Main beats have amplitude 1; offbeats use iamp. For example, ktrig metro2 4, .65, -1 produces main beats of 1 and offbeats of -1. For evenly spaced triggers of amplitude 1, use ktrig metro2 kfreq, 0.5.

The main and offbeat clocks advance together. The initial swing comes from its first control-rate value. A nonzero initial phase skips an offbeat that has already passed. At phase zero, the initial trigger is 1, including when swing places both beats at the same point. If both beats occur in a later control cycle, the offbeat amplitude takes precedence.

Control-rate limit and comparison with metro

metro2 accepts frequencies above kr and discards all completed phase cycles. High frequencies therefore do not leave whole cycles that cause extra triggers after the frequency drops to zero. This timing is always active; no extra argument is needed.

Both metro2 and metro can emit at most one trigger per control cycle. Accepting a frequency above kr does not allow metro2 to represent every requested trigger. To resolve every beat, choose a control rate high enough for the shortest interval between main beats and offbeats.

metro remains supported and keeps its historical timing for compatibility. It subtracts only one completed phase cycle per trigger, so frequencies above kr can leave extra triggers after the frequency drops. Its initial phase spans one trigger interval, while metro2's spans a main-beat/offbeat pair. Check startup timing when moving an existing score between them.

Csound 7 uses the timing described here for all metro2 calls. Earlier versions could offset the main and offbeat clocks by one control cycle and emit an offbeat that had already passed at startup. Existing metro2 scores may therefore have different startup and swing timing.

📝 Note

metro2 as well as metro will produce a trigger signal of 1 when its phase is exactly 0 or 1. If you want to skip the initial trigger, use a very small value like 0.00000001.

Examples

Here is an example of the metro2 opcode. It uses the file metro2.csd

Example of the metro2 opcode.
<CsoundSynthesizer>
<CsOptions>
-odac ; for RT audio
</CsOptions>
        <CsInstruments>

sr = 44100
ksmps = 16
nchnls = 2
0dbfs = 1.0

ga1 init 0   ; delay aux

instr 1 ; main triggering instrument
kndx init 0
kndx2 init -1
; the clicks are 1/16th notes @ 137 BPM
kT metro2 (4*137/60), p4, -1
if (kT == 0) goto Halt
        k1 table kndx, 1, 0, 0, 1
        kndx += 1
        if (k1 == 0) goto Next
                event "i", 2, 0, 0.35, k1
Next: 
; positive amplitude values of down-beat clicks 
; are used to trigger kick (instr 3)
if (kT < 1) goto Halt
        kndx2 += 1
        kndx2 wrap kndx2, 0, 2 
        if kndx2 != 0 goto Halt
        event "i", 3, 0, 0.2
Halt:
endin

instr 2  ; simple subtractive bass
kAE linsegr 0, 0.005, 1, p3/2, .7, .04, 0, .04, 0
kFE linsegr 1, 0.005, 2, p3/2, .7, .04, .1, .04, .1
ifr = cpspch(p4)
a1 vco2 1, ifr
a2 vco2 1, ifr * 1.005
a3 vco2 1, ifr * 0.993
aM = (a1+a2+a3)/3
aM moogvcf aM*kAE, 1000 * kFE, 0.5
outs aM, aM
ga1 += aM * 0.25
endin

instr 3 ; simple techno kick
k1 linseg 200, p3, 10
k2 linseg 0,0.001,1,0.25,0
a1 oscil 0.3*k2, k1
outs a1,a1
endin

instr 99 ; feedback delay for bass
a1 delayr 0.5
ab deltap 0.33
   delayw ga1 + ab*0.3
outs ab,ab
ga1 = 0
endin

</CsInstruments>
<CsScore>
t 0 137 
; ftable1 is the bass sequence to played with various swings
f1 0 16 -2 6.00 0 0 7.00 0 0 6.00 0 6.00 0 0 7.00 0 7.01 6.00 0
; 4 measure pattern of different swinging 
i1 0 16 0.5
i1 + .  0.65
i1 + .  0.4
i99 0 46
</CsScore>
</CsoundSynthesizer>

This example starts after the first offbeat and prints 1 for main beats and -1 for offbeats. It uses metro2_corrected.csd:

metro2 with a nonzero initial phase
<CsoundSynthesizer>
<CsOptions>
-n -d -m0
</CsOptions>
<CsInstruments>
sr = 48000
ksmps = 32
nchnls = 1
0dbfs = 1

instr 1
  ; Phase .75 starts after the .65 offbeat, so that offbeat is skipped.
  kTrigger metro2 4, .65, -1, .75
  kTime timeinsts
  if kTrigger != 0 then
    printks "Time: %.4f s, trigger: %+.0f\n", 0, kTime, kTrigger
  endif
endin
</CsInstruments>
<CsScore>
i 1 0 2
e
</CsScore>
</CsoundSynthesizer>

See also

Sensing and Control: Tempo and Sequencing

metro, metrobpm

Credits

Author: Gleb Rogozinsky;
St.Petersburg, October 2019

New in Csound version 6.14