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