Skip to content

earlysmps

Returns the number of inactive samples at the end of the current control block.

Syntax

kEarly = earlysmps()
kEarly earlysmps

Performance

kEarly is a count in samples, updated on each control cycle. There are no input arguments.

With --sample-accurate, a note can end partway through a control block. earlysmps reports how many samples at the end of that block fall after the note's end. It returns 0 for blocks with no inactive tail, including a final block that ends exactly at the note boundary. Without this option, it returns 0.

The count refers to the block in the current instrument or user-defined opcode. With a local ksmps, it refers to that smaller block. A zero result alone does not tell you whether the note will continue.

For a sample loop, stop before index ksmps - kEarly. The offsetsmps opcode gives the first active sample index when a note starts partway through a block. Using both bounds keeps the loop within the samples that belong to the note.

Examples

A custom RMS meter needs to exclude inactive samples from both its sum and its sample count. Otherwise, the zero padding can lower the reading for a short final block.

This example measures a constant signal with a value of 0.25. The note lasts 750 samples. With ksmps set to 64, its last block has 46 active samples and 18 inactive samples. The example prints the final count and RMS without producing audio. It uses earlysmps.csd.

Measure only the active samples in a block
<CsoundSynthesizer>
<CsOptions>
-n -d -m0 --sample-accurate
</CsOptions>
<CsInstruments>
sr = 48000
ksmps = 64
nchnls = 1
0dbfs = 1

instr 1
  ; A constant signal makes the expected RMS easy to see.
  aSignal = 0.25
  kFirst = offsetsmps()
  kEarly = earlysmps()
  kEnd = ksmps - kEarly
  kCount = kEnd - kFirst

  ; Measure only the samples that belong to this note.
  kSum = 0
  kIndex = kFirst
  while kIndex < kEnd do
    kSample = vaget(kIndex, aSignal)
    kSum += kSample * kSample
    kIndex += 1
  od
  kRms = sqrt(kSum / kCount)

  if kEarly > 0 then
    printf "Final block has %d active samples and %d inactive samples\n", 1, kCount, kEarly
    printf "RMS of the active samples is %.2f\n", 1, kRms
  endif
endin
</CsInstruments>
<CsScore>
; 750 samples fill 11 blocks of 64 and 46 samples of the last block.
i 1 0 [750/48000]
e
</CsScore>
</CsoundSynthesizer>

Expected output includes these lines.

Final block has 46 active samples and 18 inactive samples
RMS of the active samples is 0.25

See also

vaget, vaset, setksmps, Sample Level Operators

Credits

Author Victor Lazzarini. December 2024.

New in Csound 7.