Package: klang
Block based synthesis and music library for Python.Klangis German for sound.Getting StartedPrerequisitesWe use Python bindings forPortAudioandRtMidi. On Mac they can be installed viaHomebrew.InstallingKlang can be installed via PyPi / pip or directly via setup.py. Note that there
are some audio C extensions. Python fallbacks exists.python3setup.pybuild_ext--inplaceFor developing you can link your working copy withpython3setup.pydevelopRunning the testsTests can be run via withpython3setup.pytestSafety FirstAs 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 PrimerKlang 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 callingrun_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.fromklang.audioimportOscillator,Dacfromklang.klangimportrun_klang# Init blocksosc=Oscillator(frequency=440.)dac=Dac(nChannels=1)# Define networkosc.output.connect(dac.input)# Run itrun_klang(dac)Audio can be written to disk as a WAV file with thefilepathargument.run_klang(*blocks,filepath='some/filepath.wav')ConnectionsThere are two different types of connections in Klang:ValueThese 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 (InputandOutputclasses).MessageDiscrete messages. Each message input has its own internal message queue. Most
commonlyNotemessages (MessageInputandMessageOutputclasses).There are also correspondingRelayconnections (RelayandMessageRelayclasses). 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 NetworkTheconnectmethod 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.# Pipe operatora|b|c# Is equivalanet to:# >>> a.output.connect(b.input)# ... b.output.connect(c.input)# Mix operatormixer=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])ExamplesSee theexamplesdirectory 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.wavsample) 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 effectsCoding StylePEP8 / Google flavored. With the one exception for variable and argument names
(camelCase). Function and in methods aresnake_case().AuthorAlexander Theler(GitHub)AcknowledgmentsThanks for the support and inputs!Nico NeureiterAndreas SteinerLawrence Markwalder