Python port of TinySoundFont
Project description
pyTinySoundFont
This is Python porting of TinySoundFont(https://github.com/schellingb/TinySoundFont)
We are still relying on C++ for some sample level tasks, so a building process is needed (see below).
The "class TinySoundFont" interface defined in pyTinySoundFont/SF2SynthRT.py provides most of the original functions of TinySoundFont, with the following limitations:
- Loading SF2 from memory is not implemented
- "Higher level channel based functions" are not ported yet.
- Real-time playback is not part of the project, which needs a separate solution.
See test/testRT.py for a use case.
The "SynthNote()" defined in pyTinySoundFont/SF2Synth.py provides a simple interface for single note synthesis. See test/test.py for a use case.
Building with CMake
Prerequisites:
- CMake 3.0+
- Python3
You can simply run CMake to generate makefiles/project files for your system and build. You can set CMAKE_INSTALL_PREFIX to /test so that the test scripts can find pyTinySoundFont.
Building with Setuptools
$ python3 setup.py build
$ python3 setup.py install
Use cases
Realtime Synthesis
Pretty similar to the C version
import pyTinySoundFont as tsf
g_TinySoundFont = tsf.TinySoundFont('florestan-subset.sf2')
g_TinySoundFont.NoteOn(0,48,1.0) # C2
g_TinySoundFont.NoteOn(0,52,1.0) # E2
# We don't have an output device here, just open a file to simulate
with open('dmp.raw','wb') as f:
buf = bytes(512 * 4 * 2) # create a buffer of 512 samples
for i in range(200): # render 200 times to the buffer when notes are on
g_TinySoundFont.Render(buf, 512, False)
f.write(buf)
g_TinySoundFont.NoteOffAll()
for i in range(10): # render another 10 times after notes are off
g_TinySoundFont.Render(buf, 512, False)
f.write(buf)
Non-Realtime Synthesis
The use case is that sometimes we just want to render some preprogrammed notes to a buffer as soon as possible, and we don't need immediate play-back. In that case, we can render 1 note each time then blend them together. Bellow example shows how to render a single note.
import wave
import pyTinySoundFont as tsf
sf2= tsf.LoadSF2('florestan-subset.sf2')
presets = tsf.LoadPresets(sf2)
# Render C5, required length is set to 2 seconds
# The actual returned buffer will be a little longer than 2 seconds
# There will some extra samples after the loop is ended
res=tsf.SynthNote(sf2[1], presets[0], 60, 1.0, 44100*2)
# Utility to convert float32 to short16
wavS16=tsf.F32ToS16(res[1], 1.0)
# Here we write the generated samples to a wav file
# We can also program to mix the samples with other buffer
with wave.open('out.wav', mode='wb') as wavFile:
wavFile.setnchannels(2)
wavFile.setsampwidth(2)
wavFile.setframerate(44100)
wavFile.setnframes(len(wavS16)//4)
wavFile.writeframes(wavS16)
License
pyTinySoundFont is available under the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for pyTinySoundFont-0.0.1-cp39-abi3-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 700d187efe9516e9416b43a61fe5cd63842e06cc63963ba70fc5c56c4eb60e0a |
|
MD5 | 268aba5388c6fe3678fb1008c5c5f011 |
|
BLAKE2b-256 | f4fa18f2475deeda57f9acd8d29cfd27c0229abe04b60f0e8d1b180227e342f8 |
Hashes for pyTinySoundFont-0.0.1-cp32-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 157ad733ef2c1fa8606697d7151968c9b7a0c8b6f43253347d113487d7abef55 |
|
MD5 | 0d6cc9d9a1b02d85e672c038c15f55cd |
|
BLAKE2b-256 | be64484137f30e353d2a99fa19b93400c8a0dc88368b125dddbe9eeb8594b744 |
Hashes for pyTinySoundFont-0.0.1-cp32-abi3-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4920ad52ea37cf2ddeb40ae7cae098ea7b5f7c9a5027bc7f3c447084f6d46576 |
|
MD5 | 8d4a3d1a7c262bfda7f81c062d086366 |
|
BLAKE2b-256 | 5a7e32607e2b8c71c6ca5fdff19b7b9d136f2184d42fdcd1541c789f861a1e2b |
Hashes for pyTinySoundFont-0.0.1-cp32-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ecdff02f5a3fab17be9523e3263db7532b65c8a43050c55877aa4904f0598e31 |
|
MD5 | f08a652de9fe104b9faf601e75833545 |
|
BLAKE2b-256 | f71a83f29146516cb23bafddbb82378254645b451c7b74d48f0f3d1a3e2a6f2d |
Hashes for pyTinySoundFont-0.0.1-cp32-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 59693e6c3ced60c9290cd2df23a516a7dfdedaf3e5247f7977930fd745ed161f |
|
MD5 | 9d15eccbe08bb272b45a7b3534155a9c |
|
BLAKE2b-256 | 47bbeac20e6336d8b8fdb215aee7087d687fbe60ce385a57d45790594a6c3ea0 |
Hashes for pyTinySoundFont-0.0.1-cp32-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a383cb1defba5aff75bc3ec4b4d5dc3a1b05e9833cb3c09aa5f1227cfd2b8ac4 |
|
MD5 | 4c21f7aefa88d20e20569ff42aaf53eb |
|
BLAKE2b-256 | 884fc46e06303340ffca1e4078c3de58b0b744c7937a2d2bdff027d138cafd98 |
Hashes for pyTinySoundFont-0.0.1-cp32-abi3-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 098c0fda33995c9a35b9e35ac29bcc5aa5265f1d560f246f32f392cc7b1e8fb8 |
|
MD5 | 7291166e6474bc0f760830842ff9932c |
|
BLAKE2b-256 | 7f5730e703bb0f02b7473adc6108850970e6c543ce667f495600de3163e92c3c |
Hashes for pyTinySoundFont-0.0.1-cp32-abi3-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d5c1dfd7e427f5eac025e72986a5f76f2373feeb18ecf38b41823cf69bdea8c9 |
|
MD5 | 114491e3abd82d1ea7d12f440d02ad06 |
|
BLAKE2b-256 | f58f6809258139d1a9fe1f14615186ef489bae2c1517e6af22eb494a9d18fd5d |