Debian Codegraph @

pyo/examples/x16-midi/06-midi-out.py

raw symbols SCIP
1 """
2 06-midi-out.py - Sending MIDI events from pyo.
3
4 This program gives an example of how to build an algorithm with python/pyo and
5 then send MIDI events from the Server to a connected MIDI synthesizer (physical
6 or virtual).
7
8 Available MIDI output methods (see the Server documentation for the details):
9
10 - afterout(pitch, velocity, channel=0, timestamp=0)
11 - bendout(value, channel=0, timestamp=0)
12 - ctlout(ctlnum, value, channel=0, timestamp=0)
13 - makenote(pitch, velocity, duration, channel=0)
14 - noteout(pitch, velocity, channel=0, timestamp=0)
15 - pressout(value, channel=0, timestamp=0)
16 - programout(value, channel=0, timestamp=0)
17 - sysexout(msg, timestamp=0)
18
19 """
20 import random
21 from pyo import *
22
23 pm_list_devices()
24
25 s = Server()
26
27 # Open all MIDI output devices.
28 s.setMidiOutputDevice(99)
29
30 # Then boot the Server.
31 s.boot()
32
33 # Generates an audio ramp from 36 to 84, from
34 # which MIDI pitches will be extracted.
35 pitch = Phasor(freq=11, mul=48, add=36)
36
37 # Global variable to count the down and up beats.
38 count = 0
39
40
41 def midi_event():
42 global count
43 # Retrieve the value of the pitch audio stream and convert it to an int.
44 pit = int(pitch.get())
45
46 # If the count is 0 (down beat), play a louder and longer event, otherwise
47 # play a softer and shorter one.
48 if count == 0:
49 vel = random.randint(90, 110)
50 dur = 500
51 else:
52 vel = random.randint(50, 70)
53 dur = 125
54
55 # Increase and wrap the count to generate a 4 beats sequence.
56 count = (count + 1) % 4
57
58 print("pitch: %d, velocity: %d, duration: %d" % (pit, vel, dur))
59
60 # The Server's `makenote` method generates a noteon event immediately
61 # and the correponding noteoff event after `duration` milliseconds.
62 s.makenote(pitch=pit, velocity=vel, duration=dur)
63
64
65 # Generates a MIDI event every 125 milliseconds.
66 pat = Pattern(midi_event, 0.125).play()
67
68 s.gui(locals())