Debian Codegraph @

pyo/examples/x16-midi/05-control-change.py

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