After its four fixed items the header holds more information as a chain of codes. The
chain starts at byte 16 (the field sf_codes) and runs up to a code of 0. The routines getsfcode and putsfcode in
BICSF’s sfcodes.c read and write it, and the manual page sfcodes(3) describes it.
How the chain works
Each link of the chain is a 4-byte record SFCODE followed by the data it announces:
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 2 | short | code | what information follows: 0 ends the chain (SF_END), 1 the maximum amplitude, 2 a comment, 3 and 4 the codes below |
| 2 | 2 | short | bsize | the size of the record and its data together: “the size of the added data plus the size of the present SFCODE”, so the next code starts bsize bytes after this one |
| 4 | bsize − 4 | data | the data of the code |
The reader getsfcode starts at sf_codes and moves on by bsize until it finds the code it wants. It stops at a code of 0, at a
bsize below 4 (“catch possible wrap around on stack from bad header”) or when it reaches the end of the header. The writer
putsfcode replaces a code of the same kind, or puts a new one at the end of the chain and writes a fresh end code after it: SF_END with a
bsize of 4. A header with no codes has a 0 at byte 16. The shorts use the byte order of the file’s magic number; one real file
(an AU hybrid file) has big-endian numbers in the header and a little-endian chain, so a reader should be ready for both.
| Code | Name | Data | Source |
|---|---|---|---|
| 0 | SF_END | none: ends the chain | BICSF, IRCAM |
| 1 | SF_MAXAMP | SFMAXAMP, 36 bytes | BICSF, IRCAM |
| 2 | SF_COMMENT | SFCOMMENT, text | BICSF, IRCAM |
| 3 | SF_PVDATA (IRCAM), SF_LINKCODE (RTcmix) | SFPVDATA, 4 bytes, or SFLINK, 60 bytes | IRCAM sfheader.h; RTcmix sfheader.h |
| 4 | SF_AUDIOENCOD | SFAUDIOENCOD, 4 bytes | IRCAM sfheader.h |
IRCAM’s header marks SF_CODMAX as 4, the highest code it knows. The viewer shows a code it does not know as bytes, with a problem.
The maximum amplitude (code 1)
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 16 | 4 floats | value[4] | the largest sample of each channel, “peak amp per channel”, for up to SF_MAXCHAN (4) channels |
| 16 | 16 | 4 ints | samploc[4] | “location of maxamp sample” for each channel, as a sample position |
| 32 | 4 | int | timetag | “date maxamp was updated”: a Unix time in seconds |
These 36 bytes with the 4 of the SFCODE make the bsize of 40 that sfcodes(3) gives as its example. BICSF’s sndpeak and
sndnorm programs use them (sfcodes(3)), so a program can find a sound’s peak without reading the samples. The time tag is there so that a stale value can be recognised: BICSF’s ismaxampgood accepts the values when timetag + 2 is at least the file’s modification time, and
RTcmix’s header allows the stats to be at most 2 seconds older than the file (MAX_PEAK_STATS_AGE). The explorer shows the values and the time and does not check them against the samples.
The comment (code 2)
A text comment in a zero-terminated field of fixed size: SFCOMMENT is 512 bytes in the headers (MAXCOMM 512, MINCOMM 256), so the usual
bsize is 516. The example in sfcodes(3) defines its own 80-byte comment, and bsize says how long a file’s is. The text ends at the first zero byte.
Phase-vocoder data (code 3, IRCAM)
IRCAM’s header defines SFPVDATA, “this code written by pvanal”, as two 16-bit numbers: frameSize and frameIncr. The names say a frame size and a frame increment; no source
says more. The data is 4 bytes and the bsize is 8.
A link to another sound (code 3, RTcmix)
RTcmix’s header uses the code 3 as SF_LINKCODE with the structure SFLINK: a 50-byte name (reality), then the first and the last sample used
(startsamp, endsamp, two 32-bit ints). Compiled for a 32-bit machine the structure is 60 bytes, with two bytes of padding after the name. The viewer takes a code 3 with 4 bytes
of data as phase-vocoder data and one with 58 or more as a link.
Audio encoding (code 4, IRCAM)
IRCAM’s header defines SFAUDIOENCOD, “ditto” (written by the same phase-vocoder program), as two 16-bit numbers, encoding and grouping. Nothing found says
what the values mean. The viewer shows both numbers.
An example chain
From a real file (a hybrid AU and IRCAM file; here the chain starts at byte 44 of the file, 16 bytes into its IRCAM header). The numbers of its header are big-endian and the chain is little-endian:
| Offset | Bytes | Meaning |
|---|---|---|
| 16 | 01 00 28 00 | SFCODE: code 1, SF_MAXAMP; bsize 40 |
| 20 | A7 E8 E9 45 and 12 more bytes | value[4]: 7485.082 for the first channel (the other three repeat it) |
| 36 | 16 zero bytes | samploc[4]: all 0 |
| 52 | 9C CD 9F 3B | timetag: 1000328604, 12 September 2001, 21:03:24 UTC |
| 56 | 02 00 04 02 | SFCODE: code 2, SF_COMMENT; bsize 516 |
| 60 | 512 bytes | the comment field |
Sources: BICSF’s sfheader.7, sfheader.h, sfcodes.3 and sfcodes.c (the chain, SFMAXAMP, SFCOMMENT, ismaxampgood),
IRCAM’s sfheader.h version 1.5 (SF_PVDATA, SF_AUDIOENCOD, SF_CODMAX) and RTcmix’s sfheader.h (SF_LINKCODE, SFLINK, MAX_PEAK_STATS_AGE).