Debian Codegraph @

pyo/examples/x16-midi/02-notein-object.py

raw symbols SCIP
1 """
2 02-notein-object.py - How to use the Notein object.
3
4 This example shows how to use the Notein object to control audio
5 processes with events received from a MIDI keyboard (virtual or not).
6
7 """
8 from pyo import *
9
10 s = Server()
11 s.setMidiInputDevice(99) # Open all input devices.
12 s.boot()
13
14 # Default arguments of the Notein object.
15 # - 10 voices of polyphony, which means that this object will manage
16 # 10 pitch/velocity streams simultaneously.
17 # - scale=0 means that the pitch information will be MIDI numbers.
18 # Use 1 for Hertz and 2 for transposition factors.
19 # - first and last arguments are the lowest and highest MIDI notes
20 # that this object will handle. This is useful to split the complete
21 # range over multiple processes.
22 # - channel is the MIDI channel this object will listen to. 0 means all
23 # channels.
24 # - The mul argument affects velocities, which are already normalized
25 # between 0 and 1.
26 notes = Notein(poly=10, scale=0, first=0, last=127, channel=0, mul=1)
27
28 # User can show a keyboard widget to supply MIDI events.
29 notes.keyboard()
30
31 # Notein["pitch"] retrieves pitch streams.
32 # Converts MIDI pitch to frequency in Hertz.
33 freqs = MToF(notes["pitch"])
34
35 # Notein["velocity"] retrieves normalized velocity streams.
36 # Applies a portamento on the velocity changes.
37 amps = Port(notes["velocity"], risetime=0.005, falltime=0.5, mul=0.1)
38
39 # Creates two groups of oscillators (10 per channel), slightly detuned.
40 sigL = RCOsc(freq=freqs, sharp=0.5, mul=amps)
41 sigR = RCOsc(freq=freqs * 1.003, sharp=0.5, mul=amps)
42
43 # Mixes the 10 voices per channel to a single stream and send the
44 # signals to the audio output.
45 outL = sigL.mix(1).out()
46 outR = sigR.mix(1).out(1)
47
48 # Notein["trigon"] sends a trigger when a voice receive a noteon.
49 # Notein["trigoff"] sends a trigger when a voice receive a noteoff.
50
51 # These functions are called when Notein receives a MIDI note event.
52 def noteon(voice):
53 "Print pitch and velocity for noteon event."
54 pit = int(notes["pitch"].get(all=True)[voice])
55 vel = int(notes["velocity"].get(all=True)[voice] * 127)
56 print("Noteon: voice = %d, pitch = %d, velocity = %d" % (voice, pit, vel))
57
58
59 def noteoff(voice):
60 "Print pitch and velocity for noteoff event."
61 pit = int(notes["pitch"].get(all=True)[voice])
62 vel = int(notes["velocity"].get(all=True)[voice] * 127)
63 print("Noteoff: voice = %d, pitch = %d, velocity = %d" % (voice, pit, vel))
64
65
66 # TrigFunc calls a function when it receives a trigger. Because notes["trigon"]
67 # contains 10 streams, there will be 10 caller, each one with its own argument,
68 # taken from the list of integers given at `arg` argument.
69 tfon = TrigFunc(notes["trigon"], noteon, arg=list(range(10)))
70 tfoff = TrigFunc(notes["trigoff"], noteoff, arg=list(range(10)))
71
72 s.gui(locals())