A compressed sample of an SF3 file is a complete Ogg Vorbis stream, one for each sample, stored inside the smpl chunk. The sample header says where it starts and ends in bytes.
Where a stream is
dwStartis the byte offset of the first byte of the stream from the beginning of thesmpldata (FluidSynth RFC). FluidSynth opens a virtual file atsamplepos + start, wheresampleposis the offset of the smpl data in the file (fluid_sffile.c,fluid_sffile_read_vorbis).dwEnd: the RFC says it “points to the last byte in the compressed data stream”. In the files read here, and as MuseScore’s converter writes it (s->end = sampleLenafter adding the length of the stream), it is one byte past the last byte: the stream isdwEnd − dwStartbytes long. FluidSynth subtracts one when it loads a header (fluid_defsfont.c:source_end = end − 1, with the comment “marks last sample, contrary to SF spec”) and so treats the result as the last byte. The explorer reads the stream asdwEnd − dwStartbytes and reports a stream whose Ogg pages end before or after that.- The offsets must not go beyond the sample data chunk; FluidSynth refuses a sample whose offsets are beyond it.
The Ogg stream
An Ogg stream is a series of pages (RFC 3533). Each page starts with the capture pattern OggS and has a 27-byte header and a segment table:
| Offset | Size | Field | Meaning |
|---|---|---|---|
| 0 | 4 | capture_pattern | OggS (0x4F 0x67 0x67 0x53) |
| 4 | 1 | stream_structure_version | 0 |
| 5 | 1 | header_type_flag | 0x01 the page continues a packet from the previous page; 0x02 first page of the stream; 0x04 last page |
| 6 | 8 | granule_position | Codec-dependent position; −1 (all bits set) means no packet finishes on this page |
| 14 | 4 | bitstream_serial_number | The number that identifies the logical stream |
| 18 | 4 | page_sequence_number | Increases by one on each page of the stream, so a decoder can see lost pages |
| 22 | 4 | CRC_checksum | A 32-bit CRC of the whole page, the CRC field counted as zero; the generator polynomial is 0x04C11DB7 |
| 26 | 1 | number_page_segments | The number of entries of the segment table |
| 27 | number_page_segments | segment_table | The lacing values of the page’s segments |
All multi-byte fields are little-endian. The page is 27 + the number of segments + the sum of the lacing values long. A lacing value of 255 means the packet continues in the next segment; a value below 255 ends it (RFC 3533, section 6; Ogg framing).
For Vorbis (Vorbis I specification, appendix A.2): the first packet, the identification header, is alone on the first page, which is marked as the start of the stream; the comment and setup headers follow, and the third header ends its page; the first audio packet begins a fresh page; the pages of the headers have granule position zero; a page’s granule position, for audio, is the PCM sample position (per channel) at the end of the last packet completed on it; and the last page is marked as the end of the stream, and its granule position can end the stream before the end of the last packet’s samples.
The identification header packet starts with the type byte 1 and the characters vorbis, then a 32-bit vorbis_version, an 8-bit audio_channels and a 32-bit audio_sample_rate (Vorbis I, sections 4.2.1 and 4.2.2).
Decoding a sample
- Read the bytes from
dwStarttodwEndofsmpland split them into Ogg pages and packets. - Decode the three header packets and then the audio packets with a Vorbis decoder (the explorer’s own, written from the Vorbis I specification; FluidSynth uses libsndfile, TinySoundFont uses stb_vorbis).
- The decoded length is the granule position of the last page. Decoders that return whole blocks give more points; the file’s
dwEndloopand the instrument generators count from the start of the decoded data. The first sample of VintageDreamsWaves-v2.sf3 that is Ogg (FM4-0440) decodes to 108 data points from a 3,496-byte stream, as FluidSynth’s own dump of the file lists (start 0, end 107); ffmpeg’s decoder returns 128 points for the same stream.
Codecs other than Vorbis
The RFC says the compressed flag “does not necessarily indicate OGG Vorbis compression” but marks any kind of encoding, and that FLAC or Opus support might be added (FluidSynth RFC). The FluidSynth project’s tracker has the request (issue 605, “Support for FLAC compressed samples?”, opened by a user who calls such files “SF4”, with Cognitone’s sf2convert as the converter); its maintainers answered in January and February 2020 that no new version number or file extension is needed for another codec (“No further bumps are required just to support additional compressions”; a file can mix samples in different formats, as each is compressed alone), and that they wanted real use cases. In the MuseScore forum thread “Documenting and possibly extending the SF3 format” (January 2020) a FluidSynth maintainer asked for an “official unofficial” specification and a MuseScore developer answered that Werner Schweer had made SF3 and was no longer active, that such a specification would be useful, that FLAC could be added to SF3 and that, being lossless, it might be named differently (research/sf3/forum/n299957.html, issue605.json). No file with another codec was found, and no flag value for one has been defined.
FluidSynth opens a compressed sample through libsndfile’s virtual file interface, which recognises any format libsndfile reads, and only warns “OGG sample is not OGG compressed, this is not officially supported” when the format is not Ogg (fluid_sffile.c). The explorer decodes, by the same reading, a sample that is Ogg Vorbis, Ogg Opus (RFC 7845: the OpusHead packet, the granule position in 48 kHz units less the pre-skip), a native FLAC stream (it starts with fLaC, RFC 9639) or Ogg FLAC (a 0x7F “FLAC” packet, then the metadata blocks and one frame a packet), each of one channel, and reports a note that the codec is not named by the RFC. Their decoders are those of the explorer’s FLAC and Ogg plugins. The test builds one of each with ffmpeg and compares the decoded samples with ffmpeg’s: FLAC exactly, Opus within 40 steps of 32768.
When a sample is invalid
The RFC says a sample is invalid if any of these hold, and that an implementation is encouraged to reject all the instruments and presets that use it (FluidSynth RFC, “Additional flag for sfSampleType”):
- the type of compression or encoding is unknown;
- the type is not supported;
- the compression or encoding is recognised to be broken;
- the compressed sample is not mono: its channel count is not 1.
FluidSynth supports only 16-bit mono samples: it gives up on a stream with another channel count (it logs “Unsupported channel count” at debug level) and warns when the data is not Ogg (fluid_sffile.c). MuseScore’s tool compresses stereo samples as two single streams rather than as one stereo stream (sftools README, “TODO”).
In the explorer
For every compressed sample the explorer reads the pages at once and reports: a stream that does not start with an Ogg page, a stream that is not Vorbis, a stream with more than one channel, a page with a bad stream version, a CRC that does not match, a page of another stream or out of sequence, a page that continues a packet that was not started, a stream that does not end with an end-of-stream page or ends in the middle of a packet, a stream shorter or longer than the sample header says, a sample rate in the header that differs from the stream’s, and a loop beyond the decoded points. The Samples tab shows the decoded length from the last granule position, and the waveform decodes the whole stream. Clicking the waveform selects the page of the Ogg stream that holds the point. A stream of a codec the explorer does not know (not Vorbis, Opus, FLAC or Ogg FLAC) is reported as invalid and not decoded.
Source: FluidSynth’s SoundFont3 RFC; FluidSynth fluid_sffile.c and fluid_defsfont.c; sftools sfont.cpp and README; RFC 3533 (the Ogg page format); the Ogg framing document; the Vorbis I specification (4.2.2 and appendix A.2). The decoded length and samples were compared with FluidSynth’s dump, TinySoundFont with stb_vorbis and ffmpeg (parser-test/test-sf2-built.mjs).