sfplay3
Prev | Orchestra Opcodes and Operators | Next

## sfplay3

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

## Description

Plays a SoundFont2 (SF2) sample preset, 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.

## Syntax

    ar1, ar2 sfplay3 ivel, inotenum, xamp, xfreq, ipreindex [, iflag] [, ioffset] [, ienv]

## Initialization

ivel -- velocity value

inotenum -- MIDI note number value

ipreindex -- preset index

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

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

ienv (optional) -- enables and determines amplitude envelope. 0 = no envelope, 1 = linear attack and decay, 2 = linear attack, exponential decay (see below). Default = 0.

## Performance

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.

Notice that both xamp and xfreq can use k-rate as well as a-rate signals. Both arguments must use variables of the same rate, or sfplay3 will not work correctly. ipreindex must contain the number of a previously assigned preset, or Csound will crash.

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.

The ienv parameter enables and determines the type of amplitude envelope used. The default value is 0, or no envelope. If ienv is set to 1, the attack and decay portions are linear. If set to 2, the attack is linear and the decay is exponential. The release portion of the envelope has not yet been implemented.

sfplay3 plays a preset, generating a stereo sound with cubic interpolation. ivel does not directly affect the amplitude of the output, but informs sfplay3 about which sample should be chosen in multi-sample, velocity-split presets.

sfplay3 is a cubic-interpolation version of sfplay. 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.

## Examples

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

Example 970. Example of the sfplay3 opcode.

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

    <CsoundSynthesizer>
    <CsOptions>
    ; 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 sfplay3.wav -W ;;; for file output any platform
    </CsOptions>
    <CsInstruments>

    ; By  Menno Knevel - 2020

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

    gitwelve ftgen 1, 0, 16, -2, 12, 2, 440, 69, 1, 16/15, 9/8, 6/5, 5/4, 4/3, 7/5, 3/2, 8/5, 5/3, 9/5, 15/8, 2
    givife   ftgen 2, 0, 16, -2, 5, 2, 261.659, 60, 1, 1.1486, 1.3195, 1.5157, 1.7411, 2.00

    giSF	sfload	"01hpschd.sf2"
            sfplist giSF
    gipre	sfpreset 0, 0, giSF, 0

    instr 1

    ikey	= p4
    ivel	= p5
    aenv    linsegr	1, 1, 1, 1, 0			;envelope
    icps    cpstuni	ikey, gitwelve 			;12 tones per octave
    iamp    = 0.00001                                ;scale amplitude down a lot, due to 0dbfs  = 1
    iamp    *= ivel 			         ;make velocity-dependent

    aL, aR	sfplay3 ivel, ikey, iamp, icps, gipre, 1
    aL      = aL * aenv
    aR      = aR * aenv
            outs aL, aR

    	endin

    instr 2

    ikey	= p4
    ivel	= p5
    aenv    linsegr	1, 1, 1, 1, 0			;envelope
    icps    cpstuni	ikey, givife			;5 tones per octave
    iamp    = 0.00001		                  ;scale amplitude down a lot, due to 0dbfs  = 1
    iamp    *= ivel 			         ;make velocity-dependent

    aL, aR	sfplay3 ivel, ikey, iamp, icps, gipre, 1
    aL      = aL * aenv
    aR      = aR * aenv
            outs aL, aR

    endin

    </CsInstruments>
    <CsScore>

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

    ;instr.2 using ftable 2
    i2 5 1 60 100
    i2 + 1 62 <
    i2 + 1 65 <
    i2 + 1 69 40
    e
    </CsScore>
    </CsoundSynthesizer>

## See also

Soundfonts

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

## Credits

Author: Gabriel Maldonado
Italy
May 2000

New in Csound Version 4.07

New optional parameter ienv in version 5.09

Prev | Up | Next
sfpassign | Home | sfplay3m