Skip to main content

An efficient additive synthesizer that uses a binary tree algorithm and that is both serializable and can both export to WAV and stream raw PCM

Project description

# synth-tree

An efficient additive synthesis library. Simple, clean, and very typechecker-friendly; it might not be ideal for production, but the code within might be interesting to the prying eye!

Internally, it uses a binary tree representation to quickly cull away contiguous regions of zero parameters (that is, silent sine waves, in practice equivalent to no-op).

It comes with a class to read a PCM stream, as a read-only io.RawIOBase subclass. It also allows exporting to .WAV, and both serializing and deserializing TreeSynths with all parameters into a future-proof (version checking enabled) binary format. Talk about standing the test of time!

## How to use

After you install it (using pip or setup.py), you can simply use the TreeSynth class, use a index syntax (like a list) to set values, including ranges thereof using slice syntax, and then you use call syntax (like a function) to obtain values.

`py >>> import synthtree >>> synth = synthtree.SynthTree() >>> synth.values[90] = 0.5 # this is where the binary tree is constructed! >>> synth(20) -0.1950903220149695 `

Note that if you set one single value, the result will be one single sine wave. Which is kind of boring. But that’s because you only set one value!

Think of each index into the synthesizer as its own sine wave oscillator, and the corresponding value as the amplitude. Negative values simply invert the phase! The more values you set, the more interesting the wave. Hooray!

But let’s suppose you got the most awesome wave ever imaginable. There’s only one issue… you can’t really hear it now, can you? Well, you can! Simply export it to WAV:

` >>> my_cool_synth.export_wav('my_cool_wave.wav') `

The default samplerate is 44.1 kHz (44100 Hertz), and by default the volume is halved to protect your ears from potential fun arising from setting ridiculous values and parameters. You can use the amplitude keyword argument to change that, but use it at your own discretion!

What? You want to save the parameters you set so you can reuse them later? Simple! You don’t need to lug it around like a disgusting sack of pickles. ([Pun intended.](https://docs.python.org/3.8/library/pickle.html)) Simply use SynthTree.dump() to get your data in a sexy binary serialization format! Hayo!

` >>> data = my_cooler_synth.dump() `

Once you have your serialized data, you can write it to a file. Then, the next morning, you wake up, feel the Need for Synth rushing in your veins again, and load it back into a SynthTree!…actually, a tuple, containing your loaded SynthTree and any surplus data. The latter is for convenience purposes for in case you’re loading SynthTrees directly from a byte stream or a larger file (which you probably shouldn’t), but if you used dump then it should be empty, so you may safely discard that, e.g. using the syntax demonstrated in the second REPL statement of the example below. Note the comma!

` >>> synthtree.SynthTree.load(data) (<synthtree.synth.SynthTree object at 0x7ff99fca5df0>, b'') >>> my_cooler_synth_again, _ = synthtree.SynthTree.load(data) # Again, note the comma! See? *Now* you saw it, silly! >>> my_cooler_synth_again <synthtree.synth.SynthTree object at 0x7ff99fcb46a0> `

You can also use synthtree.PCMStream to get a raw PCM stream as a read-only file-like object, and use it for whatever. Cool!

` >>> stream = synthtree.PCMStream(my_super_cool_synth) >>> pcm = stream.read(8192) >>> # Step 3: ??? >>> # Step 4: Profit! `

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

synthtree-0.1.0.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

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

synthtree-0.1.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file synthtree-0.1.0.tar.gz.

File metadata

  • Download URL: synthtree-0.1.0.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.8.6

File hashes

Hashes for synthtree-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ef167893e0f40cf30117701121389ac736d09d00213e32a6e60c44fe011c8013
MD5 596bbc2f189e7a0d66dac2f3acaf2698
BLAKE2b-256 12ab318e36314f05b2755e65e22459f48b63dcd868ed5b6f5c90dd9bb1ab2b11

See more details on using hashes here.

File details

Details for the file synthtree-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: synthtree-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.8.6

File hashes

Hashes for synthtree-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0d6186d27f478b07d62b6301ac3dcc55cd625d16f1c1c21a1ed55cbc76c05575
MD5 e1de18a0fb8c9e3aeb0819e9c397d092
BLAKE2b-256 22eb756ccef55e3e6ca9f5a836d5363f4410ff4ef67a67ab7c4487e3ed335c41

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