Skip to main content

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


Download files

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

Source Distribution

pyTinySoundFont-0.0.1.tar.gz (528.5 kB view hashes)

Uploaded Source

Built Distributions

pyTinySoundFont-0.0.1-cp39-abi3-macosx_10_9_universal2.whl (28.5 kB view hashes)

Uploaded CPython 3.9+ macOS 10.9+ universal2 (ARM64, x86-64)

pyTinySoundFont-0.0.1-cp32-abi3-win_amd64.whl (25.6 kB view hashes)

Uploaded CPython 3.2+ Windows x86-64

pyTinySoundFont-0.0.1-cp32-abi3-win32.whl (24.9 kB view hashes)

Uploaded CPython 3.2+ Windows x86

pyTinySoundFont-0.0.1-cp32-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (54.2 kB view hashes)

Uploaded CPython 3.2+ manylinux: glibc 2.17+ ARM64

pyTinySoundFont-0.0.1-cp32-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.whl (46.9 kB view hashes)

Uploaded CPython 3.2+ manylinux: glibc 2.5+ x86-64

pyTinySoundFont-0.0.1-cp32-abi3-manylinux_2_5_i686.manylinux1_i686.whl (45.6 kB view hashes)

Uploaded CPython 3.2+ manylinux: glibc 2.5+ i686

pyTinySoundFont-0.0.1-cp32-abi3-macosx_10_9_x86_64.whl (22.0 kB view hashes)

Uploaded CPython 3.2+ macOS 10.9+ x86-64

pyTinySoundFont-0.0.1-cp32-abi3-macosx_10_6_intel.whl (25.9 kB view hashes)

Uploaded CPython 3.2+ macOS 10.6+ intel

Supported by

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