File: 08-function-calls.py

package info (click to toggle)
python-pyo 1.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 23,692 kB
  • sloc: ansic: 137,415; python: 135,293; makefile: 390; cpp: 242; sh: 163
file content (48 lines) | stat: -rw-r--r-- 1,122 bytes parent folder | download | duplicates (3)
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
"""
08-function-calls.py - Using custom algorithms with python function calls.

**EventCall** ::

    EventCall(function, *args, occurrences=inf, stopEventsWhenDone=True)

EventCall calls a function, with any number of arguments (\*args) and uses
its return value for the given parameter. The example below use a function
from the random module, *randrange*, with arguments and a user-defined
function, without argument, to create a rising, then falling, amplitude curve.

"""

import random
from pyo import *

s = Server().boot()

db = -30
dir = 1


def riseFallAmp():
    "Rises and falls amplitude between -30 and -3 dB, 1 db at the time."
    global db, dir
    db += dir
    if db >= -3:
        dir = -1
    elif db < -30:
        dir = 1
    return db


# Midi notes are chosen randomly with a function from the random module,
# while the amplitude change according to the riseFallAmp function's output.
e = Events(
    midinote=EventCall(random.randrange, 48, 72, 3),
    beat=1 / 4.0,
    db=EventCall(riseFallAmp),
    attack=0.001,
    decay=0.05,
    sustain=0.5,
    release=0.005,
).play()


s.gui(locals())