Skip to main content

Sound font 2 parsing library and utilities

Project description

Fork of sf2utils until author merges changes needed for python >= 3.13 Python 3.13 dropped module audioop, used for byteswapping. This fork implements a pure python byteswap


Sf2Utils is a soundfont 2 parsing library and companion utility. It is meant for developers aiming to use soundfont 2 file as input, typically for converting to another format.

Installation

Sf2Utils is installable from PyPI with a single pip command:

pip install sf2utils

Alternatively, Sf2utils can be run directly from sources after a git pull:

git clone https://gitlab.com/zeograd/sf2utils.git
cd sf2utils && python setup.py install

Companion script

sf2parse is a command line utility used to parse a sound font 2 file. It is meant as debug companion for the sf2utils parser library as it shows what is understood from its parser.

usage: sf2parse [-h] [-d] sf2_filename

LGPL v3+ 2016-2024 Olivier Jolly

positional arguments:
  sf2_filename  input file in SoundFont2 format

optional arguments:
  -h, --help    show this help message and exit
  -d, --debug   debug parsing [default: False]

Parse sf2 file and display info about it

Library use

sf2utils can be used as a library for parsing SoundFont2 files. There are 2 API levels, a low level and high level one. They both open SoundFont2 file the same way:

from sf2utils.sf2parse import Sf2File
with open('file.sf2', 'rb') as sf2_file:
    sf2 = Sf2File(sf2_file)

Note that opening the file is up to the library user because samples data are lazy loaded. Accessing sample data will seek and read data. You shouldn’t access sample data outside of the content in which the file is opened, as it will fail.

Low level library API

Once sf2 is a valid Sf2File, all metadata are available via the property raw, which has fields for the various sections in a SoundFont2 file.

  • sf2.raw.info is a dictionary with all info in the info block of the SoundFont2 file with keys as binary strings.

  • sf2.raw.smpl_offset is the offset in the original file where the sample data are located.

  • sf2.raw.sm24_offset is the offset in the original file where the complementary 8bits of sample data are located.

  • sf2.raw.pdta is the main metadata structure, a dictionary with tuples named after the specification and indexed using specification structure names :
    • Pbag -> ‘gen’, ‘mod’

    • Igen -> ‘oper’, ‘amount’

    • Imod -> ‘src_oper’, ‘dest_oper’, ‘amount’, ‘amount_src_oper’, ‘trans_oper’

    • Pmod -> ‘src_oper’, ‘dest_oper’, ‘amount’, ‘amount_src_oper’, ‘trans_oper’

    • Pgen -> ‘oper’, ‘amount’

    • Shdr -> ‘sample_name’, ‘start’, ‘end’, ‘start_loop’, ‘end_loop’, ‘sample_rate’, ‘original_pitch’, ‘pitch_correction’, ‘sample_link’, ‘sample_type’

    • Phdr -> ‘name’, ‘preset’, ‘bank’, ‘bag’, ‘library’, ‘genre’, ‘morphology’

    • Ibag -> ‘gen’, ‘mod’

    • Inst -> ‘name’, ‘bag’

With this API, field interpretation remains up to the library user.

High level library API

With this API, info are available via the info property, which is a pretty printable tuple where every field comes from the info block in the SoundFont2 file.

Samples are accessible via the samples property, which is a list of Sf2Sample from which you can retrieve loop information and raw data.

Presets are in the presets property, which is a list of Sf2Preset from which you can list generators, modulators, instruments and bags.

Instruments are in the instruments property, which is a list of Sf2Instrument from which you can list bags of generators, modulators.

Bags are of class Sf2Bag and offer a wide range of property returning various high level info (about loop, tuning, filters, envelopes, instruments, samples, …) or None when no generator was found.

All those classes have a pretty_print returning a pretty printed string, recursing over subelements.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sf2utils13-1.0.1.tar.gz (24.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sf2utils13-1.0.1-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

Details for the file sf2utils13-1.0.1.tar.gz.

File metadata

  • Download URL: sf2utils13-1.0.1.tar.gz
  • Upload date:
  • Size: 24.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.2

File hashes

Hashes for sf2utils13-1.0.1.tar.gz
Algorithm Hash digest
SHA256 64fb12fb7e429cecc0d18bfe58701da27433d81e8cd598127ece79421161712c
MD5 570e61c0ac17d316d6f16f570b1e79b9
BLAKE2b-256 c81fd801d8c1b2b810391118aa9cebbc7923d44543a63e0d2d597dbde8b6cd9f

See more details on using hashes here.

File details

Details for the file sf2utils13-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: sf2utils13-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 21.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.2

File hashes

Hashes for sf2utils13-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 95e5b8bcd2c8031d1b761192e0329dd815f3c91f890dc5f24f6605835f4ab5df
MD5 f3ed95a5d6b44d2d547aa7b9b9ebc397
BLAKE2b-256 9d2346b94486289000e386bd34be96d1a2a2837ac3a53cef4cd67ab8a97b2037

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page