sfinstr3 — Plays a SoundFont2 (SF2) sample instrument, generating a stereo sound with cubic interpolation.


Plays a SoundFont2 (SF2) sample instrument, generating a stereo sound with cubic interpolation. These opcodes allow management the sample-structure of SF2 files. 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 sfinstr3 ivel, inotenum, xamp, xfreq, instrnum, ifilhandle \
      [, iflag] [, ioffset]


ivel -- velocity value

inotenum -- MIDI note number value

instrnum -- number of an instrument of a SF2 file.

ifilhandle -- unique number generated by sfload opcode to be used as an identifier for a SF2 file. Several SF2 files can be loaded and activated at the same time.

iflag (optional) -- flag regarding the behavior of xfreq and inotenum

ioffset (optional) -- start playing at offset, in samples.


xamp -- amplitude correction factor

xfreq -- frequency value or frequency multiplier, depending by iflag. When iflag = 0, xfreq is a multiplier of a the default frequency, assigned by SF2 preset to the inotenum value. When iflag = 1, xfreq 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 xfreq is used as a multiplier. When iflag = 1, the frequency of the output, is set directly by xfreq. 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.

Adjustment of the amplitude can be done by varying the xamp argument, which acts as a multiplier.

The ioffset parameter allows the sound to start from a sample different than the first one. The user should make sure that its value is within the length of the specific sound. Otherwise, Csound will probably crash.

sfinstr3 is a cubic-interpolation version of sfinstr. Difference of sound-quality is noticeable specially in bass-frequency-transposed samples. In high-freq-transposed samples the difference is less noticeable, and I suggest to use linear-interpolation versions, because they are faster.

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.


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

Example 984. Example of the sfinstr3 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
-odac ;;;realtime audio out
;-iadc    ;;;uncomment -iadc if realtime audio input is needed too
; For Non-realtime ouput leave only the line below:
; -o sfinstr3.wav -W ;;; for file output any platform

; By  Menno Knevel - 2020

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

gi24   ftgen 1, 0, 32, -2, 24, 2, 261.626, 60, 1, 1.0293022, 1.059463, 1.0905076, 1.1224619, 1.1553525, 1.1892069, \
             1.2240532, 1.2599207, 1.2968391, 1.33483924, 1.3739531, 1.414213, 1.4556525, 1.4983063, 1.54221, 1.5874001, \
             1.6339145, 1.6817917, 1.73107, 1.7817962, 1.8340067, 1.8877471, 1.9430623,  2 ;table for microtuning, a 24 tone equal temperament

giSF	sfload	"sf_GMbank.sf2" 
        sfilist giSF 

instr 1 

ikey	= p4
ivel	= p5
aenv    linsegr	1, 1, 1, 1, 0			;envelope
icps    cpstuni ikey, 1				;24 tones per octave
iamp    = 0.0002				;scale amplitude
iamp    = iamp * ivel * 1/128 			;make velocity-dependent, small value due to 0dbfs = 1
aL, aR	sfinstr3 ivel, ikey, iamp, icps, 180, giSF, 1 ;= Slap Bass 3
aL      = aL * aenv 
aR      = aR * aenv 
        outs aL, aR 


i1 0 1 60 100 1	;using ftable 1
i1 + 1 62 <   .
i1 + 1 65 <   .
i1 + 1 69 20  .

i1 5 1 60 100 1	;using ftable 1
i1 + 1 61 <   .
i1 + 1 62 <   .
i1 + 1 63 20  .


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: Gabriel Maldonado
May 2000

New in Csound Version 4.07