Debian Codegraph @

pyo/examples/x16-midi/04-simple-midi-synth.py

raw symbols SCIP
1 """
2 04-simple-midi-synth.py - Create a MIDI synthesizer as a custom class.
3
4 A more elaborate MIDI synthesizer built in a custom python class. This
5 makes easier to use it multiple times. In this example, two objects are
6 created, one for the played pitch and another playing one octave lower.
7
8 """
9 from pyo import *
10 from random import random
11
12
13 class Synth:
14 def __init__(self, transpo=1, mul=1):
15 # Transposition factor.
16 self.transpo = Sig(transpo)
17 # Receive midi notes, convert pitch to Hz and manage 10 voices of polyphony.
18 self.note = Notein(poly=10, scale=1, first=0, last=127)
19
20 # Handle pitch and velocity (Notein outputs normalized amplitude (0 -> 1)).
21 self.pit = self.note["pitch"] * self.transpo
22 self.amp = MidiAdsr(self.note["velocity"], attack=0.001, decay=0.1, sustain=0.7, release=1, mul=0.1,)
23
24 # Anti-aliased stereo square waves, mixed from 10 streams to 1 stream
25 # to avoid channel alternation on new notes.
26 self.osc1 = LFO(self.pit, sharp=0.5, type=2, mul=self.amp).mix(1)
27 self.osc2 = LFO(self.pit * 0.997, sharp=0.5, type=2, mul=self.amp).mix(1)
28
29 # Stereo mix.
30 self.mix = Mix([self.osc1, self.osc2], voices=2)
31
32 # High frequencies damping.
33 self.damp = ButLP(self.mix, freq=5000)
34
35 # Moving notches, using two out-of-phase sine wave oscillators.
36 self.lfo = Sine(0.2, phase=[random(), random()]).range(250, 4000)
37 self.notch = ButBR(self.damp, self.lfo, mul=mul)
38
39 def out(self):
40 "Sends the synth's signal to the audio output and return the object itself."
41 self.notch.out()
42 return self
43
44 def sig(self):
45 "Returns the synth's signal for future processing."
46 return self.notch
47
48
49 s = Server()
50 s.setMidiInputDevice(99) # Open all input devices.
51 s.boot()
52
53 # Create the midi synth.
54 a1 = Synth()
55
56 # Send the synth's signal into a reverb processor.
57 rev = STRev(a1.sig(), inpos=[0.1, 0.9], revtime=2, cutoff=4000, bal=0.15).out()
58
59 # It's very easy to double the synth sound!
60 # One octave lower and directly sent to the audio output.
61 a2 = Synth(transpo=0.5, mul=0.7).out()
62
63 s.gui(locals())