File: 07-condition.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 (73 lines) | stat: -rw-r--r-- 1,895 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
72
73
Using the conditional function
============================================================================================================================================


**07-condition.py**

The `if` function let the user set different processing branches
according to the result of a condition. The syntax is:

.. code-block:: scheme

    (if (condition) (then) (else))

The example below uses two conditional statements. The first one
adjusts the duty cycle of a pulse-width-modulation waveform and 
the second one split the positive and negative parts of the wave.
A slider, controlling a user variable, sets the gain of the
negative part.

.. code-block:: python

    from pyo import *
    
    s = Server().boot()
    
    expression = """
    // Width of the duty cycle.
    (var #thresh 0.5)
    // Gain of the negative part of the waveform.
    (var #gain 1)
    
    // Running phase.
    (let #sig (~ 172))
    
    // If running phase is below threshold,
    // then 1.0, else -1.0
    (let #rect (
        if (<= #sig #thresh) (1.0) (-1.0)
        )
    )
    
    // If positive, pass through, else,
    // modulated by #gain variable. 
    (if (>= #rect 0)
        (#rect) 
        (* #gain #rect)
    )
    
    """
    
    expr = Expr(Sig(0), expression, mul=0.5)
    expr.editor()
    
    threshold = Sig(0.5)
    threshold.ctrl([SLMap(0.01, 0.5, "lin", "value", 0.5)], title="Duty Cycle")
    rectifier = Sig(1.0)
    rectifier.ctrl([SLMap(0.0, 1.0, "lin", "value", 1.0)], title="Rectifier")
    
    
    def change():
        "Sends new values to user variables in the expression."
        expr.setVar(["#thresh", "#gain"], [threshold.get(), rectifier.get()])
    
    
    # Calls the change() function every 20 ms to update the user variables.
    pat = Pattern(change, 0.025).play()
    
    sc = Scope(expr)
    
    pan = Pan(expr).out()
    
    s.gui(locals())