TinySoundFont dependencies and customization
mainfopen, math, and malloc. If you are working in a restricted environment, you can remove these dependencies by providing your own custom implementations via #define macros.repository·main·Indexed 21 days ago
https://github.com/schellingb/tinysoundfontA lightweight SoundFont2 synthesizer library provided as a single C/C++ header file. It includes the sfotool command-line utility for inspecting, dumping, and rebuilding SoundFont files, as well as converting .SF2 files to compressed .SFO files using Ogg Vorbis.
fopen, math, and malloc. If you are working in a restricted environment, you can remove these dependencies by providing your own custom implementations via #define macros.To create a heavily compressed .SFO file from a standard .SF2 SoundFont, follow these three steps:
.SF2 file into a .WAV file.
sfotool <SF2> <WAV>.WAV file to an .OGG file using a tool like Audacity. Choose your desired compression quality level during this step..SF2 metadata and your new .OGG stream.
sfotool <SF2> <OGG> <SFO>Note on Compression: .SFO files achieve much higher compression than .SF3 files because they treat the entire sound data as a single compressed stream rather than compressing individual samples. However, this comes at the cost of higher quality loss.
# 1. Dump PCM
sfotool input.sf2 output.wav
# 2. Compress output.wav to output.ogg using an external tool (e.g., Audacity)
# 3. Build SFO
sfotool input.sf2 output.ogg output.sfoTinySoundFont is a single-header library. To integrate it, define TSF_IMPLEMENTATION in exactly one C or C++ source file before including tsf.h. This tells the preprocessor to generate the implementation code in that translation unit.
#define TSF_IMPLEMENTATION
#include "tsf.h"sfotool is a command-line utility used to inspect, dump, or rewrite SoundFont files (.SF2 or .SFO). It allows you to extract sample streams (PCM or OGG) from a SoundFont or replace the sample streams within a SoundFont using new .WAV or .OGG files.
Identify Sample Type: Check if a SoundFont contains PCM or OGG streams.
sfotool <SF2/SFO>Dump Samples: Extract the sample stream from a SoundFont to a standalone file.
sfotool <SF2> <WAV>- For OGG (SFO):
```bash
sfotool <SFO> <OGG>Rewrite SoundFont: Replace the samples in an existing SoundFont with new ones.
.SF2 using a .WAV file:sfotool <SF2/SFO> <WAV> <SF2>- Replace OGG samples in an `.SFO` using an `.OGG` file:
```bash
sfotool <SF2/SFO> <OGG> <SFO># Example: Extract PCM samples from an SF2 file to a WAV file
sfotool my_instrument.sf2 extracted_samples.wav
# Example: Replace samples in an SFO file using a new OGG stream
sfotool my_instrument.sfo new_samples.ogg updated_instrument.sfoTo use the synthesizer, load a SoundFont2 file using tsf_load_filename, configure the output parameters with tsf_set_output, trigger notes with tsf_note_on, and render audio using tsf_render_short.
Note: tsf_set_output requires the desired sample rate (e.g., 44100).
tsf* TinySoundFont = tsf_load_filename("soundfont.sf2");
tsf_set_output(TinySoundFont, TSF_MONO, 44100, 0); //sample rate
tsf_note_on(TinySoundFont, 0, 60, 1.0f); //preset 0, middle C
short HalfSecond[22050]; //synthesize 0.5 seconds
tsf_render_short(TinySoundFont, HalfSecond, 22050, 0);The sfotool utility provides several commands for inspecting, dumping, and rebuilding SoundFont files. Use the following syntax patterns:
| Command | Description |
|---|---|
sfotool <SF2/SFO> | Show the type of sample stream contained (PCM or OGG) |
sfotool <SF2> <WAV> | Dump PCM sample stream from a .SF2 to a .WAV file |
sfotool <SFO> <OGG> | Dump OGG sample stream from an .SFO to an .OGG file |
sfotool <SF2/SFO> <WAV> <SF2> | Write a new .SF2 file using a PCM stream from a .WAV file |
sfotool <SF2/SFO> <OGG> <SFO> | Write a new .SFO file using an OGG stream from an .OGG file |