TinySoundFont Documentation

repository·main·Indexed 21 days ago

https://github.com/schellingb/tinysoundfont

A 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.

Tokens
1.2K
Snippets
4
Records
6
Agent score
75%

What's inside TinySoundFont

  1. TinySoundFont dependencies and customization

    main
    The library depends on standard C library functions for 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.
  2. Convert a .SF2 file to a compressed .SFO file

    main

    To create a heavily compressed .SFO file from a standard .SF2 SoundFont, follow these three steps:

    1. Dump PCM data: Extract the raw PCM sample stream from your .SF2 file into a .WAV file. sfotool <SF2> <WAV>
    2. Compress to Ogg Vorbis: Convert the resulting .WAV file to an .OGG file using a tool like Audacity. Choose your desired compression quality level during this step.
    3. Build the .SFO: Rebuild the SoundFont using the original .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.sfo
  3. Integrate TinySoundFont into C/C++ projects

    main

    TinySoundFont 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"
  4. Use SFOTool CLI to manipulate SoundFont files

    main

    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.

    Usage Modes

    1. Identify Sample Type: Check if a SoundFont contains PCM or OGG streams.

      sfotool <SF2/SFO>
    2. Dump Samples: Extract the sample stream from a SoundFont to a standalone file.

      • For PCM (SF2):
        sfotool <SF2> <WAV>
      - For OGG (SFO):
        ```bash
      sfotool <SFO> <OGG>
    3. Rewrite SoundFont: Replace the samples in an existing SoundFont with new ones.

      • Replace PCM samples in an .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.sfo
  5. Basic usage of TinySoundFont

    main

    To 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);
  6. Use sfotool CLI commands

    main

    The sfotool utility provides several commands for inspecting, dumping, and rebuilding SoundFont files. Use the following syntax patterns:

    CommandDescription
    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