File: 06-midi-out.rst.txt

package info (click to toggle)
python-pyo 1.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,332 kB
  • sloc: python: 135,133; ansic: 127,822; javascript: 16,116; sh: 395; makefile: 388; cpp: 242
file content (71 lines) | stat: -rw-r--r-- 2,239 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
06-midi-out.py - Sending MIDI events from pyo.
============================================================================================================================================


This program gives an example of how to build an algorithm with python/pyo and
then send MIDI events from the Server to a connected MIDI synthesizer (physical
or virtual).
 
Available MIDI output methods (see the Server documentation for the details):

- afterout(pitch, velocity, channel=0, timestamp=0)
- bendout(value, channel=0, timestamp=0)
- ctlout(ctlnum, value, channel=0, timestamp=0)
- makenote(pitch, velocity, duration, channel=0)
- noteout(pitch, velocity, channel=0, timestamp=0)
- pressout(value, channel=0, timestamp=0)
- programout(value, channel=0, timestamp=0)
- sysexout(msg, timestamp=0)

.. code-block:: python

    import random
    from pyo import *
    
    pm_list_devices()
    
    s = Server()
    
    # Open all MIDI output devices.
    s.setMidiOutputDevice(99)
    
    # Then boot the Server.
    s.boot()
    
    # Generates an audio ramp from 36 to 84, from
    # which MIDI pitches will be extracted.
    pitch = Phasor(freq=11, mul=48, add=36)
    
    # Global variable to count the down and up beats.
    count = 0
    
    
    def midi_event():
        global count
        # Retrieve the value of the pitch audio stream and convert it to an int.
        pit = int(pitch.get())
    
        # If the count is 0 (down beat), play a louder and longer event, otherwise
        # play a softer and shorter one.
        if count == 0:
            vel = random.randint(90, 110)
            dur = 500
        else:
            vel = random.randint(50, 70)
            dur = 125
    
        # Increase and wrap the count to generate a 4 beats sequence.
        count = (count + 1) % 4
    
        print("pitch: %d, velocity: %d, duration: %d" % (pit, vel, dur))
    
        # The Server's `makenote` method generates a noteon event immediately
        # and the correponding noteoff event after `duration` milliseconds.
        s.makenote(pitch=pit, velocity=vel, duration=dur)
    
    
    # Generates a MIDI event every 125 milliseconds.
    pat = Pattern(midi_event, 0.125).play()
    
    s.gui(locals())