How an SFZ file is written. These notes are in our words, from the Cakewalk regression suite’s test files (rgc:audio, now Plogue, public domain), the pages of sfzformat.com (headers/region.md, opcodes/include.md, opcodes/define.md, opcodes/sample.md, opcodes/key.md) and, for the exact rules of the reader, the source of the sfizz parser (Parser.cpp). The viewer follows Parser.cpp byte for byte and is checked against it.
Text and encoding
An SFZ file is plain 8-bit text. The sfizz parser reads it byte by byte: it has no notion of an encoding, so a name or a path with accented letters is whatever bytes the file holds, and a UTF-16 file is not read as SFZ at all (its zero bytes are not name characters). The viewer shows a name or value as UTF-8 when the bytes are valid UTF-8 and as Windows-1252 otherwise.
A UTF-8 byte order mark is not skipped by sfizz. Its parser reads the three bytes as an opcode name, reports “Expected opcode name.” and skips the rest of that line, so a file that starts with a mark loses its first line there. The viewer shows the mark as its own node, says so, and reads the file as if the mark were not there. What other players do with a mark is not described by any source we read.
sfizz counts only LF in its line numbers and skips to the next LF when it recovers from an error. It reads CR as white space, and a // comment and an opcode value both end at CR as well as at LF, so a file with CR LF line ends, or with lone CR line ends, reads like one with LF.
Tokens
The reader skips white space (space, tab, CR, LF) and comments, then looks at the next character:
| Next character | What follows |
|---|---|
# | A #define or #include directive. Any other word after the # is the error “Unrecognized directive”. |
< | A header: <name>. |
| anything else | An opcode: name=value. A character that cannot start a name gives “Expected opcode name.” |
Several tokens may stand on one line, and a header may be followed by opcodes on the same line (<region> sample=a.wav key=60, headers/region.md). “Opcodes within a region can appear in any order, and they have to be separated by one or more spaces or tabulation controls. Opcodes can appear in separated lines within a region” (sfzformat.com).
Opcodes and values
An opcode is a name, an equals sign with nothing around it, and a value. sfzformat.com (headers/region.md): sample=cello_a5_pp_first_take.wav is valid and sample = cello_a4_pp.wav “is not”. sfizz agrees: the name is made of letters, digits, _ and $, and the next character must be =, otherwise the error is “Expected = after opcode name.” and the rest of the line is skipped.
The value starts right after the = and runs to the end of the line or the start of a comment. It may hold spaces (opcodes/sample.md: “names with blank spaces and other special characters (excepting the = character) are allowed in the sample definition”). So that several opcodes can share a line, the value is cut at the first run of spaces or tabs that is followed by
- the end of the line, or
<(a header), or#defineor#include, or- a name followed by
=(the next opcode).
A < right inside a value (with no space before it) also ends it. The spaces that were cut stay between tokens. Because of this rule sample=a b.wav key=3 has the value a b.wav for sample, and lokey=a b=c gives lokey the value a.
The viewer gives every opcode its table entry (see Opcodes), checks the value against the type and range there, and lists what it cannot match as a problem. sfizz itself ignores an opcode it does not know (Synth.cpp, buildRegion); the Cakewalk suite’s tests 09 to 11 say that a player “should” log an unknown opcode, in a region, in an <effect> and in a <curve>.
Note names
Where an opcode takes a key, a MIDI note number from 0 to 127 or a note name may be written. The Cakewalk suite (01 - syntax note names.sfz): the format “uses the IPN (International Pitch Notation) convention … Under that standard, the center key is c4, and the midi range goes from c-1 to g9.” So C4 is note 60, C-1 is 0 and G9 is 127. sfzformat.com (tutorials/basics.md) lists C1 as 24, C2 as 36, C3 as 48, C4 as 60 and C5 as 72, and advises note numbers, because “the same note names can be mapped to note numbers an octave or even two apart in different DAWs” (opcodes/lokey.md).
The spelling sfizz reads (Opcode.cpp readNoteValue): a letter a to g in either case, an optional # (or ♯) after c, d, f, g or a, or b (or ♭) after d, e, g, a or b, then the octave number, which may be negative. e# and cb are not accepted.
The opcodes that read note names are those sfizz reads with its key type (Region.cpp): key, lokey, hikey, pitch_keycenter, amp_keycenter, fil_keycenter, fil2_keycenter, sw_lokey, sw_hikey, sw_last, sw_lolast, sw_hilast, sw_down, sw_up, sw_previous, sw_default, xfin_lokey, xfin_hikey, xfout_lokey and xfout_hikey.
key is a short form: key=72 equals lokey=72 hikey=72 pitch_keycenter=72 (opcodes/key.md), and a later lokey, hikey or pitch_keycenter overrides what it set. The values lokey=-1, hikey=-1 and key=-1 stop any key from playing the region (SFZ 2, opcodes/lokey.md).
Under <control>, octave_offset and note_offset shift the notes: the Cakewalk suite (01a - octave offset.sfz) says octave_offset=-1 makes c4 note 48 “instead of note 60” (IPN to MMA). sfizz moves the key range and the root key of every region after the setting by the offset, and the offsets stay in force when a later <control> comes (Synth.cpp, Region.cpp offsetAllKeys). The viewer shows the shifted keys.
#define and variables
#define $NAME value makes a variable (opcodes/define.md: “Variable names start with the $ character”). A use anywhere later in an opcode name, a value or an include path is replaced by the text. In sfizz the value of a #define is the first word only: the rest of the line is read again as ordinary tokens (a comment in Parser.cpp calls this “ARIA/not Cakewalk”). The name written after a $ is matched one character at a time until it is a defined variable, so $KEYS uses $KEY when only $KEY exists, and a replaced text is searched for further $ variables until none is left. The error “Expected $identifier after #define.” covers a missing $ or name. An undefined variable is only a warning (“The variable … is not defined.”) and is replaced by nothing.
sfzformat.com warns that defining the same variable twice with different values around #include lines “does not work well in ARIA/Sforzando”, and suggests a separate file for each set of values.
#include
#include "file.sfz" pastes another file at that point (opcodes/include.md). The path is in double quotes (“different from how the paths in the sample opcode works, which do not need quotes”), may use $ variables, and sfizz turns \ into /. Without a closing quote the error is “Expected "file.sfz" after #include.”. Included files may include others, and “recursion must be avoided”: sfizz stops at 32 levels, and stops a file that is already included only when its recursive guard is switched on. The extension may be .sfz or .sfzh, “functionally no different”. The path is relative to the main SFZ file, not to the file that includes.
The pasted text must keep the file’s order: after all includes are processed the <control> header has to come before the regions and the <curve> headers at the bottom.
The viewer has only the file that was dropped. It lists each include with its path as written and as resolved, marks one that names the dropped file itself as circular, and says it did not read the included files.
Sample paths
sample= holds “a relative file path from the folder where the SFZ file is” (opcodes/sample.md). The Cakewalk suite writes paths with \ (..\..\samples\440.wav); sfizz replaces \ with / in sample and default_path. default_path, under <control>, is put in front of the sample name, without a separator of its own: “either default_path should have a trailing slash, or sample should have a leading slash”. Cakewalk products allow only a relative path; “ARIA, Bassmidi and sfizz allow either absolute or relative paths”. A new <control> resets default_path in ARIA and in sfizz, and in Cakewalk products it “does not reset the path”. A name that starts with * is a generated waveform, not a file (*sine, *saw, *square, *triangle, *tri, *noise, *silence), and takes no default_path. An empty sample= is ignored by sfizz.
If the sample file is not found “the player will ignore the whole region contents as there’s nothing to play”. The viewer does not have the sample files, so it cannot say whether one is missing: its Samples tab lists each resolved path and the format its extension names (WAV, AIFF, FLAC, Ogg, MP3, WavPack are the formats the engines list, see Versions).
Errors
sfizz’s parser reports an error, skips the rest of the line and goes on, so one broken line does not stop the file. The messages: “Expected `<` at start of header.”, “Expected `>` at end of header.” (a header with no > before the line ends), “The header name … is not a valid identifier.”, “Expected opcode name.”, “Expected `=` after opcode name.”, “The opcode name … is not a valid identifier.”, “Unrecognized directive …”, “Expected $identifier after #define.”, “Expected "file.sfz" after #include.”, “Unterminated block comment.”, “Exceeded maximum include depth (32)” and “Cannot open file for reading”. The warnings are “The opcode is not under any header.”, “Expected variable name after $.” and “The variable … is not defined.”. The viewer shows each as a node and as a problem with its line and column.
Comments
Two kinds, both read by sfizz (
Parser.cppskipComment) and both used in the Cakewalk suite:// …runs to the end of the line. In the suite’s05 - syntax comments.sfz: “Comment lines extend from the first slash (‘/’) till the end of the line.” A comment may follow an opcode on the same line; the line end belongs to the comment./* … */may span lines. One that is never closed is the error “Unterminated block comment.” and covers the rest of the file. sfzformat.com does not describe this form; sfizz reads it.A comment inside an opcode value ends the value:
tune=100 // finehas the value100. Insidebase64datathere are no comments, see embedded samples.