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