Skip to content

offsetsmps

Returns the number of inactive samples before a note starts in the current control block.

Syntax

kOffset = offsetsmps()
kOffset offsetsmps

Performance

kOffset is a count in samples, updated on each control cycle. There are no input arguments. The opcode runs at k-rate only.

With --sample-accurate, a note can start partway through a control block. offsetsmps gives the index of its first active sample in that block. It returns 0 on later control cycles, when the note starts on a block boundary, or when sample-accurate timing is off.

The offset is relative to the current control block. Its value ranges from 0 to ksmps - 1. With a local ksmps in an instrument or user-defined opcode, it refers to that smaller block. Divide the offset by sr to express it in seconds.

Use this value as the first index in a sample-by-sample loop. To exclude inactive samples at the end of the note too, stop before ksmps - earlysmps(). Csound's sample-accurate option does not apply to tied notes or change MIDI timing.

Examples

A custom oscillator must advance its phase only for samples that belong to the note. If it advances through the inactive part of the first block, the note starts at the wrong phase.

This example writes a sine wave one sample at a time. It leaves the inactive samples at zero and uses offsetsmps to find where to begin. It uses offsetsmps.csd.

Start a custom oscillator at the note boundary
<CsoundSynthesizer>
<CsOptions>
-odac -d -m0 --sample-accurate
</CsOptions>
<CsInstruments>
sr = 48000
ksmps = 64
nchnls = 1
0dbfs = 1

instr 1
  aTone = 0
  kPhase init 0
  kFirst = offsetsmps()
  kEnd = ksmps - earlysmps()
  printk2 kFirst

  ; Leave the inactive samples before and after the note at zero.
  kIndex = kFirst
  while kIndex < kEnd do
    kSample = 0.1 * sin(2 * $M_PI * kPhase)
    vaset(kSample, kIndex, aTone)

    ; Advance the oscillator only for samples that belong to the note.
    kPhase = frac(kPhase + 440 / sr)
    kIndex += 1
  od
  out aTone
endin
</CsInstruments>
<CsScore>
; Start at sample 375, which is 55 samples into a 64-sample block.
i 1 [375/48000] 0.125
e
</CsScore>
</CsoundSynthesizer>

The note starts at sample 375. With ksmps set to 64, that falls 55 samples into its first block. The printed offset starts at 55 and changes to 0 for the following blocks. The oscillator begins with phase zero at the note's first active sample.

See also

vaget, vaset, setksmps, Sample Level Operators

Credits

Author Victor Lazzarini. December 2024.

New in Csound 7.