sflooper — Plays a SoundFont2 (SF2) sample preset, generating a stereo sound, with user-defined time-varying crossfade looping.


Plays a SoundFont2 (SF2) sample preset, generating a stereo sound, similarly to sfplay. Unlike that opcode, though, it ignores the looping points set in the SF2 file and substitutes them for a user-defined crossfade loop. It is a cross between sfplay and flooper2. In order to understand the usage of these opcodes, the user must have some knowledge of the SF2 format, so a brief description of this format can be found in the SoundFont2 File Format section.


ar1, ar2 sflooper ivel, inotenum, kamp, kpitch, ipreindex, kloopstart, kloopend, kcrossfade \
      [, istart, imode, ifenv, iskip, iflag] 


ivel -- velocity value

inotenum -- MIDI note number value

ipreindex -- preset index

istart -- playback start pos in seconds

imode -- loop modes: 0 forward, 1 backward, 2 back-and-forth [def: 0]

ifenv -- if non-zero, crossfade envelope shape table number. The default, 0, sets the crossfade to linear.

iskip -- if 1, the opcode initialisation is skipped, for tied notes, performance continues from the position in the loop where the previous note stopped. The default, 0, does not skip initialisation

iflag -- flag regarding the behavior of kpitch and inotenum


kamp -- amplitude scaling

kpitch -- pitch control (transposition ratio, negative values are not allowed) or frequency multiplier, depending by iflag. When iflag = 0, kpitch is a multiplier of a the default frequency, assigned by SF2 preset to the inotenum value. When iflag = 1, kpitch is the absolute frequency of the output sound, in Hz. Default is 0. When iflag = 0, inotenum sets the frequency of the output according to the MIDI note number used, and kpitch is used as a multiplier. When iflag = 1, the frequency of the output, is set directly by kpitch. This allows the user to use any kind of micro-tuning based scales. However, this method is designed to work correctly only with presets tuned to the default equal temperament. Attempts to use this method with a preset already having non-standard tunings, or with drum-kit-based presets, could give unexpected results.

kloopstart -- loop start point (secs). Note that although k-rate, loop parameters such as this are only updated once per loop cycle. If loop start is set beyond the end of the sample, no looping will result.

kloopend -- loop end point (secs), updated once per loop cycle.

kcrossfade -- crossfade length (secs), updated once per loop cycle and limited to loop length.

sflooper plays a preset, generating a stereo sound.

These opcodes only support the sample structure of SF2 files. The modulator structure of the SoundFont2 format is not supported in Csound. Any modulation or processing to the sample data is left to the Csound user, bypassing all restrictions forced by the SF2 standard.

Note: The looping points are set on the root key of every sample that is part of the preset of the soundfont. For instance, a soundfont can have a single sample for the whole keyboard. In that case, sflooper will work like flooper and flooper2, because as the sample is transposed, played back at different rates, the loop will get short or longer. If however the soundfont has a sample for each key, than there will be no transposition and the loop will stay the same length (unless you change kpitch).


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

Example 988. Example of the sflooper opcode.

See the sections Real-time Audio and Command Line Flags for more information on using command line flags.

; Select audio/midi flags here according to platform
; Audio out   Audio in    No messages
-odac   ;;;realtime audio in
; For Non-realtime ouput leave only the line below:
; -o sflooper.wav -W ;;; for file output any platform

sr = 44100
ksmps = 32
nchnls = 2
0dbfs  = 1

; by  Menno Knevel - 2021

isf   sfload "07AcousticGuitar.sf2"
      sfpassign 0, isf

instr 1	

inum  = p4
ivel  = p5
kamp  linsegr 1,1,1,.1,0	; declick envelope
kamp  = kamp * .0000015		;scale amplitude
ifreq = 1			;do not change freq from sf
;"07AcousticGuitar.sf2" contains 2 samples, on notes E1 and C#4
;start loop from beginning, loop .2 seconds - on the root key of these samples
aL,aR sflooper ivel, inum, kamp*ivel, ifreq, 0, 0, .2, .05  ; make amp velocity dependent
      outs aL, aR

instr 2	

ifreq  = p4
ivel  = p5
kamp  linsegr 1,1,1,.1,0	; declick envelope
kamp  = kamp * .0000015		;scale amplitude
inum = 60			;take soundfont samples belonging to midi index 60

;nearly identical instr, but now takes midi note sound 60 as reference, set iflag to 1
aL,aR sflooper ivel, inum, kamp*ivel, ifreq, 0, 0, .2, .05, 0, 0, 0, 0, 1  ; & use ifreq for frequency
      outs aL, aR


i1 0 1 60 120   ; p4 = midi note
i1 + 1 62 <
i1 + 1 65 <
i1 + 1 69 10

i2 5 1 200 120  ; p4 = frequency
i2 + 1 261 <
i2 + 1 300 <
i2 + 1 1000 10

See also


More information on soundfonts is in the Floss Manuals: https://flossmanual.csound.com/midi/reading-midi-files

For other information on soundfonts look in the Wikipedia: http://en.wikipedia.org/wiki/Soundfont


Author: Victor Lazzarini
August 2007

New in Csound Version 5.07