Debian Codegraph @

docs/_sources/examples/16-midi/05-control-change.rst.txt

raw
1 05-control-change.py - Adding control changes to our MIDI synthesizer class.
2 ============================================================================================================================================
3
4
5 This example adds three continuous MIDI controllers to the previous one
6 (04-simple-midi-synth.py). The first one applied the pitch bend to the
7 `transpo` argument, which multiply the pitches from the Notein object.
8 The second is a continuous controller used to change the cutoff frequency
9 (`hfdamp` argument) of the lowpass filter. The last one sets the speed
10 (`lfofreq` argument) of moving notche oscillations in the spectrum.
11
12 .. code-block:: python
13
14 from pyo import *
15 from random import random
16
17
18 class Synth:
19 # Added some arguments that will be controlled with MIDI controllers.
20 def __init__(self, transpo=1, hfdamp=5000, lfofreq=0.2, mul=1):
21 # Transposition factor.
22 self.transpo = Sig(transpo)
23 # Receive midi notes, convert pitch to Hz and manage 10 voices of polyphony.
24 self.note = Notein(poly=10, scale=1, first=0, last=127)
25
26 # Handle pitch and velocity (Notein outputs normalized amplitude (0 -> 1)).
27 self.pit = self.note["pitch"] * self.transpo
28 self.amp = MidiAdsr(self.note["velocity"], attack=0.001, decay=0.1, sustain=0.7, release=1, mul=0.1,)
29
30 # Anti-aliased stereo square waves, mixed from 10 streams to 1 stream
31 # to avoid channel alternation on new notes.
32 self.osc1 = LFO(self.pit, sharp=0.5, type=2, mul=self.amp).mix(1)
33 self.osc2 = LFO(self.pit * 0.997, sharp=0.5, type=2, mul=self.amp).mix(1)
34
35 # Stereo mix.
36 self.mix = Mix([self.osc1, self.osc2], voices=2)
37
38 # High frequencies damping, use argument `hfdamp` to allow MIDI control.
39 self.damp = ButLP(self.mix, freq=hfdamp)
40
41 # Moving notches, use argument `lfofreq` to allow MIDI control.
42 self.lfo = Sine(lfofreq, phase=[random(), random()]).range(250, 4000)
43 self.notch = ButBR(self.damp, self.lfo, mul=mul)
44
45 def out(self):
46 "Sends the synth's signal to the audio output and return the object itself."
47 self.notch.out()
48 return self
49
50 def sig(self):
51 "Returns the synth's signal for future processing."
52 return self.notch
53
54
55 s = Server()
56 s.setMidiInputDevice(99) # Open all input devices.
57 s.boot()
58
59 ### Setup the MIDI controllers.
60
61 # Bendin can output MIDI note values or transposition factors (scale=1).
62 # A value of 2 to `brange` argument means that the bending range will be
63 # 2 semitones above and below the current note.
64 transpo = Bendin(brange=2, scale=1)
65
66 # High frequency damping mapped to controller number 1.
67 hfdamp = Midictl(ctlnumber=1, minscale=500, maxscale=10000, init=5000)
68
69 # Frequency of the LFO applied to the speed of the moving notches.
70 lfofreq = Midictl(ctlnumber=2, minscale=0.1, maxscale=8, init=0.2)
71
72
73 # Create the midi synth.
74 a1 = Synth(transpo, hfdamp, lfofreq)
75
76 # Send the synth's signal into a reverb processor.
77 rev = STRev(a1.sig(), inpos=[0.1, 0.9], revtime=2, cutoff=4000, bal=0.15).out()
78
79 s.gui(locals())
80