Block based synthesis and music library
Project description
Klang
Block based synthesis and music library for Python. Klang is German for sound.
Getting Started
Prerequisites ^^^^^^^^^^^^^
We use Python bindings for PortAudio_ and RtMidi_. On Mac they can be installed via Homebrew_.
.. _PortAudio: http://www.portaudio.com .. _RtMidi: https://www.music.mcgill.ca/~gary/rtmidi/ .. _Homebrew: https://brew.sh
Installing ^^^^^^^^^^
Klang can be installed via PyPi / pip or directly via setup.py. Note that there are some audio C extensions. Python fallbacks exists.
.. code-block:: bash
python3 setup.py build_ext --inplace
..
For developing you can link your working copy with
.. code-block:: bash
python3 setup.py develop
..
Running the tests
Tests can be run via with
.. code-block:: bash
python3 setup.py test
..
Safety First
As always when programming with sound: Unplug your headphones or be very sure of what you are doing! Also with low headphone volume bugs in the code can result in very unpleasant loud noises which could probably impair your hearing. Be careful!
Klang Primer
Klang provides various audio related blocks. Every block can have multiple in-
and outputs and by connecting them with each other we can define our network.
Once we are finished with patching we can run our network with by calling
run_klang(*blocks)
. This function only needs some blocks which belong to the
network. It will then automatically discovers the other blocks of the network
and deduce an appropriate block execution order.
In the following script we create a 440 Hz sine oscillator which output gets send to the sound card.
.. code-block:: python
from klang.audio import Oscillator, Dac
from klang.klang import run_klang
# Init blocks
osc = Oscillator(frequency=440.)
dac = Dac(nChannels=1)
# Define network
osc.output.connect(dac.input)
# Run it
run_klang(dac)
..
Audio can be written to disk as a WAV file with the filepath
argument.
.. code-block:: python
run_klang(*blocks, filepath='some/filepath.wav')
..
Connections ^^^^^^^^^^^
There are two different types of connections in Klang:
Value
These connection can hold any kind of Python object which will be propagated
through the network and updated during each cycle. Most commonly these are
numpy arrays holding audio or modulation signals (Input
and Output
classes).
Message
Discrete messages. Each message input has its own internal message queue. Most
commonly Note
messages (MessageInput
and MessageOutput
classes).
There are also corresponding Relay connections (Relay
and MessageRelay
classes). These are used to build composite blocks (blocks which contain there
own network of child blocks). Relays can be used to interface between the inside
and outside of an composite block.
Defining The Network ^^^^^^^^^^^^^^^^^^^^
The connect
method can be used to connect inputs and outputs with each other.
Note that it is always possible to connect one output to multiple inputs but not
the other way round. As a shorthand there are two overloaded operators:
- Pipe operator
|
: Connect multiple blocks in series. - Mix operator
+
: Mix multiple value outputs together.
.. code-block:: python
# Pipe operator
a | b | c
# Is equivalanet to:
# >>> a.output.connect(b.input)
# ... b.output.connect(c.input)
..
.. code-block:: python
# Mix operator
mixer = a + b + c
# Is equivalanet to:
# >>> mixer = Mixer(nInputs=0)
# ... mixer.add_new_channel()
# ... a.output.connect(mixer.inputs[-1])
# ... mixer.add_new_channel()
# ... b.output.connect(mixer.inputs[-1])
# ... mixer.add_new_channel()
# ... c.output.connect(mixer.inputs[-1])
..
Examples
See the examples/
directory with a couple example script which illustrate the
core functionality of Klang. Currently there are:
hello_world.py
: 440 Hz sine wave generatoraeolian_arp.py
: More fun with random ever changing arpeggios.arpeggiator_demo.py
: Two synthesizer patch with an arpeggiator and some sound effectsaudio_file_demo.py
: Looped audio file playback (gong.wav
sample) with audio effectshaunting_envelopes.py
: Multiple oscillators controlled by looping envelopesmicro_rhythm_demo.py
: Kick and Hi-Hat pattern where the latter is phrased with a micro rhythmreverberation_demo.py
: Ambient loop showcasing the reverb effect.sequencer_demo.py
: Techno patch with sequencersynthesizer_demo.py
: This has to be started as root. Computer keyboard playable monophonic synthesizertempo_aware_effects.py
: Modulated noise with time synced effects
Coding Style
PEP8 / Google flavored. With the one exception for variable and argument names (camelCase
). Function and in methods are snake_case()
.
Author
- Alexander Theler (
GitHub <https://github.com/atheler>
_)
Acknowledgments
Thanks for the support and inputs!
- Nico Neureiter (
GitHub <https://github.com/NicoNeureiter>
_) - Andreas Steiner (
Smoke And Mirrors <http://smokeandmirrors.ch>
_) - Lawrence Markwalder (
GitHub <https://github.com/lmarkwalder>
_)
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
Built Distribution
File details
Details for the file klang-0.2.0.tar.gz
.
File metadata
- Download URL: klang-0.2.0.tar.gz
- Upload date:
- Size: 1.4 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7bd489eec17a90209659ccf06306209ab8f9b7798c41b5b920d36e7cf2f282c1 |
|
MD5 | db53872ec3b77ab9c68afd802c3ae82e |
|
BLAKE2b-256 | c8be72042434ff72138be0a46e363f08a526d2d0612ff8dc10fef4554090cb72 |
File details
Details for the file klang-0.2.0-cp37-cp37m-macosx_10_15_x86_64.whl
.
File metadata
- Download URL: klang-0.2.0-cp37-cp37m-macosx_10_15_x86_64.whl
- Upload date:
- Size: 1.4 MB
- Tags: CPython 3.7m, macOS 10.15+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2cbe4d62000d1a7b8acf80cd17d7f45b2d0ee20a7954d4fce26f2f379b48ea3b |
|
MD5 | 2ae463f2951fe4064879aafc513e1bf1 |
|
BLAKE2b-256 | e0d0cafc0fbb7d782ea3d6163fd7b5189073a6cf17430b4cd2d5153c0d972378 |